-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEducationShield.h
380 lines (310 loc) · 7.3 KB
/
EducationShield.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifndef EducationShield_H
#define EducationShield_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Servo.h>
#include "utils/CapacitiveSensor.h"
#include <SD.h>
#if defined(__arc__)
#include <CurieBLE.h>
#include <CurieIMU.h>
#endif
#define LED_LENGTH 20
#define BUTTONGROUP_LENGTH 10
class VUMeter{
public:
VUMeter();
void config(int length, int pins[]);
void begin();
void on(int index);
void off(int index);
void scrollLeft(int speed,int startIndex=0);
void scrollRight(int speed,int startIndex=0);
void fill(int length);
void fillFrom(int leftIndex,int rightIndex);
void blink(int index, int speed, int times=1);
void blinkAll(int speed,int times=1);
void clear();
void test();
private:
void blinkOnce(int index,int onTime,int offTime);
//int getPin(int index);
int pins[LED_LENGTH];
int pinCount;
};
#define MELODY_LENGTH 32
class Melody{
public:
Melody(int pin);
void play(int length, int notes[],int diration[], float speed=1.3);
void beep(int note=20, int length=30);
void playTone(int note, int length=10);
void effect_win();
void effect_gameover();
void effect_score();
private:
int pin;
};
class Button{
public:
Button(int pin=8,bool pressedValue=HIGH);
void begin();
bool pressed(int timeout=0);
bool released(int timeout=0);
bool doublePressed(int timeout=0,int tolerance=500);
bool isPressed();
virtual bool getState();
protected:
int pin;
bool pressedValue;
bool lastState;
long debounceTime;
bool checkPress(int timeout, bool requiredState);
};
class ButtonGroup{
public:
ButtonGroup();
void begin(int length, int buttons[], bool pressedValue=HIGH);
int pressed(int timeout=0);
private:
int buttons[BUTTONGROUP_LENGTH];
//bool iStarted[BUTTONGROUP_LENGTH];
int buttonsCount;
bool pressedValue;
int checkPress(int timeout, bool requiredState);
};
class LightSensor : public Button{
public:
LightSensor(int pin=A1);
void config(int baseValue,int threshold);
virtual bool getState();
void test();
void calibrate(int t=5000);
void showConfig();
protected:
int base;
int threshold;
};
class TiltSwitch : public Button{
public:
TiltSwitch(int pin, bool pressedValue=LOW);
};
class CapacitiveSwitch : public Button{
public:
CapacitiveSwitch(int pin_in=13, int pin=12);
void config(int threshold);
void test();
long getValue(int min=0);
virtual bool getState();
protected:
CapacitiveSensor sensor;
int threshold;
};
class LED{
//Mostly for the LED component in Tinkerkit
public:
LED(int pin=9);
void begin();
void on();
void off();
void blink(int speed,int times=1);
private:
int pin;
};
class PiezoKnockSensor : public Button{
public:
PiezoKnockSensor(int pin=A0);
void config(int threshold=40,int debounceTime=80);
bool knocked(int timeout=0);
void test();
private:
int threshold;
};
class Player{
public:
Player();
void begin();
void play(char* name);
private:
void initPlayer();
void initSD();
void printDirectory(File dir, int numTabs);
File root;
char* name;
};
class Knob{
public:
Knob(int pin);
int getValue();
void setLevels(int levels);
int getLevel();
private:
int pin;
int levels;
};
class Joystick{
public:
Joystick(int x, int y);
int getX();
int getY();
private:
int x;
int y;
};
class Wheels{
public:
Wheels(int lpin=10, int rpin=9);
void begin();
void goForward();
void goBackwards();
void turnLeft();
void turnRight();
void standStill();
void follow(int d);
private:
int top, low, still;
void go(int tl, int tr);
int lpin, rpin;
Servo left, right;
int fromL, fromR;
int toL, toR;
int tl, tr;
};
#define KP 30
#define KD 7
#define LINE_THRESSHOLD 30
#define ROBOT_SPEED 50 //In % [0..100]
#define INTEGRATION_TIME 10
class IRArray{
public:
IRArray(int IR1=A1, int IR2=A2, int IR3=A3);
void begin();
void test();
void setThreshold(int t);
int readBinary();
int readLine();
private:
int IR1, IR2, IR3;
int sensPins[3];
int sensorVal[3];
int toBinary[3];
int translateBinary();
int calculateVelocity(int s);
int last_diff;
int threshold;
};
class UltrasonicSensor{
public:
UltrasonicSensor(int trig=11, int echo=12);
int getDistance();
private:
int trig, echo;
};
#if defined(__arc__)
class BLEPeripheralBase{
public:
void setName(const char* name);
//virtual void begin();
bool searchCentral();
bool connected();
virtual bool dataReceived()=0;
static const int MAX_LENGTH=20;
protected:
BLEPeripheralBase(/*const char* serviceID*/);
BLEDevice central;
};
class BLEText : public BLEPeripheralBase{
public:
BLEText();
void begin();
bool dataReceived();
void fetchData();
char getCharAt(int position);
protected:
BLECharacteristic textChari;
char readBuffer[MAX_LENGTH];
};
#define SERVICEID_MESSENGER "19b10000-e8f2-537e-4f6c-d104768a1214"
#define SERVICEID_LOGOROBOT "19f82bd2-da79-11e5-b5d2-0a1d41d68578"
#define SERVICEID_TAMAGOTCHI "361dbb0c-0193-49dd-93af-753ab760a344"
#define TYPE_NONE -1
#define TYPE_MESSENGER 1
#define TYPE_LOGOROBOT 2
#define TYPE_TAMAGOTCHI 3
class BLEuart : public BLEPeripheralBase{
public:
//BLEuart(const char* serviceID="6E500001-B5A3-F393-E0A9-E50E24DCCA9E");
BLEuart(int exampleID=TYPE_NONE);
void begin();
void setExampleID(int exampleID);
bool dataReceived();
void fetchData();
void send();
void sendString(const char*, int length);
const char* receivedString();
int getReceivedLength();
void addValue(int val);
void addValueAt(unsigned char val, int position);
unsigned char getValueAt(int position);
protected:
BLEService service;
BLECharacteristic txChari;
BLECharacteristic rxChari;
BLECharacteristic typeChari;
unsigned short receivedLength;
unsigned char readBuffer[MAX_LENGTH];
unsigned char writeBuffer[MAX_LENGTH];
int writeLength;
private:
};
#define USE_ISR false
#define FILTER_TYPE 0
class IMU{
public:
IMU();
void begin(int accRange=2, int gyroRange=500);
void calibrate();
static void run();
void detectShock(int shockThreashold=192, int shockDuration=11);
void attachCallback(void (*callback)(void));
int getPitch();
int getRoll();
int getAccelerometerX();
int getAccelerometerY();
int getAccelerometerZ();
float getAccelerometerX_g();
float getAccelerometerY_g();
float getAccelerometerZ_g();
int getGyroX();
int getGyroY();
int getGyroZ();
float getGyroX_dps();
float getGyroY_dps();
float getGyroZ_dps();
private:
static void measureMotion();
static void calculateRollPitch();
static void calculateComplementaryRollPitch();
static float convertAcclerometer_g(int16_t rawVal);
static float convertGyro_dps(int16_t rawVal);
static long timer;
static bool useISR;
//Kalman kalmanX;
//Kalman kalmanY;
static int accRange, gyroRange;
static int16_t ax, ay, az; // accelerometer values
static int16_t gx, gy, gz; // gyrometer values
static float pitch, roll;
};
#endif
/*
class BLEAlienBaby : public BLEPeripheralBase{
public:
BLEAlienBaby();
void begin();
};
*/
#endif