-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_radio.ino
508 lines (469 loc) · 11 KB
/
custom_radio.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
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
/// Flopster101 @ https://github.com/Flopster101
///
/// This project implements an Arduino-based FM Radio around the RDA5807 integrated circuit.
/// It also makes use of the TM1637 4-digit display module for displaying frequency, volume and mute information.
/// Control is provided through 4 push buttons and an IR Receiver.
///
/// Open the Serial console with 9600 baud to see debug information.
///
/// Wiring
/// ------
/// Arduino port | RDA5807 signal
/// ------------ | ---------------
/// 3.3V | VCC
/// GND | GND
/// A5 | SCLK
/// A4 | SDIO
/// D2 | IR Receiver
/// D5 | TM1637 DIO
/// D6 | TM1637 CLK
/// D8 | Button 4
/// D9 | Button 3
/// D10 | Button 2
/// D11 | Button 1
///
/// * Power for certain components is provided through pin headers and external power supplies.
///
#include <Arduino.h>
#include <Wire.h>
#include <radio.h>
#include <RDA5807.h>
// TM1637 stuff
#define CLK 6
#define DIO 5
#include <TM1637TinyDisplay.h>
TM1637TinyDisplay display(CLK, DIO);
uint8_t dots = 0b01000000; // Add dots or colons (depends on display module)
// IR Remote library stuff
#define DECODE_NEC
#define SEND_PWM_BY_TIMER
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2
/// The default station that will be tuned by this sketch is 103.70 MHz.
#define DEFAULT_STATION 10370
#define DEFAULT_BAND 00 ///< The band that will be tuned by this sketch is US/Europe.
RDA5807 rx; ///< Create an instance of a RDA5807 chip radio
int default_digvol = 7;
char buf [64];
int freq_buffer_int;
String freq_buffer;
bool mute_status = false;
// Buttons
#include <DailyStruggleButton.h>
const int buttonCHUp = 9;
const int buttonCHDown = 8;
const int buttonVolUp = 10;
const int buttonVolDown = 11;
unsigned int longPressTime = 300;
unsigned int longPressTime_mute = 600;
// Initialize buttons
DailyStruggleButton buttonchup;
DailyStruggleButton buttonchdown;
DailyStruggleButton buttonvolup;
DailyStruggleButton buttonvoldown;
// Command queue
String cmdbuf = ""; // Decoded command received
int cmdbuf_int = 0; // Store command converted to integer
/// Setup a FM only radio configuration
/// with some debugging on the Serial port
void setup() {
// open the Serial port
Serial.begin(9600);
Serial.print("\nFM Radio Project by @Flopster101...");
delay(200);
// Start IRReceiver on the pin defined by IR_RECEIVE_PIN
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
// Initialize display
display.setBrightness(BRIGHT_HIGH);
display.clear();
display.showString("FM");
delay(500);
display.showString("Radio");
// Initialize buttons
buttonchup.set(buttonCHUp, CHUPBUTTON, INT_PULL_UP);
buttonchdown.set(buttonCHDown, CHDOWNBUTTON, INT_PULL_UP);
buttonvolup.set(buttonVolUp, VOLUPBUTTON, INT_PULL_UP);
buttonvoldown.set(buttonVolDown, VOLDOWNBUTTON, INT_PULL_UP);
buttonchup.enableLongPress(longPressTime);
buttonchdown.enableLongPress(longPressTime);
buttonvolup.enableLongPress(longPressTime_mute);
buttonvoldown.enableLongPress(longPressTime_mute);
// Initialize the Radio
rx.setup();
delay(300);
//rx.debugEnable();
rx.setMono(false);
rx.setMute(false);
mute_status=0;
rx.setFrequency(DEFAULT_STATION);
rx.setBand(DEFAULT_BAND);
rx.setVolume(default_digvol);
delay(300);
// Show frequency on display when initialization is done.
showFrequency_display();
} // setup
void translateIR() // takes action based on IR code received
{
switch(IrReceiver.decodedIRData.decodedRawData)
{
case 0xF6097708: // Vol down button on remote
{
Serial.println("Received! (-)");
global_VolDown();
}
break;
case 0xFD027708: // Vol up button on remote
{
Serial.println("Received! (+)");
global_VolUp();
}
break;
case 0xDC237708: // Mute button on remote
{
global_MuteRadio();
}
break;
case 0xE51A7708: // Exit button on remote
{
}
break;
case 0xFC037708: // CH UP button on remote
{
Serial.println("Received! (CH UP)");
global_CHUp();
}
break;
case 0xF50A7708: // CH DOWN button on remote
{
Serial.println("Received! (CH DOWN)");
global_CHDown();
}
break;
case 0xFE017708: // Info button on remote.
{
print_info();
}
break;
case 0xFA057708: // UP button on remote.
{
if (rx.getRealFrequency() != 10800) {
Serial.print("\n\nAdvancing 10 Hz...\n");
rx.setFrequency(rx.getRealFrequency() + 10);
print_info();
showFrequency_display();
}
else {
rx.setFrequency(8700);
}
}
break;
case 0xF9067708: // DOWN button on remote.
{
if (rx.getRealFrequency() != 8700) {
Serial.print("\n\nReceding 10 Hz...\n");
rx.setFrequency(rx.getRealFrequency() - 10);
print_info();
showFrequency_display();
}
else {
rx.setFrequency(10800);
print_info();
showFrequency_display();
}
break;
}
case 0xEE117708: // 1
{
cmdbuf = cmdbuf + "1";
delay(200);
showFrequency_counter();
break;
}
case 0xED127708: // 2
{
cmdbuf = cmdbuf + "2";
delay(200);
showFrequency_counter();
break;
}
case 0xEC137708: // 3
{
cmdbuf = cmdbuf + "3";
delay(200);
showFrequency_counter();
break;
}
case 0xEB147708: // 4
{
cmdbuf = cmdbuf + "4";
delay(200);
showFrequency_counter();
break;
}
case 0xEA157708: // 5
{
cmdbuf = cmdbuf + "5";
delay(200);
showFrequency_counter();
break;
}
case 0xE9167708: // 6
{
cmdbuf = cmdbuf + "6";
delay(200);
showFrequency_counter();
break;
}
case 0xE8177708: // 7
{
cmdbuf = cmdbuf + "7";
delay(200);
showFrequency_counter();
break;
}
case 0xE7187708: // 8
{
cmdbuf = cmdbuf + "8";
delay(200);
showFrequency_counter();
break;
}
case 0xE6197708: // 9
{
cmdbuf = cmdbuf + "9";
delay(200);
showFrequency_counter();
break;
}
case 0xEF107708: // 0
{
cmdbuf = cmdbuf + "0";
delay(200);
showFrequency_counter();
break;
}
case 0xFB047708: // OK
{
if (cmdbuf.length() < 3) {
display.showString("Err");
delay(500);
showFrequency_display();
cmdbuf = "";
}
if (cmdbuf.length() >= 3) {
cmdbuf = cmdbuf + "0";
Serial.println(cmdbuf);
cmdbuf_int = cmdbuf.toInt();
Serial.println(cmdbuf_int);
rx.setFrequency(cmdbuf_int);
showFrequency_display();
delay(200);
cmdbuf = "";
}
}
}
}
void print_info() {
Serial.print("\nCurrent Channel: ");
Serial.print(rx.getRealChannel());
delay(500);
Serial.print("\nReal Frequency.: ");
Serial.print(rx.getRealFrequency());
Serial.print("\nRSSI: ");
Serial.print(rx.getRssi());
}
void check_mute() {
if (mute_status == true) {
Serial.println("Mute: ON");
}
else if (mute_status == false) {
Serial.println("Mute: OFF");
}
}
void printVol() {
sprintf(buf,"Current volume: %3d", rx.getVolume());
Serial.println(buf);
}
void showFrequency_display() {
freq_buffer = String(rx.getRealFrequency());
int lastIndex = freq_buffer.length() - 1;
freq_buffer.remove(lastIndex);
Serial.println(freq_buffer);
freq_buffer_int = freq_buffer.toInt();
display.clear();
display.showNumber(freq_buffer_int, false, 4, 0);
delay(100);
}
void showFrequency_counter() {
cmdbuf_int = cmdbuf.toInt();
if (cmdbuf.length() > 4) {
display.showString("Err");
delay(500);
showFrequency_display();
cmdbuf = "";
}
else {
display.clear();
display.showNumber(cmdbuf_int, false, 4, 0);
delay(100);
}
}
// Shared functions for buttons and IR Remote
void global_VolUp() {
if (rx.getVolume()<15) {
rx.setVolume(rx.getVolume() + 1);
display.clear();
display.showNumberDec(rx.getVolume());
delay(200);
showFrequency_display();
}
else {
rx.setVolume(15);
Serial.println("Volume already at maximum! (15)");
display.clear();
display.showNumberDec(rx.getVolume());
delay(200);
showFrequency_display();
}
if (mute_status == true) {
rx.setMute(0);
mute_status=0;
check_mute();
}
printVol();
}
void global_VolDown() {
if (rx.getVolume() != 0) {
rx.setVolume(rx.getVolume() - 1);
display.clear();
display.showNumberDec(rx.getVolume());
delay(200);
showFrequency_display();
}
else {
rx.setVolume(0);
Serial.println("Volume already at minimum! (0)");
display.clear();
display.showNumberDec(rx.getVolume());
delay(200);
showFrequency_display();
}
if (mute_status == true) {
rx.setMute(0);
mute_status=0;
check_mute();
}
printVol();
}
void global_MuteRadio() {
if (mute_status == false) {
rx.setMute(1);
mute_status=1;
display.clear();
display.showString("MUTED");
}
else if (mute_status == true) {
rx.setMute(0);
mute_status=0;
display.clear();
display.showString("UNMUTED");
}
Serial.println("Current volume:");
Serial.println(rx.getVolume());
check_mute();
delay(300);
showFrequency_display();
}
void global_CHUp() {
Serial.print("\nSeeking next station...");
rx.seek(0, 1, NULL);
delay(300);
print_info();
showFrequency_display();
}
void global_CHDown() {
Serial.print("\nSeeking previous station...");
rx.seek(0, 0, NULL);
delay(300);
print_info();
showFrequency_display();
}
void CHUPBUTTON(byte btnStatus) {
switch (btnStatus) {
case onRelease:
Serial.print("\nChannel up Button pressed!");
global_CHUp();
delay(100);
break;
case onLongPress:
if (rx.getRealFrequency() != 10800) {
Serial.print("\n\nAdvancing 10 Hz...\n");
rx.setFrequency(rx.getRealFrequency() + 10);
print_info();
showFrequency_display();
}
else {
rx.setFrequency(8700);
print_info();
showFrequency_display();
}
break;
}
}
void CHDOWNBUTTON(byte btnStatus) {
switch (btnStatus) {
case onRelease:
Serial.print("\nChannel down Button pressed!");
global_CHDown();
delay(100);
break;
case onLongPress:
if (rx.getRealFrequency() != 8700) {
Serial.print("\n\nReceding 10 Hz...\n");
rx.setFrequency(rx.getRealFrequency() - 10);
print_info();
showFrequency_display();
}
else {
rx.setFrequency(10800);
print_info();
showFrequency_display();
}
break;
}
}
void VOLUPBUTTON(byte btnStatus) {
switch (btnStatus) {
case onRelease:
Serial.print("\nVolume up Button pressed!");
global_VolUp();
delay(100);
break;
}
}
void VOLDOWNBUTTON(byte btnStatus) {
switch (btnStatus) {
case onRelease:
Serial.print("\nVolume down Button pressed!");
global_VolDown();
delay(100);
break;
case onLongPress:
Serial.print("\nMute Button pressed!");
global_MuteRadio();
delay(200);
}
}
// Main loop. Checks for IR input and polls the buttons.
void loop() {
if (IrReceiver.decode()) { // have we received an IR signal?
translateIR();
delay(50);
IrReceiver.resume(); // Enable receiving of the next value
}
buttonchup.poll();
buttonchdown.poll();
buttonvolup.poll();
buttonvoldown.poll();
} // loop
// End.