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

/media/uploads/star297/fm_radio.jpg

Committer:
star297
Date:
Thu Jun 11 20:23:57 2015 +0000
Revision:
4:5368e5b2f312
Parent:
3:99f4c06f86a0
Added IAP flash programing for preset stations + general tidy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:879245ce9cdb 1 #include "mbed.h"
star297 0:879245ce9cdb 2 #include "RDA5807M.h"
star297 0:879245ce9cdb 3 #include "OLED32028P1T.h"
star297 4:5368e5b2f312 4
star297 4:5368e5b2f312 5 #if defined(TARGET_Freescale)
star297 4:5368e5b2f312 6 #include "FreescaleIAP.h"
star297 4:5368e5b2f312 7 //RDA5807M radio(PTC9, PTC8); // sda - scl KL25
star297 4:5368e5b2f312 8 RDA5807M radio(PTB4, PTB3); // sda - scl KL05
star297 4:5368e5b2f312 9 //OLED32028P1T oled(PTE0, PTE1, PTD7); // Oled Display tx, rx, rs KL25
star297 4:5368e5b2f312 10 OLED32028P1T oled(PTB2, PTB1, PTA11); // Oled Display tx, rx, rs KL05
star297 4:5368e5b2f312 11 #else
star297 4:5368e5b2f312 12 //RDA5807M radio(p9, p10); // sda - scl LPC1768
star297 4:5368e5b2f312 13 RDA5807M radio(p28, p27); // sda - scl LPC1768
star297 4:5368e5b2f312 14 //OLED32028P1T oled(p28, p27, p29); // Oled Display tx, rx, rs
star297 4:5368e5b2f312 15 OLED32028P1T oled(p13, p14, p26); // Oled Display tx, rx, rs
star297 4:5368e5b2f312 16 //RDA5807M radio(PC4, PC5); // sda - scl EFM32
star297 4:5368e5b2f312 17 //OLED32028P1T oled(PC0, PC1, PC2); // Oled Display tx, rx, rs EFM32
star297 4:5368e5b2f312 18 #endif
star297 0:879245ce9cdb 19
star297 1:365082270f1a 20 Timer t1;
star297 4:5368e5b2f312 21
star297 4:5368e5b2f312 22 int x,f,v,n,i,l,r,g,b,Fl,IRQ,xbuf,ybuf,pr,prgroup;
star297 4:5368e5b2f312 23 int signal,lastsignal,volume,lastvol,lastmute,lastmono,lastbass,lastrds,laststereo,lastsynchro;
star297 4:5368e5b2f312 24 int txtlen1,txtlen2;
star297 0:879245ce9cdb 25
star297 1:365082270f1a 26 float lastfreq,pointer,lastpointer,Freq;
star297 4:5368e5b2f312 27 float PresetFreq[11];
star297 0:879245ce9cdb 28
star297 1:365082270f1a 29 char k;
star297 3:99f4c06f86a0 30 char tFQ[20],freq[8],vol[4],sig[4];
star297 4:5368e5b2f312 31 char lastStationName[10];
star297 4:5368e5b2f312 32 char RDStxt[70],RDStxt1[70],RDStxt2[70],lasttext1[70],lasttext2[70],lastCT[12];
star297 4:5368e5b2f312 33 char PresetName[10][11] = {"Empty A ", "Empty A ", "Empty A ", "Empty A ", "Empty A ", "Empty B ", "Empty B ", "Empty B ", "Empty B ", "Empty B "};
star297 0:879245ce9cdb 34
star297 4:5368e5b2f312 35 void radiomain(),initdisplay(),displayrefresh(),getrds(),savepresets(),loadpresets();
star297 1:365082270f1a 36 void getkey(),flushbuffer(),Volume(),setfreq(),Power(),Preset(),Drawpresets();
star297 1:365082270f1a 37
star297 4:5368e5b2f312 38 int main() {
star297 4:5368e5b2f312 39
star297 0:879245ce9cdb 40 oled.init();
star297 0:879245ce9cdb 41 oled.clear();
star297 0:879245ce9cdb 42 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 43 oled.setFontSize(FONT5X7);oled.locate(2,20);
star297 0:879245ce9cdb 44 oled.setFontColor(oled.toRGB(255,255,255));
star297 4:5368e5b2f312 45 oled.enableTouch();oled.resetTouchArea();
star297 1:365082270f1a 46 radio.Reset(); // reset and power up radio chip
star297 0:879245ce9cdb 47
star297 1:365082270f1a 48 while(1){
star297 1:365082270f1a 49 initdisplay();
star297 4:5368e5b2f312 50 loadpresets();
star297 4:5368e5b2f312 51 if(PresetFreq[0]!=0){radio.Frequency(PresetFreq[0]);}
star297 1:365082270f1a 52 radiomain();
star297 1:365082270f1a 53 }
star297 1:365082270f1a 54 }
star297 1:365082270f1a 55
star297 1:365082270f1a 56 void radiomain()
star297 1:365082270f1a 57 {
star297 1:365082270f1a 58 while(IRQ==0){
star297 1:365082270f1a 59
star297 1:365082270f1a 60 getkey();
star297 0:879245ce9cdb 61
star297 4:5368e5b2f312 62 //oled.setFontSize(FONT5X7);oled.locate(20,0); // include to calibrate touch screen buttons.
star297 1:365082270f1a 63 //oled.printf("X %03d Y %03d ",xbuf,ybuf);
star297 0:879245ce9cdb 64
star297 1:365082270f1a 65 if(radio.power){
star297 4:5368e5b2f312 66 if(f){
star297 4:5368e5b2f312 67 t1.reset();t1.start();
star297 4:5368e5b2f312 68 setfreq();f=0;
star297 4:5368e5b2f312 69 }
star297 4:5368e5b2f312 70 radio.ProcessData();
star297 4:5368e5b2f312 71 getrds();
star297 4:5368e5b2f312 72 if(t1.read()>5){oled.drawTextGraphic(176,16,(FONT12X16),tFQ, 1, 1, oled.toRGB(0,200,200));t1.stop();}
star297 4:5368e5b2f312 73 displayrefresh();
star297 1:365082270f1a 74 wait_ms(10);
star297 4:5368e5b2f312 75 }
star297 1:365082270f1a 76 }
star297 1:365082270f1a 77 }
star297 1:365082270f1a 78
star297 1:365082270f1a 79 void displayrefresh()
star297 1:365082270f1a 80 {
star297 1:365082270f1a 81 if (strcmp(lastStationName, radio.StationName) != 0){
star297 4:5368e5b2f312 82 if(strlen(radio.StationName)<9){
star297 4:5368e5b2f312 83 oled.drawTextGraphic(26,16,(FONT12X16),radio.StationName, 1, 1, oled.toRGB(0,200,200));
star297 4:5368e5b2f312 84 strcpy(lastStationName,radio.StationName);
star297 4:5368e5b2f312 85 }
star297 1:365082270f1a 86 }
star297 4:5368e5b2f312 87 if(lastfreq != (radio.freq/1000) && (radio.freq/1000<108.10)){lastfreq = radio.freq/1000;
star297 1:365082270f1a 88 sprintf(tFQ, "%3.2f ",radio.freq/1000);
star297 1:365082270f1a 89 oled.drawTextGraphic(176,16,(FONT12X16),tFQ, 1, 1, oled.toRGB(0,200,200));
star297 1:365082270f1a 90 pointer = (lastfreq-87) *12;
star297 1:365082270f1a 91 oled.setPenSize(0);
star297 1:365082270f1a 92 oled.drawRectangle(lastpointer+29,66,10,8,oled.toRGB(0,0,0));
star297 1:365082270f1a 93 oled.drawRectangle(lastpointer+33,56,2,18,oled.toRGB(0,0,0));
star297 1:365082270f1a 94 oled.drawRectangle(pointer+29,66,10,8,oled.toRGB(255,255,255));
star297 4:5368e5b2f312 95 oled.drawRectangle(pointer+33,56,2,18,oled.toRGB(255,0,0));
star297 4:5368e5b2f312 96 oled.setPenSize(1);
star297 1:365082270f1a 97 lastpointer = pointer;
star297 4:5368e5b2f312 98 lastsynchro=0;lastrds=!lastrds;
star297 1:365082270f1a 99 }
star297 1:365082270f1a 100 if(laststereo != radio.stereo){
star297 1:365082270f1a 101 if(radio.stereo){oled.drawTextGraphic(32,79,(FONT5X7),"Stereo", 1, 1, oled.toRGB(0,255,0));}
star297 1:365082270f1a 102 else{oled.drawTextGraphic(32,79,(FONT5X7)," Mono ", 1, 1, oled.toRGB(255,0,0));}
star297 1:365082270f1a 103 laststereo = radio.stereo;
star297 1:365082270f1a 104 }
star297 1:365082270f1a 105 if(lastsignal != radio.signal){
star297 1:365082270f1a 106 signal=radio.signal;
star297 1:365082270f1a 107 if(radio.mute){signal=0;}
star297 1:365082270f1a 108 sprintf(sig, "%d ",signal);
star297 1:365082270f1a 109 oled.drawTextGraphic(124,79,(FONT5X7),sig, 1, 1, oled.toRGB(255,255,255));
star297 4:5368e5b2f312 110 if(signal<10){r=255;g=0;b=0;}
star297 4:5368e5b2f312 111 if(signal>9 && signal<24){r=255;g=255;b=0;}
star297 4:5368e5b2f312 112 if(signal>15){r=0;g=255;b=0;}
star297 1:365082270f1a 113 oled.setPenSize(0);
star297 1:365082270f1a 114 oled.drawRectangle(140,79,(lastsignal*2.4),6,oled.toRGB(0,0,0));
star297 1:365082270f1a 115 oled.drawRectangle(140,79,(signal*2.4),6,oled.toRGB(r,g,b));
star297 1:365082270f1a 116 oled.setPenSize(1);
star297 1:365082270f1a 117 lastsignal=radio.signal;
star297 1:365082270f1a 118 }
star297 1:365082270f1a 119 if(lastvol != radio.volume){
star297 1:365082270f1a 120 volume=radio.volume;
star297 3:99f4c06f86a0 121 sprintf(vol," %d ", volume);
star297 3:99f4c06f86a0 122 oled.setPenSize(0);oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 4:5368e5b2f312 123 oled.drawRectangle(82,183,58,49,oled.toRGB(100,100,100));
star297 3:99f4c06f86a0 124 oled.drawTextGraphic(90,216,(FONT8X12),"Volume", 1, 1, oled.toRGB(255,255,255));
star297 3:99f4c06f86a0 125 if(radio.volume<10){oled.drawTextGraphic(94,194,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));}
star297 3:99f4c06f86a0 126 else{oled.drawTextGraphic(88,194,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));}
star297 3:99f4c06f86a0 127 lastvol = volume;oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 128 }
star297 1:365082270f1a 129 if(lastmute != radio.mute){
star297 1:365082270f1a 130 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:365082270f1a 131 if(radio.mute){oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Mute ");}
star297 1:365082270f1a 132 else{oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Mute ");}
star297 1:365082270f1a 133 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 134 lastmute=radio.mute;
star297 1:365082270f1a 135 }
star297 1:365082270f1a 136 if(lastmono != radio.mono){
star297 1:365082270f1a 137 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:365082270f1a 138 if(radio.mono){oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,0,0)), 1, 1, " Mono ");}
star297 1:365082270f1a 139 else{oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "Stereo");}
star297 1:365082270f1a 140 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 141 lastmono=radio.mono;
star297 1:365082270f1a 142 }
star297 1:365082270f1a 143 if(lastbass != radio.bass){
star297 1:365082270f1a 144 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:365082270f1a 145 if(radio.bass){oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Bass ");}
star297 1:365082270f1a 146 else{oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Bass ");}
star297 1:365082270f1a 147 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 148 lastbass=radio.bass;
star297 1:365082270f1a 149 }
star297 1:365082270f1a 150 if(lastrds != radio.rds){
star297 1:365082270f1a 151 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 4:5368e5b2f312 152 oled.setPenSize(0);oled.drawRectangle(0,95,320,18,oled.toRGB(0,0,0));oled.setPenSize(1);
star297 4:5368e5b2f312 153 if(radio.rds){oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " RDS ");}
star297 4:5368e5b2f312 154 else{oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " RDS ");lastsynchro=0;}
star297 1:365082270f1a 155 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 156 lastrds=radio.rds;
star297 4:5368e5b2f312 157 }
star297 4:5368e5b2f312 158 if(lastsynchro == 0 && radio.rdssynchro == 0x1000){
star297 4:5368e5b2f312 159 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 4:5368e5b2f312 160 oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "RDS RT");
star297 4:5368e5b2f312 161 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 4:5368e5b2f312 162 lastsynchro=1;
star297 4:5368e5b2f312 163 }
star297 4:5368e5b2f312 164 if (strcmp(RDStxt1, lasttext1) != 0 || strcmp(RDStxt2, lasttext2) != 0){
star297 4:5368e5b2f312 165 oled.setPenSize(0);oled.drawRectangle(0,95,320,18,oled.toRGB(0,0,0));oled.setPenSize(1);
star297 4:5368e5b2f312 166 if(strlen(RDStxt1)<41){oled.drawTextGraphic(20,96,(FONT5X7),RDStxt1, 1, 1, oled.toRGB(255,255,255));}
star297 4:5368e5b2f312 167 memset(lasttext1, '\0', sizeof(lasttext1));strcpy(lasttext1, RDStxt1);
star297 4:5368e5b2f312 168 if(strlen(RDStxt2)<41){oled.drawTextGraphic(20,106,(FONT5X7),RDStxt2, 1, 1, oled.toRGB(255,255,255));}
star297 4:5368e5b2f312 169 memset(lasttext2, '\0', sizeof(lasttext2));strcpy(lasttext2, RDStxt2);
star297 4:5368e5b2f312 170 }
star297 4:5368e5b2f312 171 if(strcmp(lastCT, radio.CTtime) !=0){
star297 4:5368e5b2f312 172 oled.drawTextGraphic(265,120,(FONT5X7),radio.CTtime, 1, 1, oled.toRGB(255,255,255));
star297 4:5368e5b2f312 173 strcpy(lastCT,radio.CTtime);
star297 4:5368e5b2f312 174 }
star297 1:365082270f1a 175 }
star297 1:365082270f1a 176
star297 1:365082270f1a 177 void getrds()
star297 4:5368e5b2f312 178 {
star297 4:5368e5b2f312 179 if(strlen(radio.RDSText)>3){
star297 4:5368e5b2f312 180 memset(RDStxt1, '\0', sizeof(RDStxt1));
star297 4:5368e5b2f312 181 memset(RDStxt2, '\0', sizeof(RDStxt2));
star297 4:5368e5b2f312 182
star297 4:5368e5b2f312 183 // format into 2 lines of text seperated by first 'space' after 30 characters
star297 4:5368e5b2f312 184 strcpy(RDStxt1,radio.RDSText);
star297 4:5368e5b2f312 185 n=strlen(RDStxt1);
star297 3:99f4c06f86a0 186 for ( i = 0; i < (n); i++ ){
star297 3:99f4c06f86a0 187 if (i>30 && (RDStxt1[i] == ' ') ){
star297 3:99f4c06f86a0 188 RDStxt1 [strlen(RDStxt1) - (n-i)] = '\0';
star297 3:99f4c06f86a0 189 l=strlen(RDStxt1);
star297 4:5368e5b2f312 190 x=1;
star297 4:5368e5b2f312 191 break; // break if more than 30 characters with space
star297 4:5368e5b2f312 192 }
star297 4:5368e5b2f312 193 if(i>39){
star297 4:5368e5b2f312 194 RDStxt1 [strlen(RDStxt1) - (n-i)] = '\0';
star297 4:5368e5b2f312 195 l=strlen(RDStxt1);
star297 4:5368e5b2f312 196 x=1;
star297 4:5368e5b2f312 197 break; //break if more than 39 characters with no spaces
star297 4:5368e5b2f312 198 }
star297 3:99f4c06f86a0 199 }
star297 4:5368e5b2f312 200 if(x==1){
star297 4:5368e5b2f312 201 strcpy (RDStxt2, radio.RDSText + l);
star297 4:5368e5b2f312 202 while(RDStxt2[0]==' '){
star297 4:5368e5b2f312 203 strcpy (RDStxt2, (RDStxt2 + 1));
star297 4:5368e5b2f312 204 }
star297 4:5368e5b2f312 205 x=0;
star297 4:5368e5b2f312 206 }
star297 4:5368e5b2f312 207 }
star297 0:879245ce9cdb 208 }
star297 0:879245ce9cdb 209
star297 1:365082270f1a 210 void initdisplay()
star297 1:365082270f1a 211 {
star297 1:365082270f1a 212 oled.setPenSize(1);
star297 1:365082270f1a 213 oled.drawTextGraphic(50,6,(FONT5X7),"Station Frequency", 1, 1, oled.toRGB(0,200,200));
star297 1:365082270f1a 214 oled.drawTextGraphic(260,16,(FONT12X16),"MHz", 1, 1, oled.toRGB(0,200,200));
star297 1:365082270f1a 215 oled.drawTextGraphic(42,35,(FONT5X7),"88 90 92 94 96 98 100 102 104 106 108", 1, 1, oled.toRGB(255,255,0));
star297 1:365082270f1a 216 oled.drawTextGraphic(78,79,(FONT5X7),"Signal:", 1, 1, oled.toRGB(255,255,255));
star297 1:365082270f1a 217 oled.drawRectangle(26,55,268,20,oled.toRGB(255,255,255));
star297 1:365082270f1a 218 oled.drawRectangle(26,75,268,14,oled.toRGB(255,255,255));
star297 1:365082270f1a 219 for (i = 46; i < (300); i=i+24 )
star297 1:365082270f1a 220 {oled.drawLine(i,55,i,45,oled.toRGB(255,255,255));
star297 1:365082270f1a 221 oled.drawLine(i-12,55,i-12,50,oled.toRGB(255,255,255));
star297 4:5368e5b2f312 222 }
star297 4:5368e5b2f312 223 oled.drawTextGraphic(10,120,(FONT5X7),"Direct Tune Seek Preset", 1, 1, oled.toRGB(0,128,128));
star297 1:365082270f1a 224 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:365082270f1a 225 oled.drawTextButton(1, 10, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " 0 ");
star297 1:365082270f1a 226 oled.drawTextButton(1, 56, 210,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, ".");
star297 1:365082270f1a 227 oled.drawTextButton(1, 10, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "1");
star297 1:365082270f1a 228 oled.drawTextButton(1, 33, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "2");
star297 1:365082270f1a 229 oled.drawTextButton(1, 56, 184,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "3");
star297 1:365082270f1a 230 oled.drawTextButton(1, 10, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "4");
star297 1:365082270f1a 231 oled.drawTextButton(1, 33, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "5");
star297 1:365082270f1a 232 oled.drawTextButton(1, 56, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "6");
star297 1:365082270f1a 233 oled.drawTextButton(1, 10, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "7");
star297 1:365082270f1a 234 oled.drawTextButton(1, 33, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "8");
star297 1:365082270f1a 235 oled.drawTextButton(1, 56, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "9");
star297 3:99f4c06f86a0 236 oled.drawTextButton(1, 82, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " Up ");
star297 3:99f4c06f86a0 237 oled.drawTextButton(1, 82, 158,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, " Down ");
star297 4:5368e5b2f312 238 oled.drawTextButton(1, 146, 184,(oled.toRGB(100,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "+ ");
star297 4:5368e5b2f312 239 oled.drawTextButton(1, 146, 210,(oled.toRGB(100,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "- ");
star297 1:365082270f1a 240 oled.drawTextButton(1, 260, 134,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " RDS ");
star297 1:365082270f1a 241 oled.drawTextButton(1, 260, 155,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(0,255,0)), 1, 1, "Stereo");
star297 1:365082270f1a 242 oled.drawTextButton(1, 260, 176,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(128,128,128)), 1, 1, " Bass ");
star297 1:365082270f1a 243 oled.drawTextButton(1, 260, 197,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, " Mute ");
star297 1:365082270f1a 244 oled.drawTextButton(1, 260, 218,(oled.toRGB(0,100,100)),(FONT8X12),(oled.toRGB(255,255,255)), 1, 1, "Power ");
star297 1:365082270f1a 245
star297 4:5368e5b2f312 246 oled.drawTextButton(1, 190, 132,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets B");
star297 1:365082270f1a 247 oled.drawTextButton(1, 190, 154,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[0]);
star297 1:365082270f1a 248 oled.drawTextButton(1, 190, 171,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[1]);
star297 1:365082270f1a 249 oled.drawTextButton(1, 190, 188,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[2]);
star297 1:365082270f1a 250 oled.drawTextButton(1, 190, 205,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[3]);
star297 1:365082270f1a 251 oled.drawTextButton(1, 190, 222,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[4]);
star297 1:365082270f1a 252 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 253 }
star297 1:365082270f1a 254
star297 1:365082270f1a 255 void getkey()
star297 1:365082270f1a 256 {
star297 1:365082270f1a 257 flushbuffer();
star297 1:365082270f1a 258 oled.getTouch(&xbuf,&ybuf);
star297 1:365082270f1a 259 if(radio.power){
star297 4:5368e5b2f312 260 if((xbuf>12 && xbuf<32) && (ybuf>188 && ybuf<210)) {k='1';f=1;}
star297 4:5368e5b2f312 261 if((xbuf>34 && xbuf<52) && (ybuf>188 && ybuf<210)) {k='2';f=1;}
star297 4:5368e5b2f312 262 if((xbuf>56 && xbuf<74) && (ybuf>188 && ybuf<210)) {k='3';f=1;}
star297 4:5368e5b2f312 263 if((xbuf>12 && xbuf<32) && (ybuf>162 && ybuf<184)) {k='4';f=1;}
star297 4:5368e5b2f312 264 if((xbuf>34 && xbuf<52) && (ybuf>162 && ybuf<184)) {k='5';f=1;}
star297 4:5368e5b2f312 265 if((xbuf>56 && xbuf<74) && (ybuf>162 && ybuf<184)) {k='6';f=1;}
star297 4:5368e5b2f312 266 if((xbuf>12 && xbuf<32) && (ybuf>138 && ybuf<160)) {k='7';f=1;}
star297 4:5368e5b2f312 267 if((xbuf>34 && xbuf<52) && (ybuf>138 && ybuf<160)) {k='8';f=1;}
star297 4:5368e5b2f312 268 if((xbuf>56 && xbuf<74) && (ybuf>138 && ybuf<160)) {k='9';f=1;}
star297 4:5368e5b2f312 269 if((xbuf>12 && xbuf<54) && (ybuf>212 && ybuf<232)) {k='0';f=1;}
star297 4:5368e5b2f312 270 if((xbuf>56 && xbuf<74) && (ybuf>212 && ybuf<232)) {k='.';f=1;}
star297 4:5368e5b2f312 271 if((xbuf>248 && xbuf<296) && (ybuf>200 && ybuf<218)){radio.Mute();}
star297 4:5368e5b2f312 272 if((xbuf>248 && xbuf<296) && (ybuf>182 && ybuf<198)){radio.BassBoost();}
star297 4:5368e5b2f312 273 if((xbuf>248 && xbuf<296) && (ybuf>160 && ybuf<180)){radio.Mono();}
star297 4:5368e5b2f312 274 if((xbuf>248 && xbuf<296) && (ybuf>140 && ybuf<158)){radio.RDS();}
star297 4:5368e5b2f312 275 if((xbuf>140 && xbuf<170) && (ybuf>188 && ybuf<208)){v=1;Volume();}
star297 4:5368e5b2f312 276 if((xbuf>140 && xbuf<170) && (ybuf>212 && ybuf<230)){v=0;Volume();}
star297 4:5368e5b2f312 277 if((xbuf>80 && xbuf<176) && (ybuf>138 && ybuf<160)){radio.SeekUp();}
star297 4:5368e5b2f312 278 if((xbuf>80 && xbuf<176) && (ybuf>162 && ybuf<184)){radio.SeekDown();}
star297 1:365082270f1a 279
star297 4:5368e5b2f312 280 if((xbuf>182 && xbuf<244) && (ybuf>136 && ybuf<156)){
star297 1:365082270f1a 281 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 4:5368e5b2f312 282 if(prgroup==0){prgroup=5;oled.drawTextButton(1, 190, 132,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets A");}
star297 4:5368e5b2f312 283 else{prgroup=0;oled.drawTextButton(1, 190, 132,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(255,255,255)), 1, 1, "Presets B");}
star297 1:365082270f1a 284 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 285 Drawpresets();
star297 1:365082270f1a 286 }
star297 4:5368e5b2f312 287 if((xbuf>182 && xbuf<244) && (ybuf>160 && ybuf<172)){pr=0;Preset();}
star297 4:5368e5b2f312 288 if((xbuf>182 && xbuf<244) && (ybuf>174 && ybuf<188)){pr=1;Preset();}
star297 4:5368e5b2f312 289 if((xbuf>182 && xbuf<244) && (ybuf>190 && ybuf<204)){pr=2;Preset();}
star297 4:5368e5b2f312 290 if((xbuf>182 && xbuf<244) && (ybuf>206 && ybuf<220)){pr=3;Preset();}
star297 4:5368e5b2f312 291 if((xbuf>182 && xbuf<244) && (ybuf>224 && ybuf<234)){pr=4;Preset();}
star297 1:365082270f1a 292 }
star297 4:5368e5b2f312 293 if((xbuf>252 && xbuf<304) && (ybuf>220 && ybuf<236)){Power();}
star297 1:365082270f1a 294 }
star297 1:365082270f1a 295
star297 1:365082270f1a 296 void Preset()
star297 4:5368e5b2f312 297 {
star297 1:365082270f1a 298 if(PresetFreq[pr+prgroup]!=0 && PresetFreq[pr+prgroup]==radio.freq/1000){
star297 1:365082270f1a 299 strcpy(PresetName[pr+prgroup],"Empty ");
star297 1:365082270f1a 300 if(prgroup){strcat(PresetName[pr+prgroup],"B ");}
star297 1:365082270f1a 301 else{strcat(PresetName[pr+prgroup],"A ");}
star297 1:365082270f1a 302 PresetFreq[pr+prgroup]=0;
star297 1:365082270f1a 303 Drawpresets();
star297 4:5368e5b2f312 304 savepresets();
star297 1:365082270f1a 305 return;
star297 4:5368e5b2f312 306 }
star297 1:365082270f1a 307 if(PresetFreq[pr+prgroup]==0){
star297 4:5368e5b2f312 308 if(radio.signal>20 || radio.rdssynchro == 0x1000){
star297 1:365082270f1a 309 strcpy(PresetName[pr+prgroup],radio.StationName);}
star297 1:365082270f1a 310 else{
star297 1:365082270f1a 311 sprintf(tFQ, "%3.2f MHz",radio.freq/1000);
star297 1:365082270f1a 312 strcpy(PresetName[pr+prgroup],tFQ);
star297 1:365082270f1a 313 }
star297 1:365082270f1a 314 while(strlen(PresetName[pr+prgroup])<10){
star297 1:365082270f1a 315 strcat(PresetName[pr+prgroup]," ");
star297 1:365082270f1a 316 }
star297 1:365082270f1a 317 PresetFreq[pr+prgroup]=radio.freq/1000;
star297 1:365082270f1a 318 Drawpresets();
star297 4:5368e5b2f312 319 savepresets();
star297 1:365082270f1a 320 return;
star297 4:5368e5b2f312 321 }
star297 4:5368e5b2f312 322 radio.Frequency(PresetFreq[pr+prgroup]);
star297 4:5368e5b2f312 323 }
star297 4:5368e5b2f312 324
star297 4:5368e5b2f312 325 void savepresets()
star297 4:5368e5b2f312 326 {
star297 4:5368e5b2f312 327 #if defined(TARGET_Freescale)
star297 4:5368e5b2f312 328 int address = flash_size() - SECTOR_SIZE; //Write in last sector
star297 4:5368e5b2f312 329 erase_sector(address);
star297 4:5368e5b2f312 330 program_flash(address, (char*)&PresetFreq, 44); //10 integers of 4 bytes each: 40 bytes length
star297 4:5368e5b2f312 331 program_flash(address+44, (char*)&PresetName, 440);
star297 4:5368e5b2f312 332 #endif
star297 1:365082270f1a 333 }
star297 1:365082270f1a 334
star297 4:5368e5b2f312 335 void loadpresets()
star297 4:5368e5b2f312 336 {
star297 4:5368e5b2f312 337 #if defined(TARGET_Freescale)
star297 4:5368e5b2f312 338 int address = flash_size() - SECTOR_SIZE;
star297 4:5368e5b2f312 339 int *check = (int*)address;
star297 4:5368e5b2f312 340 float *datafreq = (float*)address;
star297 4:5368e5b2f312 341 if (check[0] != -1){
star297 4:5368e5b2f312 342 for (i = 0; i<11; i++) {
star297 4:5368e5b2f312 343 PresetFreq[i] = (datafreq[i]);
star297 4:5368e5b2f312 344 }
star297 4:5368e5b2f312 345 char *dataname = (char*)address+44;
star297 4:5368e5b2f312 346 memcpy (PresetName, dataname, 440);
star297 4:5368e5b2f312 347 }
star297 4:5368e5b2f312 348 Drawpresets();
star297 4:5368e5b2f312 349 #endif
star297 4:5368e5b2f312 350 }
star297 4:5368e5b2f312 351
star297 1:365082270f1a 352 void Drawpresets()
star297 1:365082270f1a 353 {
star297 1:365082270f1a 354 oled.setTextBackgroundType (TEXT_TRANSPARENT);
star297 1:365082270f1a 355 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]);}
star297 1:365082270f1a 356 else{oled.drawTextButton(1, 190, 154,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[0+prgroup]);}
star297 1:365082270f1a 357 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]);}
star297 1:365082270f1a 358 else{oled.drawTextButton(1, 190, 171,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[1+prgroup]);}
star297 1:365082270f1a 359 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]);}
star297 1:365082270f1a 360 else{oled.drawTextButton(1, 190, 188,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[2+prgroup]);}
star297 1:365082270f1a 361 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]);}
star297 1:365082270f1a 362 else{oled.drawTextButton(1, 190, 205,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[3+prgroup]);}
star297 1:365082270f1a 363 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]);}
star297 1:365082270f1a 364 else{oled.drawTextButton(1, 190, 222,(oled.toRGB(0,100,100)),(FONT5X7),(oled.toRGB(128,128,128)), 1, 1, PresetName[4+prgroup]);}
star297 1:365082270f1a 365 oled.setTextBackgroundType (TEXT_OPAQUE);
star297 1:365082270f1a 366 }
star297 1:365082270f1a 367
star297 1:365082270f1a 368 void setfreq()
star297 4:5368e5b2f312 369 {
star297 1:365082270f1a 370 Fl = strlen(freq);
star297 1:365082270f1a 371 if(Fl< 6){freq[Fl] = k;}
star297 1:365082270f1a 372 if (freq[0]!='1' && freq[0]!='8' && freq[0]!='9'){
star297 1:365082270f1a 373 freq[0]='\0';}
star297 4:5368e5b2f312 374 oled.setPenSize(0);oled.drawRectangle(176,16,80,14,oled.toRGB(0,0,0));oled.setPenSize(1);
star297 1:365082270f1a 375 oled.drawTextGraphic(176,16,(FONT12X16),freq, 1, 1, oled.toRGB(0,200,200));
star297 1:365082270f1a 376 if (freq[0]=='8' || freq[0]=='9'){Fl=5;}
star297 1:365082270f1a 377 else{Fl=6;}
star297 1:365082270f1a 378 if(strlen(freq)==Fl){
star297 1:365082270f1a 379 Freq = strtof (freq, NULL);
star297 4:5368e5b2f312 380 if(Freq>86.99 && Freq<108.01){
star297 4:5368e5b2f312 381 radio.Frequency(Freq);
star297 4:5368e5b2f312 382 }
star297 4:5368e5b2f312 383 else{lastfreq=0;}
star297 1:365082270f1a 384 memset(freq, '\0', sizeof(freq));
star297 1:365082270f1a 385 }
star297 1:365082270f1a 386 }
star297 1:365082270f1a 387
star297 1:365082270f1a 388 void Volume()
star297 1:365082270f1a 389 {
star297 1:365082270f1a 390 if(v){volume++;}
star297 1:365082270f1a 391 if(!v){volume--;}
star297 1:365082270f1a 392 if(volume > 15){volume = 15;}
star297 1:365082270f1a 393 if(volume < 0){volume = 0;}
star297 1:365082270f1a 394 radio.Volume(volume);
star297 1:365082270f1a 395 }
star297 1:365082270f1a 396
star297 1:365082270f1a 397 void Power()
star297 1:365082270f1a 398 {
star297 1:365082270f1a 399 if(radio.power){
star297 1:365082270f1a 400 radio.RDS();
star297 4:5368e5b2f312 401 radio.ProcessData();
star297 1:365082270f1a 402 displayrefresh();
star297 1:365082270f1a 403 radio.PowerOff();
star297 4:5368e5b2f312 404 radio.ProcessData();
star297 1:365082270f1a 405 displayrefresh();
star297 1:365082270f1a 406 }
star297 1:365082270f1a 407 else{
star297 1:365082270f1a 408 radio.PowerOn();
star297 1:365082270f1a 409 radio.RDS();
star297 1:365082270f1a 410 radio.Frequency(lastfreq);
star297 4:5368e5b2f312 411 radio.ProcessData();
star297 1:365082270f1a 412 }
star297 1:365082270f1a 413 }
star297 1:365082270f1a 414
star297 1:365082270f1a 415 void flushbuffer()
star297 1:365082270f1a 416 {
star297 1:365082270f1a 417 while (xbuf!=0 && ybuf!=0){oled.getTouch(&xbuf,&ybuf);}
star297 1:365082270f1a 418 }
star297 1:365082270f1a 419
star297 1:365082270f1a 420
star297 1:365082270f1a 421
star297 1:365082270f1a 422
star297 1:365082270f1a 423
star297 1:365082270f1a 424
star297 1:365082270f1a 425
star297 1:365082270f1a 426
star297 1:365082270f1a 427
star297 1:365082270f1a 428
star297 4:5368e5b2f312 429