forked from sbstjn/duino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduino.ino
More file actions
463 lines (408 loc) · 10.5 KB
/
Copy pathduino.ino
File metadata and controls
463 lines (408 loc) · 10.5 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <LCD4884.h>
#include <Servo.h>
// Node.js <--> Arduino serial protocol delimiters
#define SERIAL_START_CHAR 2 // ASCII code for Start of text
#define SERIAL_END_CHAR 3 // ASCII code for End of text
//keypad debounce parameter
#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON 10
#define DEBOUNCE_OFF 3
#define NUM_KEYS 5
// joystick number
#define LEFT_KEY 0
#define CENTER_KEY 1
#define DOWN_KEY 2
#define RIGHT_KEY 3
#define UP_KEY 4
#define MENU_X 0 // 0-83
#define MENU_Y 0 // 0-5
int adc_key_val[5] ={
50, 200, 400, 600, 800 };
// debounce counters
byte button_count[NUM_KEYS];
// button status - pressed/released
byte button_status[NUM_KEYS];
// button on flags for user program
byte button_flag[NUM_KEYS];
/* Timer2 reload value, globally available */
/* cf. http://http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/ */
unsigned int tcnt2;
char messageBuffer[10 + 84], cmd[3], pin[3], val[4], aux[4], msg[84 + 1];
int msg_length = 0;
unsigned char lcd_x = 0;
unsigned char lcd_y = 0;
char lcd_mode = MENU_NORMAL;
boolean debug = false;
int index = 0;
Servo servo;
void setup() {
setup_lcd();
Serial.begin(115200);
}
void loop() {
/**
* Waiting for commands
*/
while(Serial.available() > 0) {
char x = Serial.read();
if (x == SERIAL_START_CHAR) index = 0; // start
else if (x == SERIAL_END_CHAR) process(); // end
else messageBuffer[index++] = x;
}
}
/**
* Deal with a full message and determine function to call
*/
void process() {
index = 0;
char x[3], y[2], m[2];
strncpy(cmd, messageBuffer, 2);
cmd[2] = '\0';
strncpy(pin, messageBuffer + 2, 2);
pin[2] = '\0';
strncpy(val, messageBuffer + 4, 3);
val[3] = '\0';
strncpy(aux, messageBuffer + 7, 3);
aux[3] = '\0';
if (debug) {
Serial.println(messageBuffer); }
int cmdid = atoi(cmd);
switch(cmdid) {
case 0: sm(pin,val); break;
case 1: dw(pin,val); break;
case 2: dr(pin); break;
case 3: aw(pin,val); break;
case 4: ar(pin); break;
case 90: autoReply(); break;
case 96:
// Lcd message structure
//
// X: 2 bytes
// Y: 1 byte
// Mode: 1 byte
// Payload: up to 84 bytes
strncpy(x, messageBuffer + 10, 2);
x[2] = '\0';
strncpy(y, messageBuffer + 12, 1);
y[1] = '\0';
strncpy(m, messageBuffer + 13, 1);
m[1] = '\0';
msg_length = atoi(aux);
strncpy(msg, messageBuffer + 14, msg_length);
msg[msg_length] = '\0';
Serial.print("handleLcd x:");
Serial.print(x);
Serial.print(", y:");
Serial.print(y);
Serial.print(", mode:");
Serial.print(m);
Serial.print(", message (");
Serial.print(msg_length);
Serial.print("):");
Serial.println(msg);
handleLcd(val, msg_length, atoi(x), atoi(y), atoi(m), msg);
break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
default: break;
}
}
/**
* Toggle debug mode
* @param char val value for enabling or disabling debugger (0 = false, 1 = true)
*/
void toggleDebug(char *val) {
if (atoi(val) == 0) {
debug = false;
Serial.println("goodbye");
} else {
debug = true;
Serial.println("hello");
}
}
void autoReply() {
Serial.println('Is Dave there?');
}
/**
* Set pin mode
* @param char pin identifier for pin
* @param char val set pit to OUTPUT or INPUT
*/
void sm(char *pin, char *val) {
if (debug) {
Serial.println("sm"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
if (atoi(val) == 0) {
pinMode(p, OUTPUT);
} else {
pinMode(p, INPUT);
}
}
}
/**
* Digital write
* @param char pin identifier for pin
* @param char val set pin to HIGH or LOW
*/
void dw(char *pin, char *val) {
if (debug) {
Serial.println("dw"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
pinMode(p, OUTPUT);
if (atoi(val) == 0) {
digitalWrite(p, LOW);
} else {
digitalWrite(p, HIGH);
}
}
}
/**
* Digital read
* @param char pin pin identifier
*/
void dr(char *pin) {
if (debug) {
Serial.println("dr"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
pinMode(p, INPUT);
int oraw = digitalRead(p);
char m[7];
sprintf(m, "%02d::%02d", p,oraw);
Serial.println(m);
}
}
/**
* Analog read
* @param char pin pin identifier
*/
void ar(char *pin) {
if (debug) {
Serial.println("ar"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
pinMode(p, INPUT); // don't want to sw
int rval = analogRead(p);
char m[8];
sprintf(m, "%s::%03d", pin, rval);
Serial.println(m);
}
}
/*
* Analog write
* @param char pin pin identifier
* @param char val value to write
*/
void aw(char *pin, char *val) {
if (debug) {
Serial.println("aw"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
pinMode(p, OUTPUT);
analogWrite(p, atoi(val));
}
}
int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error
int ret = -1;
if (pin[0] == 'A' || pin[0] == 'a') {
switch(pin[1]) {
case '0': ret = A0; break;
case '1': ret = A1; break;
case '2': ret = A2; break;
case '3': ret = A3; break;
case '4': ret = A4; break;
case '5': ret = A5; break;
default: break;
}
} else {
ret = atoi(pin);
if (ret == 0 && (pin[0] != '0' || pin[1] != '0')) {
ret = -1; }
}
return ret;
}
// handleLcd(val, msg_length, lcd_x, lcd_y, lcd_mode, msg);
void handleLcd(char *val, int len, unsigned char x, unsigned char y, char mode, char *msg) {
char line[15];
char remaining_chars=0, printed_chars=0, nb_chars=0;
remaining_chars = len;
switch(atoi(val)) {
case 0: // LCD_clear
lcd.LCD_clear();
break;
case 1: // LCD_write_string
lcd.LCD_clear();
for (int i=y ; i < 6 ; i++) {
if (i == y) {
nb_chars = min(len, 14-x);
strncpy(line, msg, nb_chars);
line[nb_chars] = '\0';
lcd.LCD_write_string(x*6, i, line, mode);
} else {
nb_chars = min(remaining_chars, 14);
strncpy(line, msg + printed_chars, nb_chars);
line[nb_chars] = '\0';
lcd.LCD_write_string(0, i, line, mode);
}
printed_chars += nb_chars;
remaining_chars -= nb_chars;
}
break;
case 2: // LCD_write_string_big
// TDB
break;
case 3: // Writeln
nb_chars = min(len, 14-x);
strncpy(line, msg, nb_chars);
line[nb_chars] = '\0';
lcd.LCD_write_string(x*6, y, line, mode);
break;
case 98: // backlight(OFF)
lcd.backlight(OFF);
break;
case 99: // backlight(ON)
lcd.backlight(ON);
break;
default:
// Noop
break;
}
}
/*
* Handle Servo commands
* attach, detach, write, read, writeMicroseconds, attached
*/
void handleServo(char *pin, char *val, char *aux) {
if (debug) {
Serial.println("ss"); }
int p = getPin(pin);
if (p == -1 && debug) {
Serial.println("badpin");
} else {
Serial.println("got signal");
if (atoi(val) == 0) {
servo.detach();
} else if (atoi(val) == 1) {
servo.attach(p);
Serial.println("attached");
} else if (atoi(val) == 2) {
Serial.println("writing to servo");
Serial.println(atoi(aux));
servo.write(atoi(aux));
}
}
}
/*
* Initialize LCD4884 shield
*
*/
void setup_lcd() {
for(byte i=0; i<NUM_KEYS; i++){
button_count[i]=0;
button_status[i]=0;
button_flag[i]=0;
}
/* First disable the timer overflow interrupt while we're configuring */
TIMSK2 &= ~(1<<TOIE2);
/* Configure timer2 in normal mode (pure counting, no PWM etc.) */
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
/* Select clock source: internal I/O clock */
ASSR &= ~(1<<AS2);
/* Disable Compare Match A interrupt enable (only want overflow) */
TIMSK2 &= ~(1<<OCIE2A);
/* Now configure the prescaler to CPU clock divided by 256 */
TCCR2B |= (1<<CS22) | (1<<CS21); // Set bits
TCCR2B &= ~(1<<CS21); // Clear bit
/* We need to calculate a proper value to load the timer counter.
* The following loads the value 6 into the Timer 2 counter register
* The math behind this is:
* (CPU frequency) / (prescaler value) = 62500 Hz = 16us.
* (desired period) / 16us = 250
* MAX(uint8) + 1 - 250 = 6;
*/
/* Save value globally for later reload in ISR */
tcnt2 = 6;
/* Finally load end enable the timer */
TCNT2 = tcnt2;
TIMSK2 |= (1<<TOIE2);
SREG |= 1<<SREG_I;
lcd.LCD_init();
lcd.LCD_clear();
lcd.backlight(OFF);
}
// The followinging are interrupt-driven keypad reading functions
// which includes DEBOUNCE ON/OFF mechanism, and continuous pressing detection
/*
* Convert ADC value to key number
* int input key indentifier
*/
char get_key(unsigned int input)
{
char k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
return k;
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
void update_adc_key(){
int adc_key_in;
char key_in;
byte i;
adc_key_in = analogRead(0);
key_in = get_key(adc_key_in);
for(i=0; i<NUM_KEYS; i++)
{
if(key_in==i) //one key is pressed
{
if(button_count[i]<DEBOUNCE_MAX)
{
button_count[i]++;
if(button_count[i]>DEBOUNCE_ON)
{
if(button_status[i] == 0)
{
button_flag[i] = 1;
button_status[i] = 1; //button debounced to 'pressed' status
}
}
}
}
else // no button pressed
{
if (button_count[i] > 0)
{
button_flag[i] = 0;
button_count[i]--;
if (button_count[i] < DEBOUNCE_OFF) {
button_status[i] = 0; //button debounced to 'released' status
}
}
}
}
}
/*
* Timer Interrupt routine
*
* 1/(160000000/256/(256-6)) = 4ms interval
*/
ISR(TIMER2_OVF_vect) {
TCNT2 = tcnt2;
update_adc_key();
}