This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOXOcardRunner.h
370 lines (308 loc) · 9.47 KB
/
OXOcardRunner.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
#ifndef _OXOCARD_RUNNER_H_
#define _OXOCARD_RUNNER_H_
/*******************************************************************************
* \file OXOcardRunner.h
********************************************************************************
* \author Jascha Haldemann jh@oxon.ch
* \author Thomas Garaio tg@oxon.ch
* \date 01.02.2017
* \version 1.0
*
* \brief The OXOcardRunner is a set of simplified functions to programm
* the OXOcard
*
********************************************************************************
* OXOcard
*******************************************************************************/
/* ============================== Global imports ============================ */
#include "OXOcard.h"
/* ============================ Global variables ============================ */
volatile bool serialOn = false;
volatile unsigned long millisSinceLastReset = 0;
/* ====================== Global class instantiations ======================= */
OXOcard globalOXOcard;
/* ============================ Global functions ============================ */
/* hook function from Arduino to allow 3rd party variant-specific initialization */
void initVariant() {
globalOXOcard.begin();
}
/* System functions --------------------------------------------------------- */
void turnOff(bool leftButton = false, bool middleButton = false, bool rightButton = false) {
globalOXOcard.turnOff(leftButton, middleButton, rightButton);
}
void handleAutoTurnOff(unsigned int seconds = DEFAULT_AUTO_TURN_OFF, bool leftButton = false, bool middleButton = false, bool rightButton = false) {
globalOXOcard.handleAutoTurnOff(seconds, leftButton, middleButton, rightButton);
}
void resetAutoTurnOffCounter() {
globalOXOcard.resetAutoTurnOffCounter();
}
void resetOXOcard() {
((void (*)())0x0)(); // jumpt to address 0x00
}
bool isLeftButtonPressed() {
return globalOXOcard.isLeftButtonPressed();
}
bool isMiddleButtonPressed() {
return globalOXOcard.isMiddleButtonPressed();
}
bool isRightButtonPressed() {
return globalOXOcard.isRightButtonPressed();
}
/* LED-Matrix functions ----------------------------------------------------- */
void clearDisplay() {
globalOXOcard.matrix->fillScreen(0);
}
void turnDisplayOn() {
globalOXOcard.matrix->fillScreen(255);
}
void fillDisplay(byte brightness=255) {
globalOXOcard.matrix->fillScreen(brightness);
}
void drawPixel(byte x, byte y, byte brightness=255) {
globalOXOcard.matrix->drawPixel(x,y,brightness);
}
void clearPixel(byte x, byte y) {
globalOXOcard.matrix->drawPixel(x,y,0);
}
void drawRectangle(byte x, byte y, byte l, byte h, byte b=255) {
globalOXOcard.matrix->drawRectangle(x,y,l,h,b);
}
void drawFilledRectangle(byte x, byte y, byte l, byte h, byte b=255) {
globalOXOcard.matrix->drawFilledRectangle(x,y,l,h,b);
}
void drawLine(byte x0, byte y0, byte x1, byte y1, byte b=255) {
globalOXOcard.matrix->drawLine(x0,y0,x1,y1,b);
}
void drawCircle(byte x0, byte y0, byte r, byte b=255) {
globalOXOcard.matrix->drawCircle(x0,y0,r,b);
}
void drawFilledCircle(byte x0, byte y0, byte r, byte b=255) {
globalOXOcard.matrix->drawFilledCircle(x0,y0,r,b);
}
void drawTriangle(byte x0, byte y0, byte x1, byte y1, byte x2, byte y2, byte b=255) {
globalOXOcard.matrix->drawTriangle(x0,y0,x1,y1,x2,y2,b);
}
void drawFilledTriangle(byte x0, byte y0, byte x1, byte y1, byte x2, byte y2, byte b=255) {
globalOXOcard.matrix->drawFilledTriangle(x0,y0,x1,y1,x2,y2,b);
}
void drawImage(byte image[8], byte brightness=255) {
for (byte y = 0; y <= 7; y++) {
byte b = image[y];
for (byte x = 0; x <= 7; x++) {
if (b & (1 << (7-x))) {
globalOXOcard.matrix->drawPixel(x,y,brightness);
}
}
}
}
void drawImage(byte b0,
byte b1,
byte b2,
byte b3,
byte b4,
byte b5,
byte b6,
byte b7,
byte brightness=255) {
byte image[8] = {b0,b1,b2,b3,b4,b5,b6,b7};
drawImage(image,brightness);
}
void drawChar(byte x, byte y, char c, byte brightness=255) {
globalOXOcard.matrix->drawChar(x,y,c,brightness);
}
void drawDigit(byte x, byte y, byte digit, byte brightness=255) {
drawChar(x,y,'0'+(digit%10),brightness);
}
void drawNumber(byte number, byte brightness=255) {
if (number > 99) {
drawChar(0,1,'?',brightness);
drawChar(5,1,'?',brightness);
} else {
drawChar(0,1,'0'+(number/10),brightness);
drawChar(5,1,'0'+(number%10),brightness);
}
}
/* Accelerometer functions -------------------------------------------------- */
int getXAcceleration() {
float vector[3];
globalOXOcard.accel->getAccelerationVector(vector);
return vector[0]*255;
}
int getYAcceleration() {
float vector[3];
globalOXOcard.accel->getAccelerationVector(vector);
return vector[1]*255;
}
int getZAcceleration() {
float vector[3];
globalOXOcard.accel->getAccelerationVector(vector);
return vector[2]*255;
}
byte getOrientation() {
return byte(globalOXOcard.accel->getOrientation()); // 1 = UP, 2 = DOWN, 3 = HORIZONTALLY, 4 = VERTICALLY
}
bool isOrientationUp() {
return byte(globalOXOcard.accel->getOrientation()) == MMA7660FC::UP;
}
bool isOrientationDown() {
return byte(globalOXOcard.accel->getOrientation()) == MMA7660FC::DOWN;
}
bool isOrientationHorizontally() {
return byte(globalOXOcard.accel->getOrientation()) == MMA7660FC::HORIZONTALLY;
}
bool isOrientationVertically() {
return byte(globalOXOcard.accel->getOrientation()) == MMA7660FC::VERTICALLY;
}
/* BLE functions ------------------------------------------------------------ */
void setupAsIBeacon(String beaconName, HM11::advertInterval_t interv = HM11::INTERV_550MS) { // max. 20 characters
globalOXOcard.setupAsIBeacon(beaconName, interv);
}
void setupAsIBeacon(unsigned int beaconNr, HM11::advertInterval_t interv = HM11::INTERV_550MS) { // 1... 65'534 (0xFFFE)
globalOXOcard.setupAsIBeacon(beaconNr, interv);
}
int findIBeacon(String beaconName) {
return globalOXOcard.findIBeacon(beaconName);
}
int findIBeacon(unsigned int beaconNr) {
return globalOXOcard.findIBeacon(beaconNr);
}
void setBluetoothTxPower(byte txPower) { // 0 = -23dbm, 1 = -6dbm, 2 = 0dbm, 3 = 6dbm
globalOXOcard.ble->setTxPower(HM11::txPower_t(txPower));
}
byte getBluetoothTxPower() { // 0 = -23dbm, 1 = -6dbm, 2 = 0dbm, 3 = 6dbm
return byte(globalOXOcard.ble->getTxPower());
}
String getBluetoothMacAddress() {
return globalOXOcard.ble->getMacAddress();
}
void connectToBluetoothMacAddress(String macAddr, bool master) {
globalOXOcard.ble->connectToMacAddress(macAddr, master);
setLEDBlue(HIGH);
}
void bluetoothWrite(byte b) {
globalOXOcard.bleSerial->write(b);
}
void bluetoothPrint(char c) {
globalOXOcard.bleSerial->print(c);
}
void bluetoothPrintln(char c) {
globalOXOcard.bleSerial->println(c);
}
void bluetoothPrint(String str) {
for (byte i = 0; i < str.length(); i++) {
globalOXOcard.bleSerial->print(str[i]);
}
}
void bluetoothPrintln(String str) {
for (byte i = 0; i < str.length()-1; i++) {
globalOXOcard.bleSerial->print(str[i]);
}
globalOXOcard.bleSerial->println(str[str.length()-1]);
}
byte bluetoothRead() {
return globalOXOcard.bleSerial->read();
}
char bluetoothReadChar() {
return globalOXOcard.ble->readChar();
}
byte bluetoothAvailable() {
return globalOXOcard.bleSerial->available();
}
void bluetoothHandshaking(bool master) {
if (!(globalOXOcard.ble->handshaking(master))) resetOXOcard();
}
/* Tone functions ----------------------------------------------------------- */
void tone(int frequency, int duration = 0) {
if (frequency < 0) return;
tone(PIN_NR_PIEZO, (unsigned int)(frequency), (unsigned long)(duration));
}
void noTone() {
noTone(PIN_NR_PIEZO);
}
void playMelody(int tones[], int lengths[], int size, int pause = 100) {
for (int i = 0;i<size;i++) {
tone(tones[i]);
delay(lengths[i]);
noTone();
delay(pause);
}
}
/* Timer functions ---------------------------------------------------------- */
void resetTimer() {
millisSinceLastReset = millis();
}
int getTimerSeconds() {
return (millis() - millisSinceLastReset) / 1000;
}
void checkIfSerialOn() {
if (!serialOn) {
Serial.begin(DEBUG_BAUDRATE_OXOCARD);
serialOn = true;
}
}
/* Print functions ---------------------------------------------------------- */
void print(const String &s) {
checkIfSerialOn();
Serial.print(s);
}
void print(const char chrs[] ) {
checkIfSerialOn();
Serial.print(chrs);
}
void print(char c) {
checkIfSerialOn();
Serial.print(c);
}
void print(unsigned char c, int x = DEC) {
checkIfSerialOn();
Serial.print(c,x);
}
void print(int i, int x = DEC) {
checkIfSerialOn();
Serial.print(i,x);
}
void print(unsigned int i, int x = DEC) {
checkIfSerialOn();
Serial.print(i,x);
}
void print(long l, int x = DEC) {
checkIfSerialOn();
Serial.print(l,x);
}
void print(unsigned long l, int x = DEC) {
checkIfSerialOn();
Serial.print(l,x);
}
void println(const String &s) {
checkIfSerialOn();
Serial.println(s);
}
void println(const char chrs[] ) {
checkIfSerialOn();
Serial.println(chrs);
}
void println(char c) {
checkIfSerialOn();
Serial.println(c);
}
void println(unsigned char c, int x = DEC) {
checkIfSerialOn();
Serial.println(c,x);
}
void println(int i, int x = DEC) {
checkIfSerialOn();
Serial.println(i,x);
}
void println(unsigned int i, int x = DEC) {
checkIfSerialOn();
Serial.println(i,x);
}
void println(long l, int x = DEC) {
checkIfSerialOn();
Serial.println(l,x);
}
void println(unsigned long l, int x = DEC) {
checkIfSerialOn();
Serial.println(l,x);
}
#endif