forked from microbit-more/pxt-mbit-more-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MbitMoreDevice.h
482 lines (413 loc) · 10.5 KB
/
MbitMoreDevice.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#ifndef MBIT_MORE_DEVICE_H
#define MBIT_MORE_DEVICE_H
#include "pxt.h"
#include "MicroBit.h"
#include "MicroBitConfig.h"
#include "MbitMoreCommon.h"
#if MBIT_MORE_USE_SERIAL
#include "MbitMoreSerial.h"
class MbitMoreSerial;
#endif // MBIT_MORE_USE_SERIAL
#if MICROBIT_CODAL
#include "MbitMoreService.h"
class MbitMoreService;
#else // MICROBIT_CODAL
#include "MbitMoreServiceDAL.h"
class MbitMoreServiceDAL;
using MbitMoreService = MbitMoreServiceDAL;
#endif // NOT MICROBIT_CODAL
#if MICROBIT_CODAL
#define LIGHT_LEVEL_SAMPLES_SIZE 11
#define ANALOG_IN_SAMPLES_SIZE 5
#else // NOT MICROBIT_CODAL
#define LIGHT_LEVEL_SAMPLES_SIZE 5
#define ANALOG_IN_SAMPLES_SIZE 5
#endif // NOT MICROBIT_CODAL
#if MICROBIT_CODAL
#define MBIT_MORE_WAITING_DATA_LABELS_LENGTH 16
#define MBIT_MORE_WAITING_DATA_LABEL_NOT_FOUND 0xff
#define MBIT_MORE_DATA_LABEL_SIZE 8
#define MBIT_MORE_DATA_CONTENT_SIZE 11
#endif // MICROBIT_CODAL
/**
* @brief Button ID in MicrobitMore
* This number is used to memory offset in state data.
*/
enum MbitMoreButtonStateIndex
{
// GPIO array using [0..20]
P0 = 24,
P1 = 25,
P2 = 26,
A = 27,
B = 28,
LOGO = 29,
};
/**
* @brief Version of this micro:bit
*
*/
enum MbitMoreHardwareVersion
{
MICROBIT_V1 = 1,
MICROBIT_V2 = 2,
};
/**
* @brief Version of protocol to use
*
*/
enum MbitMoreProtocol
{
MBIT_MORE_V2 = 2,
};
/**
* Class definition for main logics of Micribit More Service except bluetooth connectivity.
*
*/
class MbitMoreDevice {
private:
/**
* Constructor.
* Create a representation of default extension for Scratch3.
* @param _uBit The instance of a MicroBit runtime.
*/
MbitMoreDevice(MicroBit &_uBit);
/**
* @brief Destroy the MbitMoreDevice object
*
*/
~MbitMoreDevice();
public:
// setup the class as singleton
MbitMoreDevice(const MbitMoreDevice &) = delete;
MbitMoreDevice &operator=(const MbitMoreDevice &) = delete;
MbitMoreDevice(MbitMoreDevice &&) = delete;
MbitMoreDevice &operator=(MbitMoreDevice &&) = delete;
/**
* @brief Get the Instance object as singleton
*
* @return MbitMoreDevice&
*/
static MbitMoreDevice &getInstance() {
static MbitMoreDevice instance(pxt::uBit);
return instance;
}
/**
* @brief Microbit runtime
*
*/
MicroBit &uBit;
/**
* @brief BLE service for basic micro:bit extension.
*
*/
MbitMoreService *basicService;
/**
* @brief BLE service for Microbit More extension.
*
*/
MbitMoreService *moreService;
#if MBIT_MORE_USE_SERIAL
/**
* @brief Microbit More serial port connector.
*
*/
MbitMoreSerial *serialService;
#endif // MBIT_MORE_USE_SERIAL
// ---------------------
/**
* @brief Whether the serial port communication is started.
*
*/
bool serialConnected = false;
/**
* @brief Index of controllabel GPIO pins.
*
*/
int gpioPin[9] = {0, 1, 2, 8, 12, 13, 14, 15, 16};
/**
* @brief Pins which is pull-up at connected.
*
*/
int initialPullUp[3] = {0, 1, 2};
bool touchMode[3] = {false};
/**
* @brief Shadow screen to display on the LED.
*
*/
uint8_t shadowPixcels[5][5] = {{0}};
/**
* Samples of Light Level.
*/
int lightLevelSamples[LIGHT_LEVEL_SAMPLES_SIZE] = {0};
/**
* @brief Last index of the Light Level Samples.
*
*/
size_t lightLevelSamplesLast = 0;
#if MICROBIT_CODAL
/**
* @brief Structure of received data in MbitMore.
*
*/
typedef struct {
char label[MBIT_MORE_DATA_LABEL_SIZE]; /** label of the data */
MbitMoreDataContentType type; /** type of the content */
uint8_t content[MBIT_MORE_DATA_CONTENT_SIZE + 1]; /** content of the data */
} MbitMoreLabeledData;
/**
* @brief Store of received data from Scratch.
*
*/
MbitMoreLabeledData receivedData[MBIT_MORE_WAITING_DATA_LABELS_LENGTH] = {{{0}}};
#endif // MICROBIT_CODAL
/**
* Samples of Light Level.
*/
int analogInSamples[3][ANALOG_IN_SAMPLES_SIZE] = {{0}};
#if MICROBIT_CODAL
/**
* @brief On-board microphone is in use or not.
*
*/
bool micInUse = false;
/**
* @brief Laudness on the microphone.
*
*/
float soundLevel = 0.0;
#endif // MICROBIT_CODAL
/**
* Protocol of microbit more.
*/
int mbitMoreProtocol;
/**
* Current mode of all pins.
*/
MbitMorePullMode pullMode[sizeof(gpioPin) / sizeof(gpioPin[0])];
/**
* @brief Set pin configuration for initial.
*
*/
void initializeConfig();
/**
* @brief Update version data on the charactaristic.
*
*/
void updateVersionData();
/**
* @brief Invoked when BLE connected.
*
* @param _e event which has connection data
*/
void onBLEConnected(MicroBitEvent _e);
/**
* @brief Invoked when BLE disconnected.
*
* @param _e event which has disconnection data
*/
void onBLEDisconnected(MicroBitEvent _e);
/**
* @brief Invoke when serial port connects.
*
*/
void onSerialConnected();
/**
* @brief Call when a command was received.
*
* @param data
* @param length
*/
void onCommandReceived(uint8_t *data, size_t length);
/**
* @brief Set the pattern on the line of the shadow pixels.
*
* @param line Index of the lines to set.
* @param pattern Array of brightness(0..255) according columns.
*/
void setPixelsShadowLine(int line, uint8_t *pattern);
/**
* @brief Display the shadow pixels on the LED.
*
*/
void displayShadowPixels();
/**
* @brief Display text on LED.
*
* @param text Contents to display with null termination.
* @param delay The time to delay between characters, in milliseconds.
*/
void displayText(char *text, int delay);
/**
* @brief Update GPIO and sensors state.
*
* @param data Buffer for BLE characteristics.
*/
void updateState(uint8_t *data);
/**
* @brief Update data of motion.
*
* @param data Buffer for BLE characteristics.
*/
void updateMotion(uint8_t *data);
/**
* @brief Get data of analog input of the pin.
*
* @param data Buffer for BLE characteristics.
* @param pinIndex Index of the pin [0, 1, 2].
*/
void updateAnalogIn(uint8_t *data, size_t pinIndex);
/**
* @brief Sample current light level and return filtered value.
*
* @return int Filtered light level.
*/
int sampleLightLevel();
/**
* @brief Set PMW signal to the speaker pin for play tone.
*
* @param period PWM period (1000000 / frequency)[us]
* @param volume laudness of the sound [0..255]
*/
void playTone(int period, int volume);
/**
* @brief Stop playing tone.
*
*/
void stopTone();
#if MICROBIT_CODAL
/**
* @brief Return index for the label
*
* @param dataLabel label to find
* @param dataType type of the data
* @return int index of the label
*/
int findWaitingDataLabelIndex(const char *dataLabel, MbitMoreDataContentType dataType);
/**
* @brief Register data label and retrun ID for the label.
*
* @param dataLabel label to register
* @param dataType type of the data
* @return int ID for the label
*/
int registerWaitingDataLabel(ManagedString dataLabel, MbitMoreDataContentType dataType);
/**
* @brief Get type of content for the labeled data
*
* @param labelID ID of the label in received data
* @return content type
*/
MbitMoreDataContentType dataType(int labelID);
/**
* @brief Return content of the data as number
*
* @param labelID ID of the label in received data
* @return content of the data
*/
float dataContentAsNumber(int labelID);
/**
* @brief Return content of the data as text
*
* @param labelID ID of the label in received data
* @return content of the data
*/
ManagedString dataContentAsText(int labelID);
/**
* @brief Send number with label.
*
* @param dataLabel
* @param dataContent
*/
void sendNumberWithLabel(ManagedString dataLabel, float dataContent);
/**
* @brief Send text with label.
*
* @param dataLabel
* @param dataContent
*/
void sendTextWithLabel(ManagedString dataLabel, ManagedString dataContent);
#endif // MICROBIT_CODAL
/**
* Callback. Invoked when a pin event sent.
*/
void onPinEvent(MicroBitEvent evt);
/**
* @brief Display friendly name of the micro:bit.
*
*/
void displayFriendlyName();
/**
* @brief Display software version of Microbit More.
*
*/
void displayVersion();
private:
/**
* @brief Listen pin events on the pin.
* Make it listen events of the event type on the pin.
* Remove listener if the event type is MICROBIT_PIN_EVENT_NONE.
*
* @param pinIndex index in edge pins
* @param eventType type of events
*/
void listenPinEventOn(int pinIndex, int eventType);
/**
* @brief Set pull-mode.
*
* @param pinIndex index to set
* @param pull pull-mode to set
*/
void setPullMode(int pinIndex, MbitMorePullMode pull);
/**
* @brief Set the value on the pin as digital output.
*
* @param pinIndex index in edge pins
* @param value digital value [0 | 1]
*/
void setDigitalValue(int pinIndex, int value);
/**
* @brief Set the value on the pin as analog output (PWM).
*
* @param pinIndex index in edge pins
* @param value analog value (0..1024)
*/
void setAnalogValue(int pinIndex, int value);
/**
* @brief Set the value on the pin as servo driver.
*
* @param pinIndex index in edge pins
* @param angle the level to set on the output pin, in the range 0 - 180.
* @param range which gives the span of possible values the i.e. the lower and upper bounds (center +/- range/2). Defaults to DEVICE_PIN_DEFAULT_SERVO_RANGE.
* @param center the center point from which to calculate the lower and upper bounds. Defaults to DEVICE_PIN_DEFAULT_SERVO_CENTER
*/
void setServoValue(int pinIndex, int angle, int range, int center);
/**
* @brief Invoked when button state changed.
*
* @param evt event which has button states
*/
void onButtonChanged(MicroBitEvent evt);
/**
* @brief Invoked when gesture state changed.
*
* @param evt event which has gesture states.
*/
void onGestureChanged(MicroBitEvent evt);
/**
* @brief Normalize angle when upside down.
*
* @param heading value of the compass heading
* @return normalizes angle relative to north [degree]
*/
int normalizeCompassHeading(int heading);
/**
* @brief Whether the pin is a GPIO of not.
*
* @param pinIndex index in edge pins
* @return true the pin is a GPIO
* @return false the pin is not a GPIO
*/
bool isGpio(int pinIndex);
};
#endif // MBIT_MORE_DEVICE_H