This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScratchMoreService.h
154 lines (119 loc) · 3.03 KB
/
ScratchMoreService.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
#ifndef SCRATCH_MORE_SERVICE_H
#define SCRATCH_MORE_SERVICE_H
#include "MicroBit.h"
#define SCRATCH_MORE_ID 2000
#define SCRATCH_MORE_EVT_NOTIFY 1
// UUIDs for our service and characteristics
extern const uint16_t ScratchMoreServiceUUID;
extern const uint8_t ScratchMoreServiceTxUUID[];
extern const uint8_t ScratchMoreServiceRxUUID[];
/**
* Class definition for a MicroBitMore Service.
* Provides a BLE service to remotely read the state of sensors from Scratch3.
*/
class ScratchMoreService
{
public:
/**
* Constructor.
* Create a representation of the ScratchMoreService
* @param _uBit The instance of a MicroBit runtime.
*/
ScratchMoreService(MicroBit &_uBit);
/**
* Notify data to Scratch3.
*/
void notify();
/**
* Set value to Slots.
*/
void setSlot(int slot, int value);
/**
* Get value to Slots.
*/
int getSlot(int slot);
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
void onDataWritten(const GattWriteCallbackParams *params);
/**
* Invocked when the bluetooth connected.
*/
void onBLEConnected(MicroBitEvent e);
void updateDigitalValues();
void updateAnalogValues();
private:
// Data format [1,2,3] to send.
uint8_t txDataFormat;
// Sending data to Scratch3.
uint8_t txData[20];
/**
* Sending data buffer 01.
*/
uint8_t txBuffer01[20];
/**
* Sending data buffer 02.
*/
uint8_t txBuffer02[20];
/**
* Sending data buffer 03.
*/
uint8_t txBuffer03[20];
// Recieving buffer from Scratch3.
uint8_t rxBuffer[20];
/**
* Button state.
*/
int buttonAState;
int buttonBState;
/**
* Hold gesture state until next nofification.
*/
int gesture;
/**
* Save the last accelerometer values to conpaire current for detecting moving.
*/
int lastAcc[3];
/**
* Heading angle of compass.
*/
int compassHeading;
uint32_t digitalValues;
uint16_t analogValues[6];
/**
* Slots
*/
int16_t slots[4];
void setInputMode(int pinIndex);
void setDigitalValue(int pinIndex, int value);
void setAnalogValue(int pinIndex, int value);
void setServoValue(int pinIndex, int angle, int range, int center);
void onButtonChanged(MicroBitEvent);
void onAccelerometerChanged(MicroBitEvent);
void updateGesture(void);
void resetGesture(void);
int normalizeCompassHeading(int heading);
int convertToTilt(float radians);
void composeDefaultData(uint8_t *buff);
void composeTxBuffer01(void);
void composeTxBuffer02(void);
void composeTxBuffer03(void);
void displayFriendlyName();
// microbit runtime instance
MicroBit &uBit;
// Handles to access each characteristic when they are held by Soft Device.
GattAttribute::Handle_t txCharacteristicHandle;
GattAttribute::Handle_t rxCharacteristicHandle;
enum ScratchBLECommand
{
CMD_PIN_CONFIG = 0x80,
CMD_DISPLAY_TEXT = 0x81,
CMD_DISPLAY_LED = 0x82,
CMD_PIN_INPUT = 0x90,
CMD_PIN_OUTPUT = 0x91,
CMD_PIN_PWM = 0x92,
CMD_PIN_SERVO = 0x93,
CMD_SLOT_VALUE = 0xA0
};
};
#endif