-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESPboy_FMradio.ino
232 lines (193 loc) · 5.96 KB
/
ESPboy_FMradio.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
/*
ESPboy FMradio module firmware. Using SI4703 chipset
www.espboy.com project
by RomanS 12.06.2021
Install to your Arduino IDE library https://github.com/pu2clr/SI470X
*/
#include "lib/ESPboyInit.h"
#include "lib/ESPboyInit.cpp"
//#include "lib/ESPboyTerminalGUI.h"
//#include "lib/ESPboyTerminalGUI.cpp"
//#include "lib/ESPboyOTA2.h"
//#include "lib/ESPboyOTA2.cpp"
#include <SI470X.h>
#include <ESP_EEPROM.h>
#define radioSDApin SDA
#define radioRESETpin D8
#define I2C_ADDR 0x10
#define MAX_DELAY_RDS 20
#define MAX_DELAY_SAVE 2000
struct ESP_EEPROM{
uint16_t freq;
uint8_t vol;
uint8_t stereo;
};
String RDStext, RDStime, RDSprevText, RDSprevTime;
uint16_t prevRSSI;
ESP_EEPROM esp_eeprom;
static uint8_t esp_eeprom_needsaving = 0;
ESPboyInit myESPboy;
//ESPboyTerminalGUI *terminalGUIobj = NULL;
//ESPboyOTA2 *OTA2obj = NULL;
SI470X radio;
void displayRDStext(char *nme){
RDSprevText = RDStext;
myESPboy.tft.fillRect(0, 58, 128, 16, TFT_BLACK);
myESPboy.tft.setTextSize(2);
myESPboy.tft.setTextColor(TFT_RED);
nme[10]=0;
String nmestr = (String)nme;
nmestr.trim();
myESPboy.tft.setCursor((128-strlen(nme)*12)/2, 58);
myESPboy.tft.print(nmestr.c_str());
}
void displayRDStime(char *tme) {
RDSprevTime = RDStime;
myESPboy.tft.fillRect(0, 14, 128, 10, TFT_BLACK),
myESPboy.tft.setTextSize(1);
myESPboy.tft.setTextColor(TFT_YELLOW);
myESPboy.tft.drawString(tme, 50, 14);
}
void redrawtft(){
//clear TFT
myESPboy.tft.fillScreen(TFT_BLACK);
//draw ESPboy radio
myESPboy.tft.setTextSize(1);
myESPboy.tft.setTextColor(TFT_MAGENTA);
myESPboy.tft.setCursor(10, 0);
myESPboy.tft.print(F("ESPboy FM radio v2"));
//draw freq
myESPboy.tft.setTextSize(3);
myESPboy.tft.setTextColor(TFT_GREEN);
uint16_t freq = radio.getFrequency();
uint16_t fstfreq = freq/100;
uint16_t scdfreq = freq - fstfreq*100;
String toPrint = (String)(fstfreq);
toPrint += ".";
toPrint += (String)(scdfreq/10);
myESPboy.tft.drawString(toPrint, 18,30);
//draw Volume
myESPboy.tft.setTextSize(1);
myESPboy.tft.setTextColor(TFT_YELLOW);
myESPboy.tft.setCursor(0, 80);
myESPboy.tft.print(F("Vol "));
for (int i=0; i<radio.getVolume(); i++) myESPboy.tft.print("|");
//draw MONO/STEREO
myESPboy.tft.setCursor(0, 90);
if (radio.isStereo()) myESPboy.tft.print (F("Stereo"));
else myESPboy.tft.print (F("Mono"));
//draw RSSI
myESPboy.tft.setCursor(0, 100);
myESPboy.tft.print ("RSSI "); myESPboy.tft.print (radio.getRssi());
//draw RDS
displayRDStext((char*)RDStext.c_str());
displayRDStime((char*)RDStime.c_str());
}
void runButtonsCommand(uint8_t bt){
esp_eeprom_needsaving++;
myESPboy.playTone(600, 20);
myESPboy.myLED.setRGB(0,0,10);
if (bt&PAD_RIGHT) {
myESPboy.tft.setTextSize(3);
myESPboy.tft.setTextColor(TFT_GREEN,TFT_BLACK);
myESPboy.tft.drawString(">>>>>", 18,30);
myESPboy.myLED.setRGB(10,0,0);
radio.seek(SI470X_SEEK_WRAP, SI470X_SEEK_UP);
myESPboy.myLED.setRGB(0,10,0);
myESPboy.playTone(1000, 20);
radio.setRds(true);
radio.setRdsMode(0);
RDStext="";
RDStime="";
delay(200);
}
if (bt&PAD_LEFT) {
myESPboy.tft.setTextSize(3);
myESPboy.tft.setTextColor(TFT_GREEN,TFT_BLACK);
myESPboy.tft.drawString("<<<<<", 18,30);
myESPboy.myLED.setRGB(10,0,0);
radio.seek(SI470X_SEEK_WRAP, SI470X_SEEK_DOWN);
myESPboy.myLED.setRGB(0,10,0);
myESPboy.playTone(1000, 20);
radio.setRds(true);
radio.setRdsMode(0);
RDStext="";
RDStime="";
delay(200);}
if (bt&PAD_UP) radio.setVolumeUp();
if (bt&PAD_DOWN) radio.setVolumeDown();
if (bt&PAD_ACT) {if(radio.isStereo()) radio.setMono(true); else radio.setMono(false);}
delay(200);
redrawtft();
myESPboy.myLED.setRGB(0,0,0);
}
boolean esp_eeprom_save(){
esp_eeprom.freq = radio.getFrequency();
esp_eeprom.vol = radio.getVolume();
esp_eeprom.stereo = radio.isStereo();
EEPROM.put (0, esp_eeprom);
return (EEPROM.commit());
}
void esp_eeprom_load(){
EEPROM.get(0, esp_eeprom);
radio.setFrequency(esp_eeprom.freq);
if (esp_eeprom.stereo) radio.setMono(false); else radio.setMono(true);
radio.setVolume(esp_eeprom.vol);
}
void setup() {
//Init ESPboy
myESPboy.begin("FM radio");
Wire.begin();
//Check OTA2
// if (myESPboy.getKeys()&PAD_ACT || myESPboy.getKeys()&PAD_ESC) {
// terminalGUIobj = new ESPboyTerminalGUI(&myESPboy.tft, &myESPboy.mcp);
// OTA2obj = new ESPboyOTA2(terminalGUIobj);
// }
//detect FMradio module
pinMode(D8,OUTPUT);
digitalWrite(D8, HIGH);
Wire.beginTransmission(I2C_ADDR);
if (Wire.endTransmission()){
myESPboy.tft.setTextColor(TFT_RED);
myESPboy.tft.drawString("FM-radio module", 0, 2);
myESPboy.tft.drawString("not found!", 0, 12);
while(1)delay(1000);
}
//radio init
radio.setup(radioRESETpin, radioSDApin);
// Enables SDR
radio.setRds(true);
radio.setRdsMode(0);
radio.setSeekThreshold(30); // Sets RSSI Seek Threshold (0 to 127)
//load last radio state from eeprom
EEPROM.begin(sizeof (esp_eeprom));
esp_eeprom_load();
//draw radio interface
redrawtft();
}
void loop() {
static unsigned long cnt_rds, cnt_save;
uint8_t bt = myESPboy.getKeys();
if (bt) runButtonsCommand(bt);
if (millis() - cnt_save > MAX_DELAY_SAVE){
cnt_save = millis();
if (esp_eeprom_needsaving){
esp_eeprom_save();
esp_eeprom_needsaving = 0;
}
}
if (radio.getRdsReady() && (millis() - cnt_rds) > MAX_DELAY_RDS){
cnt_rds = millis();
RDStime = (String)radio.getRdsTime();
RDStext = (String)radio.getRdsText();
if (RDStime != RDSprevTime || RDStext != RDSprevText) redrawtft();
}
if(prevRSSI != radio.getRssi()){
prevRSSI=radio.getRssi();
myESPboy.tft.setTextColor(TFT_YELLOW, TFT_BLACK);
String toPrint = (String)radio.getRssi();
toPrint+=" ";
myESPboy.tft.drawString(toPrint,5*6,100);
}
delay (5);
}