-
Notifications
You must be signed in to change notification settings - Fork 1
/
idrop_2_connection.ino
73 lines (68 loc) · 1.58 KB
/
idrop_2_connection.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
void idrop_loop() {
idrop_send();
idrop_recv();
}
void idrop_send() {
idrop_emit(true);
boolean mark = idrop_connected?idrop_next_bit():true;
delayMicroseconds(mark?1000:500);
idrop_emit(false);
}
void idrop_recv() {
if (idrop_connected) {
idrop_recv_connected();
} else {
idrop_recv_idle();
}
}
void idrop_recv_idle() {
// Look for light 4ms
boolean wasLight = false;
for (byte i = 0; i < 40; i++) {
boolean nowLit = idrop_detect();
if (wasLight && nowLit) {
idrop_connected = true;
return;
}
wasLight = nowLit;
}
}
void idrop_recv_connected() {
byte lightsSeen = 0;
boolean lit1 = false;
boolean lit2 = false;
byte darkCount = 0;
boolean waitForDark = false;
for (byte i = 0; i < 40; i++) {
boolean nowLit = idrop_detect();
if (nowLit) lightsSeen++;
if (!waitForDark) { // Waiting for 2 lit, then dark
if (lit1 && lit2 && !nowLit) { // We were lit, now darkness
waitForDark = true;
darkCount = 0;
} else {
// Advance everything along, I guess.
lit1 = lit2;
lit2 = nowLit;
}
} else { // waitForDark
if (nowLit) { // We were expecting dark, but got light instead, reset
waitForDark = false;
lit1 = false;
lit2 = false;
} else {
darkCount++;
if (darkCount >= 10) {
if (lightsSeen <= 6) { // space
idrop_recv_bit(false);
return;
} else { // mark
idrop_recv_bit(true);
return;
}
}
}
}
}
idrop_connected = false;
}