espd

ESP32_EMU_BT_V.2.01

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