This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpaduino.ino
425 lines (368 loc) · 9.34 KB
/
Spaduino.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
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
/*
* Copyright 2017 Olivier Hill
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <Wire.h>
#include <avr/sleep.h>
#include <avr/power.h>
//#define DEBUG 1
const uint8_t PUSH_DELAY = 100; // Seems to want ~100ms for debouncing
const byte MY_ADDRESS = 82; // I2C Address
uint32_t buf;
volatile __uint24 display;
volatile boolean changed;
struct
{
char c_str[4];
uint8_t int_v;
uint8_t target;
} temperature;
// list of commands
enum {
CMD_ID = 1, // ID of device (always answers 0xBA)
CMD_DISPLAY = 2, // Current display, temperature or message
CMD_SETTEMP = 3, // Set target temperature
CMD_GETTEMP = 4, // Get current target temperature
CMD_STATUS = 5 // Get current status (heater running, etc.)
};
volatile struct
{
byte command;
byte argument;
bool in_progress;
} request;
void setup()
{
// disable ADC
ADCSRA = 0;
ACSR = bit(ACD); //Disable the analog comparator
// Running on internal 8MHz clock, but could use this for an Arduino
//CLKPR = _BV(CLKPCE);
//CLKPR = 0x01; // 8MHz
power_adc_disable(); // ADC
power_spi_disable(); // SPI
power_usart0_disable(); // Serial
power_timer1_disable(); // Timer 1
// I2C Slave
Wire.begin (MY_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
// Disable internal pullups
digitalWrite(SDA, 0);
digitalWrite(SCL, 0);
pinMode(2, INPUT); // CLK
pinMode(3, INPUT); // Data
pinMode(4, INPUT); // Temperature
pinMode(11, OUTPUT); // Counter toggle
pinMode(13, INPUT); // Disable status LED
// interrupt for SS rising edge (pin 2)
EICRA &= ~(bit(ISC00) | bit (ISC01)); // clear existing flags
EICRA |= bit (ISC01) | bit (ISC00); // rising edge interrupt
EIFR = bit (INTF0); // clear flag for interrupt 0
EIMSK |= bit (INT0); // enable it
// reset Timer 2
TCCR2A = 0;
TCCR2B = 0;
// Timer 2
// 8 MHz clock, prescale of 32
// 125 ns per tick * 32 = 4 µs
// Count 125 gives 500 µs (0,5 ms)
TCCR2A = bit (WGM21) | bit (COM2A0); // CTC mode, toggle on pin 10
OCR2A = 124;
// Timer 2 interrupt
TIMSK2 = bit (OCIE2A);
// Reset count to 0
TCNT2 = 0;
#ifdef DEBUG
Serial.begin(74880);
Serial.println(F("Started"));
#endif
}
// CLK rising ISR
ISR (INT0_vect)
{
// Grab pin before it changes
byte c = PIND;
// Test if we are in "writing mode", was Timer 2 started yet?
if (TCCR2B) {
TCNT2 = 0; // Reset our counter to zero
}
else {
buf = 0xAA; // Initial value (padding)
OCR2A = 124; // Count to 125
TCNT2 = 0; // Reset counter to zero
// Start Timer 2
TCCR2B = bit (CS21) | bit (CS20); // prescaler of 32
}
// Shift buffer and add our pin value
buf <<= 1;
buf |= (c >> 3) & 0x01;
} // end of INT0_vect
ISR (TIMER2_COMPA_vect)
{
TCCR2B = 0; // stop timer 2 (we keep TCCR2A set for CTC next cycle)
TCNT2 = 0; // For next timer loop, reset to zero
// Test if we have a complete 3x7 bit sequence
if (buf >> 21 == 0xAA)
{
buf &= 0x001fffff; // Only keep 21 bits
if (display != buf) {
display = buf;
changed = true;
}
}
}
// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
request.command = Wire.read();
if (howMany > 1) {
request.argument = Wire.read();
}
request.in_progress = false;
// Empty buffer to /dev/null
while (Wire.available () > 0)
Wire.read ();
} // end of receiveEvent
void requestEvent()
{
switch (request.command)
{
case CMD_ID:
Wire.write(0xBA);
break;
case CMD_DISPLAY:
// Temperature is updated in main loop, simply output
// the buffer here, we are in an ISR
Wire.write(temperature.c_str, sizeof temperature.c_str );
break;
case CMD_GETTEMP:
Wire.write(temperature.target);
break;
case CMD_STATUS:
byte status = (display >> 14) & 0x0f;
Wire.write(status);
break;
}
}
char getChar(byte c)
{
// This will generate a lookup table from 7-segment
// display to ASCII chars. Some chars are represented
// by a number instead of letter (ie: O is 0).
switch (c) {
case 0x7e:
return '0';
case 0x30:
return '1';
case 0x6d:
return '2';
case 0x79:
return '3';
case 0x33:
return '4';
case 0x5b:
return '5';
case 0x5f:
return '6';
case 0x70:
return '7';
case 0x7f:
return '8';
case 0x73:
return '9';
case 0x77:
return 'A';
case 0x1f:
return 'b';
case 0x0d:
return 'c';
case 0x4e:
return 'C';
case 0x3d:
return 'd';
case 0x4f:
return 'E';
case 0x47:
return 'F';
case 0x37:
return 'H';
case 0x0e:
return 'L';
case 0x15:
return 'n';
case 0x67:
return 'P';
case 0x05:
return 'r';
case 0x0f:
return 't';
case 0x3e:
return 'U';
case 0x3b:
return 'Y';
case 0x01:
return '-';
case 0x00:
return ' ';
default:
return '?';
}
}
char getLetter(byte c)
{
switch(c) {
case '5':
return 'S';
break;
case '1':
return 'I';
break;
case '0':
return 'O';
break;
default:
return c;
}
}
void getDisplay()
{
__uint24 dispCopy = display; // Grab before it changes
dispCopy &= 0x1c3fff; // Discard what's not on display (3rd byte LSB)
for (int8_t i=2; i>=0; i--) {
if (i < 2 && temperature.c_str[2] >> 6 != 0) // Not a digit
temperature.c_str[i] = getLetter(getChar(dispCopy >> ((2-i)*7) & 0x7f));
else
temperature.c_str[i] = getChar(dispCopy >> ((2-i)*7) & 0x7f);
}
temperature.c_str[3] = 0x00;
// Is it a valid numerical temperature? If not, translate to letters
temperature.int_v = atoi(temperature.c_str);
}
void togglePin(uint8_t pin)
{
#ifdef DEBUG
Serial.print(millis());
Serial.print(F(": Toggling pin "));
Serial.println(pin, HEX);
#endif
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(PUSH_DELAY);
digitalWrite(pin, LOW);
pinMode(pin, INPUT); // Hi-Z
}
bool waitForBlink()
{
// Wait for display to blink
for (uint8_t j=0; j < 30; j++) { // Wait for 3 sec max. (30 x 100ms)
if (!changed || display & 0x3fff != 0) {
delay(100);
}
else {
changed = false; // Reset our change flag to see next temp
return true;
}
}
return false;
}
// main loop
void loop (void)
{
// Make sure we run it once between receiveEvent and requestEvent
if (request.in_progress == false)
{
request.in_progress = true;
switch (request.command) {
case CMD_DISPLAY:
#ifdef DEBUG
Serial.print(millis());
Serial.println(F(": Reading display"));
#endif
getDisplay();
break;
case CMD_SETTEMP:
#ifdef DEBUG
Serial.println();
Serial.println(String(millis()) + F(": Trying to set temperature to: ") + String(request.argument, DEC));
#endif
changed = false;
togglePin(4);
waitForBlink();
for (uint8_t i=0; i < 40; i++) { // Up to 40 button push
// wait for change and a valid temp
for (uint8_t j=0; j < 20; j++) { // Wait for 2 sec max. (20 x 100ms)
if (!changed || temperature.int_v < 80) {
delay(100);
getDisplay();
}
else
break;
}
if (temperature.int_v < 80) {
#ifdef DEBUG
Serial.println(String(millis()) + F(": Something went wrong"));
#endif
break;
}
#ifdef DEBUG
Serial.print(millis());
Serial.print(F(": Current target is: "));
Serial.println(temperature.int_v, DEC);
#endif
if (temperature.int_v == request.argument) {
#ifdef DEBUG
Serial.print(millis());
Serial.println(F(": All done"));
#endif
temperature.target = temperature.int_v;
break;
}
else {
changed = false;
togglePin(4);
//delay(200);
}
}
break;
case CMD_GETTEMP:
#ifdef DEBUG
Serial.print(millis());
Serial.println(F(": Reading current target temp"));
#endif
changed = false;
togglePin(4);
waitForBlink();
// wait for change and a valid temp
for (uint8_t j=0; j < 20; j++) { // Wait for 2 sec max.
if (!changed || temperature.int_v < 80) {
delay(100);
getDisplay();
}
else
break;
}
#ifdef DEBUG
if (temperature.int_v < 80) {
Serial.print(millis());
Serial.println(F(": Something went wrong"));
}
#endif
changed = false;
temperature.target = temperature.int_v;
break;
} // end switch
} // end if in progress
} // end of loop