espd

ESP Final Clean 20250410

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