-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeIR.cpp
704 lines (671 loc) · 13.9 KB
/
MeIR.cpp
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class MeIR
* \brief Driver for Me IR module.
* @file MeIR.cpp
* @author MakeBlock
* @version V1.0.4
* @date 2015/11/16
* @brief Driver for Me IR module.
*
* \par Copyright
* This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* This file is a drive for Me IR device, The IR inherited the
* MeSerial class from SoftwareSerial.
*
* \par Method List:
*
* 1. ErrorStatus MeIR::decode();
* 2. void MeIR::begin();
* 3. void MeIR::end();
* 4. void MeIR::loop();
* 5. boolean MeIR::keyPressed(unsigned char r);
* 6. String MeIR::getString();
* 7. unsigned char MeIR::getCode()
* 8. void MeIR::sendString(String s);
* 9. void MeIR::sendString(float v);
* 10. void MeIR::sendNEC(unsigned long data, int nbits);
* 11. void MeIR::sendRaw(unsigned int buf[], int len, uint8_t hz);
* 12. void MeIR::enableIROut(uint8_t khz);
* 13. void MeIR::enableIRIn();
* 14. void MeIR::mark(uint16_t us);
* 15. void MeIR::space(uint16_t us);
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* Mark Yan 2015/10/09 1.0.0 Bulid the new.
* Mark Yan 2015/10/29 1.0.1 Fix minor errors on format.
* Mark Yan 2015/11/02 1.0.2 Fix bug that IRsend and IRreceive can't work at the same time.
* forfish 2015/11/09 1.0.3 Add description.
* Mark Yan 2015/11/16 1.0.4 add data recovery when timeout.
* </pre>
*
*/
#include "MeIR.h"
// Provides ISR
#ifndef __AVR_ATmega32U4__
#include <avr/interrupt.h>
volatile irparams_t irparams;
bool MATCH(uint8_t measured_ticks, uint8_t desired_us)
{
// Serial.print(measured_ticks);Serial.print(",");Serial.println(desired_us);
return(measured_ticks >= desired_us - (desired_us>>2)-1 && measured_ticks <= desired_us + (desired_us>>2)+1);//判断前后25%的误差
}
ISR(TIMER_INTR_NAME)
{
uint8_t irdata = (uint8_t)digitalRead(2);
// uint32_t new_time = micros();
// uint8_t timer = (new_time - irparams.lastTime)>>6;
irparams.timer++; // One more 50us tick
if (irparams.rawlen >= RAWBUF)
{
// Buffer overflow
irparams.rcvstate = STATE_STOP;
}
switch(irparams.rcvstate)
{
case STATE_IDLE: // In the middle of a gap
if (irdata == MARK)
{
irparams.rawlen = 0;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
break;
case STATE_MARK: // timing MARK
if (irdata == SPACE)
{
// MARK ended, record time
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_SPACE;
}
break;
case STATE_SPACE: // timing SPACE
if (irdata == MARK)
{
// SPACE just ended, record it
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
else
{ // SPACE
if (irparams.timer > GAP_TICKS)
{
// big SPACE, indicates gap between codes
// Mark current code as ready for processing
// Switch to STOP
// Don't reset timer; keep counting space width
irparams.rcvstate = STATE_STOP;
irparams.lastTime = millis();
}
}
break;
case STATE_STOP: // waiting, measuring gap
if(millis() - irparams.lastTime > 200)
{
irparams.rawlen = 0;
irparams.timer = 0;
irparams.rcvstate = STATE_IDLE;
}
else if (irdata == MARK)
{
// reset gap timer
irparams.timer = 0;
}
break;
}
// irparams.lastTime = new_time;
}
/**
* Alternate Constructor which can call your own function to map the IR to arduino port,
* no pins are used or initialized here.
* \param[in]
* None
*/
MeIR::MeIR()
{
pinMode(2,INPUT);
irparams.recvpin = 2;
// attachInterrupt(INT0, irISR, CHANGE);
irDelayTime = 0;
irIndex = 0;
irRead = 0;
irReady = false;
irBuffer = "";
irPressed = false;
begin();
pinMode(3, OUTPUT);
digitalWrite(3, LOW); // When not sending PWM, we want it low
}
/**
* \par Function
* begin
* \par Description
* Initialize interrupt.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::begin()
{
cli();
// setup pulse clock timer interrupt
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TIMER_CONFIG_NORMAL();
//Timer2 Overflow Interrupt Enable
TIMER_ENABLE_INTR;
// TIMER_RESET;
sei(); // enable interrupts
// initialize state machine variables
irparams.rawlen = 0;
irparams.rcvstate = STATE_IDLE;
// set pin modes
// pinMode(2, INPUT);
// pinMode(irparams.recvpin, INPUT);
}
/**
* \par Function
* end
* \par Description
* Close the interrupt.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::end()
{
EIMSK &= ~(1 << INT0);
}
/**
* \par Function
* decode
* \par Description
* Decodes the received IR message.
* \param[in]
* None
* \par Output
* None
* \par Return
* Returns 0 if no data ready, 1 if data ready.
* \par Others
* Results of decoding are stored in results.
*/
ErrorStatus MeIR::decode()
{
rawbuf = irparams.rawbuf;
rawlen = irparams.rawlen;
if (irparams.rcvstate != STATE_STOP)
{
return ERROR;
}
if (decodeNEC())
{
begin();
return SUCCESS;
}
begin();
return ERROR;
}
/**
* \par Function
* decodeNEC
* \par Description
* Decodes NEC the received IR message.
* \param[in]
* None
* \par Output
* None
* \par Return
* Returns ERROR if decode NEC no done, SUCCESS if decode NEC done.
* \par Others
* Results of decode NEC.
*/
// NECs have a repeat only 4 items long
ErrorStatus MeIR::decodeNEC()
{
uint32_t data = 0;
int offset = 0; // Skip first space
// Initial mark
if (!MATCH(rawbuf[offset], NEC_HDR_MARK/50))
{
return ERROR;
}
offset++;
// Check for repeat
if (rawlen == 3 &&
MATCH(rawbuf[offset], NEC_RPT_SPACE/50) &&
MATCH(rawbuf[offset+1], NEC_BIT_MARK/50))
{
bits = 0;
// results->value = REPEAT;
// Serial.println("REPEAT");
decode_type = NEC;
return SUCCESS;
}
if (rawlen < (2 * NEC_BITS + 3))
{
return ERROR;
}
// Initial space
if (!MATCH(rawbuf[offset], NEC_HDR_SPACE/50))
{
return ERROR;
}
offset++;
for (int i = 0; i < NEC_BITS; i++)
{
if (!MATCH(rawbuf[offset], NEC_BIT_MARK/50))
{
return ERROR;
}
offset++;
if (MATCH(rawbuf[offset], NEC_ONE_SPACE/50))
{
//data = (data << 1) | 1;
data = (data >> 1) | 0x80000000;
}
else if (MATCH(rawbuf[offset], NEC_ZERO_SPACE/50))
{
//data <<= 1;
data >>= 1;
}
else
{
return ERROR;
}
offset++;
}
// Success
bits = NEC_BITS;
value = data;
decode_type = NEC;
return SUCCESS;
}
/**
* \par Function
* mark
* \par Description
* Sends an IR mark for the specified number of microseconds.
* \param[in]
* us - THe time of a PWM.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::mark(uint16_t us)
{
// Sends an IR mark for the specified number of microseconds.
// The mark output is modulated at the PWM frequency.
TIMER_ENABLE_PWM; // Enable pin 3 PWM output
delayMicroseconds(us);
}
/**
* \par Function
* space
* \par Description
* Sends an IR mark for the specified number of microseconds.
* \param[in]
* us - THe time of a PWM.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
/* Leave pin off for time (given in microseconds) */
void MeIR::space(uint16_t us)
{
// Sends an IR space for the specified number of microseconds.
// A space is no output, so the PWM output is disabled.
TIMER_DISABLE_PWM; // Disable pin 3 PWM output
delayMicroseconds(us);
}
/**
* \par Function
* enableIROut
* \par Description
* Enable an IR for the specified number of khz.
* \param[in]
* us - THe time of a INTR.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::enableIROut(uint8_t khz)
{
TIMER_DISABLE_INTR; //Timer2 disable Interrupt
TIMER_CONFIG_KHZ(khz);
}
/**
* \par Function
* enableIRIn
* \par Description
* Enable an IR to write in.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
// initialization
void MeIR::enableIRIn() {
cli();
// setup pulse clock timer interrupt
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TIMER_CONFIG_NORMAL();
//Timer2 Overflow Interrupt Enable
TIMER_ENABLE_INTR;
//TIMER_RESET;
sei(); // enable interrupts
// initialize state machine variables
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
// set pin modes
pinMode(irparams.recvpin, INPUT);
}
/**
* \par Function
* sendRaw
* \par Description
* Send the length of data with hz.
* \param[in]
* buf[] - The data's buffer.
* \param[in]
* len - The data's length.
* \param[in]
* hz - The hz for sending data.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::sendRaw(unsigned int buf[], int len, uint8_t hz)
{
enableIROut(hz);
for (int i = 0; i < len; i++)
{
if (i & 1)
{
space(buf[i]);
}
else
{
mark(buf[i]);
}
}
space(0); // Just to be sure
}
/**
* \par Function
* getString
* \par Description
* Get string in a INTR.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the result in a IRQ.
* \par Others
* None
*/
String MeIR::getString()
{
if(decode())
{
irRead = ((value >> 8) >> 8) & 0xff;
if(irRead == 0xa || irRead == 0xd)
{
irIndex = 0;
irReady = true;
}
else
{
irBuffer += irRead;
irIndex++;
}
irDelayTime = millis();
}
else
{
if(irRead > 0)
{
if(millis() - irDelayTime > 100)
{
irPressed = false;
irRead = 0;
irDelayTime = millis();
Pre_Str = "";
}
}
}
if(irReady)
{
irReady = false;
String s = String(irBuffer);
Pre_Str = s;
irBuffer = "";
return s;
}
return Pre_Str;
}
/**
* \par Function
* getCode
* \par Description
* Get the reading code.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the result of reading.
* \par Others
* None
*/
unsigned char MeIR::getCode()
{
irIndex = 0;
loop();
return irRead;
}
/**
* \par Function
* sendString
* \par Description
* Send data.
* \param[in]
* s - The string you want to send.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::sendString(String s)
{
unsigned long l;
uint8_t data;
s.concat('\n');
for(int i = 0;i < s.length();i++)
{
data = s.charAt(i);
l = 0x0000ffff & (uint8_t)(~data);
l = l << 8;
l = l + ((uint8_t)data);
l = l << 16;
l = l | 0x000000ff;
sendNEC(l,32);
delay(20);
}
enableIRIn();
}
/**
* \par Function
* sendString
* \par Description
* Send data.
* \param[in]
* v - The string you want to send.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::sendString(float v)
{
dtostrf(v,5, 2, floatString);
sendString(floatString);
}
/**
* \par Function
* sendNEC
* \par Description
* Send NEC.
* \param[in]
* data - The data you want to send.
* \param[in]
* nbits - The data bit you want to send.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::sendNEC(unsigned long data, int nbits)
{
enableIROut(38);
mark(NEC_HDR_MARK);
space(NEC_HDR_SPACE);
for (int i = 0; i < nbits; i++)
{
if (data & 1)
{
mark(NEC_BIT_MARK);
space(NEC_ONE_SPACE);
}
else
{
mark(NEC_BIT_MARK);
space(NEC_ZERO_SPACE);
}
data >>= 1;
}
mark(NEC_BIT_MARK);
space(0);
}
/**
* \par Function
* loop
* \par Description
* A circle of operation.
* \param[in]
* None
* \par Output0
* None
* \par Return
* None
* \par Others
* None
*/
void MeIR::loop()
{
if(decode())
{
irRead = ((value >> 8) >> 8) & 0xff;
irPressed = true;
if(irRead == 0xa || irRead == 0xd)
{
irIndex = 0;
irReady = true;
}
else
{
irBuffer += irRead;
irIndex++;
if(irIndex > 64)
{
irIndex = 0;
irBuffer = "";
}
}
irDelayTime = millis();
}
else
{
if(irRead > 0)
{
if(millis() - irDelayTime > 200)
{
irPressed = false;
irRead = 0;
irDelayTime = millis();
}
}
}
}
/**
* \par Function
* keyPressed
* \par Description
* Press key.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return you the pressed key or not.
* \par Others
* None
*/
boolean MeIR::keyPressed(unsigned char r)
{
irIndex = 0;
loop();
return irRead == r;
}
#endif // !defined(__AVR_ATmega32U4__)