Guest User

radio.ino

a guest
Dec 27th, 2019
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "TEF6686.h"
  2.  
  3. uint16_t frequency;
  4. byte volume;
  5.  
  6. bool isRdsSync;  
  7.  
  8. char programTypePrevious[17] = "                ";
  9. char programServicePrevious[9];
  10. char programServiceUnsafePrevious[9];
  11. char programIdPrevious[4];
  12. char radioTextPrevious[65];
  13.  
  14. TEF6686 radio;
  15. RdsInfo rdsInfo;
  16.  
  17. void setup() {
  18.   delay(1000);
  19.   Serial.begin(115200);
  20.   while (!Serial);
  21.   Serial.println(F("Begin"));
  22.   radio.init();
  23.   radio.powerOn();
  24.   radio.setFrequency(8750);
  25.   frequency = radio.getFrequency();
  26.   //displayInfo();
  27.   radioGui(1);
  28.  
  29. }
  30.  
  31. void loop() {
  32.   readRds();
  33.   radioGui(0);
  34.   if (Serial.available()) {
  35.     char ch = Serial.read();
  36.     if (ch == 'm') {
  37.       radio.setMute();
  38.       //displayInfo();
  39.     }
  40.     else if (ch == 'n') {
  41.       radio.setUnMute();
  42.       //displayInfo();
  43.     }
  44.     else if (ch == 'p') {
  45.       radio.powerOn();
  46.       //displayInfo();
  47.     }
  48.     else if (ch == 'o') {
  49.       radio.powerOff();
  50.       //displayInfo();
  51.     }
  52.     if (ch == 'u') {
  53.       frequency = radio.seekUp();
  54.       //displayInfo();
  55.       radioGui(true);
  56.     }
  57.     else if (ch == 'd') {
  58.       frequency = radio.seekDown();
  59.       //displayInfo();
  60.       radioGui(true);
  61.     }
  62.     else if (ch == '+') {
  63.       volume += 4;
  64.       if (volume >= 24) volume = 24;
  65.       radio.setVolume(volume);
  66.       displayInfo();
  67.     }
  68.     else if (ch == '-') {
  69.       volume -= 4;
  70.       if (volume < -60) volume = -60;
  71.       radio.setVolume(volume);
  72.       displayInfo();
  73.     }
  74.     else if (ch == 'a') {
  75.       frequency = radio.tuneUp();
  76.       //displayInfo();
  77.       radioGui(true);
  78.     }
  79.     else if (ch == 'b') {
  80.       frequency = radio.tuneDown();
  81.       //displayInfo();
  82.       radioGui(true);
  83.     }    
  84.     else if (ch == 'r') {
  85.       radioGui(true);
  86.     }
  87.   }
  88. }
  89.  
  90. void readRds() {
  91.   isRdsSync = radio.readRDS();
  92.   radio.getRDS(&rdsInfo);
  93. }
  94.  
  95. void showPI() {
  96.   if (isNewPi()){
  97.     strcpy(programIdPrevious, rdsInfo.programId);
  98.   }
  99.   Serial.print(rdsInfo.programId);
  100. }
  101.  
  102. bool isNewPi(){
  103.   return (strlen(rdsInfo.programId) == 4) && !strcmp(rdsInfo.programId, programIdPrevious, 4);
  104. }
  105.  
  106. //UNUSED
  107. void showPTY() {
  108.   if (isNewPty()) {
  109.     Serial.print(rdsInfo.programType);
  110.     strcpy(programTypePrevious, rdsInfo.programType);
  111.     Serial.println();  
  112.   }  
  113. }
  114.  
  115. //UNUSED
  116. bool isNewPty() {
  117.   return (isRdsSync == 1) && !strcmp(rdsInfo.programType, programTypePrevious, 16);
  118. }
  119.  
  120. void showPS() {
  121.   if (isNewPs()) {
  122.     strcpy(programServicePrevious, rdsInfo.programService);
  123.   }
  124.   Serial.print(rdsInfo.programService);
  125. }
  126.  
  127. void showPsUnsafe(){
  128.   if (isNewPsUnsafe()){
  129.     strcpy(programServiceUnsafePrevious, rdsInfo.programServiceUnsafe);
  130.   }
  131.   Serial.print(rdsInfo.programServiceUnsafe);
  132. }
  133.  
  134. bool isNewPs() {
  135.   return (strlen(rdsInfo.programService) == 8) && !strcmp(rdsInfo.programService, programServicePrevious, 8);
  136. }
  137.  
  138. bool isNewPsUnsafe() {
  139.   return (strlen(rdsInfo.programServiceUnsafe) == 8) && !strcmp(rdsInfo.programServiceUnsafe, programServiceUnsafePrevious, 8);
  140. }
  141.  
  142. //UNUSED
  143. void showRadioText() {
  144.   if (isNewRt()){
  145.     Serial.print(rdsInfo.radioText);
  146.     strcpy(radioTextPrevious, rdsInfo.radioText);
  147.     Serial.println();  
  148.   }
  149. }
  150.  
  151. //UNUSED
  152. bool isNewRt() {
  153.   return (isRdsSync == 1) && !strcmp(rdsInfo.radioText, radioTextPrevious, 65);
  154. }
  155.  
  156. bool strcmp(char* str1, char* str2, int length) {
  157.   for (int i = 0; i < length; i++) {
  158.     if (str1[i] != str2[i]) {
  159.       return false;
  160.     }    
  161.   }  
  162.   return true;
  163. }
  164.  
  165. void displayInfo() {
  166.    delay(10);
  167.    Serial.print(F("Frequency:"));
  168.    Serial.print(frequency);
  169.    Serial.print(F(" Volume:"));
  170.    Serial.println(volume);
  171.    Serial.print(F("Level:"));
  172.    Serial.println(radio.getLevel() / 10);
  173.    Serial.print(F("Stereo:"));
  174.    Serial.println(radio.getStereoStatus());
  175. }
  176.  
  177. void radioGui(bool force){
  178.   if (force || isNewPi() || isNewPs() || isNewPsUnsafe()) {
  179.     //clrscr
  180.     Serial.write(27);       // ESC command
  181.     Serial.print("[2J");    // clear screen command
  182.     Serial.write(27);
  183.     Serial.print("[H");     // cursor to home command
  184.  
  185.     //first line
  186.     Serial.print(F("FM "));
  187.     Serial.print(frequency / 100);
  188.     Serial.print(F("."));
  189.     Serial.print(frequency % 100 / 10);
  190.     Serial.print(F(" MHz   "));
  191.  
  192.     Serial.print(frequency >= 10000 ? F("[ ") : F(" [ "));
  193.     showPS();
  194.     Serial.print(" ]");
  195.  
  196.     //second line
  197.     Serial.println();
  198.     Serial.print(F("PI:  "));
  199.     showPI();
  200.     Serial.print(F("      ( "));
  201.     showPsUnsafe();
  202.     Serial.print(F(" ) "));
  203.  
  204.     Serial.println();
  205.     Serial.println();
  206.   }
  207. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×