/
mjdecode.ino
182 lines (137 loc) · 3.57 KB
/
mjdecode.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
// Decode Max and James' message by reading the LED on their Arduino
// converting it to Morse and writing the letter down the serial port.
// Milliseconds: customised for Max & James' Morse style
#define PAUSE 300
#define LONG_BEEP 200
#define SHORT_BEEP 90
// TODO: Make adpative, so the code knows a short beep
// is relative to a long beep (and v.v.) and pauses
// are relative to gap between beeps.
// Source: http://en.wikipedia.org/wiki/Morse_code
char* morse[26] = {
".-", // A
"-...",
"-.-.",
"-..",
".", // E
"..-.",
"--.",
"....", // H
"..",
".---",
"-.-",
".-..",
"--", // M
"-.",
"---", // O
".--.",
"--.-",
".-.",
"...",
"-", // T
"..-", // U
"...-",
".--",
"-..-",
"-.--",
"--.."
};
// For debugging -------------------------------------------
boolean logging = true;
void logBeep(char* title, int duration) {
if (logging) {
Serial.print(title);
Serial.print(" ");
Serial.println(duration);
}
}
void logMessage(char* msg) {
if (logging)
Serial.println(msg);
}
// Init --------------------------------------------------------
#define SENSOR A0
void setup() {
Serial.begin(9600); // 9600 baud for USB?
}
// I/O ------------------------------------------------------------
boolean see_led() {
// LED in the video appears bright in these ranges on my screen:
int brightness = analogRead(SENSOR);
return brightness >= 6 && brightness <= 15;
}
// State ---------------------------------------------------------
#define MAX_DOT_DASHES 6
char dots_and_dashes[] = "??????";
int dot_dash_pos = 0;
void reset() {
dot_dash_pos = 0;
for(int i=0; i<MAX_DOT_DASHES;i++)
dots_and_dashes[i]='?';
}
boolean beeping = false;
long beep_started = 0L;
long beep_ended = 0L;
// Compute --------------------------------------------------------
// Decode the dots and dashes we've collected so far.
void decode() {
for(int letter=0; letter<26; letter++) {
boolean found = true;
for(int pos=0; pos<dot_dash_pos && found; pos++)
found = found && morse[letter][pos]==dots_and_dashes[pos];
if (found && strlen(morse[letter]) == dot_dash_pos) {
Serial.println((char)(letter+65));
break;
}
}
reset();
}
// Event handling -------------------------------------------------
// Event: detection of the start of a beep
void start_of_beep() {
beep_started = millis();
beeping = true;
}
// Event: detection of the end of a beep (possibly)
void end_of_beep() {
long now = millis();
long beep_length = now - beep_started;
if (beep_length < 2) {
logMessage("Bounce");
return; // de-bounce very short light changes
}
beeping = false;
beep_ended = now;
if (beep_length > LONG_BEEP) {
logBeep("Long", beep_length);
dots_and_dashes[dot_dash_pos++] = '-';
} else if (beep_length > SHORT_BEEP) {
logBeep("Short", beep_length);
dots_and_dashes[dot_dash_pos++] = '.';
}
if (dot_dash_pos >= MAX_DOT_DASHES) { //oops
logMessage("overflow");
reset();
}
}
// Event: possibly a pause after a beep
void pause() {
long now = millis();
if ( (now-beep_ended) > PAUSE && dot_dash_pos > 0) {
logMessage("PAUSE");
// Long time since we beeped. That could be a pause
decode();
}
}
// Loop -----------------------------------------------------------------
void loop() {
boolean light_on = see_led();
if (light_on && !beeping) {
start_of_beep();
} else if (!light_on && beeping) {
end_of_beep();
} else if (!light_on && !beeping) {
pause();
}
delay(1); // appox 1ms cycle time
}