- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
- #include <SPI.h>
- #include <RDA5807.h>
- #include <math.h>
- RDA5807 rx;
- #define TFT_CS 2
- #define TFT_RST 3
- #define TFT_DC 10
- #define TFT_SCLK 4
- #define TFT_MOSI 6
- /* 默认的SPI接口为
- static const uint8_t SS = 7;
- static const uint8_t MOSI = 6;---SDA
- static const uint8_t MISO = 5;
- static const uint8_t SCK = 4;---SCL
- */
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
- // color definitions
- const uint16_t Display_Color_Black = 0x0000;
- const uint16_t Display_Color_Blue = 0x001F;
- const uint16_t Display_Color_Red = 0xF800;
- const uint16_t Display_Color_Green = 0x07E0;
- const uint16_t Display_Color_Cyan = 0x07FF;
- const uint16_t Display_Color_Magenta = 0xF81F;
- const uint16_t Display_Color_Yellow = 0xFFE0;
- const uint16_t Display_Color_White = 0xFFFF;
- // The colors we actually want to use
- uint16_t Display_Text_Color = Display_Color_White;
- uint16_t Display_Backround_Color = Display_Color_Black;
- const size_t MaxString = 32;
- char oldTimeString[MaxString] = "4 14:29:30";
- char oldFre[6]="97.5";
- void displayUpTime()
- {
- unsigned long upSeconds = millis() / 1000;
- unsigned long days = upSeconds / 86400;
- upSeconds = upSeconds % 86400;
- unsigned long hours = upSeconds / 3600;
- upSeconds = upSeconds % 3600;
- unsigned long minutes = upSeconds / 60;
- upSeconds = upSeconds % 60;
- char newTimeString[MaxString] = { 0 };
- sprintf(newTimeString,"%lu %02lu:%02lu:%02lu",days, hours, minutes, upSeconds);
- if (strcmp(newTimeString,oldTimeString) != 0) {
- tft.setTextSize(2);
- tft.setCursor(0,0);
- tft.setTextColor(Display_Backround_Color);
- tft.print(oldTimeString);
- tft.setCursor(0,0);
- tft.setTextColor(Display_Text_Color);
- tft.print(newTimeString);
- strcpy(oldTimeString,newTimeString);
- }
- }
- // Show current frequency
- void showStatus()
- {
- char aux[80];
- sprintf(aux,"\nYou are tuned on %.1f MHz | RSSI: %3.3u dbUv | Vol: %2.2u | %s ",rx.getFrequency()/100.0, rx.getRssi(), rx.getVolume(), (rx.isStereo()) ? "Yes" : "No" );
- Serial.print(aux);
- tft.setTextSize(1);
- tft.setTextColor(Display_Backround_Color);
- tft.setCursor(70, 79);
- tft.print(oldFre);
- tft.setTextColor(Display_Text_Color);
- tft.setCursor(70, 79);
- sprintf(oldFre,"%.1f",rx.getFrequency()/100.0);
- tft.print(oldFre);
- }
- void showHelp()
- {
- Serial.println(F("Type U to increase and D to decrease the frequency"));
- Serial.println(F(" S or s to seek station Up or Down"));
- Serial.println(F(" + or - to volume Up or Down"));
- Serial.println(F(" B or b to Bass On or Off"));
- Serial.println(F(" 0 to use 87-108 MHz (US/Europe)"));
- Serial.println(F(" 1 to use 76-91 MHz (Japan)"));
- Serial.println(F(" 2 to use 76-108 MHz (world wide)"));
- Serial.println(F(" 3 to use 65-76 MHz (East Europe) or 50-65MHz (see bit 9 of gegister 0x06)"));
- Serial.println(F(" 4 to use 50-65 MHz"));
- Serial.println(F(" ? to this help."));
- Serial.println(F("=================================================="));
- delay(1000);
- }
- void setup() {
- Serial.begin(115200);
- while (!Serial);
- Serial.println();
- Wire.begin(); // 初始化I2C总线
- rx.setup();
- rx.setVolume(2);
- // Select a station with RDS service in your place
- Serial.print("\nEstacao 97.50MHz");
- rx.setFrequency(9750); // It is the frequency you want to select in MHz multiplied by 100.
- delay(250);
- // rx.setLnaPortSel(3); // Trying improve sensitivity.
- // rx.setLnaIcSel(0);
- rx.setAFC(true); // Sets Automatic Frequency Control
- showHelp();
- tft.initR(INITR_BLACKTAB); //初始化屏幕
- tft.setFont();//初始化字体
- tft.fillScreen(Display_Backround_Color);
- tft.setTextColor(Display_Text_Color);
- tft.setTextSize(2);
- Serial.println("Hello");
- // a single pixel
- delay(500);
- for(uint8_t i=0;i<128;i++)
- {
- tft.drawPixel(i, 30+(sin(3.14159*i/32.0)*10), ST77XX_GREEN);
- }
- delay(500);
- tft.fillRect( 2,45,30,10,ST77XX_GREEN);
- delay(500);
- tft.drawRect(40,45,30,10,ST77XX_GREEN);
- delay(500);
- tft.fillCircle( 90, 50, 5, ST77XX_GREEN);
- tft.drawCircle(110, 50, 5, ST77XX_GREEN);
- delay(500);
- tft.drawFastHLine(0, 65, tft.width(), ST77XX_RED);
- delay(500);
- for(uint8_t x=4;x<128;x=x+12)
- {
- tft.drawFastVLine(x, 65, 5, ST77XX_BLUE);
- }
- delay(500);
- tft.drawRect(2,75,tft.width()-(2*2),15,ST77XX_GREEN);
- tft.drawRect(4,77,18,11,ST77XX_BLUE);
- tft.drawRect(67,77,58,11,ST77XX_BLUE);
- tft.setTextSize(1);
- tft.setCursor(7, 77+2);
- tft.print("FM");
- tft.setCursor(70, 79);
- tft.print(oldFre);
- tft.setCursor(104, 77+2);
- tft.print("MHz");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- displayUpTime();
- delay(100);
- if (Serial.available() > 0)
- {
- char key = Serial.read();
- switch (key)
- {
- case '+':
- rx.setVolumeUp();
- break;
- case '-':
- rx.setVolumeDown();
- break;
- case 'B':
- rx.setBass(true);
- break;
- case 'b':
- rx.setBass(false);
- break;
- case 'U':
- case 'u':
- rx.setFrequencyUp();
- break;
- case 'D':
- case 'd':
- rx.setFrequencyDown();
- break;
- case 'S':
- rx.seek(RDA_SEEK_WRAP, RDA_SEEK_UP, showStatus);
- break;
- case 's':
- rx.seek(RDA_SEEK_WRAP, RDA_SEEK_DOWN, showStatus);
- break;
- case '0':
- case '1':
- case '2':
- case '3':
- rx.setBand( key - 48 );
- rx.setBand3_50_65_Mode(1); // Band 3 will work from 65 to 76 MHz;
- rx.setFrequencyToBeginBand();
- Serial.print(F("\n**** Switching to band: "));
- Serial.print(rx.getBand());
- break;
- case '4':
- // ATTENTION: The functions setFrequencyToBeginBand and setFrequencyToEnBand do not work for 50-65MHz setup. You have to control it by yourself.
- // Also, you must control the band limits from 50 to 65 MHz. The setFrequencyUp and setFrequencyDown do not work properly.
- rx.setBand(3);
- rx.setBand3_50_65_Mode(0); // Band 3 will work from 50 to 65 MHz;
- rx.setFrequency(5500); // 55 Mhz; rx.setFrequencyDown and rx.setFrequencyUP, rx.setFrequencyToBeginBand() and rx.setFrequencyToEndBand() do not work properly for this setup;
- Serial.print(F("\n**** Switching to band: 3 from 50 to 65 MHz) "));
- break;
- break;
- case '?':
- showHelp();
- break;
- default:
- break;
- }
- delay(200);
- showStatus();
- }
- delay(5);
- }