forked from Arduino-IRremote/Arduino-IRremote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRSend.hpp
1249 lines (1107 loc) · 48.2 KB
/
IRSend.hpp
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* IRSend.hpp
*
* Contains common functions for sending
*
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
*
************************************************************************************
* MIT License
*
* Copyright (c) 2009-2023 Ken Shirriff, Rafi Khan, Armin Joachimsmeyer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************************
*/
#ifndef _IR_SEND_HPP
#define _IR_SEND_HPP
#if defined(DEBUG) && !defined(LOCAL_DEBUG)
#define LOCAL_DEBUG
#else
//#define LOCAL_DEBUG // This enables debug output only for this file
#endif
#if defined(TRACE) && !defined(LOCAL_TRACE)
#define LOCAL_TRACE
#else
//#define LOCAL_TRACE // This enables debug output only for this file
#endif
/*
* Low level hardware timing measurement
*/
//#define _IR_MEASURE_TIMING // for mark()
//#define _IR_TIMING_TEST_PIN 7 // "pinModeFast(_IR_TIMING_TEST_PIN, OUTPUT);" is executed at begin()
//
/*
* This improves readability of code by avoiding a lot of #if defined clauses
*/
#if defined(IR_SEND_PIN)
#define sendPin IR_SEND_PIN
#endif
/** \addtogroup Sending Sending IR data for multiple protocols
* @{
*/
// The sender instance
IRsend IrSender;
IRsend::IRsend() { // @suppress("Class members should be properly initialized")
#if !defined(IR_SEND_PIN)
sendPin = 0;
#endif
#if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(0, DO_NOT_ENABLE_LED_FEEDBACK);
#endif
}
#if defined(IR_SEND_PIN)
/**
* Only required to set LED feedback
* Simple start with defaults - LED feedback enabled! Used if IR_SEND_PIN is defined. Saves program memory.
*/
void IRsend::begin(){
# if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(USE_DEFAULT_FEEDBACK_LED_PIN, LED_FEEDBACK_ENABLED_FOR_SEND);
# endif
#if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
pinModeFast(_IR_TIMING_TEST_PIN, OUTPUT);
#endif
}
/**
* Only required to set LED feedback
* @param aEnableLEDFeedback If true / ENABLE_LED_FEEDBACK, the feedback LED is activated while receiving or sending a PWM signal /a mark
* @param aFeedbackLEDPin If 0 / USE_DEFAULT_FEEDBACK_LED_PIN, then take board specific FEEDBACK_LED_ON() and FEEDBACK_LED_OFF() functions
*/
void IRsend::begin(bool aEnableLEDFeedback, uint_fast8_t aFeedbackLEDPin) {
#if !defined(NO_LED_FEEDBACK_CODE)
uint_fast8_t tEnableLEDFeedback = DO_NOT_ENABLE_LED_FEEDBACK;
if(aEnableLEDFeedback) {
tEnableLEDFeedback = LED_FEEDBACK_ENABLED_FOR_SEND;
}
setLEDFeedback(aFeedbackLEDPin, tEnableLEDFeedback);
#else
(void) aEnableLEDFeedback;
(void) aFeedbackLEDPin;
#endif
}
#else // defined(IR_SEND_PIN)
IRsend::IRsend(uint_fast8_t aSendPin) { // @suppress("Class members should be properly initialized")
sendPin = aSendPin;
# if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(0, DO_NOT_ENABLE_LED_FEEDBACK);
# endif
}
/**
* Initializes the send pin and enable LED feedback with board specific FEEDBACK_LED_ON() and FEEDBACK_LED_OFF() functions
* @param aSendPin The Arduino pin number, where a IR sender diode is connected.
*/
void IRsend::begin(uint_fast8_t aSendPin) {
sendPin = aSendPin;
# if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(USE_DEFAULT_FEEDBACK_LED_PIN, LED_FEEDBACK_ENABLED_FOR_SEND);
# endif
}
void IRsend::setSendPin(uint_fast8_t aSendPin) {
sendPin = aSendPin;
}
/**
* Initializes the send and feedback pin
* @param aSendPin The Arduino pin number, where a IR sender diode is connected.
* @param aEnableLEDFeedback If true the feedback LED is activated while receiving or sending a PWM signal /a mark
* @param aFeedbackLEDPin If 0 / USE_DEFAULT_FEEDBACK_LED_PIN, then take board specific FEEDBACK_LED_ON() and FEEDBACK_LED_OFF() functions
*/
void IRsend::begin(uint_fast8_t aSendPin, bool aEnableLEDFeedback, uint_fast8_t aFeedbackLEDPin) {
#if defined(IR_SEND_PIN)
(void) aSendPin; // for backwards compatibility
#else
sendPin = aSendPin;
#endif
#if !defined(NO_LED_FEEDBACK_CODE)
uint_fast8_t tEnableLEDFeedback = DO_NOT_ENABLE_LED_FEEDBACK;
if (aEnableLEDFeedback) {
tEnableLEDFeedback = LED_FEEDBACK_ENABLED_FOR_SEND;
}
setLEDFeedback(aFeedbackLEDPin, tEnableLEDFeedback);
#else
(void) aEnableLEDFeedback;
(void) aFeedbackLEDPin;
#endif
}
#endif // defined(IR_SEND_PIN)
/**
* Interprets and sends a IRData structure.
* @param aIRSendData The values of protocol, address, command and repeat flag are taken for sending.
* @param aNumberOfRepeats Number of repeats to send after the initial data if data is no repeat.
* @return 1 if data sent, 0 if no data sent (i.e. for BANG_OLUFSEN, which is currently not supported here)
*/
/**
* Interprets and sends a IRData structure.
* @param aIRSendData The values of protocol, address, command and repeat flag are taken for sending.
* @param aNumberOfRepeats Number of repeats to send after the initial data if data is no repeat.
* @return 1 if data sent, 0 if no data sent (i.e. for BANG_OLUFSEN, which is currently not supported here)
*/
size_t IRsend::write(IRData *aIRSendData, int_fast8_t aNumberOfRepeats) {
auto tProtocol = aIRSendData->protocol;
auto tAddress = aIRSendData->address;
auto tCommand = aIRSendData->command;
bool tIsRepeat = (aIRSendData->flags & IRDATA_FLAGS_IS_REPEAT);
if (tIsRepeat) {
aNumberOfRepeats = -1; // if aNumberOfRepeats < 0 then only a special repeat frame will be sent
}
// switch (tProtocol) { // 26 bytes bigger than if, else if, else
// case NEC:
// sendNEC(tAddress, tCommand, aNumberOfRepeats, tSendRepeat);
// break;
// case SAMSUNG:
// sendSamsung(tAddress, tCommand, aNumberOfRepeats);
// break;
// case SONY:
// sendSony(tAddress, tCommand, aNumberOfRepeats, aIRSendData->numberOfBits);
// break;
// case PANASONIC:
// sendPanasonic(tAddress, tCommand, aNumberOfRepeats);
// break;
// case DENON:
// sendDenon(tAddress, tCommand, aNumberOfRepeats);
// break;
// case SHARP:
// sendSharp(tAddress, tCommand, aNumberOfRepeats);
// break;
// case JVC:
// sendJVC((uint8_t) tAddress, (uint8_t) tCommand, aNumberOfRepeats); // casts are required to specify the right function
// break;
// case RC5:
// sendRC5(tAddress, tCommand, aNumberOfRepeats, !tSendRepeat); // No toggle for repeats
// break;
// case RC6:
// // No toggle for repeats// sendRC6(tAddress, tCommand, aNumberOfRepeats, !tSendRepeat); // No toggle for repeats
// break;
// default:
// break;
// }
/*
* Order of protocols is in guessed relevance :-)
*/
if (tProtocol == NEC) {
sendNEC(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == SAMSUNG) {
sendSamsung(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == SAMSUNG48) {
sendSamsung48(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == SAMSUNG_LG) {
sendSamsungLG(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == SONY) {
sendSony(tAddress, tCommand, aNumberOfRepeats, aIRSendData->numberOfBits);
} else if (tProtocol == PANASONIC) {
sendPanasonic(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == DENON) {
sendDenon(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == SHARP) {
sendSharp(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == LG) {
sendLG(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == JVC) {
sendJVC((uint8_t) tAddress, (uint8_t) tCommand, aNumberOfRepeats); // casts are required to specify the right function
} else if (tProtocol == RC5) {
sendRC5(tAddress, tCommand, aNumberOfRepeats, !tIsRepeat); // No toggle for repeats
} else if (tProtocol == RC6) {
sendRC6(tAddress, tCommand, aNumberOfRepeats, !tIsRepeat); // No toggle for repeats
} else if (tProtocol == KASEIKYO_JVC) {
sendKaseikyo_JVC(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == KASEIKYO_DENON) {
sendKaseikyo_Denon(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == KASEIKYO_SHARP) {
sendKaseikyo_Sharp(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == KASEIKYO_MITSUBISHI) {
sendKaseikyo_Mitsubishi(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == NEC2) {
sendNEC2(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == ONKYO) {
sendOnkyo(tAddress, tCommand, aNumberOfRepeats);
} else if (tProtocol == APPLE) {
sendApple(tAddress, tCommand, aNumberOfRepeats);
#if !defined(EXCLUDE_EXOTIC_PROTOCOLS)
} else if (tProtocol == BOSEWAVE) {
sendBoseWave(tCommand, aNumberOfRepeats);
} else if (tProtocol == MAGIQUEST) {
// we have a 32 bit ID/address
sendMagiQuest(aIRSendData->decodedRawData, tCommand);
} else if (tProtocol == FAST) {
// We have only 8 bit command
sendFAST(tCommand, aNumberOfRepeats);
} else if (tProtocol == LEGO_PF) {
sendLegoPowerFunctions(tAddress, tCommand, tCommand >> 4, tIsRepeat); // send 5 autorepeats
#endif
} else {
return 0; // Not supported by write. E.g for BANG_OLUFSEN
}
return 1;
}
/**
* Simple version of write without support for MAGIQUEST and numberOfBits for SONY protocol
* @param aNumberOfRepeats If aNumberOfRepeats < 0 then only a special repeat frame without leading and trailing space
* will be sent by calling NECProtocolConstants.SpecialSendRepeatFunction().
*/
size_t IRsend::write(decode_type_t aProtocol, uint16_t aAddress, uint16_t aCommand, int_fast8_t aNumberOfRepeats) {
// switch (aProtocol) { // 26 bytes bigger than if, else if, else
// case NEC:
// sendNEC(aAddress, aCommand, aNumberOfRepeats, tSendRepeat);
// break;
// case SAMSUNG:
// sendSamsung(aAddress, aCommand, aNumberOfRepeats);
// break;
// case SONY:
// sendSony(aAddress, aCommand, aNumberOfRepeats, aIRSendData->numberOfBits);
// break;
// case PANASONIC:
// sendPanasonic(aAddress, aCommand, aNumberOfRepeats);
// break;
// case DENON:
// sendDenon(aAddress, aCommand, aNumberOfRepeats);
// break;
// case SHARP:
// sendSharp(aAddress, aCommand, aNumberOfRepeats);
// break;
// case JVC:
// sendJVC((uint8_t) aAddress, (uint8_t) aCommand, aNumberOfRepeats); // casts are required to specify the right function
// break;
// case RC5:
// sendRC5(aAddress, aCommand, aNumberOfRepeats, !tSendRepeat); // No toggle for repeats
// break;
// case RC6:
// // No toggle for repeats// sendRC6(aAddress, aCommand, aNumberOfRepeats, !tSendRepeat); // No toggle for repeats
// break;
// default:
// break;
// }
/*
* Order of protocols is in guessed relevance :-)
*/
if (aProtocol == NEC) {
sendNEC(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == SAMSUNG) {
sendSamsung(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == SAMSUNG48) {
sendSamsung48(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == SAMSUNG_LG) {
sendSamsungLG(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == SONY) {
sendSony(aAddress, aCommand, aNumberOfRepeats, SIRCS_12_PROTOCOL);
} else if (aProtocol == PANASONIC) {
sendPanasonic(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == DENON) {
sendDenon(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == SHARP) {
sendSharp(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == LG) {
sendLG(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == JVC) {
sendJVC((uint8_t) aAddress, (uint8_t) aCommand, aNumberOfRepeats); // casts are required to specify the right function
} else if (aProtocol == RC5) {
sendRC5(aAddress, aCommand, aNumberOfRepeats, (aNumberOfRepeats > 0)); // No toggle for repeats
} else if (aProtocol == RC6) {
sendRC6(aAddress, aCommand, aNumberOfRepeats, (aNumberOfRepeats > 0)); // No toggle for repeats
} else if (aProtocol == KASEIKYO_JVC) {
sendKaseikyo_JVC(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == KASEIKYO_DENON) {
sendKaseikyo_Denon(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == KASEIKYO_SHARP) {
sendKaseikyo_Sharp(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == KASEIKYO_MITSUBISHI) {
sendKaseikyo_Mitsubishi(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == NEC2) {
sendNEC2(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == ONKYO) {
sendOnkyo(aAddress, aCommand, aNumberOfRepeats);
} else if (aProtocol == APPLE) {
sendApple(aAddress, aCommand, aNumberOfRepeats);
#if !defined(EXCLUDE_EXOTIC_PROTOCOLS)
} else if (aProtocol == BOSEWAVE) {
sendBoseWave(aCommand, aNumberOfRepeats);
} else if (aProtocol == FAST) {
// We have only 8 bit command
sendFAST(aCommand, aNumberOfRepeats);
} else if (aProtocol == LEGO_PF) {
sendLegoPowerFunctions(aAddress, aCommand, aCommand >> 4, (aNumberOfRepeats < 0)); // send 5 autorepeats, except for dedicated repeats
#endif
} else {
return 0; // Not supported by write. E.g for BANG_OLUFSEN
}
return 1;
}
/**
* Function using an 16 byte microsecond timing array for every purpose.
* Raw data starts with a Mark. No leading space as in received timing data!
*/
void IRsend::sendRaw(const uint16_t aBufferWithMicroseconds[], uint_fast16_t aLengthOfBuffer, uint_fast8_t aIRFrequencyKilohertz) {
// Set IR carrier frequency
enableIROut(aIRFrequencyKilohertz);
/*
* Raw data starts with a mark.
*/
for (uint_fast16_t i = 0; i < aLengthOfBuffer; i++) {
if (i & 1) {
// Odd
space(aBufferWithMicroseconds[i]);
} else {
mark(aBufferWithMicroseconds[i]);
}
}
}
/**
* Function using an 8 byte tick timing array to save program memory
* Raw data starts with a Mark. No leading space as in received timing data!
*/
void IRsend::sendRaw(const uint8_t aBufferWithTicks[], uint_fast16_t aLengthOfBuffer, uint_fast8_t aIRFrequencyKilohertz) {
// Set IR carrier frequency
enableIROut(aIRFrequencyKilohertz);
for (uint_fast16_t i = 0; i < aLengthOfBuffer; i++) {
if (i & 1) {
// Odd
space(aBufferWithTicks[i] * MICROS_PER_TICK);
} else {
mark(aBufferWithTicks[i] * MICROS_PER_TICK);
}
}
IRLedOff(); // Always end with the LED off
}
/**
* Function using an 16 byte microsecond timing array in FLASH for every purpose.
* Raw data starts with a Mark. No leading space as in received timing data!
*/
void IRsend::sendRaw_P(const uint16_t aBufferWithMicroseconds[], uint_fast16_t aLengthOfBuffer,
uint_fast8_t aIRFrequencyKilohertz) {
#if !defined(__AVR__)
sendRaw(aBufferWithMicroseconds, aLengthOfBuffer, aIRFrequencyKilohertz); // Let the function work for non AVR platforms
#else
// Set IR carrier frequency
enableIROut(aIRFrequencyKilohertz);
/*
* Raw data starts with a mark
*/
for (uint_fast16_t i = 0; i < aLengthOfBuffer; i++) {
auto duration = pgm_read_word(&aBufferWithMicroseconds[i]);
if (i & 1) {
// Odd
space(duration);
} else {
mark(duration);
}
}
#endif
}
/**
* New function using an 8 byte tick timing array in FLASH to save program memory
* Raw data starts with a Mark. No leading space as in received timing data!
*/
void IRsend::sendRaw_P(const uint8_t aBufferWithTicks[], uint_fast16_t aLengthOfBuffer, uint_fast8_t aIRFrequencyKilohertz) {
#if !defined(__AVR__)
sendRaw(aBufferWithTicks, aLengthOfBuffer, aIRFrequencyKilohertz); // Let the function work for non AVR platforms
#else
// Set IR carrier frequency
enableIROut(aIRFrequencyKilohertz);
for (uint_fast16_t i = 0; i < aLengthOfBuffer; i++) {
uint_fast16_t duration = pgm_read_byte(&aBufferWithTicks[i]) * (uint_fast16_t) MICROS_PER_TICK;
if (i & 1) {
// Odd
space(duration);
} else {
mark(duration);
}
}
IRLedOff(); // Always end with the LED off
#endif
}
/**
* Sends PulseDistance data from array
* For LSB First the LSB of array[0] is sent first then all bits until MSB of array[0]. Next is LSB of array[1] and so on.
* The output always ends with a space
* Stop bit is always sent
*/
void IRsend::sendPulseDistanceWidthFromArray(uint_fast8_t aFrequencyKHz, uint16_t aHeaderMarkMicros, uint16_t aHeaderSpaceMicros,
uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros, uint16_t aZeroSpaceMicros,
IRRawDataType *aDecodedRawDataArray, uint16_t aNumberOfBits, bool aMSBFirst, bool aSendStopBit,
uint16_t aRepeatPeriodMillis, int_fast8_t aNumberOfRepeats) {
uint8_t tFlags = 0;
if (aMSBFirst) {
tFlags = PROTOCOL_IS_MSB_FIRST;
}
(void) aSendStopBit;
sendPulseDistanceWidthFromArray(aFrequencyKHz, aHeaderMarkMicros, aHeaderSpaceMicros, aOneMarkMicros, aOneSpaceMicros,
aZeroMarkMicros, aZeroSpaceMicros, aDecodedRawDataArray, aNumberOfBits, tFlags, aRepeatPeriodMillis, aNumberOfRepeats);
}
void IRsend::sendPulseDistanceWidthFromArray(uint_fast8_t aFrequencyKHz, DistanceWidthTimingInfoStruct *aDistanceWidthTimingInfo,
IRRawDataType *aDecodedRawDataArray, uint16_t aNumberOfBits, uint8_t aFlags, uint16_t aRepeatPeriodMillis,
int_fast8_t aNumberOfRepeats) {
sendPulseDistanceWidthFromArray(aFrequencyKHz, aDistanceWidthTimingInfo->HeaderMarkMicros,
aDistanceWidthTimingInfo->HeaderSpaceMicros, aDistanceWidthTimingInfo->OneMarkMicros,
aDistanceWidthTimingInfo->OneSpaceMicros, aDistanceWidthTimingInfo->ZeroMarkMicros,
aDistanceWidthTimingInfo->ZeroSpaceMicros, aDecodedRawDataArray, aNumberOfBits, aFlags, aRepeatPeriodMillis,
aNumberOfRepeats);
}
void IRsend::sendPulseDistanceWidthFromArray(uint_fast8_t aFrequencyKHz, uint16_t aHeaderMarkMicros, uint16_t aHeaderSpaceMicros,
uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros, uint16_t aZeroSpaceMicros,
IRRawDataType *aDecodedRawDataArray, uint16_t aNumberOfBits, uint8_t aFlags, uint16_t aRepeatPeriodMillis,
int_fast8_t aNumberOfRepeats) {
// Set IR carrier frequency
enableIROut(aFrequencyKHz);
uint_fast8_t tNumberOfCommands = aNumberOfRepeats + 1;
uint_fast8_t tNumberOf32Or64BitChunks = ((aNumberOfBits - 1) / BITS_IN_RAW_DATA_TYPE) + 1;
#if defined(LOCAL_DEBUG)
// fist data
Serial.print(F("Data[0]=0x"));
Serial.print(aDecodedRawDataArray[0], HEX);
if (tNumberOf32Or64BitChunks > 1) {
Serial.print(F(" Data[1]=0x"));
Serial.print(aDecodedRawDataArray[1], HEX);
}
Serial.print(F(" #="));
Serial.println(aNumberOfBits);
Serial.flush();
#endif
while (tNumberOfCommands > 0) {
unsigned long tStartOfFrameMillis = millis();
// Header
mark(aHeaderMarkMicros);
space(aHeaderSpaceMicros);
for (uint_fast8_t i = 0; i < tNumberOf32Or64BitChunks; ++i) {
uint8_t tNumberOfBitsForOneSend;
// Manage stop bit
uint8_t tFlags;
if (i == (tNumberOf32Or64BitChunks - 1)) {
// End of data
tNumberOfBitsForOneSend = aNumberOfBits;
tFlags = aFlags;
} else {
// intermediate data
tNumberOfBitsForOneSend = BITS_IN_RAW_DATA_TYPE;
tFlags = aFlags | SUPPRESS_STOP_BIT_FOR_THIS_DATA; // No stop bit for leading data
}
sendPulseDistanceWidthData(aOneMarkMicros, aOneSpaceMicros, aZeroMarkMicros, aZeroSpaceMicros, aDecodedRawDataArray[i],
tNumberOfBitsForOneSend, tFlags);
aNumberOfBits -= BITS_IN_RAW_DATA_TYPE;
}
tNumberOfCommands--;
// skip last delay!
if (tNumberOfCommands > 0) {
/*
* Check and fallback for wrong RepeatPeriodMillis parameter. I.e the repeat period must be greater than each frame duration.
*/
auto tFrameDurationMillis = millis() - tStartOfFrameMillis;
if (aRepeatPeriodMillis > tFrameDurationMillis) {
delay(aRepeatPeriodMillis - tFrameDurationMillis);
}
}
}
}
/**
* Sends PulseDistance data from array using PulseDistanceWidthProtocolConstants
* For LSB First the LSB of array[0] is sent first then all bits until MSB of array[0]. Next is LSB of array[1] and so on.
* The output always ends with a space
* Stop bit is always sent
*/
void IRsend::sendPulseDistanceWidthFromArray(PulseDistanceWidthProtocolConstants *aProtocolConstants,
IRRawDataType *aDecodedRawDataArray, uint16_t aNumberOfBits, int_fast8_t aNumberOfRepeats) {
// Calling sendPulseDistanceWidthFromArray() costs 68 bytes program memory compared to the implementation below
// sendPulseDistanceWidthFromArray(aProtocolConstants->FrequencyKHz, aProtocolConstants->DistanceWidthTimingInfo.HeaderMarkMicros,
// aProtocolConstants->DistanceWidthTimingInfo.HeaderSpaceMicros,
// aProtocolConstants->DistanceWidthTimingInfo.OneMarkMicros, aProtocolConstants->DistanceWidthTimingInfo.OneSpaceMicros,
// aProtocolConstants->DistanceWidthTimingInfo.ZeroMarkMicros, aProtocolConstants->DistanceWidthTimingInfo.ZeroSpaceMicros,
// aDecodedRawDataArray, aNumberOfBits, aProtocolConstants->Flags, aProtocolConstants->RepeatPeriodMillis,
// aNumberOfRepeats);
// Set IR carrier frequency
enableIROut(aProtocolConstants->FrequencyKHz);
uint_fast8_t tNumberOf32Or64BitChunks = ((aNumberOfBits - 1) / BITS_IN_RAW_DATA_TYPE) + 1;
#if defined(LOCAL_DEBUG)
// fist data
Serial.print(F("Data[0]=0x"));
Serial.print(aDecodedRawDataArray[0], HEX);
if (tNumberOf32Or64BitChunks > 1) {
Serial.print(F(" Data[1]=0x"));
Serial.print(aDecodedRawDataArray[1], HEX);
}
Serial.print(F(" #="));
Serial.println(aNumberOfBits);
Serial.flush();
#endif
uint_fast8_t tNumberOfCommands = aNumberOfRepeats + 1;
while (tNumberOfCommands > 0) {
auto tStartOfFrameMillis = millis();
auto tNumberOfBits = aNumberOfBits; // refresh value for repeats
// Header
mark(aProtocolConstants->DistanceWidthTimingInfo.HeaderMarkMicros);
space(aProtocolConstants->DistanceWidthTimingInfo.HeaderSpaceMicros);
uint8_t tOriginalFlags = aProtocolConstants->Flags;
for (uint_fast8_t i = 0; i < tNumberOf32Or64BitChunks; ++i) {
uint8_t tNumberOfBitsForOneSend;
uint8_t tFlags;
if (i == (tNumberOf32Or64BitChunks - 1)) {
// End of data
tNumberOfBitsForOneSend = tNumberOfBits;
tFlags = tOriginalFlags;
} else {
// intermediate data
tNumberOfBitsForOneSend = BITS_IN_RAW_DATA_TYPE;
tFlags = tOriginalFlags | SUPPRESS_STOP_BIT_FOR_THIS_DATA; // No stop bit for leading data
}
sendPulseDistanceWidthData(aProtocolConstants->DistanceWidthTimingInfo.OneMarkMicros,
aProtocolConstants->DistanceWidthTimingInfo.OneSpaceMicros,
aProtocolConstants->DistanceWidthTimingInfo.ZeroMarkMicros,
aProtocolConstants->DistanceWidthTimingInfo.ZeroSpaceMicros, aDecodedRawDataArray[i], tNumberOfBitsForOneSend,
tFlags);
tNumberOfBits -= BITS_IN_RAW_DATA_TYPE;
}
tNumberOfCommands--;
// skip last delay!
if (tNumberOfCommands > 0) {
/*
* Check and fallback for wrong RepeatPeriodMillis parameter. I.e the repeat period must be greater than each frame duration.
*/
auto tFrameDurationMillis = millis() - tStartOfFrameMillis;
if (aProtocolConstants->RepeatPeriodMillis > tFrameDurationMillis) {
delay(aProtocolConstants->RepeatPeriodMillis - tFrameDurationMillis);
}
}
}
}
/**
* Sends PulseDistance frames and repeats and enables receiver again
* @param aProtocolConstants The constants to use for sending this protocol.
* @param aData uint32 or uint64 holding the bits to be sent.
* @param aNumberOfBits Number of bits from aData to be actually sent.
* @param aNumberOfRepeats If < 0 and a aProtocolConstants->SpecialSendRepeatFunction() is specified
* then it is called without leading and trailing space.
*/
void IRsend::sendPulseDistanceWidth(PulseDistanceWidthProtocolConstants *aProtocolConstants, IRRawDataType aData,
uint_fast8_t aNumberOfBits, int_fast8_t aNumberOfRepeats) {
#if defined(LOCAL_DEBUG)
Serial.print(F("Data=0x"));
Serial.print(aData, HEX);
Serial.print(F(" #="));
Serial.println(aNumberOfBits);
Serial.flush();
#endif
if (aNumberOfRepeats < 0) {
if (aProtocolConstants->SpecialSendRepeatFunction != NULL) {
aProtocolConstants->SpecialSendRepeatFunction();
return;
} else {
aNumberOfRepeats = 0; // send a plain frame as repeat
}
}
// Set IR carrier frequency
enableIROut(aProtocolConstants->FrequencyKHz);
uint_fast8_t tNumberOfCommands = aNumberOfRepeats + 1;
while (tNumberOfCommands > 0) {
unsigned long tStartOfFrameMillis = millis();
if (tNumberOfCommands < ((uint_fast8_t) aNumberOfRepeats + 1) && aProtocolConstants->SpecialSendRepeatFunction != NULL) {
// send special repeat
aProtocolConstants->SpecialSendRepeatFunction();
} else {
/*
* Send Header and regular frame
*/
mark(aProtocolConstants->DistanceWidthTimingInfo.HeaderMarkMicros);
space(aProtocolConstants->DistanceWidthTimingInfo.HeaderSpaceMicros);
sendPulseDistanceWidthData(aProtocolConstants, aData, aNumberOfBits);
}
tNumberOfCommands--;
// skip last delay!
if (tNumberOfCommands > 0) {
/*
* Check and fallback for wrong RepeatPeriodMillis parameter. I.e the repeat period must be greater than each frame duration.
*/
auto tFrameDurationMillis = millis() - tStartOfFrameMillis;
if (aProtocolConstants->RepeatPeriodMillis > tFrameDurationMillis) {
delay(aProtocolConstants->RepeatPeriodMillis - tFrameDurationMillis);
}
}
}
}
/**
* Sends PulseDistance frames and repeats.
* @param aFrequencyKHz, aHeaderMarkMicros, aHeaderSpaceMicros, aOneMarkMicros, aOneSpaceMicros, aZeroMarkMicros, aZeroSpaceMicros, aFlags, aRepeatPeriodMillis Values to use for sending this protocol, also contained in the PulseDistanceWidthProtocolConstants of this protocol.
* @param aData uint32 or uint64 holding the bits to be sent.
* @param aNumberOfBits Number of bits from aData to be actually sent.
* @param aNumberOfRepeats If < 0 and a aProtocolConstants->SpecialSendRepeatFunction() is specified
* then it is called without leading and trailing space.
* @param aSpecialSendRepeatFunction If NULL, the first frame is repeated completely, otherwise this function is used for sending the repeat frame.
*/
void IRsend::sendPulseDistanceWidth(uint_fast8_t aFrequencyKHz, uint16_t aHeaderMarkMicros, uint16_t aHeaderSpaceMicros,
uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros, uint16_t aZeroSpaceMicros, IRRawDataType aData,
uint_fast8_t aNumberOfBits, bool aMSBFirst, bool aSendStopBit, uint16_t aRepeatPeriodMillis, int_fast8_t aNumberOfRepeats,
void (*aSpecialSendRepeatFunction)()) {
uint8_t tFlags = 0;
if (aMSBFirst) {
tFlags = PROTOCOL_IS_MSB_FIRST;
}
(void) aSendStopBit;
sendPulseDistanceWidth(aFrequencyKHz, aHeaderMarkMicros, aHeaderSpaceMicros, aOneMarkMicros, aOneSpaceMicros, aZeroMarkMicros,
aZeroSpaceMicros, aData, aNumberOfBits, tFlags, aRepeatPeriodMillis, aNumberOfRepeats, aSpecialSendRepeatFunction);
}
void IRsend::sendPulseDistanceWidth(uint_fast8_t aFrequencyKHz, uint16_t aHeaderMarkMicros, uint16_t aHeaderSpaceMicros,
uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros, uint16_t aZeroSpaceMicros, IRRawDataType aData,
uint_fast8_t aNumberOfBits, uint8_t aFlags, uint16_t aRepeatPeriodMillis, int_fast8_t aNumberOfRepeats,
void (*aSpecialSendRepeatFunction)()) {
if (aNumberOfRepeats < 0) {
if (aSpecialSendRepeatFunction != NULL) {
aSpecialSendRepeatFunction();
return;
} else {
aNumberOfRepeats = 0; // send a plain frame as repeat
}
}
// Set IR carrier frequency
enableIROut(aFrequencyKHz);
uint_fast8_t tNumberOfCommands = aNumberOfRepeats + 1;
while (tNumberOfCommands > 0) {
unsigned long tStartOfFrameMillis = millis();
if (tNumberOfCommands < ((uint_fast8_t) aNumberOfRepeats + 1) && aSpecialSendRepeatFunction != NULL) {
// send special repeat
aSpecialSendRepeatFunction();
} else {
// Header and regular frame
mark(aHeaderMarkMicros);
space(aHeaderSpaceMicros);
sendPulseDistanceWidthData(aOneMarkMicros, aOneSpaceMicros, aZeroMarkMicros, aZeroSpaceMicros, aData, aNumberOfBits,
aFlags);
}
tNumberOfCommands--;
// skip last delay!
if (tNumberOfCommands > 0) {
/*
* Check and fallback for wrong RepeatPeriodMillis parameter. I.e the repeat period must be greater than each frame duration.
*/
auto tFrameDurationMillis = millis() - tStartOfFrameMillis;
if (aRepeatPeriodMillis > tFrameDurationMillis) {
delay(aRepeatPeriodMillis - tFrameDurationMillis);
}
}
}
}
/**
* Sends PulseDistance data
* The output always ends with a space
* Each additional call costs 16 bytes program memory
*/
void IRsend::sendPulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, IRRawDataType aData,
uint_fast8_t aNumberOfBits) {
sendPulseDistanceWidthData(aProtocolConstants->DistanceWidthTimingInfo.OneMarkMicros,
aProtocolConstants->DistanceWidthTimingInfo.OneSpaceMicros, aProtocolConstants->DistanceWidthTimingInfo.ZeroMarkMicros,
aProtocolConstants->DistanceWidthTimingInfo.ZeroSpaceMicros, aData, aNumberOfBits, aProtocolConstants->Flags);
}
/**
* Sends PulseDistance data
* The output always ends with a space
*/
void IRsend::sendPulseDistanceWidthData(uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros,
uint16_t aZeroSpaceMicros, IRRawDataType aData, uint_fast8_t aNumberOfBits, bool aMSBFirst, bool aSendStopBit) {
uint8_t tFlags = 0;
if (aMSBFirst) {
tFlags = PROTOCOL_IS_MSB_FIRST;
}
(void) aSendStopBit;
sendPulseDistanceWidthData(aOneMarkMicros, aOneSpaceMicros, aZeroMarkMicros, aZeroSpaceMicros, aData, aNumberOfBits, tFlags);
}
void IRsend::sendPulseDistanceWidthData(uint16_t aOneMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros,
uint16_t aZeroSpaceMicros, IRRawDataType aData, uint_fast8_t aNumberOfBits, uint8_t aFlags) {
#if defined(LOCAL_DEBUG)
Serial.print(aData, HEX);
Serial.print('|');
Serial.println(aNumberOfBits);
Serial.flush();
#endif
// For MSBFirst, send data from MSB to LSB until mask bit is shifted out
IRRawDataType tMask = 1ULL << (aNumberOfBits - 1);
for (uint_fast8_t i = aNumberOfBits; i > 0; i--) {
if (((aFlags & PROTOCOL_IS_MSB_FIRST) && (aData & tMask)) || (!(aFlags & PROTOCOL_IS_MSB_FIRST) && (aData & 1))) {
#if defined(LOCAL_TRACE)
Serial.print('1');
#endif
mark(aOneMarkMicros);
space(aOneSpaceMicros);
} else {
#if defined(LOCAL_TRACE)
Serial.print('0');
#endif
mark(aZeroMarkMicros);
space(aZeroSpaceMicros);
}
if (aFlags & PROTOCOL_IS_MSB_FIRST) {
tMask >>= 1;
} else {
aData >>= 1;
}
}
/*
* Stop bit is sent for all pulse distance protocols i.e. aOneMarkMicros == aZeroMarkMicros.
* Therefore it is not sent for Sony and Magiquest :-)
*/
if (!(aFlags & SUPPRESS_STOP_BIT_FOR_THIS_DATA) && aOneMarkMicros == aZeroMarkMicros) {
// Send stop bit here
#if defined(LOCAL_TRACE)
Serial.print('S');
#endif
mark(aZeroMarkMicros); // Use aZeroMarkMicros for stop bits. This seems to be correct for all protocols :-)
}
#if defined(LOCAL_TRACE)
Serial.println();
#endif
}
/**
* Sends Biphase data MSB first
* Always send start bit, do not send the trailing space of the start bit
* 0 -> mark+space
* 1 -> space+mark
* The output always ends with a space
* can only send 31 bit data, since we put the start bit as 32th bit on front
*/
void IRsend::sendBiphaseData(uint16_t aBiphaseTimeUnit, uint32_t aData, uint_fast8_t aNumberOfBits) {
IR_TRACE_PRINT(F("0x"));
IR_TRACE_PRINT(aData, HEX);
#if defined(LOCAL_TRACE)
Serial.print('S');
#endif
// Data - Biphase code MSB first
// prepare for start with sending the start bit, which is 1
uint32_t tMask = 1UL << aNumberOfBits; // mask is now set for the virtual start bit
uint_fast8_t tLastBitValue = 1; // Start bit is a 1
bool tNextBitIsOne = 1; // Start bit is a 1
for (uint_fast8_t i = aNumberOfBits + 1; i > 0; i--) {
bool tCurrentBitIsOne = tNextBitIsOne;
tMask >>= 1;
tNextBitIsOne = ((aData & tMask) != 0) || (i == 1); // true for last bit to avoid extension of mark
if (tCurrentBitIsOne) {
#if defined(LOCAL_TRACE)
Serial.print('1');
#endif
space(aBiphaseTimeUnit);
if (tNextBitIsOne) {
mark(aBiphaseTimeUnit);
} else {
// if next bit is 0, extend the current mark in order to generate a continuous signal without short breaks
mark(2 * aBiphaseTimeUnit);
}
tLastBitValue = 1;
} else {
#if defined(LOCAL_TRACE)
Serial.print('0');
#endif
if (!tLastBitValue) {
mark(aBiphaseTimeUnit);
}
space(aBiphaseTimeUnit);
tLastBitValue = 0;
}
}
IR_TRACE_PRINTLN(F(""));
}
/**
* Sends an IR mark for the specified number of microseconds.
* The mark output is modulated at the PWM frequency if USE_NO_SEND_PWM is not defined.
* The output is guaranteed to be OFF / inactive after after the call of the function.
* This function may affect the state of feedback LED.
* Period time is 26 us for 38.46 kHz, 27 us for 37.04 kHz, 25 us for 40 kHz.
* On time is 8 us for 30% duty cycle
*
* The mark() function relies on the correct implementation of:
* delayMicroseconds() for pulse time, and micros() for pause time.
* The delayMicroseconds() of pulse time is guarded on AVR CPU's with noInterrupts() / interrupts().
* At the start of pause time, interrupts are enabled once, the rest of the pause is also guarded on AVR CPU's with noInterrupts() / interrupts().
* The maximum length of an interrupt during sending should not exceed 26 us - 8 us = 18 us, otherwise timing is disturbed.
* This disturbance is no problem, if the exceedance is small and does not happen too often.
*/
void IRsend::mark(uint16_t aMarkMicros) {
#if defined(SEND_PWM_BY_TIMER) || defined(USE_NO_SEND_PWM)
# if !defined(NO_LED_FEEDBACK_CODE)
if (FeedbackLEDControl.LedFeedbackEnabled == LED_FEEDBACK_ENABLED_FOR_SEND) {
setFeedbackLED(true);
}
# endif
#endif
#if defined(SEND_PWM_BY_TIMER)
/*
* Generate hardware PWM signal
*/
enableSendPWMByTimer(); // Enable timer or ledcWrite() generated PWM output
customDelayMicroseconds(aMarkMicros);
IRLedOff(); // disables hardware PWM and manages feedback LED
return;
#elif defined(USE_NO_SEND_PWM)
/*
* Here we generate no carrier PWM, just simulate an active low receiver signal.
*/
# if defined(USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN) && !defined(OUTPUT_OPEN_DRAIN)
pinModeFast(sendPin, OUTPUT); // active state for mimicking open drain
# else
digitalWriteFast(sendPin, LOW); // Set output to active low.
# endif
customDelayMicroseconds(aMarkMicros);
IRLedOff();
# if !defined(NO_LED_FEEDBACK_CODE)
if (FeedbackLEDControl.LedFeedbackEnabled == LED_FEEDBACK_ENABLED_FOR_SEND) {
setFeedbackLED(false);
}
return;
# endif
#else // defined(SEND_PWM_BY_TIMER)
/*
* Generate PWM by bit banging
*/
unsigned long tStartMicros = micros();
unsigned long tNextPeriodEnding = tStartMicros;
unsigned long tMicros;
# if !defined(NO_LED_FEEDBACK_CODE)
bool FeedbackLedIsActive = false;
# endif
do {
// digitalToggleFast(_IR_TIMING_TEST_PIN);
/*
* Output the PWM pulse
*/
noInterrupts(); // do not let interrupts extend the short on period