espd

ESP32_EMU_BT_V.3.07

May 25th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.50 KB | None | 0 0
  1. #include <Arduino.h>
  2. #define LV_COLOR_16_SWAP 0
  3. #include <lvgl.h>
  4. #include <TFT_eSPI.h>
  5. #include "BluetoothSerial.h"
  6. #include <string>
  7. #include <stdexcept>
  8. #include "CST820.h"
  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. #define I2C_SDA 33
  47. #define I2C_SCL 32
  48. #define TP_RST 25
  49. #define TP_INT 21
  50.  
  51. static const uint16_t screenWidth = 320;
  52. static const uint16_t screenHeight = 240;
  53.  
  54. TFT_eSPI tft = TFT_eSPI();
  55. CST820 touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT);
  56.  
  57. unsigned long previousMillis = 0;
  58. const unsigned long reconnectInterval = 5000;
  59.  
  60. LV_FONT_DECLARE(lv_font_montserrat_18);
  61. LV_FONT_DECLARE(lv_font_montserrat_22);
  62. LV_FONT_DECLARE(lv_font_montserrat_28);
  63.  
  64. lv_obj_t *bt_icon_label;
  65.  
  66. // Display & LVGL setup
  67. static lv_disp_draw_buf_t draw_buf;
  68. static lv_color_t buf[LV_HOR_RES_MAX * 10];
  69. lv_obj_t *table;
  70.  
  71. #define NUM_TABS 3
  72. lv_obj_t *tabview;
  73. lv_obj_t *tabs[NUM_TABS];
  74. const char *tab_titles[NUM_TABS] = {"1", "2", "3"};
  75.  
  76. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  77.   uint16_t w = area->x2 - area->x1 + 1;
  78.   uint16_t h = area->y2 - area->y1 + 1;
  79.  
  80.   tft.startWrite();
  81.   tft.setAddrWindow(area->x1, area->y1, w, h);
  82.   tft.pushColors((uint16_t *)&color_p->full, w * h, true);
  83.   tft.endWrite();
  84.  
  85.   lv_disp_flush_ready(disp);
  86. }
  87.  
  88. void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
  89.   uint16_t rawX, rawY;
  90.   uint8_t gesture;
  91.   bool touched = touch.getTouch(&rawX, &rawY, &gesture);
  92.   if (!touched) {
  93.     data->state = LV_INDEV_STATE_REL;
  94.   } else {
  95.     data->state = LV_INDEV_STATE_PR;
  96.  
  97.     uint16_t tmp = rawX;
  98.     rawX = rawY;
  99.     rawY = 240 - tmp - 1;
  100.  
  101.     data->point.x = rawX;
  102.     data->point.y = rawY;
  103.   }
  104. }
  105.  
  106. // Initialize LVGL Table
  107. void create_table() {
  108.   tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 0);
  109.   lv_obj_t *tab_btns = lv_tabview_get_tab_btns(tabview);
  110.   lv_obj_add_flag(tab_btns, LV_OBJ_FLAG_HIDDEN);
  111.   lv_obj_set_style_text_opa(tabview, LV_OPA_COVER, 0);
  112.   lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
  113.  
  114.   lv_obj_clear_flag(tabview, LV_OBJ_FLAG_SCROLLABLE);
  115.   lv_obj_set_scrollbar_mode(tabview, LV_SCROLLBAR_MODE_OFF);
  116.  
  117.   static lv_style_t style_cell0;
  118.   lv_style_init(&style_cell0);
  119.   lv_style_set_pad_top(&style_cell0, 10);
  120.   lv_style_set_pad_bottom(&style_cell0, 10);
  121.   lv_style_set_pad_left(&style_cell0, 6);
  122.   lv_style_set_pad_right(&style_cell0, 6);
  123.  
  124.   static lv_style_t style_cell1;
  125.   lv_style_init(&style_cell1);
  126.   lv_style_set_pad_top(&style_cell1, 19);
  127.   lv_style_set_pad_bottom(&style_cell1, 19);
  128.   lv_style_set_pad_left(&style_cell1, 4);
  129.   lv_style_set_pad_right(&style_cell1, 1);
  130.  
  131.   for(int i = 0; i < NUM_TABS; i++) {
  132.     tabs[i] = lv_tabview_add_tab(tabview, tab_titles[i]);
  133.     table = lv_table_create(tabs[i]);
  134.  
  135.     lv_obj_clear_flag(tabs[i], LV_OBJ_FLAG_SCROLLABLE);
  136.     lv_obj_set_scrollbar_mode(tabs[i], LV_SCROLLBAR_MODE_OFF);
  137.    
  138.     lv_obj_clear_flag(table, LV_OBJ_FLAG_SCROLLABLE);
  139.     lv_obj_set_scrollbar_mode(table, LV_SCROLLBAR_MODE_OFF);
  140.  
  141.     lv_obj_align(table, LV_ALIGN_CENTER, -1, 0);
  142.     lv_obj_set_style_text_opa(table, LV_OPA_COVER, 0);
  143.     lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(30, 30, 30), LV_PART_MAIN);
  144.     lv_obj_set_style_text_color(table, lv_color_white(), LV_PART_ITEMS);
  145.     lv_obj_set_style_bg_color(table, lv_color_make(30, 30, 30), LV_PART_MAIN);
  146.  
  147.     lv_table_set_col_width(table, 0, 60);
  148.     lv_table_set_col_width(table, 1, 100);
  149.     lv_table_set_col_width(table, 2, 60);
  150.     lv_table_set_col_width(table, 3, 100);
  151.  
  152.     lv_obj_set_style_border_width(table, 1, LV_PART_ITEMS);
  153.     lv_obj_set_style_border_color(table, lv_color_white(), LV_PART_ITEMS);
  154.     lv_obj_set_style_border_side(table, LV_BORDER_SIDE_FULL, LV_PART_ITEMS);
  155.  
  156.     if(i == 0) {
  157.       lv_obj_set_style_text_font(table, &lv_font_montserrat_18, LV_PART_ITEMS);
  158.       lv_obj_add_style(table, &style_cell0, LV_PART_ITEMS);
  159.       lv_table_set_col_cnt(table, 4);
  160.       lv_table_set_row_cnt(table, 6);
  161.    
  162.       lv_table_add_cell_ctrl(table, 5, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  163.       lv_table_add_cell_ctrl(table, 5, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  164.       lv_table_add_cell_ctrl(table, 5, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  165.    
  166.       lv_table_set_cell_value(table, 0, 0, "RPM");
  167.       lv_table_set_cell_value(table, 0, 2, "SPD");
  168.       lv_table_set_cell_value(table, 1, 0, "AFR");
  169.       lv_table_set_cell_value(table, 1, 2, "CLT");
  170.       lv_table_set_cell_value(table, 2, 0, "TPS");
  171.       lv_table_set_cell_value(table, 2, 2, "BAT");
  172.       lv_table_set_cell_value(table, 3, 0, "MAP");
  173.       lv_table_set_cell_value(table, 3, 2, "BST");
  174.       lv_table_set_cell_value(table, 4, 0, "INJ");
  175.       lv_table_set_cell_value(table, 4, 2, "IGN");
  176.       lv_table_set_cell_value(table, 5, 0, "CEL");
  177.    
  178.       lv_obj_add_event_cb(table, my_table_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
  179.       lv_obj_add_event_cb(table, table_event_cb_bg, LV_EVENT_DRAW_PART_BEGIN, NULL);
  180.     } else if (i == 1) {
  181.       lv_obj_set_style_text_font(table, &lv_font_montserrat_22, LV_PART_ITEMS);
  182.       lv_obj_add_style(table, &style_cell1, LV_PART_ITEMS);
  183.       lv_table_set_col_cnt(table, 4);
  184.       lv_table_set_row_cnt(table, 4);
  185.    
  186.       lv_table_add_cell_ctrl(table, 3, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  187.       lv_table_add_cell_ctrl(table, 3, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  188.       lv_table_add_cell_ctrl(table, 3, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  189.    
  190.       lv_table_set_cell_value(table, 0, 0, "RPM");
  191.       lv_table_set_cell_value(table, 0, 2, "SPD");
  192.       lv_table_set_cell_value(table, 1, 0, "AFR");
  193.       lv_table_set_cell_value(table, 1, 2, "BST");
  194.       lv_table_set_cell_value(table, 2, 0, "TPS");
  195.       lv_table_set_cell_value(table, 2, 2, "BAT");
  196.       lv_table_set_cell_value(table, 3, 0, "CEL");
  197.    
  198.       lv_obj_add_event_cb(table, my_table_event_cb2, LV_EVENT_DRAW_PART_BEGIN, NULL);
  199.       lv_obj_add_event_cb(table, table_event_cb_bg2, LV_EVENT_DRAW_PART_BEGIN, NULL);
  200.     } else if (i == 2) {
  201.       lv_obj_set_style_text_font(table, &lv_font_montserrat_22, LV_PART_ITEMS);
  202.       lv_obj_add_style(table, &style_cell1, LV_PART_ITEMS);
  203.       lv_table_set_col_cnt(table, 4);
  204.       lv_table_set_row_cnt(table, 4);
  205.    
  206.       lv_table_add_cell_ctrl(table, 3, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  207.       lv_table_add_cell_ctrl(table, 3, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  208.       lv_table_add_cell_ctrl(table, 3, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  209.    
  210.       lv_table_set_cell_value(table, 0, 0, "RPM");
  211.       lv_table_set_cell_value(table, 0, 2, "SPD");
  212.       lv_table_set_cell_value(table, 1, 0, "AFR");
  213.       lv_table_set_cell_value(table, 1, 2, "CLT");
  214.       lv_table_set_cell_value(table, 2, 0, "INJ");
  215.       lv_table_set_cell_value(table, 2, 2, "IGN");
  216.       lv_table_set_cell_value(table, 3, 0, "CEL");
  217.    
  218.       lv_obj_add_event_cb(table, my_table_event_cb2, LV_EVENT_DRAW_PART_BEGIN, NULL);
  219.       lv_obj_add_event_cb(table, table_event_cb_bg3, LV_EVENT_DRAW_PART_BEGIN, NULL);
  220.     }
  221.   }
  222.   create_bt_icon();
  223.   lv_timer_handler();
  224. }
  225.  
  226. void setup() {
  227.   tft.init();
  228.   pinMode(backLightPin, OUTPUT);
  229.   digitalWrite(backLightPin, LOW);
  230.   uint16_t darkGray = ((30 & 0xF8) << 8) | ((30 & 0xFC) << 3) | (30 >> 3);
  231.   tft.fillScreen(darkGray);
  232.   tft.setRotation(1);
  233.   Serial.begin(1000000);
  234.  
  235.   pinMode(buzzerPin, OUTPUT);
  236.  
  237.   // Initialize LVGL
  238.   lv_init();
  239.   lv_refr_now(NULL);
  240.   lv_disp_draw_buf_init(&draw_buf, buf, NULL, LV_HOR_RES_MAX * 10);
  241.  
  242.   static lv_disp_drv_t disp_drv;
  243.   lv_disp_drv_init(&disp_drv);
  244.   disp_drv.hor_res = screenWidth;
  245.   disp_drv.ver_res = screenHeight;
  246.   disp_drv.flush_cb = my_disp_flush;
  247.   disp_drv.draw_buf = &draw_buf;
  248.   lv_disp_drv_register(&disp_drv);
  249.  
  250.   touch.begin();
  251.  
  252.   static lv_indev_drv_t indev_drv;
  253.   lv_indev_drv_init(&indev_drv);
  254.   indev_drv.type = LV_INDEV_TYPE_POINTER;
  255.   indev_drv.read_cb = my_touchpad_read;
  256.   lv_indev_drv_register(&indev_drv);
  257.  
  258.   SerialBT.begin(myBtName, true);
  259.  
  260.   create_table();
  261.   digitalWrite(backLightPin, HIGH);
  262.   connectToBt();
  263.   lv_timer_handler();
  264. }
  265.  
  266. void connectToBt() {
  267.   bool connected;
  268.   #ifndef USE_NAME
  269.     SerialBT.setPin(pin);
  270.   #endif
  271.  
  272.   #ifdef USE_NAME
  273.     connected = SerialBT.connect(slaveName);
  274.   #else
  275.     connected = SerialBT.connect(address);
  276.   #endif
  277.  
  278.   if (connected) {
  279.     Serial.println("Connected Successfully!");
  280.   } else {
  281.     Serial.println("Initial connect failed. Will retry in loop...");
  282.   }
  283.   update_bt_icon_color(SerialBT.hasClient(), false);
  284.   lv_timer_handler();
  285. }
  286.  
  287. void loop() {
  288.   uint8_t frame[5];
  289.   uint8_t channel;
  290.   uint16_t value;
  291.   int chData;
  292.   unsigned long currentMillis = millis();
  293.  
  294. /*  if (!SerialBT.connected()) {
  295.     // Attempt reconnection every 5 seconds
  296.     if (currentMillis - previousMillis >= reconnectInterval) {
  297.       previousMillis = currentMillis;
  298.       connectToBt();
  299.     }
  300.   }*/
  301.  
  302.   update_bt_icon_color(SerialBT.hasClient(), false);
  303.  
  304.   // Wait until at least 5 bytes are available
  305.   while (SerialBT.available() >= 5) {
  306.     SerialBT.readBytes(frame, 5);  // Read exactly 5 bytes
  307.  
  308.     channel = frame[0];
  309.     value = (frame[2] << 8) | frame[3];
  310.     chData = static_cast<int>(channel);
  311.     if (chData == 1) {
  312.       rpm = static_cast<int>(value);
  313.       lv_table_set_cell_value(table, 0, 1, String(rpm).c_str());
  314.     } else if (chData == 28) {
  315.       spd = (static_cast<int>(value) / 2.8);
  316.       lv_table_set_cell_value(table, 0, 3, (String(spd) + " KM/H").c_str());
  317.     } else if (chData == 12) {
  318.       afr = (static_cast<float>(value) / 10);
  319.       lv_table_set_cell_value(table, 1, 1, String(afr).c_str());
  320.     } else if (chData == 2) {
  321.       mapR = (static_cast<float>(value) / 100);
  322.       boost = (mapR - 1.0132f);
  323.       lv_table_set_cell_value(table, 3, 1, (String(mapR) + " BAR").c_str());
  324.       lv_table_set_cell_value(table, 3, 3, (String(boost) + " BAR").c_str());
  325.     } else if (chData == 3) {
  326.       tps = static_cast<int>(value);
  327.       lv_table_set_cell_value(table, 2, 1, (String(tps) + " %").c_str());
  328.     } else if (chData == 24) {
  329.       clt = static_cast<int>(value);
  330.       lv_table_set_cell_value(table, 1, 3, (String(clt) + " °C").c_str());
  331.     } else if (chData == 6) {
  332.       ign = (static_cast<int>(value) / 2);
  333.       lv_table_set_cell_value(table, 4, 3, (String(ign) + " °").c_str());
  334.     } else if (chData == 19) {
  335.       inj = (static_cast<int>(value) / 2);
  336.       lv_table_set_cell_value(table, 4, 1, (String(inj) + " %").c_str());
  337.     } else if (chData == 5) {
  338.       bat = (static_cast<float>(value) / 37);
  339.       lv_table_set_cell_value(table, 2, 3, (String(bat) + " V").c_str());
  340.     } else if (chData == 255) {
  341.       cel = decodeCheckEngine(value);
  342.     }
  343.   }
  344.  
  345.   /*if (cel > 0 || clt > 105 || rpm > 7000 || boost > 1.10 || (bat < 12.00 && bat > 1.00)) {
  346.     digitalWrite(buzzerPin, HIGH);  // Buzzer ON
  347.   } else {
  348.     digitalWrite(buzzerPin, LOW);   // Buzzer OFF
  349.   }*/
  350.  
  351.   buzzerOn = (cel > 0 || clt > 105 || rpm > 7000 || boost > 1.10 || (bat < 12.00 && bat > 1.00));
  352.   digitalWrite(buzzerPin, (millis() % 600 < 300) && buzzerOn);
  353.  
  354.   lv_obj_invalidate(table);
  355.   lv_timer_handler();
  356. }
  357.  
  358. int decodeCheckEngine(uint16_t value) {
  359.   int cel_codes = 0; string cel_names = "";
  360.   if (value == 0) {
  361.     return 0;
  362.   }
  363.   else {
  364.     if (value & (1 << 0)) {
  365.       cel_codes++;  // Bit 0
  366.       cel_names = "CLT ";
  367.     }
  368.     if (value & (1 << 1)) {
  369.       //cel_codes++;  // Bit 1
  370.       //cel_names += "IAT ";
  371.     }
  372.     if (value & (1 << 2)) {
  373.       cel_codes++;  // Bit 2
  374.       cel_names += "MAP ";
  375.     }
  376.     if (value & (1 << 3)) {
  377.       cel_codes++;  // Bit 3
  378.       cel_names += "WBO ";
  379.     }
  380.     if (value & (1 << 8)) {
  381.       cel_codes++;  // Bit 8
  382.       cel_names += "FF SENSOR ";
  383.     }
  384.     if (value & (1 << 9)) {
  385.       cel_codes++;  // Bit 9
  386.       cel_names += "DBW ";
  387.     }
  388.     if (value & (1 << 10)) {
  389.       cel_codes++;  // Bit 10
  390.       cel_names += "FPR ";
  391.     }
  392.  
  393.     lv_table_set_cell_value(table, 5, 1, cel_names.c_str());
  394.     return cel_codes;
  395.   }
  396. }
  397.  
  398. // Cell alignment fix
  399. void my_table_event_cb(lv_event_t * e) {
  400.   lv_obj_t * table = lv_event_get_target(e);
  401.   lv_obj_draw_part_dsc_t * dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  402.  
  403.   if (dsc->part == LV_PART_ITEMS) {
  404.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  405.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  406.  
  407.     dsc->label_dsc->align = LV_TEXT_ALIGN_LEFT;
  408.     if ((row == 0 && col == 1) || (row == 0 && col == 3) || (row == 1 && col == 1) || (row == 1 && col == 3) || (row == 2 && col == 1) || (row == 2 && col == 3) || (row == 3 && col == 1) || (row == 3 && col == 3) ||
  409.         (row == 4 && col == 1) || (row == 4 && col == 3)) {
  410.       dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;
  411.     }
  412.     if (row == 5 && col == 1) {
  413.       dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
  414.     }
  415.   }
  416. }
  417.  
  418. // Cell alignment fix
  419. void my_table_event_cb2(lv_event_t * e) {
  420.   lv_obj_t * table = lv_event_get_target(e);
  421.   lv_obj_draw_part_dsc_t * dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  422.  
  423.   if (dsc->part == LV_PART_ITEMS) {
  424.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  425.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  426.  
  427.     dsc->label_dsc->align = LV_TEXT_ALIGN_LEFT;
  428.     if ((row == 0 && col == 1) || (row == 0 && col == 3) || (row == 1 && col == 1) || (row == 1 && col == 3) || (row == 2 && col == 1) || (row == 2 && col == 3)) {
  429.       dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;
  430.     }
  431.     if (row == 3 && col == 1) {
  432.       dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
  433.     }
  434.   }
  435. }
  436.  
  437. static void table_event_cb_bg(lv_event_t *e) {
  438.   lv_obj_t *table = lv_event_get_target(e);
  439.   lv_obj_draw_part_dsc_t *dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  440.  
  441.   // Ensure dsc and rect_dsc are valid
  442.   if (!dsc || !dsc->rect_dsc) return;
  443.  
  444.   // Only modify table cell backgrounds
  445.   if (dsc->part == LV_PART_ITEMS) {
  446.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  447.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  448.  
  449.     const char *value_str = lv_table_get_cell_value(table, row, col);
  450.  
  451.     // Check if value_str is null or empty before conversion
  452.     float value = 0.0f;  // Default value
  453.     if (value_str != nullptr && value_str[0] != '\0') {
  454.       try {
  455.         value = std::stof(value_str);  // Convert string to float safely
  456.       } catch (...) {
  457.         value = 0.0f;  // Handle invalid conversions
  458.       }
  459.     }
  460.  
  461.     // Default cell color
  462.     lv_color_t bg_color = lv_color_make(30, 30, 30);
  463.     lv_color_t text_color = lv_color_white();
  464.  
  465.     if (row == 0 && col == 1 && value > 7000.00) {
  466.       bg_color = lv_color_make(0, 0, 255);
  467.       text_color = lv_color_white();
  468.     }
  469.     if (row == 1 && col == 3 && value > 100.00) {
  470.       bg_color = lv_color_make(0, 0, 255);
  471.       text_color = lv_color_white();
  472.     }
  473.     if (row == 1 && col == 3 && value < 55.00 && value > 01.00) {
  474.       bg_color = lv_color_make(0, 255, 255);
  475.       text_color = lv_color_black();
  476.     }
  477.     if (row == 2 && col == 3 && value < 12.00 && value > 01.00) {
  478.       bg_color = lv_color_make(0, 0, 255);
  479.       text_color = lv_color_white();
  480.     }
  481.     if (row == 3 && col == 3 && value > 1.10) {
  482.       bg_color = lv_color_make(0, 0, 255);
  483.       text_color = lv_color_white();
  484.     }
  485.     if (row == 5 && col == 1 && value_str != nullptr && value_str[0] != '\0') {
  486.       bg_color = lv_color_make(0, 0, 255);
  487.       text_color = lv_color_white();
  488.     }
  489.  
  490.     // Apply background color to the cell
  491.     dsc->rect_dsc->bg_color = bg_color;
  492.     dsc->rect_dsc->bg_opa = LV_OPA_COVER;
  493.     dsc->label_dsc->color = text_color;
  494.   }
  495. }
  496.  
  497. static void table_event_cb_bg2(lv_event_t *e) {
  498.   lv_obj_t *table = lv_event_get_target(e);
  499.   lv_obj_draw_part_dsc_t *dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  500.  
  501.   // Ensure dsc and rect_dsc are valid
  502.   if (!dsc || !dsc->rect_dsc) return;
  503.  
  504.   // Only modify table cell backgrounds
  505.   if (dsc->part == LV_PART_ITEMS) {
  506.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  507.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  508.  
  509.     const char *value_str = lv_table_get_cell_value(table, row, col);
  510.  
  511.     // Check if value_str is null or empty before conversion
  512.     float value = 0.0f;  // Default value
  513.     if (value_str != nullptr && value_str[0] != '\0') {
  514.       try {
  515.         value = std::stof(value_str);  // Convert string to float safely
  516.       } catch (...) {
  517.         value = 0.0f;  // Handle invalid conversions
  518.       }
  519.     }
  520.  
  521.     // Default cell color
  522.     lv_color_t bg_color = lv_color_make(30, 30, 30);
  523.     lv_color_t text_color = lv_color_white();
  524.  
  525.     if (row == 0 && col == 1 && value > 7000.00) {
  526.       bg_color = lv_color_make(0, 0, 255);
  527.       text_color = lv_color_white();
  528.     }
  529.     if (row == 2 && col == 3 && value < 12.00 && value > 01.00) {
  530.       bg_color = lv_color_make(0, 0, 255);
  531.       text_color = lv_color_white();
  532.     }
  533.     if (row == 1 && col == 3 && value > 1.10) {
  534.       bg_color = lv_color_make(0, 0, 255);
  535.       text_color = lv_color_white();
  536.     }
  537.     if (row == 5 && col == 1 && value_str != nullptr && value_str[0] != '\0') {
  538.       bg_color = lv_color_make(0, 0, 255);
  539.       text_color = lv_color_white();
  540.     }
  541.  
  542.     // Apply background color to the cell
  543.     dsc->rect_dsc->bg_color = bg_color;
  544.     dsc->rect_dsc->bg_opa = LV_OPA_COVER;
  545.     dsc->label_dsc->color = text_color;
  546.   }
  547. }
  548.  
  549. static void table_event_cb_bg3(lv_event_t *e) {
  550.   lv_obj_t *table = lv_event_get_target(e);
  551.   lv_obj_draw_part_dsc_t *dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  552.  
  553.   // Ensure dsc and rect_dsc are valid
  554.   if (!dsc || !dsc->rect_dsc) return;
  555.  
  556.   // Only modify table cell backgrounds
  557.   if (dsc->part == LV_PART_ITEMS) {
  558.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  559.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  560.  
  561.     const char *value_str = lv_table_get_cell_value(table, row, col);
  562.  
  563.     // Check if value_str is null or empty before conversion
  564.     float value = 0.0f;  // Default value
  565.     if (value_str != nullptr && value_str[0] != '\0') {
  566.       try {
  567.         value = std::stof(value_str);  // Convert string to float safely
  568.       } catch (...) {
  569.         value = 0.0f;  // Handle invalid conversions
  570.       }
  571.     }
  572.  
  573.     // Default cell color
  574.     lv_color_t bg_color = lv_color_make(30, 30, 30);
  575.     lv_color_t text_color = lv_color_white();
  576.  
  577.     if (row == 0 && col == 1 && value > 7000.00) {
  578.       bg_color = lv_color_make(0, 0, 255);
  579.       text_color = lv_color_white();
  580.     }
  581.     if (row == 1 && col == 3 && value > 100.00) {
  582.       bg_color = lv_color_make(0, 0, 255);
  583.       text_color = lv_color_white();
  584.     }
  585.     if (row == 1 && col == 3 && value < 55.00 && value > 01.00) {
  586.       bg_color = lv_color_make(0, 255, 255);
  587.       text_color = lv_color_black();
  588.     }
  589.     if (row == 5 && col == 1 && value_str != nullptr && value_str[0] != '\0') {
  590.       bg_color = lv_color_make(0, 0, 255);
  591.       text_color = lv_color_white();
  592.     }
  593.  
  594.     // Apply background color to the cell
  595.     dsc->rect_dsc->bg_color = bg_color;
  596.     dsc->rect_dsc->bg_opa = LV_OPA_COVER;
  597.     dsc->label_dsc->color = text_color;
  598.   }
  599. }
  600.  
  601. void update_bt_icon_color(bool is_connected, bool firstTime) {
  602.   if(btIconSts != is_connected || firstTime) {
  603.     if (!style_initialized) {
  604.       lv_style_init(&style_bt);
  605.       style_initialized = true;
  606.     }
  607.     if (is_connected) {
  608.       lv_style_set_text_color(&style_bt, lv_color_make(0, 255, 0)); // Green
  609.     } else {
  610.       lv_style_set_text_color(&style_bt, lv_color_make(0, 0, 255)); // Red
  611.     }
  612.     lv_obj_add_style(bt_icon_label, &style_bt, 0);
  613.     btIconSts = is_connected;
  614.   }
  615.   lv_timer_handler();
  616. }
  617.  
  618. void create_bt_icon() {
  619.   bt_icon_label = lv_label_create(lv_scr_act());
  620.   lv_label_set_text(bt_icon_label, LV_SYMBOL_BLUETOOTH);
  621.   lv_obj_set_style_text_font(bt_icon_label, &lv_font_montserrat_28, LV_PART_MAIN);
  622.   lv_obj_align(bt_icon_label, LV_ALIGN_BOTTOM_RIGHT, -3, -1);
  623.   update_bt_icon_color(SerialBT.hasClient(), true);
  624.   lv_timer_handler();
  625. }
Advertisement
Add Comment
Please, Sign In to add comment