acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Simple_WiFi_Radio.ino (9052B)
1 #include <Arduino.h> 2 #include <Preferences.h> 3 #include <SPI.h> 4 #include <WiFi.h> 5 #include "tft.h" //see my repository at github "https://github.com/schreibfaul1/ESP32-TFT-Library-ILI9486" 6 #include "Audio.h" //see my repository at github "https://github.com/schreibfaul1/ESP32-audioI2S" 7 8 #define TFT_CS 22 9 #define TFT_DC 21 10 #define TP_CS 14 //16 11 #define TP_IRQ 39 12 #define SPI_MOSI 23 13 #define SPI_MISO 19 14 #define SPI_SCK 18 15 #define I2S_DOUT 25 16 #define I2S_BCLK 27 17 #define I2S_LRC 26 18 19 Preferences pref; 20 TFT tft; 21 TP tp(TP_CS, TP_IRQ); 22 Audio audio; 23 24 String ssid = "*****"; 25 String password = "*****"; 26 27 String stations[] ={ 28 "0n-80s.radionetz.de:8000/0n-70s.mp3", 29 "mediaserv30.live-streams.nl:8000/stream", 30 "www.surfmusic.de/m3u/100-5-das-hitradio,4529.m3u", 31 "stream.1a-webradio.de/deutsch/mp3-128/vtuner-1a", 32 "mp3.ffh.de/radioffh/hqlivestream.aac", // 128k aac 33 "www.antenne.de/webradio/antenne.m3u", 34 "listen.rusongs.ru/ru-mp3-128", 35 "edge.audio.3qsdn.com/senderkw-mp3", 36 "https://stream.srg-ssr.ch/rsp/aacp_48.asx", // SWISS POP 37 }; 38 39 //some global variables 40 41 uint8_t max_volume = 21; 42 uint8_t max_stations = 0; //will be set later 43 uint8_t cur_station = 0; //current station(nr), will be set later 44 uint8_t cur_volume = 0; //will be set from stored preferences 45 int8_t cur_btn =-1; //current button (, -1 means idle) 46 47 enum action{VOLUME_UP=0, VOLUME_DOWN=1, STATION_UP=2, STATION_DOWN=3}; 48 enum staus {RELEASED=0, PRESSED=1}; 49 50 struct _btns{ 51 uint16_t x; //PosX 52 uint16_t y; //PosY 53 uint16_t w; //Width 54 uint16_t h; //Hight 55 uint8_t a; //Action 56 uint8_t s; //Status 57 }; 58 typedef _btns btns; 59 60 btns btn[4]; 61 62 //************************************************************************************************** 63 // G U I * 64 //************************************************************************************************** 65 void draw_button(btns b){ 66 uint16_t color=TFT_BLACK; 67 uint8_t r=4, o=r*3; 68 if(b.s==RELEASED) color=TFT_GOLD; 69 if(b.s==PRESSED) color=TFT_DEEPSKYBLUE; 70 tft.drawRoundRect(b.x, b.y, b.w, b.h, r, color); 71 switch(b.a){ 72 case VOLUME_UP: 73 tft.fillTriangle(b.x+b.w/2, b.y+o, b.x+o, b.y+b.h-o, b.x+b.w-o, b.y+b.h-o, color); break; 74 case VOLUME_DOWN: 75 tft.fillTriangle(b.x+o, b.y+o, b.x+b.w/2, b.y+b.h-o, b.x+b.w-o, b.y+o, color); break; 76 case STATION_UP: 77 tft.fillTriangle(b.x+o, b.y+o, b.x+o, b.y+b.h-o, b.x+b.w-o, b.y+b.h/2, color); break; 78 case STATION_DOWN: 79 tft.fillTriangle(b.x+b.w-o, b.y+o, b.x+o, b.y+b.h/2, b.x+b.w-o, b.y+b.h-o, color); break; 80 } 81 } 82 void write_stationNr(uint8_t nr){ 83 tft.fillRect(80, 250, 80, 60, TFT_BLACK); 84 String snr = String(nr); 85 if(snr.length()<2) snr = "0"+snr; 86 tft.setCursor(98, 255); 87 tft.setFont(Times_New_Roman66x53); 88 tft.setTextColor(TFT_YELLOW); 89 tft.print(snr); 90 } 91 void write_volume(uint8_t vol){ 92 tft.fillRect(320, 250, 80, 60, TFT_BLACK); 93 String svol = String(vol); 94 if(svol.length()<2) svol = "0"+svol; 95 tft.setCursor(338, 255); 96 tft.setFont(Times_New_Roman66x53); 97 tft.setTextColor(TFT_YELLOW); 98 tft.print(svol); 99 } 100 void write_stationName(String sName){ 101 tft.fillRect(0, 0, 480, 100, TFT_BLACK); 102 tft.setFont(Times_New_Roman43x35); 103 tft.setTextColor(TFT_CORNSILK); 104 tft.setCursor(20, 20); 105 tft.print(sName); 106 } 107 void write_streamTitle(String sTitle){ 108 tft.fillRect(0, 100, 480, 150, TFT_BLACK); 109 tft.setFont(Times_New_Roman43x35); 110 tft.setTextColor(TFT_LIGHTBLUE); 111 tft.setCursor(20, 100); 112 int l = tft.writeText((const uint8_t*) sTitle.c_str(), 100 + 150); // do not write under y=250 113 if(l < sTitle.length()){ 114 // sTitle has been shortened, is too long for the display 115 } 116 } 117 //************************************************************************************************** 118 // S E T U P * 119 //************************************************************************************************** 120 void setup() { 121 btn[0].x= 20; btn[0].y=250; btn[0].w=60; btn[0].h=60; btn[0].a=STATION_DOWN; btn[0].s=RELEASED; 122 btn[1].x=160; btn[1].y=250; btn[1].w=60; btn[1].h=60; btn[1].a=STATION_UP; btn[1].s=RELEASED; 123 btn[2].x=260; btn[2].y=250; btn[2].w=60; btn[2].h=60; btn[2].a=VOLUME_UP; btn[2].s=RELEASED; 124 btn[3].x=400; btn[3].y=250; btn[3].w=60; btn[3].h=60; btn[3].a=VOLUME_DOWN; btn[3].s=RELEASED; 125 max_stations= sizeof(stations)/sizeof(stations[0]); log_i("max stations %i", max_stations); 126 Serial.begin(115200); 127 SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); 128 pref.begin("WebRadio", false); // instance of preferences for defaults (station, volume ...) 129 if(pref.getShort("volume", 1000) == 1000){ // if that: pref was never been initialized 130 pref.putShort("volume", 10); 131 pref.putShort("station", 0); 132 } 133 else{ // get the stored values 134 cur_station = pref.getShort("station"); 135 cur_volume = pref.getShort("volume"); 136 } 137 138 WiFi.begin(ssid.c_str(), password.c_str()); 139 while (WiFi.status() != WL_CONNECTED){ 140 delay(2000); 141 Serial.print("."); 142 } 143 log_i("Connect to %s", WiFi.SSID().c_str()); 144 tft.begin(TFT_CS, TFT_DC, SPI_MOSI, SPI_MISO, SPI_SCK); 145 tft.setRotation(3); 146 tp.setRotation(3); 147 tft.setFont(Times_New_Roman43x35); 148 tft.fillScreen(TFT_BLACK); 149 audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); 150 audio.setVolume(cur_volume); // 0...21 151 audio.connecttohost(stations[cur_station].c_str()); 152 for(uint8_t i=0; i<(sizeof(btn)/sizeof(*btn)); i++) draw_button(btn[i]); 153 write_volume(cur_volume); 154 write_stationNr(cur_station); 155 } 156 //************************************************************************************************** 157 // L O O P * 158 //************************************************************************************************** 159 void loop() 160 { 161 audio.loop(); 162 tp.loop(); 163 } 164 //************************************************************************************************** 165 // E V E N T S * 166 //************************************************************************************************** 167 void audio_info(const char *info){ 168 Serial.print("audio_info: "); Serial.println(info); 169 } 170 void audio_showstation(const char *info){ 171 write_stationName(String(info)); 172 } 173 void audio_showstreamtitle(const char *info){ 174 String sinfo=String(info); 175 sinfo.replace("|", "\n"); 176 write_streamTitle(sinfo); 177 } 178 179 void tp_pressed(uint16_t x, uint16_t y){ 180 for(uint8_t i=0; i<(sizeof(btn)/sizeof(*btn)); i++){ 181 if(x>btn[i].x && (x<btn[i].x+btn[i].w)){ 182 if(y>btn[i].y && (y<btn[i].y+btn[i].h)){ 183 cur_btn=i; 184 btn[cur_btn].s=PRESSED; 185 draw_button(btn[cur_btn]); 186 } 187 } 188 } 189 } 190 void tp_released(){ 191 if(cur_btn !=-1){ 192 btn[cur_btn].s=RELEASED; 193 draw_button(btn[cur_btn]); 194 switch(btn[cur_btn].a){ 195 case VOLUME_UP: if(cur_volume<max_volume){ 196 cur_volume++; 197 write_volume(cur_volume); 198 audio.setVolume(cur_volume); 199 pref.putShort("volume", cur_volume);} // store the current volume in nvs 200 break; 201 case VOLUME_DOWN: if(cur_volume>0){ 202 cur_volume-- ; 203 write_volume(cur_volume); 204 audio.setVolume(cur_volume); 205 pref.putShort("volume", cur_volume);} // store the current volume in nvs 206 break; 207 case STATION_UP: if(cur_station<max_stations-1){ 208 cur_station++; 209 write_stationNr(cur_station); 210 tft.fillRect(0, 0, 480, 250, TFT_BLACK); 211 audio.connecttohost(stations[cur_station].c_str()); 212 pref.putShort("station", cur_station);} // store the current station in nvs 213 break; 214 case STATION_DOWN:if(cur_station>0){ 215 cur_station--; 216 write_stationNr(cur_station); 217 tft.fillRect(0, 0, 480, 250, TFT_BLACK); 218 audio.connecttohost(stations[cur_station].c_str()); 219 pref.putShort("station", cur_station);} // store the current station in nvs 220 break; 221 } 222 } 223 cur_btn=-1; 224 }