espd

ESP32_EMU_BT_V.3.01

May 17th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. #include <Arduino.h>
  2. #define LV_COLOR_16_SWAP 1
  3. #include <lvgl.h>
  4. #include <TFT_eSPI.h>
  5. #include "CST820.h"
  6. #include "BluetoothSerial.h"
  7. #include <string>
  8. #include <stdexcept>
  9. using namespace std;
  10.  
  11. //#define USE_NAME
  12. const char *pin = "1234";
  13. String myBtName = "ESP32-BT-Master";
  14.  
  15. #if !defined(CONFIG_BT_SPP_ENABLED)
  16. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  17. #endif
  18.  
  19. BluetoothSerial SerialBT;
  20.  
  21. #ifdef USE_NAME
  22. String slaveName = "EMUCANBT_SPP";
  23. #else
  24. uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };
  25. #endif
  26.  
  27. const int backLightPin = 27;
  28. const int buzzerPin = 22;
  29. bool buzzerOn = false;
  30. bool btIconSts = false;
  31. static lv_style_t style_bt;
  32. static bool style_initialized = false;
  33.  
  34. int rpm;
  35. int spd;
  36. float afr;
  37. float mapR;
  38. float boost;
  39. int tps;
  40. int clt;
  41. int ign;
  42. int inj;
  43. float bat;
  44. int cel;
  45.  
  46. unsigned long previousMillis = 0;
  47. const unsigned long reconnectInterval = 5000;
  48.  
  49. /* Screen resolution */
  50. static const uint16_t screenWidth = 320;  // width after rotation 1
  51. static const uint16_t screenHeight = 240; // height after rotation 1
  52. static const uint16_t buf_height = 80;    // smaller buffer height to save RAM
  53.  
  54. /* Touch screen I2C pins */
  55. #define I2C_SDA 33
  56. #define I2C_SCL 32
  57. #define TP_RST 25
  58. #define TP_INT 21
  59.  
  60. static lv_disp_draw_buf_t draw_buf;
  61. static lv_color_t *buf1;
  62. static lv_color_t *buf2;
  63.  
  64. TFT_eSPI tft = TFT_eSPI();                      /* TFT instance */
  65. CST820 touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT); /* Touch instance */
  66.  
  67. LV_FONT_DECLARE(lv_font_montserrat_14);
  68. LV_FONT_DECLARE(lv_font_montserrat_28);
  69.  
  70. lv_obj_t *bt_icon_label;
  71.  
  72. #if LV_USE_LOG != 0
  73. void my_print(const char *buf)
  74. {
  75.     Serial.printf(buf);
  76.     Serial.flush();
  77. }
  78. #endif
  79.  
  80. /* Display flush */
  81. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
  82. {
  83.     uint32_t w = (area->x2 - area->x1 + 1);
  84.     uint32_t h = (area->y2 - area->y1 + 1);
  85.  
  86.     tft.pushImageDMA(area->x1, area->y1, w, h, (uint16_t *)color_p);
  87.     lv_disp_flush_ready(disp);
  88. }
  89.  
  90. /* Read touchpad */
  91. void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
  92. {
  93.     bool touched;
  94.     uint8_t gesture;
  95.     uint16_t rawX, rawY;
  96.  
  97.     touched = touch.getTouch(&rawX, &rawY, &gesture);
  98.  
  99.     if (!touched)
  100.     {
  101.         data->state = LV_INDEV_STATE_REL;
  102.     }
  103.     else
  104.     {
  105.         data->state = LV_INDEV_STATE_PR;
  106.  
  107.         // Rotate touch for tft.setRotation(1)
  108.         data->point.x = rawY;
  109.         data->point.y = screenHeight - 1 - rawX;
  110.     }
  111. }
  112.  
  113. void create_tabview_with_tables()
  114. {
  115.     lv_obj_t *tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 40);
  116.  
  117.     // Set tabview background to black
  118.     lv_obj_set_style_bg_color(tabview, lv_color_black(), 0);
  119.  
  120.     lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
  121.  
  122.     lv_obj_clear_flag(tabview, LV_OBJ_FLAG_SCROLLABLE);
  123.  
  124.     lv_obj_set_style_text_font(tabview, &lv_font_montserrat_14, LV_PART_ITEMS);
  125.  
  126.     // Create a reusable style for tables (black bg, white border)
  127.     static lv_style_t style_table;
  128.     lv_style_init(&style_table);
  129.     lv_style_set_bg_color(&style_table, lv_color_black());
  130.     lv_style_set_border_color(&style_table, lv_color_white());
  131.     lv_style_set_border_width(&style_table, 2);
  132.     lv_style_set_border_side(&style_table, LV_BORDER_SIDE_FULL);
  133.     lv_style_set_text_color(&style_table, lv_color_white());
  134.    
  135.     // Also style the tab buttons to have white text on black
  136.     lv_obj_t *tab_btns = lv_tabview_get_tab_btns(tabview);
  137.     lv_obj_set_style_text_color(tab_btns, lv_color_white(), 0);
  138.     lv_obj_set_style_bg_color(tab_btns, lv_color_black(), 0);
  139.  
  140.     // Add tabs
  141.     lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
  142.     lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
  143.     lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Tab 3");
  144.  
  145.     // Function to create and style a table in a tab
  146.     auto create_table = [&](lv_obj_t *parent) {
  147.         lv_obj_t *table = lv_table_create(parent);
  148.         lv_obj_add_style(table, &style_table, 0);
  149.         lv_obj_set_size(table, 320, 200);
  150.         lv_obj_align(table, LV_ALIGN_CENTER, 0, 0);
  151.         lv_table_set_col_cnt(table, 4);
  152.         lv_table_set_row_cnt(table, 4);
  153.         return table;
  154.     };
  155.  
  156.     // Create tables
  157.     lv_obj_t *table1 = create_table(tab1);
  158.     lv_table_set_cell_value(table1, 0, 0, "Item");
  159.     lv_table_set_cell_value(table1, 0, 1, "Value");
  160.     lv_table_set_cell_value(table1, 1, 0, "Speed");
  161.     lv_table_set_cell_value(table1, 1, 1, "60 km/h");
  162.     lv_table_set_cell_value(table1, 2, 0, "Temp");
  163.     lv_table_set_cell_value(table1, 2, 1, "22 C");
  164.  
  165.     lv_obj_t *table2 = create_table(tab2);
  166.     lv_table_set_cell_value(table2, 0, 0, "Item");
  167.     lv_table_set_cell_value(table2, 0, 1, "Value");
  168.     lv_table_set_cell_value(table2, 1, 0, "RPM");
  169.     lv_table_set_cell_value(table2, 1, 1, "3000");
  170.     lv_table_set_cell_value(table2, 2, 0, "Fuel");
  171.     lv_table_set_cell_value(table2, 2, 1, "Full");
  172.  
  173.     lv_obj_t *table3 = create_table(tab3);
  174.     lv_table_set_cell_value(table3, 0, 0, "Item");
  175.     lv_table_set_cell_value(table3, 0, 1, "Value");
  176.     lv_table_set_cell_value(table3, 1, 0, "Voltage");
  177.     lv_table_set_cell_value(table3, 1, 1, "12.5V");
  178.     lv_table_set_cell_value(table3, 2, 0, "Status");
  179.     lv_table_set_cell_value(table3, 2, 1, "OK");
  180.  
  181.     lv_obj_set_style_text_font(table1, &lv_font_montserrat_14, LV_PART_ITEMS);
  182. }
  183.  
  184.  
  185. void setup() {
  186.     tft.init();
  187.     pinMode(backLightPin, OUTPUT);
  188.     digitalWrite(backLightPin, LOW);
  189.     uint16_t darkGray = ((30 & 0xF8) << 8) | ((30 & 0xFC) << 3) | (30 >> 3);
  190.     tft.fillScreen(darkGray);
  191.     tft.setRotation(1);
  192.  
  193.    // tft.begin();
  194.    // tft.setRotation(1);
  195.     tft.initDMA();
  196.     touch.begin();
  197.  
  198.     Serial.begin(1000000);
  199.  
  200.     lv_init();
  201.    
  202.     buf1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * buf_height, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  203.     buf2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * buf_height, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  204.  
  205.     lv_disp_draw_buf_init(&draw_buf, buf1, buf2, screenWidth * buf_height);
  206.  
  207.     static lv_disp_drv_t disp_drv;
  208.     lv_disp_drv_init(&disp_drv);
  209.     disp_drv.hor_res = screenWidth;
  210.     disp_drv.ver_res = screenHeight;
  211.     disp_drv.flush_cb = my_disp_flush;
  212.     disp_drv.draw_buf = &draw_buf;
  213.     lv_disp_drv_register(&disp_drv);
  214.  
  215.     static lv_indev_drv_t indev_drv;
  216.     lv_indev_drv_init(&indev_drv);
  217.     indev_drv.type = LV_INDEV_TYPE_POINTER;
  218.     indev_drv.read_cb = my_touchpad_read;
  219.     lv_indev_drv_register(&indev_drv);
  220.  
  221.     // Create the tab view layout
  222.     create_tabview_with_tables();
  223.  
  224.     digitalWrite(27, HIGH);
  225.     Serial.println("Setup done");
  226.     tft.startWrite();
  227. }
  228.  
  229. void loop()
  230. {
  231.     lv_timer_handler();
  232.     delay(5);
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment