-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson_sensor_tv_remote.ino
278 lines (242 loc) · 7.76 KB
/
person_sensor_tv_remote.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
/* Infrared_Send.ino Example sketch for IRLib2 and Circuit Playground Express
Illustrates how to transmit an IR signal whenever you do push one of the
built-in pushbuttons.
*/
#include <math.h>
#include <Adafruit_CircuitPlayground.h>
#include <FlashStorage.h>
#include "person_sensor.h"
#include "audio_data_done.h"
#include "audio_data_down.h"
#include "audio_data_enter.h"
#include "audio_data_left.h"
#include "audio_data_lg.h"
#include "audio_data_mute.h"
#include "audio_data_pause.h"
#include "audio_data_play.h"
#include "audio_data_press.h"
#include "audio_data_recording.h"
#include "audio_data_right.h"
#include "audio_data_samsung.h"
#include "audio_data_sony.h"
#include "audio_data_tcl.h"
#include "audio_data_up.h"
#include "audio_data_vizio.h"
#if !defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
#error "Infrared support is only for the Circuit Playground Express, it doesn't work with the Classic version"
#endif
// How long to wait between reading the sensor. The sensor can be read as
// frequently as you like, but the results only change at about 5FPS, so
// waiting for 200ms is reasonable.
const int32_t SAMPLE_DELAY_MS = 200;
//These flags keep track of whether we received the first code
//and if we have have received a new different code from a previous one.
bool gotOne = false;
bool gotNew = false;
uint8_t codeProtocol = UNKNOWN;
uint32_t codeValue = 0;
uint8_t codeBits = 0;
typedef struct __attribute__ ((__packed__)) {
bool isValid;
uint8_t playCodeProtocol;
uint32_t playCodeValue;
uint8_t playCodeBits;
uint8_t pauseCodeProtocol;
uint32_t pauseCodeValue;
uint8_t pauseCodeBits;
} CodeConfig_t;
const CodeConfig_t samsungCodes = {
.playCodeProtocol = NECX,
.playCodeValue = 0xE0E0E21D,
.playCodeBits = 32,
.pauseCodeProtocol = NECX,
.pauseCodeValue = 0xE0E052AD,
.pauseCodeBits = 32,
};
CodeConfig_t codeConfig;
FlashStorage(flashStore, CodeConfig_t);
bool waitingForPlay = false;
bool waitingForPause = false;
bool isPaused = false;
// How long to wait to pause and play. Alter these to adjust the behavior.
const int32_t pauseDelaySeconds = 5;
const int32_t playDelaySeconds = 1;
// Convert timeout seconds into loop iteration counts.
const int32_t pauseDelayCount = (pauseDelaySeconds * 1000) / SAMPLE_DELAY_MS;
const int32_t playDelayCount = (playDelaySeconds * 1000) / SAMPLE_DELAY_MS;
int32_t timeSinceFaceSeen = 0;
int32_t timeFaceSeen = 0;
bool isPlaying = true;
const int audioSampleRate = 22050;
void ir_setup() {
gotOne=false;
gotNew=false;
Serial.begin(9600);
delay(2000);
CircuitPlayground.irReceiver.enableIRIn(); // Start the receiver
}
// Stores the code for later playback
void receiveCode(void) {
codeProtocol = CircuitPlayground.irDecoder.protocolNum;
if (codeProtocol == UNKNOWN) {
return;
}
Serial.print(F("Received "));
Serial.print(Pnames(codeProtocol));
if (CircuitPlayground.irDecoder.value == REPEAT_CODE) {
// Don't record a NEC repeat value as that's useless.
Serial.println(F("repeat; ignoring."));
return;
}
codeValue = CircuitPlayground.irDecoder.value;
codeBits = CircuitPlayground.irDecoder.bits;
gotNew=true;
gotOne=true;
Serial.print(F(" Value:0x"));
Serial.println(codeValue, HEX);
}
void flash_read_config() {
codeConfig = flashStore.read();
if (codeConfig.isValid) {
Serial.println("Read config from flash");
} else {
Serial.println("No config found, writing to flash");
codeConfig = samsungCodes;
codeConfig.isValid = true;
}
Serial.print(F("Read play = 0x"));
Serial.println(codeConfig.playCodeValue, HEX);
Serial.print(F("Read pause = 0x"));
Serial.println(codeConfig.pauseCodeValue, HEX);
}
void flash_write_config() {
flashStore.write(codeConfig);
}
// Macro to automatically look up the right variable name based on a phrase, and play it.
#define PLAY_AUDIO(NAME) do { play_audio(g_##NAME##_data, g_##NAME##_data_len); } while (false)
void play_audio(const uint8_t* bytes, int bytes_count) {
CircuitPlayground.speaker.playSound(bytes, bytes_count, audioSampleRate, false);
}
void setup() {
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
ir_setup();
flash_read_config();
Wire.begin();
}
bool is_recording() {
return (waitingForPlay || waitingForPause);
}
void ir_loop() {
static int pixelIndex = 0;
static int pixelInc = 1;
if (is_recording()) {
int8_t red;
int8_t green;
int8_t blue;
if (waitingForPlay) {
red = 255;
green = 255;
blue = 0;
} else {
red = 0;
green = 0;
blue = 255;
}
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(pixelIndex, red, green, blue);
pixelIndex = (pixelIndex + pixelInc + 10) % 10;
delay(SAMPLE_DELAY_MS);
}
if (CircuitPlayground.irReceiver.getResults()) {
CircuitPlayground.irDecoder.decode();
receiveCode();
CircuitPlayground.irReceiver.enableIRIn(); // Re-enable receiver
}
if (gotNew) {
if (waitingForPlay) {
codeConfig.playCodeProtocol = codeProtocol;
codeConfig.playCodeValue = codeValue;
codeConfig.playCodeBits = codeBits;
waitingForPlay = false;
waitingForPause = true;
PLAY_AUDIO(press);
PLAY_AUDIO(pause);
pixelInc = -pixelInc;
Serial.print(F("Play = 0x"));
Serial.println(codeConfig.playCodeValue, HEX);
} else if (waitingForPause) {
codeConfig.pauseCodeProtocol = codeProtocol;
codeConfig.pauseCodeValue = codeValue;
codeConfig.pauseCodeBits = codeBits;
waitingForPause = false;
PLAY_AUDIO(done);
pixelInc = -pixelInc;
CircuitPlayground.clearPixels();
Serial.print(F("Pause = 0x"));
Serial.println(codeConfig.pauseCodeValue, HEX);
flash_write_config();
}
gotNew = false;
}
}
void person_sensor_loop() {
if (is_recording()) {
return;
}
person_sensor_results_t results = {};
// Perform a read action on the I2C address of the sensor to get the
// current face information detected.
if (!person_sensor_read(&results)) {
Serial.println("No person sensor results found on the i2c bus");
delay(SAMPLE_DELAY_MS);
return;
}
const bool hasFace = (results.num_faces > 0);
if (hasFace) {
timeSinceFaceSeen = 0;
timeFaceSeen += 1;
} else {
timeSinceFaceSeen += 1;
timeFaceSeen = 0;
}
if (isPlaying) {
if (timeSinceFaceSeen == pauseDelayCount) {
CircuitPlayground.irSend.send(codeConfig.pauseCodeProtocol, codeConfig.pauseCodeValue, codeConfig.pauseCodeBits);
PLAY_AUDIO(pause);
isPlaying = false;
}
} else if (hasFace && (timeFaceSeen > playDelayCount)) {
CircuitPlayground.irSend.send(codeConfig.playCodeProtocol, codeConfig.playCodeValue, codeConfig.playCodeBits);
PLAY_AUDIO(play);
isPlaying = true;
}
delay(SAMPLE_DELAY_MS);
}
void loop() {
ir_loop();
person_sensor_loop();
// If the left button is pressed send a mute code.
if (CircuitPlayground.leftButton()) {
waitingForPlay = true;
PLAY_AUDIO(recording);
while (CircuitPlayground.leftButton()) {}//wait until button released
PLAY_AUDIO(press);
PLAY_AUDIO(play);
}
// If the right button is pressed send a play/pause code.
if (CircuitPlayground.rightButton()) {
if (isPaused) {
CircuitPlayground.irSend.send(codeConfig.playCodeProtocol, codeConfig.playCodeValue, codeConfig.playCodeBits);
Serial.print(F("Sending play = 0x"));
Serial.println(codeConfig.playCodeValue, HEX);
isPaused = false;
} else {
CircuitPlayground.irSend.send(codeConfig.pauseCodeProtocol, codeConfig.pauseCodeValue, codeConfig.pauseCodeBits);
Serial.print(F("Sending pause = 0x"));
Serial.println(codeConfig.pauseCodeValue, HEX);
isPaused = true;
}
while (CircuitPlayground.rightButton()) {}//wait until button released
}
}