-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
400 lines (383 loc) · 9.58 KB
/
main.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
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
#include <TFT.h>
#include <SPI.h>
#include <Esplora.h>
#define READ_PAUSE 50 //pause between reading another value of sensor
//following values refers to how many of the read_pause it will take
#define LIGHTS_SIZE 100 //this many past values it will remember
#define TIME_BOUND 20 //everything above is detected as -
#define PAUSE_LENGTH 40 //after this many stable detects, the char is constructed from the signals
#define DEVIATION 10 //if the signal differs more than this, then it will be detected as - or .
//-------------------------------------------------------
// Global variables
//variables for finite-state machine
enum states {WAITING, DETECTING, READING, DISPLAYING};
enum states state = WAITING, nextState;
int lights[LIGHTS_SIZE]; //array that remembers intensity of the light in last LIGHTS_SIZE * READ_PAUSE / 1000 seconds
int stable; //average light intensity intensity of the last stable interval of length LIGHTS_SIZE * READ_PAUSE / 1000
int charsShown = 0; //how many chars are shown on the screen
//-------------------------------------------------------
//Changing screens for different states
void setupScreen()
{
EsploraTFT.background(255, 255, 255);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.setTextSize(3);
EsploraTFT.text("Wait", 10, 30);
EsploraTFT.text("loading", 10, 70);
}
void waitingScreen()
{
EsploraTFT.background(255, 255, 255);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.setTextSize(3);
EsploraTFT.text("flash S", 10, 30);
EsploraTFT.text("to START", 10, 70);
}
void readingScreen()
{
EsploraTFT.background(255, 255, 255);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.setTextSize(1);
EsploraTFT.text("Listening", 0, 0);
}
void changeToDetectingScreen()
{
EsploraTFT.fill(255, 255, 255);
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.rect(0, 0, EsploraTFT.width(), 10);
EsploraTFT.setTextSize(1);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text("detecting", 0, 0);
}
void changeToDisplayingScreen(bool exceeded = false)
{
EsploraTFT.fill(255, 255, 255);
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.rect(0, 0, EsploraTFT.width(), 10);
EsploraTFT.setTextSize(1);
EsploraTFT.stroke(0, 0, 0);
if(exceeded)
{
EsploraTFT.text("Limit exceeded, showing", 0, 0);
}
else
{
EsploraTFT.text("Showing results", 0, 0);
}
EsploraTFT.text("F->Finish, C->Continue,", 0, EsploraTFT.height() - 20);
EsploraTFT.text("B->Backspace", 0, EsploraTFT.height() - 10);
}
void backToReadingScreen()
{
EsploraTFT.fill(255, 255, 255);
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.rect(0, 0, EsploraTFT.width(), 10);
EsploraTFT.rect(0, EsploraTFT.height() - 20, EsploraTFT.width(), EsploraTFT.height());
EsploraTFT.setTextSize(1);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text("Listening", 0, 0);
}
//-------------------------------------------------------
//working with light intensity
//returns light intensity at the moment
int readLight()
{
delay(READ_PAUSE);
return Esplora.readLightSensor();
}
//returns average light intensity in the last LIGHTS_SIZE * READ_PAUSE / 1000 seconds
int getAvg()
{
unsigned long int sum = 0; //int is only 16 bits long
for(int i = 0; i<LIGHTS_SIZE; ++i)
{
sum += lights[i];
}
return sum/LIGHTS_SIZE;
}
//updates stable to the average light intensity in last LIGHTS_SIZE * READ_PAUSE / 1000 seconds
void updateStable()
{
stable = getAvg();
}
//waits until the light intensity is stable for LIGHTS_SIZE * READ_PAUSE / 1000 seconds and then sets stable
void setupStable()
{
setupScreen();
for(int i = 0; i<LIGHTS_SIZE; ++i)
{
rememberLight(readLight());
}
while(!isStable())
{
rememberLight(readLight());
}
updateStable();
}
//saves new light intensity and discards th last one
void rememberLight(int l)
{
for(int i = 0; i<LIGHTS_SIZE - 1; ++i)
{
lights[i] = lights[i + 1];
}
lights[LIGHTS_SIZE - 1] = l;
}
//checks if all of the measured light intensities are within a DEVIATION from average light intensity in last LIGHTS_SIZE * READ_PAUSE / 1000 seconds
bool isStable()
{
int lmin = 1024;
int lmax = 0;
unsigned long int sum = 0; //int is only 16 bits long
for(int i = 0; i<LIGHTS_SIZE; ++i)
{
if(lights[i] < lmin)
{
lmin = lights[i];
}
if(lights[i] > lmax)
{
lmax = lights[i];
}
sum += lights[i];
}
int avg = sum/LIGHTS_SIZE;
return lmax - avg < DEVIATION && avg - lmin < DEVIATION;
}
//checks if last saved light intensity is within a DEVIATION from stable
bool isLastStable()
{
return abs(lights[LIGHTS_SIZE - 1] - stable) < DEVIATION;
}
//-------------------------------------------------------
//Interpreting signal
//checks end of sequence that defines letter
bool isPause()
{
for(int i = LIGHTS_SIZE - 1; i > LIGHTS_SIZE - PAUSE_LENGTH - 1; --i)
{
if(abs(lights[i] - stable) > DEVIATION)
{
return false;
}
}
return true;
}
//goes through lights[] until it finds the start of signal and then decides if it was - or .
bool isLong()
{
//We don't want to check the last one because that is stable
for(int i = LIGHTS_SIZE - 2; i>=0; --i)
{
if(abs(lights[i] - stable) < DEVIATION)
{
return (LIGHTS_SIZE - i) > TIME_BOUND;
}
}
return true;
}
//-------------------------------------------------------
//Morse decoding
//returns letter based on sequence of signals, signals are received like this: . = 0, - = 1
char morseToChar(int code, int l)
{
//# means undefined or invalid character
char l1[2] = {'E', 'T'};
char l2[4] = {'I', 'A', 'N', 'M'};
char l3[8] = {'S', 'U', 'R', 'W', 'D', 'K', 'G', 'O'};
char l4[16] = {'H', 'V', 'F', '#', 'L', '#', 'P', 'J', 'B', 'X', 'C', 'Y', 'Z', 'Q', '#', '#'};
char l5[32] = {'5', '4', '#', '3', '#', '#', '#', '2', '#', '#', '+', '#', '#', '#', '#', '1', '6', '=', '/', '#', '#', '#', '#', '#', '7', '#', '#', '#', '8', '#', '9', '0'};
if(code < pow(2, l))
{
switch(l)
{
case 0: return '@'; //this is not an invalid character from user but from the measuring so we want to distinguish them
case 1: return l1[code];
case 2: return l2[code];
case 3: return l3[code];
case 4: return l4[code];
case 5: return l5[code];
default: return '#';
}
}
return '#';
}
//packs signals into a sequence, then through function morseToChar, it returns letter
char readChar()
{
int res = 0, resl = 0;
bool awaiting = true;
while(!isPause())
{
rememberLight(readLight());
if(isLastStable())
{
if(awaiting)
{
++resl;
res = res << 1;
res += isLong() ? 1 : 0;
awaiting = false;
}
}
else
{
awaiting = true;
}
}
return morseToChar(res, resl);
}
//-------------------------------------------------------
//showing received letters to the screen
//setting x and y coordinates to the given return variables
void getCoordinates(int &x, int &y)
{
x = (charsShown % 14) * 10 + 10; //how many letters fit the screen + offset
y = (charsShown / 14) * 10 + 15;
}
//shows given char to the screen at next position
bool showChar(char c)
{
if(charsShown >= 14 * 9)
{
return false;
}
int xpos,ypos;
getCoordinates(xpos, ypos);
EsploraTFT.setTextSize(1);
EsploraTFT.stroke(0, 0, 0);
char show[2];
show[0] = c;
show[1] = 0;
EsploraTFT.text(show, xpos, ypos);
++charsShown;
return true;
}
//erases last character
void backspace()
{
if(charsShown == 0)
{
return;
}
--charsShown;
int xpos, ypos;
getCoordinates(xpos, ypos);
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.fill(255, 255, 255);
EsploraTFT.rect(xpos, ypos, xpos + 10, ypos + 10);
EsploraTFT.stroke(0, 0, 0);
}
void setup()
{
Serial.begin(9600);
EsploraTFT.begin();
setupStable();
waitingScreen();
}
void loop()
{
rememberLight(readLight());
Esplora.writeRed(0);
switch(state) //finite-state machine
{
case WAITING:
if(isLastStable())
{
updateStable();
nextState = WAITING;
}
else
{
changeToDetectingScreen();
nextState = DETECTING;
}
break;
case DETECTING:
if(isStable())
{
waitingScreen();
updateStable();
nextState = WAITING;
}
else
{
if(readChar() == 'S')
{
readingScreen();
charsShown = 0;
nextState = READING;
}
else
{
Esplora.writeRed(50);
nextState = DETECTING;
}
}
break;
case READING:
if(!isLastStable())
{
char c = readChar();
if(c == '#')
{
Esplora.writeRed(50);
}
else if(c == '@')
{}
else
{
if(showChar(c))
{
nextState = READING;
}
else
{
changeToDisplayingScreen(true);
nextState = DISPLAYING;
}
}
}
else if(isStable())
{
changeToDisplayingScreen();
nextState = DISPLAYING;
}
else
{
nextState = READING;
}
break;
case DISPLAYING:
if(isLastStable())
{
nextState = DISPLAYING;
}
else
{
char c = readChar();
if(c == 'F')
{
setupStable();
waitingScreen();
nextState = WAITING;
}
else if(c == 'C')
{
backToReadingScreen();
nextState = READING;
}
else if(c == 'B')
{
backspace();
nextState = DISPLAYING;
}
else
{
Esplora.writeRed(50);
}
}
break;
}
state = nextState;
}