-
Notifications
You must be signed in to change notification settings - Fork 1
/
idrop_3_bit.ino
77 lines (75 loc) · 1.62 KB
/
idrop_3_bit.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
void idrop_recv_bit(boolean mark) {
static byte state = 0;
static char rchar;
static byte ri;
if (state == 0) { // Skip the first bit
state = 1;
} else if (state == 1) { // Idle
if (!mark) {
state = 2; // A space was received, the byte begins
rchar = 0x00;
ri=0;
}
} else if (state == 2) {
if (mark) {
rchar |= (1 << ri);
}
if (ri == 8) {
state = 3;
}
ri++;
} else if (state == 3) {
if (mark) {
idrop_recv_byte(rchar);
state=1;
} else {
Serial.print("Recv error, out of sync!");
state = 0;
}
} else {
Serial.println("FATAL: unknown error!");
}
if (!idrop_connected) {
state = 0;
}
}
boolean idrop_next_bit() {
static boolean sending = false;
static char sendingChar;
static byte sendingCharPos = 0;
static char bufBegin = 0;
if (!sending) {
boolean idle;
byte next;
idrop_next_byte(idle, next);
if (!idle) {
sendingChar = next;
//Serial.print("Send: ");
//Serial.println(sendingChar);
sendingCharPos = 0;
bufBegin=true;
sending = true;
}
return true; // Send marks to pad the byte
} else {
static boolean start = true;
if (bufBegin < 2) {
bufBegin++;
start = true;
return true;
}
if (start) {
start = false;
return false; // space at beginning of byte
}
if (sendingCharPos == 8) {
sending = false;
return true; // Send a mark at the end of a byte
} else {
boolean isMark = sendingChar&1;
sendingChar = sendingChar >> 1;
sendingCharPos++;
return isMark;
}
}
}