espd

ESP32_EMU_BT_V.2.04

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