-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimothyIR.ino
387 lines (340 loc) · 9.73 KB
/
timothyIR.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
/*
Check http://en.wikipedia.org/wiki/Consumer_IR for frequency tables!!
Or perhaps the Pronto format:
http://www.remotecentral.com/features/irdisp2.htm
*/
#include <IRremote.h>
#include <IRremoteInt.h>
#include "bitlash.h"
#include <EEPROM.h>
#include "eeprom_any.h"
#include <avr/eeprom.h>
#define NUMPIRS 3
// PIRS are on analog pins A0-A5
int pirPins[NUMPIRS] = {A0,A1,A2};
int pirState[NUMPIRS] = {LOW,LOW,LOW}; // we start, assuming no motion detected
int pirval[NUMPIRS] = {0,0,0};
int PIRenable = 1; // Detectors are on by default.
#define NUMLEDS 6
// LEDs are on 2, 4, 5, 6, 7, 8
int LEDpins[NUMLEDS] = {2, 4, 5, 6, 7, 8};
int RECV_PIN = 11; // IR detector pin
int STATUS_PIN = 13; // Pin with visible light LED on it.
IRrecv irrecv(RECV_PIN); // IR receiver
IRsend irsend; // IR sending interface
decode_results results;
extern volatile irparams_t irparams;
#define MAX_EEPROM 1024
#define NONE 0
// Permanent storage locations
#define SLOTSIZE 170
int codelocs[] = {SLOTSIZE * 0,
SLOTSIZE * 1,
SLOTSIZE * 2,
SLOTSIZE * 3,
SLOTSIZE * 4,
SLOTSIZE * 5};
// Temporary storage for the recorded code
byte codeType = 0; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
byte codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state
// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
codeType = results->decode_type;
int count = results->rawlen;
if (codeType == UNKNOWN) {
Serial.println("Received unknown code, saving as raw");
codeLen = results->rawlen - 1;
// To store raw codes:
// Drop first value (gap)
// Convert from ticks to microseconds
// Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
for (int i = 1; i <= codeLen; i++) {
if (i % 2) {
// Mark
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
Serial.print(" m");
} else {
// Space
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
Serial.print(" s");
}
Serial.print(rawCodes[i - 1], DEC);
}
Serial.println("");
} else {
if (codeType == NEC) {
Serial.print("Received NEC: ");
if (results->value == REPEAT) {
// Don't record a NEC repeat value as that's useless.
Serial.println("repeat; ignoring.");
Serial.print("> "); // Restore prompt
return;
}
}
else if (codeType == SONY) {
Serial.print("Received SONY: ");
}
else if (codeType == RC5) {
Serial.print("Received RC5: ");
}
else if (codeType == RC6) {
Serial.print("Received RC6: ");
}
else {
Serial.print("Unexpected codeType ");
Serial.print(codeType, DEC);
Serial.println("");
}
Serial.println(results->value, HEX);
codeValue = results->value;
codeLen = results->bits;
}
Serial.print("> "); // Restore prompt
}
numvar store(void) {
numvar arg;
int i;
arg = getarg(0);
if (arg != 1) {
Serial.println("store takes a single argument: the IR blaster to link.");
return (numvar)1;
}
arg = getarg(1);
if (arg < 1 || arg > 6) {
Serial.println("Specify a slot, 1-6");
return (numvar)1;
}
if (codeType == NONE) {
Serial.println("I've no code. Send me one, would you?");
return (numvar)1;
}
arg--;
// Have a valid code and slot to store it in. Save it!
int Eloc = arg*SLOTSIZE;
EEPROM.write(Eloc++,codeType);
EEPROM.write(Eloc++,codeLen);
if (codeType == UNKNOWN) {
Serial.print("Storing raw code of size ");
Serial.print(codeLen, DEC);
for (i=1; i<codeLen; i++) {
Eloc += EEPROM_writeAnything(Eloc,rawCodes[i]);
}
} else {
Serial.print("Storing code type ");
Serial.print(codeType, DEC);
EEPROM_writeAnything(Eloc,codeValue);
}
Serial.println("...done.");
codeType = NONE;
return (numvar)1;
}
void sendCode() {
numvar arg;
arg = getarg(0);
if (arg != 1) {
Serial.println("send takes a single argument: the code number to blast.");
return;
}
arg = getarg(1);
if (arg < 1 || arg > 6) {
Serial.println("Specify a slot, 1-6");
return;
}
arg--;
blastCode(arg);
}
void blastCode(int unit) {// Unit is 0 - 5
int i;
int Eloc = unit*SLOTSIZE;
LEDselect(unit);
codeType = EEPROM.read(Eloc++);
codeLen = EEPROM.read(Eloc++);
cli(); // Ditch interrupts for now
if (codeType == NEC) {
Eloc += EEPROM_readAnything(Eloc, codeValue);
irsend.sendNEC(codeValue, codeLen);
Serial.print("Sent NEC ");
Serial.print(codeValue, HEX);
}
else if (codeType == SONY) {
Eloc += EEPROM_readAnything(Eloc, codeValue);
irsend.sendSony(codeValue, codeLen);
Serial.print("Sent Sony ");
Serial.println(codeValue, HEX);
}
else if (codeType == RC5 || codeType == RC6) {
Eloc += EEPROM_readAnything(Eloc, codeValue);
// Flip the toggle bit for a new button press
toggle = 1 - toggle;
// Put the toggle bit into the code to send
codeValue = codeValue & ~(1 << (codeLen - 1));
codeValue = codeValue | (toggle << (codeLen - 1));
if (codeType == RC5) {
Serial.print("Sent RC5 ");
Serial.println(codeValue, HEX);
irsend.sendRC5(codeValue, codeLen);
}
else {
Eloc += EEPROM_readAnything(Eloc, codeValue);
irsend.sendRC6(codeValue, codeLen);
Serial.print("Sent RC6 ");
Serial.println(codeValue, HEX);
}
}
else if (codeType == UNKNOWN /* i.e. raw */) {
Eloc += EEPROM_readAnything(Eloc, codeValue);
for (i=1; i<codeLen; i++) {
Eloc += EEPROM_readAnything(Eloc,rawCodes[i]);
}
// Assume 38 KHz
irsend.sendRaw(rawCodes, codeLen, 38);
Serial.println("Sent raw");
}
sei();
irrecv.enableIRIn(); // Start the receiver
irrecv.resume(); // resume receiver
}
// Show system state
numvar state(void) {
Serial.print("rcvstate: ");
Serial.print(irparams.rcvstate,DEC);
Serial.println("");
Serial.print("recvpin: ");
Serial.print(irparams.recvpin,DEC);
Serial.println("");
Serial.print("blinkflag: ");
Serial.print(irparams.blinkflag,DEC);
Serial.println("");
Serial.print("timer: ");
Serial.print(irparams.timer,DEC);
Serial.println("");
Serial.print("rawlen: ");
Serial.print(irparams.rawlen,DEC);
Serial.println("");
}
numvar dump(void) {
numvar arg;
int i,j;
for(i=0;i<MAX_EEPROM;i++) {
j=EEPROM.read(i);
if (i%16 == 0) {
Serial.println("");
if (i < 16) Serial.print("0");
Serial.print(i,HEX);
Serial.print(": ");
}
Serial.print(j,HEX);
Serial.print(" ");
}
Serial.println("");
}
numvar wipe(void) {
numvar arg;
int i;
arg = getarg(0);
if (arg != 1) {
Serial.println("Wipe takes takes a single argument: a (1) to confirm.");
return (numvar)1;
} else {
Serial.print("wiping...");
for(i=0;i<MAX_EEPROM;i++)
EEPROM.write(i,0);
Serial.println("done.");
}
return (numvar)1;
}
void LEDselect(int led) {
for(int i=0; i<NUMLEDS; i++) {
if (i==led) {
digitalWrite(LEDpins[i], LOW);
Serial.print("Setting pin ");
Serial.print(LEDpins[i],DEC);
Serial.println(" to LOW");
} else {
digitalWrite(LEDpins[i], HIGH);
}
}
}
void checkMotion(int unit){
pirval[unit] = analogRead(pirPins[unit]); // read input value
if (pirval[unit] > 0) { // check if the input is HIGH
if (pirState[unit] == LOW) {
// we have just turned on
Serial.print("Motion detected on unit ");
Serial.print(unit+1,DEC);
Serial.println("");
// Trigger a send!
blastCode(unit);
// We only want to print on the output change, not state
pirState[unit] = HIGH;
}
} else {
if (pirState[unit] == HIGH){
// we have just turned of
Serial.print("Motion ended on unit ");
Serial.print(unit+1,DEC);
Serial.println("");
// We only want to print on the output change, not state
pirState[unit] = LOW;
}
}
}
numvar disablePIR(void) {
PIRenable = 0;
}
numvar enablePIR(void) {
PIRenable = 1;
}
void setup(void) {
// initialize bitlash and set primary serial port baud
// print startup banner and run the startup macro
initBitlash(57600);
//Serial.begin(57600);
addBitlashFunction("store", (bitlash_function) store);
addBitlashFunction("send", (bitlash_function) sendCode);
addBitlashFunction("wipe", (bitlash_function) wipe);
addBitlashFunction("dump", (bitlash_function) dump);
addBitlashFunction("state", (bitlash_function) state);
addBitlashFunction("disable", (bitlash_function) disablePIR);
addBitlashFunction("enable", (bitlash_function) enablePIR);
irrecv.enableIRIn(); // Start the receiver
pinMode(STATUS_PIN, OUTPUT);
for(int i=0; i<NUMLEDS; i++) { // Enable and delselect IR LEDs
pinMode(LEDpins[i], OUTPUT);
digitalWrite(LEDpins[i], HIGH);
}
// Set up the PIRs
for(int i=0;i<NUMPIRS;i++)
pinMode(pirPins[i], INPUT); // declare sensor as input
}
#define FLASHER 1000
#define DOUBLE_FLASHER 6000
unsigned long t = 0;
void loop(void) {
// Light-flashing code
if (t++ < FLASHER){
digitalWrite(STATUS_PIN, HIGH);
} else {
digitalWrite(STATUS_PIN, LOW);
}
if (t > DOUBLE_FLASHER) {
t = 0;
}
// Check for commands.
runBitlash();
// Do we see a motion detector triggered?
if (PIRenable) {
for(int i=0;i<NUMPIRS;i++)
checkMotion(i);
}
// Got a code? Store it in the temp location.
if (irrecv.decode(&results)) {
storeCode(&results);
irrecv.enableIRIn(); // Start the receiver
irrecv.resume(); // resume receiver
}
}