/
SerialRadio.ino
352 lines (283 loc) · 9.54 KB
/
SerialRadio.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
///
/// \file SerialRadio.ino
/// \brief Radio implementation using the Serial communication.
///
/// \author Matthias Hertel, http://www.mathertel.de
/// \copyright Copyright (c) by Matthias Hertel.\n
/// This work is licensed under a BSD 3-Clause license.\n
/// See http://www.mathertel.de/License.aspx
///
/// \details
/// This is a Arduino sketch radio implementation that can be controlled using commands on the Serial input.
/// It can be used with various chips after adjusting the radio object definition.\n
/// Open the Serial console with 115200 baud to see current radio information and change various settings.
///
/// Wiring
/// ------
/// The necessary wiring of the various chips are described in the Testxxx example sketches.
/// No additional components are required because all is done through the serial interface.
///
/// More documentation is available at http://www.mathertel.de/Arduino
/// Source Code is available on https://github.com/mathertel/Radio
///
/// History:
/// --------
/// * 05.08.2014 created.
/// * 04.10.2014 working.
/// * 15.01.2023 ESP32, cleanup compiler warnings.
#include <Arduino.h>
#include <Wire.h>
#include <radio.h>
// all possible radio chips included.
#include <RDA5807M.h>
#include <RDA5807FP.h>
#include <SI4703.h>
#include <SI4705.h>
#include <SI47xx.h>
#include <TEA5767.h>
#include <RDSParser.h>
// Define some stations available at your locations here:
// 89.30 MHz as 8930
RADIO_FREQ preset[] = {
8930, // Sender:< hr3 >
9060, // Sender:< hr1 >
9310, //
9340, // Sender:<BAYERN 3>
9440, // Sender:< hr1 >
9530, // Sender:< YOU FM >
9670, // Sender:< hr2 >
9870, // Sender:< Dlf >
10020, // Sender:< planet >
10140, // Sender:<RADIOBOB>
10160, // Sender:< hr4 >
10300 // Sender:<ANTENNE >
};
uint16_t presetIndex = 0; ///< Start at Station with index = 1
// ===== SI4703 specific pin wiring =====
#define ENABLE_SI4703
#ifdef ENABLE_SI4703
#if defined(ARDUINO_ARCH_AVR)
#define RESET_PIN 2
#define MODE_PIN A4 // same as SDA
#elif defined(ESP8266)
#define RESET_PIN D5
#define MODE_PIN D2 // same as SDA
#elif defined(ESP32)
#define RESET_PIN 4
#define MODE_PIN 21 // same as SDA
#endif
#endif
// Standard I2C/Wire pins for Arduino UNO: = SDA:A4, SCL:A5
// Standard I2C/Wire pins for ESP8266: SDA:D2, SCL:D1
// Standard I2C/Wire pins for ESP32: SDA:21, SCL:22
/// The radio object has to be defined by using the class corresponding to the used chip.
/// by uncommenting the right radio object definition.
// RADIO radio; ///< Create an instance of a non functional radio.
// RDA5807FP radio; ///< Create an instance of a RDA5807FP chip radio
// RDA5807M radio; ///< Create an instance of a RDA5807M chip radio
SI4703 radio; ///< Create an instance of a SI4703 chip radio.
// SI4705 radio; ///< Create an instance of a SI4705 chip radio.
// SI47xx radio; ///< Create an instance of a SI4720,21,30 (and maybe more) chip radio.
// TEA5767 radio; ///< Create an instance of a TEA5767 chip radio.
/// get a RDS parser
RDSParser rds;
/// State of Keyboard input for this radio implementation.
enum RADIO_STATE {
STATE_PARSECOMMAND, ///< waiting for a new command character.
STATE_PARSEINT, ///< waiting for digits for the parameter.
STATE_EXEC ///< executing the command.
};
RADIO_STATE kbState; ///< The state of parsing input characters.
char kbCommand;
int16_t kbValue;
bool lowLevelDebug = false;
/// Update the Frequency on the LCD display.
void DisplayFrequency() {
char s[12];
radio.formatFrequency(s, sizeof(s));
Serial.print("FREQ:");
Serial.println(s);
} // DisplayFrequency()
/// Update the ServiceName text on the LCD display.
void DisplayServiceName(const char *name) {
Serial.print("RDS:");
Serial.println(name);
} // DisplayServiceName()
// - - - - - - - - - - - - - - - - - - - - - - - - - -
void RDS_process(uint16_t block1, uint16_t block2, uint16_t block3, uint16_t block4) {
rds.processData(block1, block2, block3, block4);
}
/// Execute a command identified by a character and an optional number.
/// See the "?" command for available commands.
/// \param cmd The command character.
/// \param value An optional parameter for the command.
void runSerialCommand(char cmd, int16_t value) {
if (cmd == '?') {
Serial.println();
Serial.println("? Help");
Serial.println("+ increase volume");
Serial.println("- decrease volume");
Serial.println("> next preset");
Serial.println("< previous preset");
Serial.println(". scan up : scan up to next sender");
Serial.println(", scan down ; scan down to next sender");
Serial.println("fnnnnn: direct frequency input");
Serial.println("i station status");
Serial.println("s mono/stereo mode");
Serial.println("b bass boost");
Serial.println("u mute/unmute");
}
// ----- control the volume and audio output -----
else if (cmd == '+') {
// increase volume
int v = radio.getVolume();
radio.setVolume(++v);
} else if (cmd == '-') {
// decrease volume
int v = radio.getVolume();
if (v > 0)
radio.setVolume(--v);
}
else if (cmd == 'u') {
// toggle mute mode
radio.setMute(!radio.getMute());
}
// toggle stereo mode
else if (cmd == 's') {
radio.setMono(!radio.getMono());
}
// toggle bass boost
else if (cmd == 'b') {
radio.setBassBoost(!radio.getBassBoost());
}
// ----- control the frequency -----
else if (cmd == '>') {
// next preset
if (presetIndex < (sizeof(preset) / sizeof(RADIO_FREQ)) - 1) {
presetIndex++;
radio.setFrequency(preset[presetIndex]);
} // if
} else if (cmd == '<') {
// previous preset
if (presetIndex > 0) {
presetIndex--;
radio.setFrequency(preset[presetIndex]);
} // if
} else if (cmd == 'f') {
radio.setFrequency(value);
}
else if (cmd == '.') {
radio.seekUp(false);
} else if (cmd == ':') {
radio.seekUp(true);
} else if (cmd == ',') {
radio.seekDown(false);
} else if (cmd == ';') {
radio.seekDown(true);
}
else if (cmd == '!') {
// not in help
RADIO_FREQ f = radio.getFrequency();
if (value == 0) {
radio.term();
} else if (value == 1) {
radio.initWire(Wire);
radio.setBandFrequency(RADIO_BAND_FM, f);
radio.setVolume(10);
}
} else if (cmd == 'i') {
// info
char s[12];
radio.formatFrequency(s, sizeof(s));
Serial.print("Station:");
Serial.println(s);
Serial.print("Radio:");
radio.debugRadioInfo();
Serial.print("Audio:");
radio.debugAudioInfo();
} else if (cmd == 'x') {
radio.debugStatus(); // print chip specific data.
} else if (cmd == '*') {
lowLevelDebug = !lowLevelDebug;
radio._wireDebug(lowLevelDebug);
}
} // runSerialCommand()
/// Setup a FM only radio configuration with I/O for commands and debugging on the Serial port.
void setup() {
delay(3000);
// open the Serial port
Serial.begin(115200);
Serial.println("SerialRadio...");
delay(200);
#if defined(RESET_PIN)
// This is required for SI4703 chips:
radio.setup(RADIO_RESETPIN, RESET_PIN);
radio.setup(RADIO_MODEPIN, MODE_PIN);
#endif
// Enable information to the Serial port
radio.debugEnable(true);
radio._wireDebug(lowLevelDebug);
// Set FM Options for Europe
radio.setup(RADIO_FMSPACING, RADIO_FMSPACING_100); // for EUROPE
radio.setup(RADIO_DEEMPHASIS, RADIO_DEEMPHASIS_50); // for EUROPE
// Initialize the Radio
if (!radio.initWire(Wire)) {
Serial.println("no radio chip found.");
delay(4000);
while (1) {};
};
radio.setBandFrequency(RADIO_BAND_FM, preset[presetIndex]);
radio.setMono(false);
radio.setMute(false);
radio.setVolume(radio.getMaxVolume() / 2);
// setup the information chain for RDS data.
radio.attachReceiveRDS(RDS_process);
rds.attachServiceNameCallback(DisplayServiceName);
runSerialCommand('?', 0);
kbState = STATE_PARSECOMMAND;
} // Setup
/// Constantly check for serial input commands and trigger command execution.
void loop() {
unsigned long now = millis();
static unsigned long nextFreqTime = 0;
// some internal static values for parsing the input
static RADIO_FREQ lastFrequency = 0;
RADIO_FREQ f = 0;
if (Serial.available() > 0) {
// read the next char from input.
char c = Serial.peek();
if ((kbState == STATE_PARSECOMMAND) && (c < 0x20)) {
// ignore unprintable chars
Serial.read();
} else if (kbState == STATE_PARSECOMMAND) {
// read a command.
kbCommand = Serial.read();
kbState = STATE_PARSEINT;
} else if (kbState == STATE_PARSEINT) {
if ((c >= '0') && (c <= '9')) {
// build up the value.
c = Serial.read();
kbValue = (kbValue * 10) + (c - '0');
} else {
// not a value -> execute
runSerialCommand(kbCommand, kbValue);
kbCommand = ' ';
kbState = STATE_PARSECOMMAND;
kbValue = 0;
} // if
} // if
} // if
// check for RDS data
radio.checkRDS();
// update the display from time to time
if (now > nextFreqTime) {
f = radio.getFrequency();
if (f != lastFrequency) {
// print current tuned frequency
DisplayFrequency();
lastFrequency = f;
} // if
nextFreqTime = now + 400;
} // if
} // loop
// End.