espd

esp32 - working all - 20250403

Apr 4th, 2025 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.77 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. // Display & LVGL setup
  27. TFT_eSPI tft = TFT_eSPI();
  28. static lv_disp_draw_buf_t draw_buf;
  29. static lv_color_t buf[LV_HOR_RES_MAX * 10];
  30. lv_obj_t *table;
  31.  
  32. // LVGL Display Flush Callback
  33. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
  34.   uint16_t w = area->x2 - area->x1 + 1;
  35.   uint16_t h = area->y2 - area->y1 + 1;
  36.   tft.startWrite();
  37.   tft.setAddrWindow(area->x1, area->y1, w, h);
  38.   tft.pushColors((uint16_t *)&color_p->full, w * h, true);
  39.   tft.endWrite();
  40.   lv_disp_flush_ready(disp);
  41. }
  42.  
  43. // Initialize LVGL Table
  44. void create_table() {
  45.   table = lv_table_create(lv_scr_act());
  46.   lv_obj_align(table, LV_ALIGN_CENTER, 0, 0);
  47.  
  48.   lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
  49.  
  50.   lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(255, 255, 255), LV_PART_MAIN);
  51.  
  52.   lv_obj_set_style_text_color(table, lv_color_black(), LV_PART_ITEMS);
  53.  
  54.   lv_obj_set_style_bg_color(table, lv_color_white(), LV_PART_MAIN);
  55.  
  56.   // Set table properties
  57.   lv_table_set_col_cnt(table, 4);
  58.   lv_table_set_row_cnt(table, 6);
  59.  
  60.   lv_obj_set_style_border_width(table, 1, LV_PART_ITEMS);
  61.   lv_obj_set_style_border_color(table, lv_palette_main(LV_PALETTE_BLUE), LV_PART_ITEMS);
  62.   lv_obj_set_style_border_side(table, LV_BORDER_SIDE_FULL, LV_PART_ITEMS);
  63.  
  64.   lv_table_set_col_width(table, 0, 60);
  65.   lv_table_set_col_width(table, 1, 100);
  66.   lv_table_set_col_width(table, 2, 60);
  67.   lv_table_set_col_width(table, 3, 100);
  68.  
  69.   lv_table_add_cell_ctrl(table, 5, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  70.   lv_table_add_cell_ctrl(table, 5, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  71.   lv_table_add_cell_ctrl(table, 5, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
  72.  
  73.   lv_table_set_cell_value(table, 0, 0, "RPM");
  74.   lv_table_set_cell_value(table, 0, 2, "SPD");
  75.   lv_table_set_cell_value(table, 1, 0, "AFR");
  76.   lv_table_set_cell_value(table, 1, 2, "CLT");
  77.   lv_table_set_cell_value(table, 2, 0, "TPS");
  78.   lv_table_set_cell_value(table, 2, 2, "BAT");
  79.   lv_table_set_cell_value(table, 3, 0, "MAP");
  80.   lv_table_set_cell_value(table, 3, 2, "BST");
  81.   lv_table_set_cell_value(table, 4, 0, "INJ");
  82.   lv_table_set_cell_value(table, 4, 2, "IGN");
  83.   lv_table_set_cell_value(table, 5, 0, "CEL");
  84.  
  85.   lv_timer_handler();
  86. }
  87.  
  88. void setup() {
  89.   Serial.begin(1000000);
  90.  
  91.   tft.begin();
  92.   tft.setRotation(3);
  93.  
  94.   // Initialize LVGL
  95.   lv_init();
  96.   lv_refr_now(NULL);
  97.   lv_disp_draw_buf_init(&draw_buf, buf, NULL, LV_HOR_RES_MAX * 10);
  98.  
  99.   // Setup LVGL Display Driver
  100.   static lv_disp_drv_t disp_drv;
  101.   lv_disp_drv_init(&disp_drv);
  102.   disp_drv.hor_res = 320;
  103.   disp_drv.ver_res = 240;
  104.   disp_drv.flush_cb = my_disp_flush;
  105.   disp_drv.draw_buf = &draw_buf;
  106.   lv_disp_drv_register(&disp_drv);
  107.  
  108.   // Create table
  109.   create_table();
  110.  
  111.   bool connected;
  112.   SerialBT.begin(myName, true);
  113.   Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  114.  
  115.   #ifndef USE_NAME
  116.     SerialBT.setPin(pin);
  117.     Serial.println("Using PIN");
  118.   #endif
  119.  
  120.   #ifdef USE_NAME
  121.     connected = SerialBT.connect(slaveName);
  122.     Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  123.   #else
  124.     connected = SerialBT.connect(address);
  125.     Serial.print("Connecting to slave BT device with MAC ");
  126.     Serial.println(MACadd);
  127.   #endif
  128.  
  129.   if (connected) {
  130.     lv_timer_handler();
  131.     Serial.println("Connected Successfully!");
  132.   } else {
  133.     lv_timer_handler();
  134.     while (!SerialBT.connected(30000)) {
  135.       lv_timer_handler();
  136.       Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
  137.     }
  138.   }
  139.   // Disconnect() may take up to 10 secs max
  140.   if (SerialBT.disconnect()) {
  141.     lv_timer_handler();
  142.     Serial.println("Disconnected Successfully!");
  143.   }
  144.   // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  145.   SerialBT.connect();
  146.   if (connected) {
  147.     lv_timer_handler();
  148.     Serial.println("Reconnected Successfully!");
  149.   } else {
  150.     lv_timer_handler();
  151.     while (!SerialBT.connected(10000)) {
  152.       lv_timer_handler();
  153.       Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  154.     }
  155.   }
  156.  
  157. }
  158.  
  159. void loop() {
  160.   uint8_t frame[5];
  161.   uint8_t channel;
  162.   uint16_t value;
  163.  
  164.   int chData;
  165.   int rpm;
  166.   int spd;
  167.   float afr;
  168.   float mapR;
  169.   float boost;
  170.   int tps;
  171.   int clt;
  172.   int ign;
  173.   int inj;
  174.   float bat;
  175.   int cel;
  176.  
  177.  
  178.   // Wait until at least 5 bytes are available
  179.   while (SerialBT.available() >= 5) {
  180.     SerialBT.readBytes(frame, 5);  // Read exactly 5 bytes
  181.  
  182.     // Extract values
  183.     channel = frame[0];
  184.     value = (frame[2] << 8) | frame[3];  // Combine High and Low byte
  185.  
  186.     chData = static_cast<int>(channel);
  187.     if (chData == 1) {
  188.       rpm = static_cast<int>(value);
  189.       Serial.println("RPM: " + String(rpm));
  190.       lv_table_set_cell_value(table, 0, 1, String(rpm).c_str());
  191.     } else if (chData == 28) {
  192.       spd = (static_cast<int>(value) * 1.609);
  193.       Serial.println("SPD: " + String(spd) + " KM/H");
  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.       Serial.println("AFR: " + String(afr));
  198.       lv_table_set_cell_value(table, 1, 1, String(afr).c_str());
  199.     } else if (chData == 2) {
  200.       mapR = (static_cast<float>(value) / 100);
  201.       boost = (mapR - 1.0132f);
  202.       Serial.println("MAP: " + String(mapR) + " BAR");
  203.       Serial.println("BST: " + String(boost) + " BAR");
  204.       lv_table_set_cell_value(table, 3, 1, (String(mapR) + " BAR").c_str());
  205.       lv_table_set_cell_value(table, 3, 3, (String(boost) + " BAR").c_str());
  206.     } else if (chData == 3) {
  207.       tps = static_cast<int>(value);
  208.       Serial.println("TPS: " + String(tps) + " %");
  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.       Serial.println("CLT: " + String(clt) + " °C");
  213.       lv_table_set_cell_value(table, 1, 3, (String(clt) + " °C").c_str());
  214.     } else if (chData == 6) {
  215.       ign = static_cast<int>(value);
  216.       Serial.println("IGN: " + String(ign) + " °");
  217.       lv_table_set_cell_value(table, 4, 3, (String(ign) + " °").c_str());
  218.     } else if (chData == 19) {
  219.       inj =  static_cast<int>(value);
  220.       Serial.println("INJ: " + String(inj) + " %");
  221.       lv_table_set_cell_value(table, 4, 1, (String(inj) + " %").c_str());
  222.     } else if (chData == 5) {
  223.       bat = (static_cast<float>(value) / 37);
  224.       Serial.println("BAT: " + String(bat) + " V");
  225.       lv_table_set_cell_value(table, 2, 3, (String(bat) + " V").c_str());
  226.     } else if (chData == 255) {
  227.       cel = decodeCheckEngine(value);
  228.       Serial.println("CEL: " + String(cel));
  229.     }
  230.   }
  231.   lv_timer_handler();
  232.   // Run LVGL
  233.   //delay(10);
  234. }
  235.  
  236. int decodeCheckEngine(uint16_t value) {
  237.   int cel_codes = 0; string cel_names = "";
  238.   if (value == 0) {
  239.     return 0;
  240.   }
  241.   else {
  242.     Serial.print("CEL Codes: ");
  243.     if (value & (1 << 0)) {
  244.       cel_codes++;  // Bit 0
  245.       Serial.print("CLT ");
  246.       cel_names = "CLT ";
  247.     }
  248.     if (value & (1 << 1)) {
  249.       cel_codes++;  // Bit 1
  250.       Serial.print("IAT ");
  251.       cel_names += "IAT ";
  252.     }
  253.     if (value & (1 << 2)) {
  254.       cel_codes++;  // Bit 2
  255.       Serial.print("MAP ");
  256.       cel_names += "MAP ";
  257.     }
  258.     if (value & (1 << 3)) {
  259.       cel_codes++;  // Bit 3
  260.       Serial.print("WBO ");
  261.       cel_names += "WBO ";
  262.     }
  263.     if (value & (1 << 8)) {
  264.       cel_codes++;  // Bit 8
  265.       Serial.print("FF SENSOR ");
  266.       cel_names += "FF SENSOR ";
  267.     }
  268.     if (value & (1 << 9)) {
  269.       cel_codes++;  // Bit 9
  270.       Serial.print("DBW ");
  271.       cel_names += "DBW ";
  272.     }
  273.     if (value & (1 << 10)) {
  274.       cel_codes++;  // Bit 10
  275.       Serial.print("FPR ");
  276.       cel_names += "FPR ";
  277.     }
  278.  
  279.     //Serial.print("Total CEL Codes: " + cel_codes);
  280.     Serial.println();
  281.  
  282.     lv_table_set_cell_value(table, 5, 1, cel_names.c_str());
  283.  
  284.     return cel_codes;
  285.   }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment