-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
TV-B-Gone.cpp
204 lines (175 loc) · 5.66 KB
/
TV-B-Gone.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
/*
Last Updated: 30 Mar. 2018
By Anton Grimpelhuber ([email protected])
------------------------------------------------------------
LICENSE:
------------------------------------------------------------
Distributed under Creative Commons 2.5 -- Attribution & Share Alike
*/
#include "core/display.h"
#include "core/mykeyboard.h"
#include "core/sd_functions.h"
#include "core/settings.h"
#include "WORLD_IR_CODES.h"
#include "TV-B-Gone.h"
#include "core/utils.h"
/*
Last Updated: 30 Mar. 2018
By Anton Grimpelhuber ([email protected])
*/
// The TV-B-Gone for Arduino can use either the EU (European Union) or the NA (North America) database of POWER CODES
// EU is for Europe, Middle East, Australia, New Zealand, and some countries in Africa and South America
// NA is for North America, Asia, and the rest of the world not covered by EU
// Two regions!
#define NA 1 //set by a HIGH on REGIONSWITCH pin
#define EU 0 //set by a LOW on REGIONSWITCH pin
// Lets us calculate the size of the NA/EU databases
#define NUM_ELEM(x) (sizeof (x) / sizeof (*(x)));
// set define to 0 to turn off debug output
#define DEBUG 0
#define DEBUGP(x) if (DEBUG == 1) { x ; }
// Shortcut to insert single, non-optimized-out nop
#define NOPP __asm__ __volatile__ ("nop")
// Not used any more on esp8266, so don't bother
// Tweak this if cessary to change timing
// -for 8MHz Arduinos, a good starting value is 11
// -for 16MHz Arduinos, a good starting value is 25
#define DELAY_CNT 25
void xmitCodeElement(uint16_t ontime, uint16_t offtime, uint8_t PWM_code );
void quickflashLEDx( uint8_t x );
void delay_ten_us(uint16_t us);
void quickflashLED( void );
uint8_t read_bits(uint8_t count);
uint16_t rawData[300];
#define MAX_WAIT_TIME 65535 //tens of us (ie: 655.350ms)
extern const IrCode* const NApowerCodes[];
extern const IrCode* const EUpowerCodes[];
uint8_t num_NAcodes = NUM_ELEM(NApowerCodes);
uint8_t num_EUcodes = NUM_ELEM(EUpowerCodes);
uint8_t bitsleft_r = 0;
uint8_t bits_r = 0;
uint8_t code_ptr;
volatile const IrCode * powerCode;
uint8_t read_bits(uint8_t count)
{
uint8_t i;
uint8_t tmp = 0;
for (i = 0; i < count; i++) {
if (bitsleft_r == 0) {
bits_r = powerCode->codes[code_ptr++];
bitsleft_r = 8;
}
bitsleft_r--;
tmp |= (((bits_r >> (bitsleft_r)) & 1) << (count - 1 - i));
}
return tmp;
}
uint16_t ontime, offtime;
uint8_t i, num_codes;
uint8_t region;
void delay_ten_us(uint16_t us) {
uint8_t timer;
while (us != 0) {
for (timer = 0; timer <= DELAY_CNT; timer++) {
NOPP;
NOPP;
}
NOPP;
us--;
}
}
void quickflashLED( void ) {
#if defined(M5LED)
digitalWrite(IRLED, M5LED_ON);
delay_ten_us(3000); // 30 ms ON-time delay
digitalWrite(IRLED, M5LED_OFF);
#endif
}
void quickflashLEDx( uint8_t x ) {
quickflashLED();
while (--x) {
delay_ten_us(25000); // 250 ms OFF-time delay between flashes
quickflashLED();
}
}
void checkIrTxPin(){
const std::vector<std::pair<std::string, int>> pins = IR_TX_PINS;
int count=0;
for (auto pin : pins) {
if(pin.second==bruceConfig.irTx) count++;
}
if(count>0) return;
else gsetIrTxPin(true);
}
void StartTvBGone() {
Serial.begin(115200);
checkIrTxPin();
IRsend irsend(bruceConfig.irTx); // Set the GPIO to be used to sending the message.
irsend.begin();
pinMode(bruceConfig.irTx, OUTPUT);
// determine region
options = {
{"Region NA", [&]() { region = NA; }},
{"Region EU", [&]() { region = EU; }},
{"Main Menu", [=]() { backToMenu(); }},
};
delay(300);
loopOptions(options);
delay(300);
if (!returnToMenu) {
if (region) num_codes=num_NAcodes;
else num_codes=num_EUcodes;
bool endingEarly = false; //will be set to true if the user presses the button during code-sending
checkSelPress();
for (i=0 ; i<num_codes; i++) {
if (region == NA) powerCode = NApowerCodes[i];
else powerCode = EUpowerCodes[i];
const uint8_t freq = powerCode->timer_val;
const uint8_t numpairs = powerCode->numpairs;
const uint8_t bitcompression = powerCode->bitcompression;
// For EACH pair in this code....
code_ptr = 0;
for (uint8_t k=0; k<numpairs; k++) {
uint16_t ti;
ti = (read_bits(bitcompression)) * 2;
offtime = powerCode->times[ti]; // read word 1 - ontime
ontime = powerCode->times[ti + 1]; // read word 2 - offtime
rawData[k*2] = offtime * 10;
rawData[(k*2)+1] = ontime * 10;
}
progressHandler(i, num_codes);
irsend.sendRaw(rawData, (numpairs*2) , freq);
bitsleft_r=0;
delay_ten_us(20500);
// if user is pushing (holding down) TRIGGER button, stop transmission early
if (checkSelPress()) // Pause TV-B-Gone
{
while (checkSelPress()) yield();
displaySomething("Paused");
while (!checkSelPress()){ // If Presses Select again, continues
if(checkEscPress()) {
endingEarly= true;
break;
}
}
while (checkSelPress()){
yield();
}
if (endingEarly) break; // Cancels TV-B-Gone
displaySomething("Running, Wait");
}
} //end of POWER code for loop
if (endingEarly==false)
{
displaySomething("All codes sent!");
//pause for ~1.3 sec, then flash the visible LED 8 times to indicate that we're done
delay_ten_us(MAX_WAIT_TIME); // wait 655.350ms
delay_ten_us(MAX_WAIT_TIME); // wait 655.350ms
} else {
displayRedStripe("User Stoped");
delay(2000);
}
//turnoff LED
digitalWrite(bruceConfig.irTx,LED_OFF);
}
} //end of sendAllCodes