Advertisement
espd

test_code

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