-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonboard.ino
199 lines (176 loc) · 4.8 KB
/
monboard.ino
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
#include <bluefruit.h>
#include <Vector.h>
#include <Adafruit_NeoPixel.h>
#define LED_STRIPE_PIN 6
#define LED_NUMBER 200
#define PROBLEM_LEN 60
#define LED_OFFSET 0
#define HOLD_LEN 10
#define ZERO (-48)
#define START0 'l'
#define START1 '#'
#define START2STOP '#'
#define DEL ','
struct hold_{
uint8_t type;
uint8_t number;
};
Adafruit_NeoPixel pixels(LED_NUMBER, LED_STRIPE_PIN, NEO_RGB + NEO_KHZ800);
enum class State:uint32_t {wait0,wait1,acquire,ready,error};
// BLE Service
BLEUart bleuart; // uart over ble
uint32_t connections_since_start;
void setup()
{
Serial.begin(115200);
Serial.println("MOONBOARD");
Serial.println("---------\n");
pixels.begin();
pixels.show();
Bluefruit.autoConnLed(true);
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();
Bluefruit.setTxPower(4);
Bluefruit.setName("Moonboard A");
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
bleuart.begin();
startAdv();
Serial.println("Moonboard Ready");
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addService(bleuart);
Bluefruit.ScanResponse.addName();
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop()
{
enum class State state=State::wait0;
//Vector continig the hold characters
char hold_storage_array[HOLD_LEN];
Vector<char> current_hold(hold_storage_array);
//Vector containing the problem
struct hold_ problem_storage_array[PROBLEM_LEN];
Vector<struct hold_> problem(problem_storage_array);
while(true)
{
//begin loop
while ( bleuart.available() )
{//begin bleuart available
char ch;
ch = bleuart.read();
switch(ch)
{
case START0:
if( (state==State::wait0) || (state == State::error) )
{
state=State::wait1;
}else{
Serial.println(format_unexpected(ch,state));
state=State::error;
}
break;
case START2STOP:
if (state==State::wait1)
{
state=State::acquire;
current_hold.clear();
problem.clear();
}else if (state==State::acquire)
{
problem.push_back(hold_from_char(current_hold));
current_hold.clear();
Serial.println("NEW problem.");
state=State::ready;
}else{
Serial.println(format_unexpected(ch,state));
state=State::error;
}
break;
case DEL:
if(state==State::acquire)
{
problem.push_back(hold_from_char(current_hold));
current_hold.clear();
}else
{
Serial.println(format_unexpected(ch, state));
state=State::error;
}
break;
default:
if (state==State::acquire)
{
current_hold.push_back(ch);
}else
{
Serial.println(format_unexpected(ch, state));
state=State::error;
}
break;
}
Serial.flush();
}//end bleuart available
if(state==State::ready)
{
show_problem(problem, pixels);
state=State::wait0;
}
}//end loop
}
struct hold_ hold_from_char(Vector<char>& hold){
struct hold_ temp_hold;
int zero = ZERO;
temp_hold.number=0;
int base=1;
for (size_t i = hold.size()-1; i > 0; i--)
{
temp_hold.number += (zero + ((uint8_t)hold.at(i)))*base;
base*=10;
}
temp_hold.type=hold.at(0);
return temp_hold;
}
void show_problem(Vector<struct hold_>& problem, Adafruit_NeoPixel& pixels)
{
pixels.clear();
for (size_t i = 0; i < problem.size(); i++)
{
switch (problem[i].type)
{
case 'S':
pixels.setPixelColor(problem[i].number + LED_OFFSET,0,255,0);
break;
case 'P':
pixels.setPixelColor(problem[i].number + LED_OFFSET,0,0,255);
break;
case 'E':
pixels.setPixelColor(problem[i].number + LED_OFFSET,255,0,0);
break;
}
}
pixels.show();
}
String format_unexpected(char ch, State state)
{
String uexpected="Unexpected character: ";
return uexpected + ch + "; state: " + static_cast<uint32_t>(state) + ".";
}
void connect_callback(uint16_t conn_handle)
{
Serial.print("Device Connected");
Serial.println(++connections_since_start, DEC);
Serial.flush();
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
}