espd

ESP32_EMU_BT_V.2.06 - bigger text

May 26th, 2025 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.29 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. using namespace std;
  9.  
  10. //#define USE_NAME
  11. const char *pin = "1234";
  12. String myBtName = "ESP32-BT-Master-1";
  13.  
  14. #if !defined(CONFIG_BT_SPP_ENABLED)
  15. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  16. #endif
  17.  
  18. BluetoothSerial SerialBT;
  19.  
  20. #ifdef USE_NAME
  21. String slaveName = "EMUCANBT_SPP";
  22. #else
  23. uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };
  24. #endif
  25.  
  26. const int backLightPin = 27;
  27. const int buzzerPin = 22;
  28. bool buzzerOn = false;
  29. bool btIconSts = false;
  30. static lv_style_t style_bt;
  31. static bool style_initialized = false;
  32.  
  33. int rpm;
  34. int spd;
  35. float afr;
  36. float mapR;
  37. float boost;
  38. int tps;
  39. int clt;
  40. int ign;
  41. int inj;
  42. float bat;
  43. int cel;
  44.  
  45. unsigned long previousMillis = 0;
  46. const unsigned long reconnectInterval = 5000;
  47.  
  48. LV_FONT_DECLARE(lv_font_montserrat_14);
  49. LV_FONT_DECLARE(lv_font_montserrat_20);                    
  50. LV_FONT_DECLARE(lv_font_montserrat_28);
  51.  
  52. lv_obj_t *bt_icon_label;
  53.  
  54. // Display & LVGL setup
  55. TFT_eSPI tft = TFT_eSPI();
  56. static lv_disp_draw_buf_t draw_buf;
  57. static lv_color_t buf[LV_HOR_RES_MAX * 20];
  58. lv_obj_t *table;
  59.  
  60. // LVGL Display Flush Callback
  61. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  62.   uint16_t w = area->x2 - area->x1 + 1;
  63.   uint16_t h = area->y2 - area->y1 + 1;
  64.   tft.startWrite();
  65.   tft.setAddrWindow(area->x1, area->y1, w, h);
  66.   tft.pushColors((uint16_t *)&color_p->full, w * h, true);
  67.   tft.endWrite();
  68.   lv_disp_flush_ready(disp);
  69. }
  70.  
  71. // Initialize LVGL Table
  72. void create_table() {
  73.   lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(30, 30, 30), LV_PART_MAIN);
  74.  
  75.   table = lv_table_create(lv_scr_act());
  76.   lv_obj_align(table, LV_ALIGN_CENTER, -1, 0);
  77.   lv_obj_set_style_text_opa(table, LV_OPA_COVER, 0);
  78.   lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
  79.   lv_obj_set_scrollbar_mode(table, LV_SCROLLBAR_MODE_OFF);  
  80.   lv_obj_set_style_text_color(table, lv_color_white(), LV_PART_ITEMS);
  81.   lv_obj_set_style_bg_color(table, lv_color_make(30, 30, 30), LV_PART_MAIN);
  82.   lv_obj_set_style_text_font(table, &lv_font_montserrat_20, LV_PART_ITEMS);
  83.  
  84.   static lv_style_t style_cell0;
  85.   lv_style_init(&style_cell0);
  86.   lv_style_set_pad_top(&style_cell0, 10);
  87.   lv_style_set_pad_bottom(&style_cell0, 10);
  88.   lv_style_set_pad_left(&style_cell0, 2);
  89.   lv_style_set_pad_right(&style_cell0, 2);
  90.   lv_obj_add_style(table, &style_cell0, LV_PART_ITEMS);
  91.  
  92.   lv_table_set_col_cnt(table, 4);
  93.   lv_table_set_row_cnt(table, 6);
  94.  
  95.   lv_obj_set_style_border_width(table, 1, LV_PART_ITEMS);
  96.   lv_obj_set_style_border_color(table, lv_color_white(), LV_PART_ITEMS);
  97.   lv_obj_set_style_border_side(table, LV_BORDER_SIDE_FULL, LV_PART_ITEMS);
  98.  
  99.   lv_table_set_col_width(table, 0, 53);
  100.   lv_table_set_col_width(table, 1, 105);
  101.   lv_table_set_col_width(table, 2, 47);
  102.   lv_table_set_col_width(table, 3, 115);
  103.  
  104.   lv_table_add_cell_ctrl(table, 5, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  105.   lv_table_add_cell_ctrl(table, 5, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  106.   lv_table_add_cell_ctrl(table, 5, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  107.  
  108.   lv_table_set_cell_value(table, 0, 0, "RPM");
  109.   lv_table_set_cell_value(table, 0, 2, "SPD");
  110.   lv_table_set_cell_value(table, 1, 0, "AFR");
  111.   lv_table_set_cell_value(table, 1, 2, "CLT");
  112.   lv_table_set_cell_value(table, 2, 0, "TPS");
  113.   lv_table_set_cell_value(table, 2, 2, "BAT");
  114.   lv_table_set_cell_value(table, 3, 0, "MAP");
  115.   lv_table_set_cell_value(table, 3, 2, "BST");
  116.   lv_table_set_cell_value(table, 4, 0, "INJ");
  117.   lv_table_set_cell_value(table, 4, 2, "IGN");
  118.   lv_table_set_cell_value(table, 5, 0, "CEL");
  119.  
  120.   lv_obj_add_event_cb(table, my_table_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
  121.   lv_obj_add_event_cb(table, table_event_cb_bg, LV_EVENT_DRAW_PART_BEGIN, NULL);
  122.  
  123.   create_bt_icon();
  124.  
  125.   lv_timer_handler();
  126. }
  127.  
  128. void setup() {
  129.   tft.init();
  130.   pinMode(backLightPin, OUTPUT);
  131.   digitalWrite(backLightPin, LOW);
  132.   uint16_t darkGray = ((30 & 0xF8) << 8) | ((30 & 0xFC) << 3) | (30 >> 3);
  133.   tft.fillScreen(darkGray);
  134.   tft.setRotation(1);
  135.   Serial.begin(1000000);
  136.  
  137.   pinMode(buzzerPin, OUTPUT);
  138.  
  139.   // Initialize LVGL
  140.   lv_init();
  141.   lv_refr_now(NULL);
  142.   lv_disp_draw_buf_init(&draw_buf, buf, NULL, LV_HOR_RES_MAX * 10);
  143.  
  144.   // Setup LVGL Display Driver
  145.   static lv_disp_drv_t disp_drv;
  146.   lv_disp_drv_init(&disp_drv);
  147.   disp_drv.hor_res = 320;
  148.   disp_drv.ver_res = 240;
  149.   disp_drv.flush_cb = my_disp_flush;
  150.   disp_drv.draw_buf = &draw_buf;
  151.   lv_disp_drv_register(&disp_drv);
  152.  
  153.   SerialBT.begin(myBtName, true);
  154.  
  155.   create_table();
  156.   digitalWrite(backLightPin, HIGH);
  157.   connectToBt();
  158. }
  159.  
  160. void connectToBt() {
  161.   bool connected;
  162.   #ifndef USE_NAME
  163.     SerialBT.setPin(pin);
  164.   #endif
  165.  
  166.   #ifdef USE_NAME
  167.     connected = SerialBT.connect(slaveName);
  168.   #else
  169.     connected = SerialBT.connect(address);
  170.   #endif
  171.  
  172.   if (connected) {
  173.     Serial.println("Connected Successfully!");
  174.   } else {
  175.     Serial.println("Initial connect failed. Will retry in loop...");
  176.   }
  177.   update_bt_icon_color(SerialBT.hasClient(), false);
  178. }
  179.  
  180. void loop() {
  181.   uint8_t frame[5];
  182.   uint8_t channel;
  183.   uint16_t value;
  184.   int chData;
  185.   unsigned long currentMillis = millis();
  186.  
  187.   if (!SerialBT.connected()) {
  188.     // Attempt reconnection every 5 seconds
  189.     if (currentMillis - previousMillis >= reconnectInterval) {
  190.       previousMillis = currentMillis;
  191.       connectToBt();
  192.     }
  193.   }
  194.  
  195.   update_bt_icon_color(SerialBT.hasClient(), false);
  196.  
  197.   // Wait until at least 5 bytes are available
  198.   while (SerialBT.available() >= 5) {
  199.     SerialBT.readBytes(frame, 5);  // Read exactly 5 bytes
  200.     channel = frame[0];
  201.     value = (frame[2] << 8) | frame[3];
  202.     chData = static_cast<int>(channel);
  203.     if (chData == 1) {
  204.       rpm = static_cast<int>(value);
  205.       lv_table_set_cell_value(table, 0, 1, String(rpm).c_str());
  206.     } else if (chData == 28) {
  207.       spd = (static_cast<int>(value) / 2.8);
  208.       lv_table_set_cell_value(table, 0, 3, (String(spd) + " KM/H").c_str());
  209.     } else if (chData == 12) {
  210.       afr = (static_cast<float>(value) / 10);
  211.       lv_table_set_cell_value(table, 1, 1, String(afr).c_str());
  212.     } else if (chData == 2) {
  213.       mapR = (static_cast<float>(value) / 100);
  214.       boost = (mapR - 1.0132f);
  215.       lv_table_set_cell_value(table, 3, 1, (String(mapR) + " BAR").c_str());
  216.       lv_table_set_cell_value(table, 3, 3, (String(boost) + " BAR").c_str());
  217.     } else if (chData == 3) {
  218.       tps = static_cast<int>(value);
  219.       lv_table_set_cell_value(table, 2, 1, (String(tps) + " %").c_str());
  220.     } else if (chData == 24) {
  221.       clt = static_cast<int>(value);
  222.       lv_table_set_cell_value(table, 1, 3, (String(clt) + " °C").c_str());
  223.     } else if (chData == 6) {
  224.       ign = (static_cast<int>(value) / 2);
  225.       lv_table_set_cell_value(table, 4, 3, (String(ign) + " °").c_str());
  226.     } else if (chData == 19) {
  227.       inj = (static_cast<int>(value) / 2);
  228.       lv_table_set_cell_value(table, 4, 1, (String(inj) + " %").c_str());
  229.     } else if (chData == 5) {
  230.       bat = (static_cast<float>(value) / 37);
  231.       lv_table_set_cell_value(table, 2, 3, (String(bat) + " V").c_str());
  232.     } else if (chData == 255) {
  233.       cel = decodeCheckEngine(value);
  234.     }
  235.   }
  236.  
  237.   /*if (cel > 0 || clt > 105 || rpm > 7000 || boost > 1.10 || (bat < 12.00 && bat > 1.00)) {
  238.     digitalWrite(buzzerPin, HIGH);  // Buzzer ON
  239.   } else {
  240.     digitalWrite(buzzerPin, LOW);   // Buzzer OFF
  241.   }*/
  242.  
  243.   buzzerOn = (cel > 0 || clt > 105 || rpm > 7000 || boost > 1.10 || (bat < 12.00 && bat > 1.00));
  244.   digitalWrite(buzzerPin, (millis() % 600 < 300) && buzzerOn);
  245.  
  246.   lv_obj_invalidate(table);
  247.   lv_timer_handler();
  248. }
  249.  
  250. int decodeCheckEngine(uint16_t value) {
  251.   int cel_codes = 0; string cel_names = "";
  252.   if (value == 0) {
  253.     return 0;
  254.   }
  255.   else {
  256.     if (value & (1 << 0)) {
  257.       cel_codes++;  // Bit 0
  258.       cel_names = "CLT ";
  259.     }
  260.     if (value & (1 << 1)) {
  261.       //cel_codes++;  // Bit 1
  262.       //cel_names += "IAT ";
  263.     }
  264.     if (value & (1 << 2)) {
  265.       cel_codes++;  // Bit 2
  266.       cel_names += "MAP ";
  267.     }
  268.     if (value & (1 << 3)) {
  269.       cel_codes++;  // Bit 3
  270.       cel_names += "WBO ";
  271.     }
  272.     if (value & (1 << 8)) {
  273.       cel_codes++;  // Bit 8
  274.       cel_names += "FF SENSOR ";
  275.     }
  276.     if (value & (1 << 9)) {
  277.       cel_codes++;  // Bit 9
  278.       cel_names += "DBW ";
  279.     }
  280.     if (value & (1 << 10)) {
  281.       cel_codes++;  // Bit 10
  282.       cel_names += "FPR ";
  283.     }
  284.  
  285.     lv_table_set_cell_value(table, 5, 1, cel_names.c_str());
  286.     return cel_codes;
  287.   }
  288. }
  289.  
  290. // Cell alignment fix
  291. void my_table_event_cb(lv_event_t * e) {
  292.   lv_obj_t * table = lv_event_get_target(e);
  293.   lv_obj_draw_part_dsc_t * dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  294.  
  295.   if (dsc->part == LV_PART_ITEMS) {
  296.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  297.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  298.  
  299.     dsc->label_dsc->align = LV_TEXT_ALIGN_LEFT;
  300.     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) ||
  301.         (row == 4 && col == 1) || (row == 4 && col == 3)) {
  302.       dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;
  303.     }
  304.     if (row == 5 && col == 1) {
  305.       dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
  306.     }
  307.   }
  308. }
  309.  
  310. static void table_event_cb_bg(lv_event_t *e) {
  311.   lv_obj_t *table = lv_event_get_target(e);
  312.   lv_obj_draw_part_dsc_t *dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  313.  
  314.   // Ensure dsc and rect_dsc are valid
  315.   if (!dsc || !dsc->rect_dsc) return;
  316.  
  317.   // Only modify table cell backgrounds
  318.   if (dsc->part == LV_PART_ITEMS) {
  319.     uint16_t row = dsc->id / lv_table_get_col_cnt(table);
  320.     uint16_t col = dsc->id % lv_table_get_col_cnt(table);
  321.  
  322.     const char *value_str = lv_table_get_cell_value(table, row, col);
  323.  
  324.     // Check if value_str is null or empty before conversion
  325.     float value = 0.0f;  // Default value
  326.     if (value_str != nullptr && value_str[0] != '\0') {
  327.       try {
  328.         value = std::stof(value_str);  // Convert string to float safely
  329.       } catch (...) {
  330.         value = 0.0f;  // Handle invalid conversions
  331.       }
  332.     }
  333.  
  334.     // Default cell color
  335.     lv_color_t bg_color = lv_color_make(30, 30, 30);
  336.     lv_color_t text_color = lv_color_white();
  337.  
  338.     if (row == 0 && col == 1 && value > 7000.00) {
  339.       bg_color = lv_color_make(0, 0, 255);
  340.       text_color = lv_color_white();
  341.     }
  342.     if (row == 1 && col == 3 && value > 100.00) {
  343.       bg_color = lv_color_make(0, 0, 255);
  344.       text_color = lv_color_white();
  345.     }
  346.     if (row == 1 && col == 3 && value < 55.00 && value > 01.00) {
  347.       bg_color = lv_color_make(0, 255, 255);
  348.       text_color = lv_color_black();
  349.     }
  350.     if (row == 2 && col == 3 && value < 12.00 && value > 01.00) {
  351.       bg_color = lv_color_make(0, 0, 255);
  352.       text_color = lv_color_white();
  353.     }
  354.     if (row == 3 && col == 3 && value > 1.10) {
  355.       bg_color = lv_color_make(0, 0, 255);
  356.       text_color = lv_color_white();
  357.     }
  358.     if (row == 5 && col == 1 && value_str != nullptr && value_str[0] != '\0') {
  359.       bg_color = lv_color_make(0, 0, 255);
  360.       text_color = lv_color_white();
  361.     }
  362.  
  363.     // Apply background color to the cell
  364.     dsc->rect_dsc->bg_color = bg_color;
  365.     dsc->rect_dsc->bg_opa = LV_OPA_COVER;
  366.     dsc->label_dsc->color = text_color;
  367.   }
  368. }
  369.  
  370. void update_bt_icon_color(bool is_connected, bool firstTime) {
  371.   if(btIconSts != is_connected || firstTime) {
  372.     if (!style_initialized) {
  373.       lv_style_init(&style_bt);
  374.       style_initialized = true;
  375.     }
  376.     if (is_connected) {
  377.       lv_style_set_text_color(&style_bt, lv_color_make(0, 255, 0)); // Green
  378.     } else {
  379.       lv_style_set_text_color(&style_bt, lv_color_make(0, 0, 255)); // Red
  380.     }
  381.     lv_obj_add_style(bt_icon_label, &style_bt, 0);
  382.     btIconSts = is_connected;
  383.   }
  384. }
  385.  
  386. void create_bt_icon() {
  387.   bt_icon_label = lv_label_create(lv_scr_act());
  388.   lv_label_set_text(bt_icon_label, LV_SYMBOL_BLUETOOTH);
  389.   lv_obj_set_style_text_font(bt_icon_label, &lv_font_montserrat_28, LV_PART_MAIN);
  390.   lv_obj_align(bt_icon_label, LV_ALIGN_BOTTOM_RIGHT, -3, -1);
  391.   update_bt_icon_color(SerialBT.hasClient(), true);
  392. }
Advertisement
Add Comment
Please, Sign In to add comment