-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·131 lines (115 loc) · 3.76 KB
/
main.cpp
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
#include "MicroBit.h"
MicroBit uBit;
const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
uint8_t advertising = 0;
uint8_t tx_power_level = 6;
uint8_t AdvData [26] = {0xff, 0xff,
0x55, 0x98,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t buttonsState = 0;
uint8_t eventCounter = 0;
char digit(uint8_t n)
{
return '0' + n;
}
void startAdvertising()
{
int connectable = 0;
int interval = 160;
uBit.bleManager.setTransmitPower(tx_power_level);
uBit.bleManager.ble->setAdvertisingType(connectable ? GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED : GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
uBit.bleManager.ble->setAdvertisingInterval(interval);
uBit.bleManager.ble->clearAdvertisingPayload();
uBit.bleManager.ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
uBit.bleManager.ble->accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
uBit.bleManager.ble->startAdvertising();
uBit.display.printAsync("ULTIBO 98", 200);
advertising = 1;
}
void updatePayload ()
{
uBit.bleManager.ble->clearAdvertisingPayload();
uBit.bleManager.ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
AdvData [4] = eventCounter;
for (int i = 20; i >= 1; i--)
{
AdvData [i + 5] = AdvData [i + 5 - 1];
}
AdvData [5] = (buttonsState << 6);
uBit.bleManager.ble->accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
}
void stopAdvertising()
{
uBit.bleManager.stopAdvertising();
uBit.display.scroll("OFF");
advertising = 0;
}
char text[] = "?";
void onButton(MicroBitEvent e)
{
uint8_t mask;
uint8_t prev = buttonsState;
if (e.source == MICROBIT_ID_BUTTON_A)
mask = 0x01;
if (e.source == MICROBIT_ID_BUTTON_B)
mask = 0x02;
if (e.source == MICROBIT_ID_BUTTON_AB)
mask = 0x04;
if (e.source == MICROBIT_ID_IO_P0)
mask = 0x08;
if (e.source == MICROBIT_ID_IO_P1)
mask = 0x10;
if (e.source == MICROBIT_ID_IO_P2)
mask = 0x20;
if (e.value == MICROBIT_BUTTON_EVT_DOWN)
buttonsState |= mask;
if (e.value == MICROBIT_BUTTON_EVT_UP)
buttonsState &= ~mask;
if (buttonsState != prev)
{
if (eventCounter == 255)
{
eventCounter = 128;
}
else
{
eventCounter = eventCounter + 1;
}
switch (buttonsState)
{
case 0: text [0] = ' ';
break;
case 1: text [0] = 'A';
break;
case 2: text [0] = 'B';
break;
case 3: text [0] = '2';
break;
default: text [0] = '?';
}
uBit.display.printAsync (text);
updatePayload ();
}
// if (e.value == MICROBIT_BUTTON_EVT_CLICK)
// uBit.serial.printf("CLICK");
//
// if (e.value == MICROBIT_BUTTON_EVT_LONG_CLICK)
// uBit.serial.printf("LONG_CLICK");
//
// if (e.value == MICROBIT_BUTTON_EVT_HOLD)
// uBit.serial.printf("HOLD");
//
// if (e.value == MICROBIT_BUTTON_EVT_DOUBLE_CLICK)
// uBit.serial.printf("DOUBLE_CLICK");
//
// uBit.serial.printf("\r\n");
}
int main()
{
uBit.init();
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton);
startAdvertising();
release_fiber();
}