espd

ESP32_EMU_BT_V.3.09

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