-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble-proximity.ino
397 lines (373 loc) · 8.57 KB
/
ble-proximity.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
#include <SoftwareSerial.h>
#include <LowPower.h>
SoftwareSerial ble(8, 5); // TX, RX
#define DEBUG 0
#define CLEAN_CYCLE 1000
#define TIMEOUT 1000
#define SHORT_TIMEOUT 50
#define ACTIVE_WAIT_INTERVAL 10
#define CLIENT_SLEEP_TIME SLEEP_8S
#define SERVER_SLEEP_TIME SLEEP_8S
#define BLE_DELAY 1000
// 0 --> 35
// .25 --> 45
// 1m --> 50
// 2m --> 60
// 5m --> 70
// 20m --> 80
// 60m --> 90
// Przekraczanie maksow nie jest zle, bo jest cap.
#define MIN_RSSI 40
#define MAX_RSSI 95
#define SMOOTHING 10
const int UNCERTAIN = 0;
const int CONNECTED_AS_SERVER = 1;
const int CONNECTED_AS_CLIENT = 2;
int state = UNCERTAIN;
char buffer[50];
#define N 2
String MACS[] = {
"D436399B4914",
"D436399B3A02",
};
String addr;
#define FAIL "FAIL"
int blueLED = 6;
int lampStrength = 0;
void soft_reset() {
asm volatile (" jmp 0");
}
void debug(String s) {
Serial.println(s);
}
void debug(String name, long val) {
String result = name + ": " + val;
debug(result);
}
int toInt(String s) {
int result = 0;
int currentTen = 1;
for (int i = s.length() - 1; i >= 0; i--) {
result += currentTen * (s[i] - '0');
currentTen *= 10;
}
//debug("toInt", result);
return result;
}
// WHO ARE WE
int zz_which_am_i = -1;
int which_am_i() {
if (zz_which_am_i == -1) {
for (int i = 0; i < N; i++) {
if (addr == MACS[i]) {
zz_which_am_i = i;
break;
}
}
}
return zz_which_am_i;
}
const int my_pair() {
return which_am_i() + (is_server() ? 1 : -1);
}
boolean is_server() {
if (state == CONNECTED_AS_SERVER) return true;
if (which_am_i() == -1) return false;
return which_am_i() % 2 == 0;
}
boolean is_client() {
if (state == CONNECTED_AS_CLIENT) return true;
if (which_am_i() == -1) return false;
return which_am_i() % 2 == 1;
}
// END OF WHO ARE WE
// SETUP
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, 0);
pinMode(blueLED, OUTPUT);
ble.begin(9600);
Serial.begin(9600);
refreshFade();
delay(BLE_DELAY);
readRSSI();
check_identity();
if (which_am_i() == -1) {
// If we don't know who we are, we can't do much.
return;
}
sendSet("AT+POWE", "2");
sendSet("AT+IMME", is_server() ? "1" : "0");
// What happens in connected state. Remote control.
sendSet("AT+MODE", is_server() ? "0" : "2");
// LED
sendSet("AT+PIO1", DEBUG ? "0" : "1");
}
// Can only be called after readRSSI.
bool check_identity() {
if (state == CONNECTED_AS_SERVER) return false;
toBle("AT+ADDR?");
addr = consumeAnswer("OK+ADDR:", 12, TIMEOUT);
if (addr == FAIL) {
return false;
}
setName();
sendSet("AT+ROLE", is_server() ? "1" : "0");
return which_am_i() != -1;
}
void sendSet(String prefix, String value) {
String result = prefix + value;
toBle(result);
consumeAnswer("OK+Set:", value.length(), SHORT_TIMEOUT);
}
void setName() {
String name = "Z";
name = name + which_am_i();
sendSet("AT+NAME", name);
}
// END OF SETUP
// SLEEP
bool sleeping = false;
void wake_up() {
if (sleeping) {
// TODO(kkrolak): Ignoring an error here.
ble_wake();
sleeping = false;
}
readRSSI();
if (state != CONNECTED_AS_SERVER) {
// This might change due to sleeping
sendSet("AT+ROLE", is_server() ? "1" : "0");
}
}
// You can only go to sleep if you know who you are. Otherwise just reset.
void sleep(period_t duration) {
if (!is_client() && !is_server()) {
soft_reset();
}
// Scary shit.
sendSet("AT+ROLE", "0");
if (!ble_sleep()) {
return;
}
// Don't ask me.
delay(BLE_DELAY);
sleeping = true;
ard_sleep(duration);
}
bool ble_sleep() {
toBle("AT+SLEEP");
return consumeAnswer("OK+SLEEP", 0, SHORT_TIMEOUT) != FAIL;
}
bool ble_wake() {
toBle(
"Litwo ojczyzno moja, Ty jestes jak zdrowie. Ile cie trzeba cenic ten tylko sie dowie, kto cie straci.");
// What you know is true.
delay(BLE_DELAY);
return consumeAnswer("OK+WAKE", 0, SHORT_TIMEOUT) != FAIL;
}
// END OF SLEEP
// CLIENT
// Can be called by potential server, when we don't know who we are.
void client() {
String result = consumeAnswer("BS", 3, TIMEOUT);
if (result != FAIL) {
state = CONNECTED_AS_CLIENT;
setSignal(toInt(result));
} else {
if (getSignal() == 0) {
sleep(CLIENT_SLEEP_TIME);
}
}
}
// END OF CLIENT
// SERVER
void connect() {
String command = "AT+CON" + MACS[my_pair()];
toBle(command);
String result = consumeAnswer("OK+CONN", 1, TIMEOUT);
if (result == "A") {
state = CONNECTED_AS_SERVER;
} else {
state = UNCERTAIN;
}
}
// Can be done under client, will set state to UNCERTAIN.
void readRSSI() {
toBle("AT+RSSI?");
String result = consumeAnswer("OK+Get:-", 2, TIMEOUT);
if (result == FAIL) {
state = UNCERTAIN;
} else {
state = CONNECTED_AS_SERVER;
int rssi = toInt(result);
if (rssi < MIN_RSSI) {
rssi = MIN_RSSI;
}
if (rssi > MAX_RSSI) {
rssi = MAX_RSSI;
}
// Receiving any rssi is a signal of at least perceivable 5%.
int signal = map(-rssi, -MAX_RSSI, -MIN_RSSI, 0, 240) + 15;
//Serial.println(rssi);
setSignal(signal);
sprintf(buffer, "BS%03d", signal);
toBle(String(buffer));
}
}
// Can only be called if we are certain that we are the server.
void connectOrSleep() {
if (state == UNCERTAIN) {
connect();
}
if (state == UNCERTAIN && getSignal() == 0) {
// If start is still uncertain, better sleep a bit to wait.
sleep(SERVER_SLEEP_TIME);
}
}
// END OF SERVER
// COMM
void toBle(String s) {
debug("Sending to BLE: " + s);
//ble.print(s);
for (int i = 0; i < s.length(); i++) {
ble.print(s[i]);
}
}
String consumeAnswer(String prefix, int additional, int timeout) {
if (prefix == "") {
debug("Cleanup cycle with consumeAnswer");
} else {
debug("consumeAnswer with prefix " + prefix);
}
String result = "";
int remaining = timeout;
for (int i = 0; i < prefix.length() + additional; i++) {
startTimer();
char r = activeRead(remaining);
if (r <= 0) {
debug(String("activeRead returned error. Returning FAIL. Read so far: ") + result);
return FAIL;
}
//Serial.println(r);
if (i >= prefix.length()) {
result = result + r;
}
if (i < prefix.length() && r != prefix[i]) {
debug(String("consumeAnswer encountered invalid prefix, at ") + i + ", dropping:");
Serial.println(r);
return FAIL;
}
timeout -= stopTimer();
}
if (remaining <= 0) {
debug("consumeAnswer failed timeout. Returning FAIL");
debug("remaining", remaining);
return FAIL;
}
debug("consumeAnswer returning " + result);
return result;
}
char activeRead(int timeout) {
int remaining = timeout;
while (!ble.available()) {
if (remaining <= 0) {
return -1;
}
delay(ACTIVE_WAIT_INTERVAL);
remaining -= ACTIVE_WAIT_INTERVAL;
}
return ble.read();
}
// END OF COMM
long long startAt = 0;
void startTimer() {
startAt = millis();
}
long long stopTimer() {
return millis() - startAt;
}
// VISUAL
int history[SMOOTHING];
int currHistory = 0;
long long whenFade = 0;
const int FADE_TIME = 2000;
void refreshFade() {
whenFade = millis() + FADE_TIME;
}
void maybeFade() {
if (whenFade < millis()) {
setSignal(0);
}
}
int setSignal(int s) {
//debug("new signal", s);
refreshFade();
history[currHistory] = s;
currHistory = (currHistory + 1) % SMOOTHING;
}
int getSignal() {
int smoothedSignal = 0;
for (int i = 0; i < SMOOTHING; i++) {
smoothedSignal += history[i];
}
smoothedSignal = smoothedSignal / SMOOTHING;
//debug("smoothedSignal", smoothedSignal);
return smoothedSignal;
}
int lamp() {
//int result = smoothedSignal * (whenReset - millis()) / RESET_TIME;
int smoothedSignal = getSignal();
int result = 1.0L * smoothedSignal * smoothedSignal / 255;
//debug("perception", result);
return result;
}
// END OF VISUAL
void loop() {
state = UNCERTAIN;
if (sleeping) {
wake_up();
}
maybeFade();
if (DEBUG) {
passthrough();
} else {
work();
}
analogWrite(blueLED, lamp());
}
void work() {
if (!is_client()) {
// Only try RSSI, when we are certain we are not the client to save cycles.
readRSSI();
}
if (is_server()) {
connectOrSleep();
} else {
client();
}
// Spend the rest of the time cleaning up shit.
consumeAnswer("", 30, CLEAN_CYCLE);
}
void passthrough() {
char c;
String s = "AT+SLEEP";
if (Serial.available()) {
c = Serial.read();
if (c != 'X') {
ble.print(c);
}
}
if (c == 'X') {
//toBle("AT+SLEEP");
sleep(SLEEP_4S);
} else {
if (ble.available()) {
c = ble.read();
Serial.print(c);
}
}
}
void ard_sleep(period_t sleep_time) {
LowPower.powerDown(sleep_time, ADC_OFF, BOD_OFF);
}