RDA5807M FM RDS radio program using 4DSystems 32028 Oled touch screen display. Search and direct frequency selection, RDS text, 10 preset memories
Dependencies: 4Dsystems_uOLED-32028-P1T RDA5807M FreescaleIAP mbed-src
main.cpp
- Committer:
- star297
- Date:
- 2015-04-12
- Revision:
- 3:99f4c06f86a0
- Parent:
- 1:365082270f1a
- Child:
- 4:5368e5b2f312
File content as of revision 3:99f4c06f86a0:
#include "mbed.h" #include "RDA5807M.h" #include "OLED32028P1T.h" RDA5807M radio(p9, p10); // sda - scl OLED32028P1T oled(p28, p27, p26); // Oled Display tx, rx, rs DigitalOut led(LED1); Timer t1; int f,v,n,i,l,r,g,b,Fl,IRQ,xbuf,ybuf,pr,prgroup; int signal,lastsignal,volume,lastvol,lastmute,lastmono,lastbass,lastrds,laststereo,rdstextgood; float lastfreq,pointer,lastpointer,Freq; float PresetFreq[10]; char k; char tFQ[20],freq[8],vol[4],sig[4]; char station[10],lastStationName[10]; char RDStxt[70],RDStxt1[70],RDStxt2[70],text1[50],lasttext1[50],text2[50],lasttext2[50]; char PresetName[10][12] = {"Empty A ", "Empty A ", "Empty A ", "Empty A ", "Empty A ", "Empty B ", "Empty B ", "Empty B ", "Empty B ", "Empty B "}; void radiomain(),initdisplay(),displayrefresh(),getrds(); void getkey(),flushbuffer(),Volume(),setfreq(),Power(),Preset(),Drawpresets(); int main() { oled.init(); oled.clear(); oled.setTextBackgroundType (TEXT_OPAQUE); oled.setFontSize(FONT5X7);oled.locate(2,20); oled.setFontColor(oled.toRGB(255,255,255)); oled.enableTouch();oled.resetTouchArea(); radio.Reset(); // reset and power up radio chip while(1){ initdisplay(); radiomain(); } } void radiomain() { while(IRQ==0){ getkey(); //oled.setFontSize(FONT5X7);oled.locate(20,0); //oled.printf("X %03d Y %03d ",xbuf,ybuf); if(radio.power){ if(f){setfreq();f=0;} radio.processData(); getrds(); displayrefresh(); wait_ms(10); led=!led; } } } void displayrefresh() { if (strcmp(lastStationName, radio.StationName) != 0){ oled.drawTextGraphic(26,16,(FONT12X16),radio.StationName, 1, 1, oled.toRGB(0,200,200)); strcpy(lastStationName,radio.StationName); } if(lastfreq != radio.freq/1000){lastfreq = radio.freq/1000; sprintf(tFQ, "%3.2f ",radio.freq/1000); oled.drawTextGraphic(176,16,(FONT12X16),tFQ, 1, 1, oled.toRGB(0,200,200)); pointer = (lastfreq-87) *12; oled.setPenSize(0); oled.drawRectangle(lastpointer+29,66,10,8,oled.toRGB(0,0,0)); oled.drawRectangle(lastpointer+33,56,2,18,oled.toRGB(0,0,0)); oled.drawRectangle(pointer+29,66,10,8,oled.toRGB(255,255,255)); oled.drawRectangle(pointer+33,56,2,18,oled.toRGB(255,0,0)); lastpointer = pointer; rdstextgood=0;lastrds=!lastrds; oled.setPenSize(1); } if(laststereo != radio.stereo){ if(radio.stereo){oled.drawTextGraphic(32,79,(FONT5X7),"Stereo", 1, 1, oled.toRGB(0,255,0));} else{oled.drawTextGraphic(32,79,(FONT5X7)," Mono ", 1, 1, oled.toRGB(255,0,0));} laststereo = radio.stereo; } if(lastsignal != radio.signal){ signal=radio.signal; if(radio.mute){signal=0;} sprintf(sig, "%d ",signal); oled.drawTextGraphic(124,79,(FONT5X7),sig, 1, 1, oled.toRGB(255,255,255)); if(signal<20){r=255;g=0;b=0;} if(signal>19 && signal<24){r=255;g=255;b=0;} if(signal>23){r=0;g=255;b=0;} oled.setPenSize(0); oled.drawRectangle(140,79,(lastsignal*2.4),6,oled.toRGB(0,0,0)); oled.drawRectangle(140,79,(signal*2.4),6,oled.toRGB(r,g,b)); oled.setPenSize(1); lastsignal=radio.signal; } if(lastvol != radio.volume){ volume=radio.volume; sprintf(vol," %d ", volume); oled.setPenSize(0);oled.setTextBackgroundType (TEXT_TRANSPARENT); oled.drawRectangle(84,186,58,46,oled.toRGB(100,100,100)); oled.drawTextGraphic(90,216,(FONT8X12),"Volume", 1, 1, oled.toRGB(255,255,255)); if(radio.volume<10){oled.drawTextGraphic(94,194,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));} else{oled.drawTextGraphic(88,194,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));} lastvol = volume;oled.setTextBackgroundType (TEXT_OPAQUE); } if(lastmute != radio.mute){ oled.setTextBackgroundType (TEXT_TRANSPARENT); if(radio.mute){oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Mute ");} else{oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Mute ");} oled.setTextBackgroundType (TEXT_OPAQUE); lastmute=radio.mute; } if(lastmono != radio.mono){ oled.setTextBackgroundType (TEXT_TRANSPARENT); if(radio.mono){oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,0,0)), 1, 1, " Mono ");} else{oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "Stereo");} oled.setTextBackgroundType (TEXT_OPAQUE); lastmono=radio.mono; } if(lastbass != radio.bass){ oled.setTextBackgroundType (TEXT_TRANSPARENT); if(radio.bass){oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Bass ");} else{oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Bass ");} oled.setTextBackgroundType (TEXT_OPAQUE); lastbass=radio.bass; } if(lastrds != radio.rds){ oled.setTextBackgroundType (TEXT_TRANSPARENT); oled.setPenSize(0);oled.drawRectangle(30,96,280,18,oled.toRGB(0,0,0));oled.setPenSize(1); if(radio.rds){ if(rdstextgood){oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "RDS RT");} else{oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " RDS ");} } else{oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " RDS ");rdstextgood=0;} oled.setTextBackgroundType (TEXT_OPAQUE); lastrds=radio.rds; } if (strcmp(text1, lasttext1) != 0){ oled.setPenSize(0);oled.drawRectangle(30,96,280,8,oled.toRGB(0,0,0));oled.setPenSize(1); oled.drawTextGraphic(30,96,(FONT5X7),text1, 1, 1, oled.toRGB(255,255,255)); memset(lasttext1, '\0', sizeof(lasttext1));strcpy(lasttext1, text1);} if (strcmp(text2, lasttext2) != 0){ oled.setPenSize(0);oled.drawRectangle(30,106,280,8,oled.toRGB(0,0,0));oled.setPenSize(1); oled.drawTextGraphic(30,106,(FONT5X7),text2, 1, 1, oled.toRGB(255,255,255)); memset(lasttext2, '\0', sizeof(lasttext2));strcpy(lasttext2, text2);} } void getrds() { if(strlen(radio.RDSText)==64 && signal>23){ //oled.drawTextGraphic(10,200,(FONT5X7),radio._RDSText, 1, 1, oled.toRGB(255,255,255)); memset(text1, '\0', sizeof(text1)); memset(text2, '\0', sizeof(text2)); if(!rdstextgood){rdstextgood=1;lastrds=!lastrds;} // remove non-printable ASCCi error characters n=0; for(i=0;i<(64);i++){ if(radio.RDSText[i] > 31){ RDStxt[n] = radio.RDSText[i]; n++; } } // slit into 2 lines of text seperated by 'space' strcpy(RDStxt1,RDStxt); for ( i = 0; i < (n); i++ ){ if (i>30 && (RDStxt1[i] == ' ') ){ RDStxt1 [strlen(RDStxt1) - (n-i)] = '\0'; l=strlen(RDStxt1); i=n; } } strcpy (RDStxt2, RDStxt + l); while(RDStxt2[0]==' '){ strcpy (RDStxt2, (RDStxt2 + 1)); } if(strlen(RDStxt1)<40){strcpy (text1, RDStxt1);} if(strlen(RDStxt2)<40){strcpy (text2, RDStxt2);} memset(radio.RDSText, '\0', sizeof(radio.RDSText)); } } void initdisplay() { oled.setPenSize(1); oled.drawTextGraphic(50,6,(FONT5X7),"Station Frequency", 1, 1, oled.toRGB(0,200,200)); oled.drawTextGraphic(260,16,(FONT12X16),"MHz", 1, 1, oled.toRGB(0,200,200)); oled.drawTextGraphic(42,35,(FONT5X7),"88 90 92 94 96 98 100 102 104 106 108", 1, 1, oled.toRGB(255,255,0)); oled.drawTextGraphic(78,79,(FONT5X7),"Signal:", 1, 1, oled.toRGB(255,255,255)); oled.drawRectangle(26,55,268,20,oled.toRGB(255,255,255)); oled.drawRectangle(26,75,268,14,oled.toRGB(255,255,255)); for (i = 46; i < (300); i=i+24 ) {oled.drawLine(i,55,i,45,oled.toRGB(255,255,255)); oled.drawLine(i-12,55,i-12,50,oled.toRGB(255,255,255)); } oled.drawTextGraphic(90,220,(FONT8X12),"Volume", 1, 1, oled.toRGB(255,255,255)); oled.setTextBackgroundType (TEXT_TRANSPARENT); oled.drawTextButton(1, 10, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " 0 "); oled.drawTextButton(1, 56, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "."); oled.drawTextButton(1, 10, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "1"); oled.drawTextButton(1, 33, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "2"); oled.drawTextButton(1, 56, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "3"); oled.drawTextButton(1, 10, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "4"); oled.drawTextButton(1, 33, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "5"); oled.drawTextButton(1, 56, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "6"); oled.drawTextButton(1, 10, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "7"); oled.drawTextButton(1, 33, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "8"); oled.drawTextButton(1, 56, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "9"); oled.drawTextButton(1, 82, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " Up "); oled.drawTextButton(1, 82, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " Down "); oled.drawTextButton(1, 146, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "+ "); oled.drawTextButton(1, 146, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "- "); oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " RDS "); oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "Stereo"); oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Bass "); oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Mute "); oled.drawTextButton(1, 260, 218,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "Power "); oled.drawTextButton(1, 190, 134,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets A"); oled.drawTextButton(1, 190, 154,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[0]); oled.drawTextButton(1, 190, 171,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[1]); oled.drawTextButton(1, 190, 188,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[2]); oled.drawTextButton(1, 190, 205,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[3]); oled.drawTextButton(1, 190, 222,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[4]); oled.setTextBackgroundType (TEXT_OPAQUE); } void getkey() { flushbuffer(); oled.getTouch(&xbuf,&ybuf); if(radio.power){ if((xbuf>16 && xbuf<34) && (ybuf>180 && ybuf<202)) {k='1';f=1;} if((xbuf>38 && xbuf<54) && (ybuf>180 && ybuf<202)) {k='2';f=1;} if((xbuf>58 && xbuf<78) && (ybuf>180 && ybuf<202)) {k='3';f=1;} if((xbuf>16 && xbuf<34) && (ybuf>158 && ybuf<178)) {k='4';f=1;} if((xbuf>38 && xbuf<54) && (ybuf>158 && ybuf<178)) {k='5';f=1;} if((xbuf>58 && xbuf<78) && (ybuf>158 && ybuf<178)){k='6';f=1;} if((xbuf>16 && xbuf<34) && (ybuf>135 && ybuf<154)) {k='7';f=1;} if((xbuf>38 && xbuf<54) && (ybuf>135 && ybuf<154)){k='8';f=1;} if((xbuf>58 && xbuf<78) && (ybuf>135 && ybuf<154)){k='9';f=1;} if((xbuf>16 && xbuf<56) && (ybuf>205 && ybuf<225)) {k='0';f=1;} if((xbuf>60 && xbuf<78) && (ybuf>205 && ybuf<225)) {k='.';f=1;} if((xbuf>248 && xbuf<298) && (ybuf>194 && ybuf<210)){radio.Mute();} if((xbuf>248 && xbuf<298) && (ybuf>176 && ybuf<192)){radio.BassBoost();} if((xbuf>248 && xbuf<298) && (ybuf>158 && ybuf<174)){radio.Mono();} if((xbuf>248 && xbuf<298) && (ybuf>138 && ybuf<156)){radio.RDS();} if((xbuf>140 && xbuf<160) && (ybuf>180 && ybuf<202)){v=1;Volume();} if((xbuf>140 && xbuf<160) && (ybuf>204 && ybuf<226)){v=0;Volume();} if((xbuf>86 && xbuf<160) && (ybuf>135 && ybuf<155)){radio.SeekUp();} if((xbuf>86 && xbuf<160) && (ybuf>157 && ybuf<179)){radio.SeekDown();} if((xbuf>182 && xbuf<244) && (ybuf>137 && ybuf<150)){ oled.setTextBackgroundType (TEXT_TRANSPARENT); if(prgroup==0){prgroup=5;oled.drawTextButton(1, 190, 134,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets A");} else{prgroup=0;oled.drawTextButton(1, 190, 134,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets B");} oled.setTextBackgroundType (TEXT_OPAQUE); Drawpresets(); } if((xbuf>182 && xbuf<244) && (ybuf>154 && ybuf<166)){pr=0;Preset();} if((xbuf>182 && xbuf<244) && (ybuf>170 && ybuf<180)){pr=1;Preset();} if((xbuf>182 && xbuf<244) && (ybuf>184 && ybuf<196)){pr=2;Preset();} if((xbuf>182 && xbuf<244) && (ybuf>198 && ybuf<210)){pr=3;Preset();} if((xbuf>182 && xbuf<244) && (ybuf>212 && ybuf<225)){pr=4;Preset();} } if((xbuf>248 && xbuf<298) && (ybuf>212 && ybuf<228)){Power();} } void Preset() { if(PresetFreq[pr+prgroup]!=0 && PresetFreq[pr+prgroup]==radio.freq/1000){ strcpy(PresetName[pr+prgroup],"Empty "); if(prgroup){strcat(PresetName[pr+prgroup],"B ");} else{strcat(PresetName[pr+prgroup],"A ");} PresetFreq[pr+prgroup]=0; Drawpresets(); return; } if(PresetFreq[pr+prgroup]==0){ if(radio.signal>25){ strcpy(PresetName[pr+prgroup],radio.StationName);} else{ sprintf(tFQ, "%3.2f MHz",radio.freq/1000); strcpy(PresetName[pr+prgroup],tFQ); } while(strlen(PresetName[pr+prgroup])<10){ strcat(PresetName[pr+prgroup]," "); } PresetFreq[pr+prgroup]=radio.freq/1000; Drawpresets(); return; } radio.Frequency(PresetFreq[pr+prgroup]); } void Drawpresets() { oled.setTextBackgroundType (TEXT_TRANSPARENT); if(PresetFreq[0+prgroup]!=0){oled.drawTextButton(1, 190, 154,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(0,255,255)), 1, 1, PresetName[0+prgroup]);} else{oled.drawTextButton(1, 190, 154,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[0+prgroup]);} if(PresetFreq[1+prgroup]!=0){oled.drawTextButton(1, 190, 171,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(0,255,255)), 1, 1, PresetName[1+prgroup]);} else{oled.drawTextButton(1, 190, 171,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[1+prgroup]);} if(PresetFreq[2+prgroup]!=0){oled.drawTextButton(1, 190, 188,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(0,255,255)), 1, 1, PresetName[2+prgroup]);} else{oled.drawTextButton(1, 190, 188,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[2+prgroup]);} if(PresetFreq[3+prgroup]!=0){oled.drawTextButton(1, 190, 205,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(0,255,255)), 1, 1, PresetName[3+prgroup]);} else{oled.drawTextButton(1, 190, 205,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[3+prgroup]);} if(PresetFreq[4+prgroup]!=0){oled.drawTextButton(1, 190, 222,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(0,255,255)), 1, 1, PresetName[4+prgroup]);} else{oled.drawTextButton(1, 190, 222,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[4+prgroup]);} oled.setTextBackgroundType (TEXT_OPAQUE); } void setfreq() { oled.setPenSize(0);oled.drawRectangle(176,16,80,14,oled.toRGB(0,0,0));oled.setPenSize(1); Fl = strlen(freq); if(Fl< 6){freq[Fl] = k;} if (freq[0]!='1' && freq[0]!='8' && freq[0]!='9'){ freq[0]='\0';} oled.drawTextGraphic(176,16,(FONT12X16),freq, 1, 1, oled.toRGB(0,200,200)); if (freq[0]=='8' || freq[0]=='9'){Fl=5;} else{Fl=6;} if(strlen(freq)==Fl){ Freq = strtof (freq, NULL); radio.Frequency(Freq); memset(freq, '\0', sizeof(freq)); } } void Volume() { if(v){volume++;} if(!v){volume--;} if(volume > 15){volume = 15;} if(volume < 0){volume = 0;} radio.Volume(volume); } void Power() { if(radio.power){ radio.RDS(); radio.processData(); displayrefresh(); rdstextgood=0; radio.PowerOff(); radio.processData(); displayrefresh(); led=0; } else{ radio.PowerOn(); radio.RDS(); radio.Frequency(lastfreq); radio.processData(); } } void flushbuffer() { while (xbuf!=0 && ybuf!=0){oled.getTouch(&xbuf,&ybuf);} }