espd

esp code 20250405-1600

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