Facebook
From kuba , 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 675
  1. ///
  2. /// file SerialRadio.ino
  3. /// brief Radio implementation using the Serial communication.
  4. ///
  5. /// author Matthias Hertel, http://www.mathertel.de
  6. /// copyright Copyright (c) 2014 by Matthias Hertel.n
  7. /// This work is licensed under a BSD style license.n
  8. /// See http://www.mathertel.de/License.aspx
  9. ///
  10. /// details
  11. /// This is a full function radio implementation that uses a LCD display to show the current station information.n
  12. /// It can be used with various chips after adjusting the radio object definition.n
  13. /// Open the Serial console with 57600 baud to see current radio information and change various settings.
  14. ///
  15. /// Wiring
  16. /// ------
  17. /// The necessary wiring of the various chips are described in the Testxxx example sketches.
  18. /// The boards have to be connected by using the following connections:
  19. ///
  20. /// Arduino port | SI4703 signal | RDA5807M signal
  21. /// :----------: | :-----------: | :-------------:
  22. ///          GND | GND           | GND  
  23. ///         3.3V | VCC           | -
  24. ///           5V | -             | VCC
  25. ///           A5 | SCLK          | SCLK
  26. ///           A4 | SDIO          | SDIO
  27. ///           D2 | RST           | -
  28. ///
  29. ///
  30. /// More documentation and source code is available at http://www.mathertel.de/Arduino
  31. ///
  32. /// History:
  33. /// --------
  34. /// * 05.08.2014 created.
  35. /// * 04.10.2014 working.
  36.  
  37. #include <Wire.h>
  38.  
  39. #include <RADIO.h>
  40. #include <RDA5807M.h>
  41. #include <SI4703.h>
  42. #include <SI4705.h>
  43. #include <TEA5767.h>
  44.  
  45. #include <RDSParser.h>
  46.  
  47. #include <LiquidCrystal.h>
  48. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  49. int button1_pin = 3;
  50. int button2_pin = 2;
  51. int button3_pin = 1;
  52. int button4_pin = 0;
  53. int button;  //zmienna pomocnicza
  54.  
  55.  
  56. lcd.begin(16.2);
  57. lcd.print("HELLO");
  58. delay(500)
  59.  
  60. pinMode(button1_pin, INPUT_PULLUP);
  61. pinMode(button2_pin, INPUT_PULLUP);
  62. pinMode(button3_pin, INPUT_PULLUP);
  63. pinMode(button4_pin, INPUT_PULLUP);
  64.  
  65.  
  66. // Define some stations available at your locations here:
  67. // 89.40 MHz as 8940
  68.  
  69. RADIO_FREQ preset[] = {
  70.   8770,
  71.   8810, // hr1
  72.   8820,
  73.   8850, // Bayern2
  74.   8890, // ???
  75.   8930, // * hr3
  76.   8980,
  77.   9180,
  78.   9220, 9350,
  79.   9440, // * hr1
  80.   9510, // - Antenne Frankfurt
  81.   9530,
  82.   9560, // Bayern 1
  83.   9680, 9880,
  84.   10020, // planet
  85.   10090, // ffh
  86.   10110, // SWR3
  87.   10030, 10260, 10380, 10400,
  88.   10500 // * FFH
  89. };
  90.  
  91. int    i_sidx=5;        ///< Start at Station with index=5
  92.  
  93. /// The radio object has to be defined by using the class corresponding to the used chip.
  94. /// by uncommenting the right radio object definition.
  95.  
  96. // RADIO radio;       ///< Create an instance of a non functional radio.
  97.  RDA5807M radio;    ///< Create an instance of a RDA5807 chip radio
  98. // SI4703   radio;    ///< Create an instance of a SI4703 chip radio.
  99. // SI4705   radio;    ///< Create an instance of a SI4705 chip radio.
  100. // TEA5767  radio;    ///< Create an instance of a TEA5767 chip radio.
  101.  
  102.  
  103. /// get a RDS parser
  104. RDSParser rds;
  105.  
  106.  
  107. /// State definition for this radio implementation.
  108. enum RADIO_STATE {
  109.   STATE_PARSECOMMAND, ///< waiting for a new command character.
  110.  
  111.   STATE_PARSEINT,     ///< waiting for digits for the parameter.
  112.   STATE_EXEC          ///< executing the command.
  113. };
  114.  
  115. RADIO_STATE state; ///< The state variable is used for parsing input characters.
  116.  
  117. // - - - - - - - - - - - - - - - - - - - - - - - - - -
  118.  
  119.  
  120.  
  121. /// Update the Frequency on the LCD display.
  122. void DisplayFrequency(RADIO_FREQ f)
  123. {
  124.   char s[12];
  125.   radio.formatFrequency(s, sizeof(s));
  126.   Serial.print("FREQ:"); Serial.println(s);
  127. lcd.setCursor(0, 0);
  128. lcd.print("FREQ:");
  129. lcd.print(s);
  130. } // DisplayFrequency()
  131.  
  132.  
  133. /// Update the ServiceName text on the LCD display.
  134. void DisplayServiceName(char *name)
  135. {
  136.   //Serial.print("RDS:");
  137.   //Serial.println(name);
  138.   lcd.setCursor(0, 1);
  139.   lcd.print(" RDS:");
  140.   lcd.print(name);
  141.  } // DisplayServiceName()
  142.  
  143.  
  144. // - - - - - - - - - - - - - - - - - - - - - - - - - -
  145.  
  146.  
  147. void RDS_process(uint16_t block1, uint16_t block2, uint16_t block3, uint16_t block4) {
  148.   rds.processData(block1, block2, block3, block4);
  149. }
  150.  
  151.  
  152. /// Execute a command identified by a character and an optional number.
  153. /// See the "?" command for available commands.
  154. /// param cmd The command character.
  155. /// param value An optional parameter for the command.
  156. void runSerialCommand(char cmd, int16_t value)
  157. {
  158.   if (cmd == '?') {
  159.     Serial.println();
  160.     Serial.println("? Help");
  161.     Serial.println("+ increase volume");
  162.     Serial.println("- decrease volume");
  163.     Serial.println("> next preset");
  164.     Serial.println("< previous preset");
  165.     Serial.println(". scan up   : scan up to next sender");
  166.     Serial.println(", scan down ; scan down to next sender");
  167.     Serial.println("fnnnnn: direct frequency input");
  168.     Serial.println("i station status");
  169.     Serial.println("s mono/stereo mode");
  170.     Serial.println("b bass boost");
  171.     Serial.println("u mute/unmute");
  172.   }
  173.  
  174.   // ----- control the volume and audio output -----
  175.  
  176.   else if (cmd == '+') {
  177.     // increase volume
  178.     int v = radio.getVolume();
  179.     if (v < 15) radio.setVolume(++v);
  180.   }
  181.   else if (cmd == '-') {
  182.     // decrease volume
  183.     int v = radio.getVolume();
  184.     if (v > 0) radio.setVolume(--v);
  185.   }
  186.  
  187.   else if (cmd == 'u') {
  188.     // toggle mute mode
  189.     radio.setMute(! radio.getMute());
  190.   }
  191.  
  192.   // toggle stereo mode
  193.   else if (cmd == 's') { radio.setMono(! radio.getMono()); }
  194.  
  195.   // toggle bass boost
  196.   else if (cmd == 'b') { radio.setBassBoost(! radio.getBassBoost()); }
  197.  
  198.   // ----- control the frequency -----
  199.  
  200.   else if (cmd == '>') {
  201.     // next preset
  202.     if (i_sidx < (sizeof(preset) / sizeof(RADIO_FREQ))-1) {
  203.       i_sidx++; radio.setFrequency(preset[i_sidx]);
  204.     } // if
  205.   }
  206.   else if (cmd == '<') {
  207.     // previous preset
  208.     if (i_sidx > 0) {
  209.       i_sidx--;
  210.       radio.setFrequency(preset[i_sidx]);
  211.     } // if
  212.  
  213.   }
  214.   else if (cmd == 'f') { radio.setFrequency(value); }
  215.  
  216.   else if (cmd == '.') { radio.seekUp(false); }
  217.   else if (cmd == ':') { radio.seekUp(true); }
  218.   else if (cmd == ',') { radio.seekDown(false); }
  219.   else if (cmd == ';') { radio.seekDown(true); }
  220.  
  221.  
  222.   // not in help:
  223.   else if (cmd == '!') {
  224.     if (value == 0) radio.term();
  225.     if (value == 1) radio.init();
  226.  
  227.   }
  228.   else if (cmd == 'i') {
  229.     char s[12];
  230.     radio.formatFrequency(s, sizeof(s));
  231.     Serial.print("Station:"); Serial.println(s);
  232.     Serial.print("Radio:"); radio.debugRadioInfo();
  233.     Serial.print("Audio:"); radio.debugAudioInfo();
  234.  
  235.   } // info
  236.  
  237.   else if (cmd == 'x') {
  238.     radio.debugStatus(); // print chip specific data.
  239.   }
  240. } // runSerialCommand()
  241.  
  242.  
  243. /// Setup a FM only radio configuration with I/O for commands and debugging on the Serial port.
  244. void setup() {
  245.   // open the Serial port
  246.   Serial.begin(57600);
  247.   Serial.print("Radio...");
  248.   delay(500);
  249.  
  250.   // Initialize the Radio
  251.   radio.init();
  252.  
  253.   // Enable information to the Serial port
  254.   radio.debugEnable();
  255.  
  256.   radio.setBandFrequency(RADIO_BAND_FM, preset[i_sidx]); // 5. preset.
  257.  
  258.   // delay(100);
  259.  
  260.   radio.setMono(false);
  261.   radio.setMute(false);
  262.   // radio.debugRegisters();
  263.   radio.setVolume(8);
  264.  
  265.   Serial.write('>');
  266.  
  267.   state = STATE_PARSECOMMAND;
  268.  
  269.   // setup the information chain for RDS data.
  270.   radio.attachReceiveRDS(RDS_process);
  271.   rds.attachServicenNameCallback(DisplayServiceName);
  272.  
  273.   runSerialCommand('?', 0);
  274. } // Setup
  275.  
  276.  
  277. /// Constantly check for serial input commands and trigger command execution.
  278. void loop() {
  279.   int newPos;
  280.   unsigned long now = millis();
  281.   static unsigned long nextFreqTime = 0;
  282.   static unsigned long nextRadioInfoTime = 0;
  283.  
  284.   // some internal static values for parsing the input
  285.   static char command;
  286.   static int16_t value;
  287.   static RADIO_FREQ lastf = 0;
  288.   RADIO_FREQ f = 0;
  289.  
  290.   char c;
  291.   if (Serial.available() > 0) {
  292.     // read the next char from input.
  293.     c = Serial.peek();
  294.  
  295.     if ((state == STATE_PARSECOMMAND) && (c < 0x20)) {
  296.       // ignore unprintable chars
  297.       Serial.read();
  298.  
  299.     }
  300.     else if (state == STATE_PARSECOMMAND) {
  301.       // read a command.
  302.       command = Serial.read();
  303.       state = STATE_PARSEINT;
  304.  
  305.     }
  306.     else if (state == STATE_PARSEINT) {
  307.       if ((c >= '0') && (c <= '9')) {
  308.         // build up the value.
  309.         c = Serial.read();
  310.         value = (value * 10) + (c - '0');
  311.       }
  312.       else {
  313.         // not a value -> execute
  314.         runSerialCommand(command, value);
  315.         command = ' ';
  316.         state = STATE_PARSECOMMAND;
  317.         value = 0;
  318.       } // if
  319.     } // if
  320.   } // if
  321.  
  322.  
  323.   // check for RDS data
  324.   radio.checkRDS();
  325.  
  326.   // update the display from time to time
  327.   if (now > nextFreqTime) {
  328.     f = radio.getFrequency();
  329.     if (f != lastf) {
  330.       // print current tuned frequency
  331.       DisplayFrequency(f);
  332.       lastf = f;
  333.     } // if
  334.     nextFreqTime = now + 400;
  335.   } // if  
  336. button = ButtonCheck();
  337. if (button > 0){
  338.   if(button == 1){
  339.     runSerialComand('>', 0);
  340.   }
  341.   if (button == 2) {
  342.     runSerialCommand('<', 0);
  343.   }  
  344.   if (button == 3) {
  345.     runSerialCommand('+', 0);
  346.   }
  347.   if (button == 4) {
  348.     runSerialComand('-', 0);
  349.   }
  350.   button = 0;
  351.  
  352.   }
  353. }
  354.  
  355. } // loop
  356.  
  357. // End.
  358.  
  359.  

Replies to rda radio rss

Title Name Language When
Re: rda radio Subtle Lemur text 2 Years ago.