Advertisement
bal_gennady

Radio_Touch_Screen_25

Jan 15th, 2022
1,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 15.40 KB | None | 0 0
  1. #define LCD_CS A3 // Chip Select goes to Analog 3
  2. #define LCD_CD A2 // Command/Data goes to Analog 2
  3. #define LCD_WR A1 // LCD Write goes to Analog 1
  4. #define LCD_RD A0 // LCD Read goes to Analog 0
  5.  
  6. #include "Adafruit_GFX.h"// Hardware-specific library
  7. #include <MCUFRIEND_kbv.h>
  8. MCUFRIEND_kbv tft;
  9.  
  10. #include <BME280I2C.h>
  11. #include <Wire.h>
  12. #include <radio.h>
  13. #include <RDA5807M.h>
  14. #define FIX_VOLUME   3  
  15.  
  16.  
  17. bool BT3 = false;
  18. byte StN = 0;          // Номер станции из массива биндов станций
  19. int StFreq [] = {9190, 10340, 9640,9860,10090,9320,10450,10780,9910};
  20.  
  21.                     // 13 символов
  22. char StName[][14] = { "  Humor FM   ", "    ROCKS    ", "Radio Mogilev", "Russkoe radio", "  Radius FM  ",
  23.                     "   Pilot FM  ", "  Radio BA   ", "     Mir     ", "   Culture   "  };
  24.  
  25. // датчик температуры, давления и влажности
  26. BME280I2C bme;                   // Default : forced mode, standby time = 1000 ms
  27.                               // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
  28. //bool metric = true;           // задаем метрическую систему измерений
  29.    float temp(NAN), hum(NAN), pres(NAN);
  30.   // uint8_t pressureUnit(5);    
  31.  
  32.  
  33. // Часы
  34. #include <iarduino_RTC.h>
  35. iarduino_RTC time(RTC_DS1307);
  36.  
  37. #define FIX_BAND     RADIO_BAND_FM   ///< The band that will be tuned by this sketch is FM.
  38. #define FIX_STATION  9190            ///< The station that will be tuned by this sketch is 89.30 MHz.
  39. #define FIX_VOLUME   3               ///< The volume that will be set by this sketch is level 4.
  40.  
  41. #include <TouchScreen.h>
  42. // калибровка тачскрина
  43. #define TS_MINX 84
  44. #define TS_MINY 62
  45. #define TS_MAXX 953
  46. #define TS_MAXY 904
  47.  
  48. // Сила нажатия на дисплей
  49. #define MINPRESSURE 10
  50. #define MAXPRESSURE 1000
  51.  
  52. // Последним параметром указывается сопротивление между X+ и X-
  53. // Для данных дисплеев 300 подойдет
  54. uint8_t YP = A1;  // must be an analog pin, use "An" notation!
  55. uint8_t XM = A2;  // must be an analog pin, use "An" notation!
  56. uint8_t YM = 7;   // can be a digital pin
  57. uint8_t XP = 6;   // can be a digital pin
  58.  
  59. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  60.  
  61. /* some RGB color definitions            */                                  
  62. #define Black           0x0000      /*   0,   0,   0 */
  63. #define Navy            0x000F      /*   0,   0, 128 */
  64. #define DarkGreen       0x03E0      /*   0, 128,   0 */
  65. #define DarkCyan        0x03EF      /*   0, 128, 128 */
  66. #define Maroon          0x7800      /* 128,   0,   0 */
  67. #define Purple          0x780F      /* 128,   0, 128 */
  68. #define Olive           0x7BE0      /* 128, 128,   0 */
  69. #define LightGrey       0xC618      /* 192, 192, 192 */
  70. #define DarkGrey        0x7BEF      /* 128, 128, 128 */
  71. #define Blue            0x001F      /*   0,   0, 255 */
  72. #define Green           0x07E0      /*   0, 255,   0 */
  73. #define Cyan            0x07FF      /*   0, 255, 255 */
  74. #define Red             0xF800      /* 255,   0,   0 */
  75. #define Magenta         0xF81F      /* 255,   0, 255 */
  76. #define Yellow          0xFFE0      /* 255, 255,   0 */
  77. #define White           0xFFFF      /* 255, 255, 255 */
  78. #define Orange          0xFD20      /* 255, 165,   0 */
  79. #define GreenYellow     0xAFE5      /* 173, 255,  47 */
  80.  
  81.  
  82. int p_X, p_Y, Vol;
  83. byte P_X, P_Y, P_Button;
  84.  
  85. RDA5807M radio;    // Create an instance of Class for RDA5807M Chip
  86.  
  87. void setup(void) {
  88.     pinMode(10, OUTPUT); //реле
  89.     digitalWrite(10, HIGH); //Отключаем реле
  90.     pinMode(13, OUTPUT);
  91.     uint16_t ID = tft.readID(); //
  92.     tft.begin(ID);
  93.     tft.setRotation(1);    //Landscape
  94.     tft.fillScreen(Black);
  95.      bme.begin();
  96.      time.begin();
  97.      
  98.   // установка времени                  
  99.   // time.settime(0,6,8,13,06,17,2);  // _ сек, _ мин, _ час, _ день месяца, _ месяц, _ год, день недели 0-вск 6-суб
  100.  
  101.     tft.drawRoundRect(1,1,318,46,10, Cyan);
  102.     tft.drawTriangle(40, 8, 40, 38, 8,24 , Cyan);
  103.     tft.drawTriangle(280, 8, 280, 38, 310, 24, Cyan);
  104.     delay(200);
  105.  // Initialize the Radio
  106.     radio.init();
  107.  // Set all radio setting to the fixed values.
  108.    StChange(0);
  109.    radio.setVolume(FIX_VOLUME);
  110.    radio.setMono(false);
  111.    radio.setMute(false);
  112.    radio.setBassBoost(false);
  113.  
  114.     tft.fillCircle(160, 108, 4, White); //Точки между час и мин
  115.     tft.fillCircle(160, 130, 4, White);
  116.      
  117. //    tft.fillCircle(265, 126, 2, LightGrey); //Точки между мин и сек
  118. //    tft.fillCircle(265, 136, 2, LightGrey);
  119.            
  120.          tft.setTextSize(2);
  121.          tft.setTextColor(Green, Black);
  122.          tft.setCursor(71,210);
  123.          tft.print(radio.getVolume());      
  124.          tft.fillTriangle(64, 238, 255, 238, 255, 206, Black);  
  125.          tft.drawTriangle(64, 238, 255, 238, 255, 206, Green);
  126.          tft.fillTriangle(64, 238, 64+((radio.getVolume()+1)*12), 238, 64+((radio.getVolume()+1)*12), 238-((radio.getVolume()+1)*2), Green);
  127.          
  128.           tft.setTextSize(2);
  129.           tft.setCursor(8,70);          
  130.           tft.drawRect(1,61,48,34,DarkGrey);
  131.           tft.setTextColor(DarkGrey, Black);
  132.           tft.print("BT1");
  133.                  
  134.           tft.setCursor(8,118);          
  135.           tft.drawRect(1,109,48,34, DarkGrey);
  136.           tft.setTextColor(DarkGrey, Black);
  137.           tft.print("BT2");
  138.        
  139.           tft.setCursor(8,166);          
  140.           tft.drawRoundRect(1,157,48,34,8, DarkGrey);
  141.           tft.setTextColor(DarkGrey, Black);
  142.           tft.print("FAN");
  143.          
  144.           tft.setCursor(8,214);          
  145.           tft.drawRoundRect(1,205,48,34,8, DarkGrey);
  146.           tft.setTextColor(DarkGrey, Black);
  147.           tft.print("MUT");  
  148.  
  149.           tft.setCursor(276,70);
  150.           tft.drawRoundRect(271,109,48,34,8, DarkGrey);
  151.           tft.setTextColor(DarkGrey, Black);
  152.           tft.print("BT4");  
  153.  
  154.           tft.setCursor(276,118);
  155.           tft.drawRoundRect(271,61,48,34,8, DarkGrey);
  156.           tft.setTextColor(DarkGrey, Black);
  157.           tft.print("BT5");  
  158.          
  159.           tft.setCursor(276,166);
  160.           tft.drawRoundRect(271,157,48,34,8, DarkGrey);
  161.           tft.setTextColor(DarkGrey, Black);
  162.           tft.print("BT6");  
  163.  
  164.           tft.setCursor(276,214);
  165.           tft.drawRoundRect(271,205,48,34,8, DarkGrey);
  166.           tft.setTextColor(DarkGrey, Black);
  167.           tft.print("BAS");  
  168.  
  169.  }
  170.  
  171.  
  172. void loop()
  173. {
  174.        // Отображаем Дату и время
  175.         if(millis()%1000==0){ // если прошла 1 секунда
  176.         tft.setTextColor(White, Black);
  177.         tft.setTextSize(7);
  178.         tft.setCursor(67, 92);           // выводим время - час
  179.         tft.print(time.gettime("H"));  
  180.  //       tft.setCursor(160, 90);           // выводим :
  181.  //       tft.print(":");  
  182.         tft.setCursor(175, 92);           // выводим время - мин
  183.         tft.print(time.gettime("i"));      
  184.        
  185.         tft.setCursor(274,120);
  186.         tft.setTextColor(LightGrey, Black);
  187.         tft.setTextSize(3);
  188.   //      tft.println(time.gettime("s"));
  189.         tft.setTextColor(Yellow, Black);
  190.         tft.setTextSize(2);
  191.         tft.setCursor(72, 156);
  192.         tft.println(time.gettime("d.m.Y  D")); // выводим дату
  193. //        tft.println(time.gettime("d.m.Y")); // выводим дату
  194.         delay(1); // приостанавливаем на 1 мс, чтоб не выводить время несколько раз за 1мс
  195.  // Отображаем данные с датчика BME-280
  196.       bme.read(pres, temp, hum, true, B101);   // unit: B000 = Pa, B001 = hPa, B010 = Hg, B011 = atm, B100 = bar, B101 = torr, B110 = N/m^2, B111 = psi
  197.       tft.setTextColor(Orange, Black);
  198.       tft.setTextSize(2);
  199.       tft.setCursor(82, 60);
  200.       tft.print(temp,1);
  201.       tft.print("'C  ");
  202.       tft.setTextColor(Blue, Black);
  203.       tft.print(hum,1);
  204.       tft.print("% ");
  205.       tft.setCursor(108, 180);
  206.       tft.setTextColor(Olive, Black);
  207.       tft.print(pres,1);
  208.       tft.print(" torr");      
  209.        
  210.        
  211.         } // Конецблока вывода даты/времени
  212.  
  213.  
  214.   digitalWrite(13, HIGH);
  215.   // Считываем координаты нажатия
  216.   TSPoint p = ts.getPoint();
  217.   digitalWrite(13, LOW);
  218.   pinMode(XM, OUTPUT);
  219.   pinMode(YP, OUTPUT);
  220.  
  221.  
  222.   // **************************** ОБРАБОТКА НАЖАТИЯ НА ТАЧСКРИН ********************************************
  223.   // Если сила нажатия попадает в допустимый диапазон то начинаем обработку
  224.  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
  225.   // Переводим координаты в размеры разрешения дисплея
  226.   p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
  227.   p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
  228.  
  229.   // Преобразуем снятые координаты в координаты экрана
  230.   p_X=320-p.y;
  231.   p_Y=p.x;
  232.   // Получаем координаты кнопок в поле 5х4
  233.   P_X = p_X/64;
  234.   P_Y = p_Y/48;
  235.   P_Button = P_X*10+P_Y; // Формируем номер кнопки
  236.  
  237.  // Если попали в зону меню то очищаем экран
  238.  //  if (p_X > tft.width()) {  tft.fillScreen(Black);   }
  239.            
  240.  //-------------- ОБРАБОТКА ВИРТУАЛЬНЫХ КНОПОК --------------
  241.  
  242. switch ( P_Button ) {
  243.     case 0: { // Если нажата кнопка "0"
  244.           if (StN==0) StN=8;
  245.           else --StN;
  246.           StChange (StN);
  247.  
  248.     tft.fillTriangle(40, 8, 40, 38, 8,24 , Cyan);
  249.      delay (300); // ждем 0,3 сек чтобы предотвратить повторное нажатие
  250.     tft.fillTriangle(40, 8, 40, 38, 8,24 , Black);
  251.     tft.drawTriangle(40, 8, 40, 38, 8,24 , Cyan);
  252.             }   break;
  253.     case 40: { // Если нажата кнопка "40"
  254.            if (StN==8) StN=0;
  255.            else ++StN;
  256.            StChange (StN);
  257.     tft.fillTriangle(280, 8, 280, 38, 310, 24, Cyan);
  258.     delay (300); // ждем 0,3 сек чтобы предотвратить повторное нажатие
  259.     tft.fillTriangle(280, 8, 280, 38, 310, 24, Black);    
  260.     tft.drawTriangle(280, 8, 280, 38, 310, 24, Cyan);
  261.              }   break;
  262.  
  263.     case 3: { // Если нажата кнопка "3"
  264.    
  265.      BT3 = !BT3;
  266.      tft.setTextSize(2);
  267.      tft.setCursor(7,214);
  268.           tft.setCursor(8,166);    
  269.      if (BT3) {
  270.           digitalWrite(10, LOW); //Включаем реле (5 Вольт подаем на катушку реле и притягиваем к земле через эмиттер-коллектор транзистора)
  271.           tft.drawRoundRect(1,157,48,34,8, White);   //(ток через катушку  ~ 50 мА)
  272.           tft.setTextColor(White);
  273.           tft.print("FAN");  
  274.           }
  275.      else {
  276.           digitalWrite(10, HIGH); //Отключаем реле
  277.           tft.drawRoundRect(1,157,48,34,8, DarkGrey);
  278.           tft.setTextColor(DarkGrey);
  279.           tft.print("FAN");  
  280.            }
  281.      delay (300); // ждем 0,3 сек чтобы предотвратить повторное нажатие
  282.           }   break;  
  283.    
  284.    
  285.    
  286.    
  287.     case 4: { // Если нажата кнопка "4" включаем/отключаем громкость (MUTE)
  288.    
  289.      radio.setMute(! radio.getMute());
  290.      // Отображаем на экране значение "MUTE"
  291.      tft.setTextSize(2);
  292.      tft.setCursor(8,214);
  293.      if (radio.getMute()) {
  294.  
  295.           tft.drawRoundRect(1,205,48,34,8, Yellow);
  296.           tft.setTextColor(Yellow, Black);
  297.           tft.print("MUT");  
  298.           }
  299.      else {
  300.           tft.drawRoundRect(1,205,48,34,8, DarkGrey);
  301.           tft.setTextColor(DarkGrey, Black);
  302.           tft.print("MUT");            }
  303.      delay (300); // ждем 0,3 сек чтобы предотвратить повторное нажатие
  304.           }   break;
  305.  
  306.   // --- Область ползунка громкости занимает 3 нижних кнопки ---
  307.     case 14: { // Если нажата кнопка "13"
  308.     Volume (p_X);  
  309.             }   break;
  310.     case 24: { // Если нажата кнопка "23"
  311.     Volume (p_X);  
  312.             }   break;
  313.     case 34: { // Если нажата кнопка "33"
  314.     Volume (p_X);  
  315.             }   break;
  316.  
  317.    // --- Область ползунка громкости ---
  318.     case 44: { // Если нажата кнопка "43"
  319.      radio.setBassBoost(! radio.getBassBoost());
  320.     // Отображаем на экране значение "BASS"  
  321.      tft.setTextSize(2);
  322.      tft.setCursor(276,214);
  323.           if (radio.getBassBoost()) {
  324.            tft.drawRoundRect(271,205,48,34,8, Green);
  325.            tft.setTextColor(Green, Black);
  326.            tft.print("BAS");
  327.           }
  328.      else {
  329.           tft.drawRoundRect(271,205,48,34,8, DarkGrey);
  330.           tft.setTextColor(DarkGrey, Black);
  331.           tft.print("BAS");  
  332.           }            
  333.    delay (300); // ждем 0,3 сек чтобы успеть убрать палец с кнопки
  334.    
  335.             }   break;
  336.  
  337.                 } //-------------- КОНЕЦ ОБРАБОТКИ ВИРТУАЛЬНЫХ КНОПОК --------------
  338.  
  339.  
  340.  
  341.  
  342.   } // **************************** КОНЕЦ ОБРАБОТКИ НАЖАТИЯ НА ТАЧСКРИН ********************************************
  343.  
  344.  }
  345.  
  346.  
  347.  
  348.   void Volume (int Vol_p_X) //функция установки значения громкости и отображения информации о громкости на экране
  349.    {
  350.  //        tft.setContrast(12);
  351.          
  352.          Vol = radio.getVolume();
  353.          if (((Vol_p_X-64)/12) != Vol )
  354.          {  
  355.          radio.setVolume((Vol_p_X-64)/12);  
  356.          Vol = ((Vol_p_X-64)/12);
  357.          // Отображаем на экране значение громкости
  358.          tft.setTextSize(2);
  359.          tft.setTextColor(Green, Black);
  360.          tft.setCursor(71,210);
  361.          tft.print(Vol);
  362.          if (Vol<10)tft.print(" ");
  363.          tft.fillTriangle(64, 238, 255, 238, 255, 206, Black);  
  364.          tft.drawTriangle(64, 238, 255, 238, 255, 206, Green);
  365.          tft.fillTriangle( 64, 238, 63+((Vol+1)*12), 238,63+((Vol+1)*12), 238-((Vol+1)*2), Green);
  366.          }
  367.    }
  368.  
  369.   void StChange (int St) //функция смены радиостанции и отображения информации о новой станции на экране
  370.    {
  371.       radio.setBandFrequency(RADIO_BAND_FM,StFreq[St]); // то устанавливаем частоту из массива станций
  372.       delay(50);  // даём время радиомодулю сменить станцию
  373.       char s[12];
  374.       tft.setTextColor(Cyan, Black);
  375.       tft.setTextSize(4);
  376.       tft.setCursor(68, 10);
  377.       tft.print((St+1));
  378.       tft.setTextSize(2);
  379.       tft.setTextColor(Cyan, Black);
  380.       tft.setCursor(120, 6);
  381.       radio.formatFrequency(s, sizeof(s));
  382.       tft.println(s);
  383.       tft.setTextSize(2);
  384.       tft.setTextColor(Cyan, Black);
  385.       tft.setCursor(106, 28);
  386.       tft.println((StName[St]));
  387.   //  tft.println(utf8rus(StName[St]));
  388.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement