espd

esp32 full code - 20250404

Apr 4th, 2025 (edited)
11
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.34 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. using namespace std;
  7.  
  8. //#define USE_NAME           // Comment this to use MAC address instead of a slaveName
  9. const char *pin = "1234";
  10.  
  11. #if !defined(CONFIG_BT_SPP_ENABLED)
  12. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  13. #endif
  14.  
  15. BluetoothSerial SerialBT;
  16.  
  17. #ifdef USE_NAME
  18. String slaveName = "EMUCANBT_SPP";
  19. #else
  20. String MACadd = "98:DA:20:02:BE:A4";                          // This only for printing
  21. uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };  // Change this to reflect real MAC address of your slave BT device
  22. #endif
  23.  
  24. String myName = "ESP32-BT-Master";
  25.  
  26. const int buzzerPin = 22;
  27.  
  28. // Display & LVGL setup
  29. TFT_eSPI tft = TFT_eSPI();
  30. static lv_disp_draw_buf_t draw_buf;
  31. static lv_color_t buf[LV_HOR_RES_MAX * 10];
  32. lv_obj_t *table;
  33.  
  34. // LVGL Display Flush Callback
  35. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  36.   uint16_t w = area->x2 - area->x1 + 1;
  37.   uint16_t h = area->y2 - area->y1 + 1;
  38.   tft.startWrite();
  39.   tft.setAddrWindow(area->x1, area->y1, w, h);
  40.   tft.pushColors((uint16_t *)&color_p->full, w * h, true);
  41.   tft.endWrite();
  42.   lv_disp_flush_ready(disp);
  43. }
  44.  
  45. // Initialize LVGL Table
  46. void create_table() {
  47.   table = lv_table_create(lv_scr_act());
  48.   lv_obj_align(table, LV_ALIGN_CENTER, 0, 0);
  49.  
  50.   lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
  51.  
  52.   lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(255, 255, 255), LV_PART_MAIN);
  53.  
  54.   lv_obj_set_style_text_color(table, lv_color_black(), LV_PART_ITEMS);
  55.  
  56.   lv_obj_set_style_bg_color(table, lv_color_white(), LV_PART_MAIN);
  57.  
  58.   // Set table properties
  59.   lv_table_set_col_cnt(table, 4);
  60.   lv_table_set_row_cnt(table, 6);
  61.  
  62.   lv_obj_set_style_border_width(table, 1, LV_PART_ITEMS);
  63.   lv_obj_set_style_border_color(table, lv_palette_main(LV_PALETTE_BLUE), LV_PART_ITEMS);
  64.   lv_obj_set_style_border_side(table, LV_BORDER_SIDE_FULL, LV_PART_ITEMS);
  65.  
  66.   lv_table_set_col_width(table, 0, 60);
  67.   lv_table_set_col_width(table, 1, 100);
  68.   lv_table_set_col_width(table, 2, 60);
  69.   lv_table_set_col_width(table, 3, 100);
  70.  
  71.   lv_table_add_cell_ctrl(table, 5, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  72.   lv_table_add_cell_ctrl(table, 5, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  73.   lv_table_add_cell_ctrl(table, 5, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  74.  
  75.   lv_table_set_cell_value(table, 0, 0, "RPM");
  76.   lv_table_set_cell_value(table, 0, 2, "SPD");
  77.   lv_table_set_cell_value(table, 1, 0, "AFR");
  78.   lv_table_set_cell_value(table, 1, 2, "CLT");
  79.   lv_table_set_cell_value(table, 2, 0, "TPS");
  80.   lv_table_set_cell_value(table, 2, 2, "BAT");
  81.   lv_table_set_cell_value(table, 3, 0, "MAP");
  82.   lv_table_set_cell_value(table, 3, 2, "BST");
  83.   lv_table_set_cell_value(table, 4, 0, "INJ");
  84.   lv_table_set_cell_value(table, 4, 2, "IGN");
  85.   lv_table_set_cell_value(table, 5, 0, "CEL");
  86.  
  87.   lv_obj_add_event_cb(table, my_table_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);  
  88.  
  89.   lv_timer_handler();
  90. }
  91.  
  92. void setup() {
  93.   Serial.begin(1000000);
  94.  
  95.   pinMode(buzzerPin, OUTPUT);
  96.  
  97.   tft.begin();
  98.   tft.setRotation(3);
  99.  
  100.   // Initialize LVGL
  101.   lv_init();
  102.   lv_refr_now(NULL);
  103.   lv_disp_draw_buf_init(&draw_buf, buf, NULL, LV_HOR_RES_MAX * 10);
  104.  
  105.   // Setup LVGL Display Driver
  106.   static lv_disp_drv_t disp_drv;
  107.   lv_disp_drv_init(&disp_drv);
  108.   disp_drv.hor_res = 320;
  109.   disp_drv.ver_res = 240;
  110.   disp_drv.flush_cb = my_disp_flush;
  111.   disp_drv.draw_buf = &draw_buf;
  112.   lv_disp_drv_register(&disp_drv);
  113.  
  114.   // Create table
  115.   create_table();
  116.  
  117.   bool connected;
  118.   SerialBT.begin(myName, true);
  119.   //Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  120.  
  121.   #ifndef USE_NAME
  122.     SerialBT.setPin(pin);
  123.     //Serial.println("Using PIN");
  124.   #endif
  125.  
  126.   #ifdef USE_NAME
  127.     connected = SerialBT.connect(slaveName);
  128.     //Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  129.   #else
  130.     connected = SerialBT.connect(address);
  131.     //Serial.print("Connecting to slave BT device with MAC ");
  132.     //Serial.println(MACadd);
  133.   #endif
  134.  
  135.   if (connected) {
  136.     lv_timer_handler();
  137.     lv_table_set_cell_value(table, 5, 1, "");
  138.     Serial.println("Connected Successfully!");
  139.   } else {
  140.     lv_timer_handler();
  141.     while (!SerialBT.connected(30000)) {
  142.       lv_timer_handler();
  143.       lv_table_set_cell_value(table, 5, 1, "BT ERR");
  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.     lv_table_set_cell_value(table, 5, 1, "");
  157.     Serial.println("Reconnected Successfully!");
  158.   } else {
  159.     lv_timer_handler();
  160.     while (!SerialBT.connected(10000)) {
  161.       lv_timer_handler();
  162.       lv_table_set_cell_value(table, 5, 1, "BT ERR");
  163.       Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  164.     }
  165.   }
  166. }
  167.  
  168. int rpm;
  169. int spd;
  170. float afr;
  171. float mapR;
  172. float boost;
  173. int tps;
  174. int clt;
  175. int ign;
  176. int inj;
  177. float bat;
  178. int cel;
  179.  
  180. void loop() {
  181.   uint8_t frame[5];
  182.   uint8_t channel;
  183.   uint16_t value;
  184.   int chData;
  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.     // Extract values
  191.     channel = frame[0];
  192.     value = (frame[2] << 8) | frame[3];  // Combine High and Low byte
  193.  
  194.     chData = static_cast<int>(channel);
  195.     if (chData == 1) {
  196.       rpm = static_cast<int>(value);
  197.      // Serial.println("RPM: " + String(rpm));
  198.       lv_table_set_cell_value(table, 0, 1, String(rpm).c_str());
  199.     } else if (chData == 28) {
  200.       spd = (static_cast<int>(value));
  201.       //Serial.println("SPD: " + String(spd) + " KM/H");
  202.       lv_table_set_cell_value(table, 0, 3, (String(spd) + " KM/H").c_str());
  203.     } else if (chData == 12) {
  204.       afr = (static_cast<float>(value) / 10);
  205.       //Serial.println("AFR: " + String(afr));
  206.       lv_table_set_cell_value(table, 1, 1, String(afr).c_str());
  207.     } else if (chData == 2) {
  208.       mapR = (static_cast<float>(value) / 100);
  209.       boost = (mapR - 1.0132f);
  210.       //Serial.println("MAP: " + String(mapR) + " BAR");
  211.      // Serial.println("BST: " + String(boost) + " BAR");
  212.       lv_table_set_cell_value(table, 3, 1, (String(mapR) + " BAR").c_str());
  213.       lv_table_set_cell_value(table, 3, 3, (String(boost) + " BAR").c_str());
  214.     } else if (chData == 3) {
  215.       tps = static_cast<int>(value);
  216.       //Serial.println("TPS: " + String(tps) + " %");
  217.       lv_table_set_cell_value(table, 2, 1, (String(tps) + " %").c_str());
  218.     } else if (chData == 24) {
  219.       clt = static_cast<int>(value);
  220.       //Serial.println("CLT: " + String(clt) + " °C");
  221.       lv_table_set_cell_value(table, 1, 3, (String(clt) + " °C").c_str());
  222.     } else if (chData == 6) {
  223.       ign = static_cast<int>(value);
  224.       //Serial.println("IGN: " + String(ign) + " °");
  225.       lv_table_set_cell_value(table, 4, 3, (String(ign) + " °").c_str());
  226.     } else if (chData == 19) {
  227.       inj =  static_cast<int>(value);
  228.       //Serial.println("INJ: " + String(inj) + " %");
  229.       lv_table_set_cell_value(table, 4, 1, (String(inj) + " %").c_str());
  230.     } else if (chData == 5) {
  231.       bat = (static_cast<float>(value) / 37);
  232.       //Serial.println("BAT: " + String(bat) + " V");
  233.       lv_table_set_cell_value(table, 2, 3, (String(bat) + " V").c_str());
  234.     } else if (chData == 255) {
  235.       cel = decodeCheckEngine(value);
  236.       //Serial.println("CEL: " + String(cel));
  237.     }
  238.   }
  239.  
  240.   if(cel > 0 || clt > 100 || rpm > 7500 || boost > 1.10 || bat < 12.00) {
  241.      digitalWrite(buzzerPin, HIGH);  // Buzzer ON
  242.   } else {
  243.      digitalWrite(buzzerPin, LOW);   // Buzzer OFF
  244.   }
  245.  
  246.   lv_obj_invalidate(table);
  247.   lv_timer_handler();
  248.   // Run LVGL
  249.   //delay(10);
  250. }
  251.  
  252. int decodeCheckEngine(uint16_t value) {
  253.   int cel_codes = 0; string cel_names = "";
  254.   if (value == 0) {
  255.     return 0;
  256.   }
  257.   else {
  258.     //Serial.print("CEL Codes: ");
  259.     if (value & (1 << 0)) {
  260.       cel_codes++;  // Bit 0
  261.       //Serial.print("CLT ");
  262.       cel_names = "CLT ";
  263.     }
  264.     if (value & (1 << 1)) {
  265.     //  cel_codes++;  // Bit 1
  266.     //  Serial.print("IAT ");
  267.    //   cel_names += "IAT ";
  268.     }
  269.     if (value & (1 << 2)) {
  270.       cel_codes++;  // Bit 2
  271.       //Serial.print("MAP ");
  272.       cel_names += "MAP ";
  273.     }
  274.     if (value & (1 << 3)) {
  275.       cel_codes++;  // Bit 3
  276.       //Serial.print("WBO ");
  277.       cel_names += "WBO ";
  278.     }
  279.     if (value & (1 << 8)) {
  280.       cel_codes++;  // Bit 8
  281.       //Serial.print("FF SENSOR ");
  282.       cel_names += "FF SENSOR ";
  283.     }
  284.     if (value & (1 << 9)) {
  285.       cel_codes++;  // Bit 9
  286.       //Serial.print("DBW ");
  287.       cel_names += "DBW ";
  288.     }
  289.     if (value & (1 << 10)) {
  290.       cel_codes++;  // Bit 10
  291.       //Serial.print("FPR ");
  292.       cel_names += "FPR ";
  293.     }
  294.  
  295.     //Serial.print("Total CEL Codes: " + cel_codes);
  296.     //Serial.println();
  297.  
  298.     lv_table_set_cell_value(table, 5, 1, cel_names.c_str());
  299.     return cel_codes;
  300.   }
  301. }
  302.  
  303. // Custom draw callback for right-aligned text in specific cells
  304. void my_table_event_cb(lv_event_t * e) {
  305.     lv_obj_t * table = lv_event_get_target(e);
  306.     lv_obj_draw_part_dsc_t * dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);
  307.  
  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.         // Default Left Align
  313.         dsc->label_dsc->align = LV_TEXT_ALIGN_LEFT;
  314.  
  315.         // Right-align specific cells
  316.         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) ||
  317.             (row == 4 && col == 1) || (row == 4 && col == 3)) {
  318.             dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;
  319.         }
  320.         if(row == 5 && col == 1) {
  321.             dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
  322.         }
  323.     }
  324. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment