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

Revision:
3:99f4c06f86a0
Parent:
1:365082270f1a
Child:
4:5368e5b2f312
--- a/main.cpp	Sat Apr 11 23:02:17 2015 +0000
+++ b/main.cpp	Sun Apr 12 16:26:28 2015 +0000
@@ -14,17 +14,14 @@
 int signal,lastsignal,volume,lastvol,lastmute,lastmono,lastbass,lastrds,laststereo,rdstextgood;
 
 float lastfreq,pointer,lastpointer,Freq;
+float PresetFreq[10];
 
 char k;
-char sig[4];
-char tFQ[20],freq[8],vol[4];
+char tFQ[20],freq[8],vol[4],sig[4];
 char station[10],lastStationName[10];
-char text1[50],lasttext1[50];
-char text2[50],lasttext2[50];
+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 "};
 
-float PresetFreq[10];
-
 void radiomain(),initdisplay(),displayrefresh(),getrds();
 void getkey(),flushbuffer(),Volume(),setfreq(),Power(),Preset(),Drawpresets(); 
  
@@ -105,10 +102,13 @@
         }
     if(lastvol != radio.volume){
         volume=radio.volume;
-        sprintf(vol," %d ", volume); 
-        if(radio.volume<10){oled.drawTextGraphic(94,196,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));} 
-            else{oled.drawTextGraphic(88,196,(FONT12X16),vol, 1, 1, oled.toRGB(255,255,255));}    
-        lastvol = 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);
@@ -145,11 +145,11 @@
     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));
-        strcpy(lasttext1, text1);}
+        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));
-        strcpy(lasttext2, text2);}    
+        memset(lasttext2, '\0', sizeof(lasttext2));strcpy(lasttext2, text2);}    
 }       
 
 void getrds()
@@ -158,23 +158,30 @@
         //oled.drawTextGraphic(10,200,(FONT5X7),radio._RDSText, 1, 1, oled.toRGB(255,255,255));        
         memset(text1, '\0', sizeof(text1));
         memset(text2, '\0', sizeof(text2));
-        memset(lasttext1, '\0', sizeof(lasttext1));
-        memset(lasttext2, '\0', sizeof(lasttext2));
-        if(!rdstextgood){rdstextgood=1;lastrds=!lastrds;}   
-        strcpy (text1, radio.RDSText);  
-    
-        for ( i = 0; i < (64); i++ )
-            if (i>30 && (text1[i] == ' ') ){                
-                text1 [strlen(text1) - (64-i)] = '\0';               
-                l=strlen(text1);
-                i=64;
-            }      
-        strcpy (text2, radio.RDSText + l);
-        while(text2[0]==' '){
-            strcpy (text2, (text2 + 1));
+        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++;
             }
-        strcat (text1,"\0");
-        strcat (text2,"\0");
+        }                              
+    // 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));      
     }
 }
@@ -191,7 +198,7 @@
     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 ");
@@ -205,10 +212,10 @@
     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, 86, 132,(oled.toRGB(0,100,100)),(FONT12X16),(oled.toRGB(255,255,255)), 1, 1, "  Up  ");
-    oled.drawTextButton(1, 86, 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, 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 ");