Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #define LV_COLOR_16_SWAP 1
- #include <lvgl.h>
- #include <TFT_eSPI.h>
- #include "CST820.h"
- #include "BluetoothSerial.h"
- #include <string>
- #include <stdexcept>
- using namespace std;
- //#define USE_NAME
- const char *pin = "1234";
- String myBtName = "ESP32-BT-Master";
- #if !defined(CONFIG_BT_SPP_ENABLED)
- #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
- #endif
- BluetoothSerial SerialBT;
- #ifdef USE_NAME
- String slaveName = "EMUCANBT_SPP";
- #else
- uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };
- #endif
- const int backLightPin = 27;
- const int buzzerPin = 22;
- bool buzzerOn = false;
- bool btIconSts = false;
- static lv_style_t style_bt;
- static bool style_initialized = false;
- int rpm;
- int spd;
- float afr;
- float mapR;
- float boost;
- int tps;
- int clt;
- int ign;
- int inj;
- float bat;
- int cel;
- unsigned long previousMillis = 0;
- const unsigned long reconnectInterval = 5000;
- /* Screen resolution */
- static const uint16_t screenWidth = 320; // width after rotation 1
- static const uint16_t screenHeight = 240; // height after rotation 1
- static const uint16_t buf_height = 80; // smaller buffer height to save RAM
- /* Touch screen I2C pins */
- #define I2C_SDA 33
- #define I2C_SCL 32
- #define TP_RST 25
- #define TP_INT 21
- static lv_disp_draw_buf_t draw_buf;
- static lv_color_t *buf1;
- static lv_color_t *buf2;
- TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
- CST820 touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT); /* Touch instance */
- LV_FONT_DECLARE(lv_font_montserrat_14);
- LV_FONT_DECLARE(lv_font_montserrat_28);
- lv_obj_t *bt_icon_label;
- #if LV_USE_LOG != 0
- void my_print(const char *buf)
- {
- Serial.printf(buf);
- Serial.flush();
- }
- #endif
- /* Display flush */
- void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
- {
- uint32_t w = (area->x2 - area->x1 + 1);
- uint32_t h = (area->y2 - area->y1 + 1);
- tft.pushImageDMA(area->x1, area->y1, w, h, (uint16_t *)color_p);
- lv_disp_flush_ready(disp);
- }
- /* Read touchpad */
- void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
- {
- bool touched;
- uint8_t gesture;
- uint16_t rawX, rawY;
- touched = touch.getTouch(&rawX, &rawY, &gesture);
- if (!touched)
- {
- data->state = LV_INDEV_STATE_REL;
- }
- else
- {
- data->state = LV_INDEV_STATE_PR;
- // Rotate touch for tft.setRotation(1)
- data->point.x = rawY;
- data->point.y = screenHeight - 1 - rawX;
- }
- }
- void create_tabview_with_tables()
- {
- lv_obj_t *tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 40);
- // Set tabview background to black
- lv_obj_set_style_bg_color(tabview, lv_color_black(), 0);
- lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
- lv_obj_clear_flag(tabview, LV_OBJ_FLAG_SCROLLABLE);
- lv_obj_set_style_text_font(tabview, &lv_font_montserrat_14, LV_PART_ITEMS);
- // Create a reusable style for tables (black bg, white border)
- static lv_style_t style_table;
- lv_style_init(&style_table);
- lv_style_set_bg_color(&style_table, lv_color_black());
- lv_style_set_border_color(&style_table, lv_color_white());
- lv_style_set_border_width(&style_table, 2);
- lv_style_set_border_side(&style_table, LV_BORDER_SIDE_FULL);
- lv_style_set_text_color(&style_table, lv_color_white());
- // Also style the tab buttons to have white text on black
- lv_obj_t *tab_btns = lv_tabview_get_tab_btns(tabview);
- lv_obj_set_style_text_color(tab_btns, lv_color_white(), 0);
- lv_obj_set_style_bg_color(tab_btns, lv_color_black(), 0);
- // Add tabs
- lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
- lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
- lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Tab 3");
- // Function to create and style a table in a tab
- auto create_table = [&](lv_obj_t *parent) {
- lv_obj_t *table = lv_table_create(parent);
- lv_obj_add_style(table, &style_table, 0);
- lv_obj_set_size(table, 320, 200);
- lv_obj_align(table, LV_ALIGN_CENTER, 0, 0);
- lv_table_set_col_cnt(table, 4);
- lv_table_set_row_cnt(table, 4);
- return table;
- };
- // Create tables
- lv_obj_t *table1 = create_table(tab1);
- lv_table_set_cell_value(table1, 0, 0, "Item");
- lv_table_set_cell_value(table1, 0, 1, "Value");
- lv_table_set_cell_value(table1, 1, 0, "Speed");
- lv_table_set_cell_value(table1, 1, 1, "60 km/h");
- lv_table_set_cell_value(table1, 2, 0, "Temp");
- lv_table_set_cell_value(table1, 2, 1, "22 C");
- lv_obj_t *table2 = create_table(tab2);
- lv_table_set_cell_value(table2, 0, 0, "Item");
- lv_table_set_cell_value(table2, 0, 1, "Value");
- lv_table_set_cell_value(table2, 1, 0, "RPM");
- lv_table_set_cell_value(table2, 1, 1, "3000");
- lv_table_set_cell_value(table2, 2, 0, "Fuel");
- lv_table_set_cell_value(table2, 2, 1, "Full");
- lv_obj_t *table3 = create_table(tab3);
- lv_table_set_cell_value(table3, 0, 0, "Item");
- lv_table_set_cell_value(table3, 0, 1, "Value");
- lv_table_set_cell_value(table3, 1, 0, "Voltage");
- lv_table_set_cell_value(table3, 1, 1, "12.5V");
- lv_table_set_cell_value(table3, 2, 0, "Status");
- lv_table_set_cell_value(table3, 2, 1, "OK");
- lv_obj_set_style_text_font(table1, &lv_font_montserrat_14, LV_PART_ITEMS);
- }
- void setup() {
- tft.init();
- pinMode(backLightPin, OUTPUT);
- digitalWrite(backLightPin, LOW);
- uint16_t darkGray = ((30 & 0xF8) << 8) | ((30 & 0xFC) << 3) | (30 >> 3);
- tft.fillScreen(darkGray);
- tft.setRotation(1);
- // tft.begin();
- // tft.setRotation(1);
- tft.initDMA();
- touch.begin();
- Serial.begin(1000000);
- lv_init();
- buf1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * buf_height, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
- buf2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * buf_height, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
- lv_disp_draw_buf_init(&draw_buf, buf1, buf2, screenWidth * buf_height);
- static lv_disp_drv_t disp_drv;
- lv_disp_drv_init(&disp_drv);
- disp_drv.hor_res = screenWidth;
- disp_drv.ver_res = screenHeight;
- disp_drv.flush_cb = my_disp_flush;
- disp_drv.draw_buf = &draw_buf;
- lv_disp_drv_register(&disp_drv);
- static lv_indev_drv_t indev_drv;
- lv_indev_drv_init(&indev_drv);
- indev_drv.type = LV_INDEV_TYPE_POINTER;
- indev_drv.read_cb = my_touchpad_read;
- lv_indev_drv_register(&indev_drv);
- // Create the tab view layout
- create_tabview_with_tables();
- digitalWrite(27, HIGH);
- Serial.println("Setup done");
- tft.startWrite();
- }
- void loop()
- {
- lv_timer_handler();
- delay(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment