From 260b489352137b94eaee854565ba5d0ef90ee8ea Mon Sep 17 00:00:00 2001 From: Chad Date: Sun, 29 Mar 2026 15:44:49 -0500 Subject: [PATCH] Initial commit: Bitcoin dashboard with working WiFi, keyboard, and price fetching for BTC, Gold, Silver, MSTR, STRC, NASDAQ, DOW, SPX --- include/lgfx_config.hpp | 108 +++++++++++ include/lv_conf.h | 145 +++++++++++++++ lv_conf.h | 145 +++++++++++++++ platformio.ini | 30 ++++ src/lv_conf.h | 145 +++++++++++++++ src/main.cpp | 384 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 957 insertions(+) create mode 100644 include/lgfx_config.hpp create mode 100644 include/lv_conf.h create mode 100644 lv_conf.h create mode 100644 platformio.ini create mode 100644 src/lv_conf.h create mode 100644 src/main.cpp diff --git a/include/lgfx_config.hpp b/include/lgfx_config.hpp new file mode 100644 index 0000000..8ace796 --- /dev/null +++ b/include/lgfx_config.hpp @@ -0,0 +1,108 @@ +/** + * LGFX Display Configuration for Elecrow 7-inch HMI Display + * Based on working Arduino example + */ + +#ifndef LGFX_CONFIG_HPP +#define LGFX_CONFIG_HPP + +#define LGFX_USE_V1 +#include +#include +#include +#include + +class LGFX : public lgfx::LGFX_Device { +public: + lgfx::Bus_RGB _bus_instance; + lgfx::Panel_RGB _panel_instance; + lgfx::Light_PWM _light_instance; + lgfx::Touch_GT911 _touch_instance; + + LGFX(void) { + { + auto cfg = _panel_instance.config(); + cfg.memory_width = 800; + cfg.memory_height = 480; + cfg.panel_width = 800; + cfg.panel_height = 480; + cfg.offset_x = 0; + cfg.offset_y = 0; + _panel_instance.config(cfg); + } + + { + auto cfg = _bus_instance.config(); + cfg.panel = &_panel_instance; + + cfg.pin_d0 = GPIO_NUM_15; + cfg.pin_d1 = GPIO_NUM_7; + cfg.pin_d2 = GPIO_NUM_6; + cfg.pin_d3 = GPIO_NUM_5; + cfg.pin_d4 = GPIO_NUM_4; + cfg.pin_d5 = GPIO_NUM_9; + cfg.pin_d6 = GPIO_NUM_46; + cfg.pin_d7 = GPIO_NUM_3; + cfg.pin_d8 = GPIO_NUM_8; + cfg.pin_d9 = GPIO_NUM_16; + cfg.pin_d10 = GPIO_NUM_1; + cfg.pin_d11 = GPIO_NUM_14; + cfg.pin_d12 = GPIO_NUM_21; + cfg.pin_d13 = GPIO_NUM_47; + cfg.pin_d14 = GPIO_NUM_48; + cfg.pin_d15 = GPIO_NUM_45; + + cfg.pin_henable = GPIO_NUM_41; + cfg.pin_vsync = GPIO_NUM_40; + cfg.pin_hsync = GPIO_NUM_39; + cfg.pin_pclk = GPIO_NUM_0; + cfg.freq_write = 12000000; + + cfg.hsync_polarity = 0; + cfg.hsync_front_porch = 40; + cfg.hsync_pulse_width = 48; + cfg.hsync_back_porch = 40; + + cfg.vsync_polarity = 0; + cfg.vsync_front_porch = 1; + cfg.vsync_pulse_width = 31; + cfg.vsync_back_porch = 13; + + cfg.pclk_active_neg = 1; + cfg.de_idle_high = 0; + cfg.pclk_idle_high = 0; + + _bus_instance.config(cfg); + } + _panel_instance.setBus(&_bus_instance); + + { + auto cfg = _light_instance.config(); + cfg.pin_bl = GPIO_NUM_2; + _light_instance.config(cfg); + } + _panel_instance.light(&_light_instance); + + { + auto cfg = _touch_instance.config(); + cfg.x_min = 0; + cfg.x_max = 799; + cfg.y_min = 0; + cfg.y_max = 479; + cfg.pin_int = -1; + cfg.pin_rst = -1; + cfg.bus_shared = false; + cfg.offset_rotation = 0; + cfg.i2c_port = I2C_NUM_1; + cfg.pin_sda = GPIO_NUM_19; + cfg.pin_scl = GPIO_NUM_20; + cfg.freq = 400000; + cfg.i2c_addr = 0x14; + _touch_instance.config(cfg); + _panel_instance.setTouch(&_touch_instance); + } + setPanel(&_panel_instance); + } +}; + +#endif // LGFX_CONFIG_HPP diff --git a/include/lv_conf.h b/include/lv_conf.h new file mode 100644 index 0000000..1eac01b --- /dev/null +++ b/include/lv_conf.h @@ -0,0 +1,145 @@ +/** + * LVGL Configuration for Bitcoin Dashboard + * Target: Elecrow 7-inch HMI Display (800x480) + */ + +#ifndef LV_CONF_H +#define LV_CONF_H + +#include + +#define LV_COLOR_DEPTH 16 +#define LV_COLOR_16_SWAP 0 + +#define LV_HOR_RES_MAX 800 +#define LV_VER_RES_MAX 480 + +#define LV_DPI_DEF 100 + +#define LV_DISP_DEF_REFR_PERIOD 16 + +#define LV_TICK_CUSTOM 1 +#define LV_TICK_CUSTOM_INCLUDE "Arduino.h" +#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) + +#define LV_MEM_CUSTOM 1 +#define LV_MEM_CUSTOM_INCLUDE "Arduino.h" +#define LV_MEM_CUSTOM_ALLOC malloc +#define LV_MEM_CUSTOM_FREE free + +#define LV_MEM_SIZE (64 * 1024U) +#define LV_MEM_ADR 0 + +#define LV_DRAW_CPU_ARM_ALIGN 4 + +#define LV_USE_DRAW_MASKS 1 +#define LV_USE_DRAW_SW 1 +#define LV_DRAW_SW_DRAW_UNIT_CNT 2 +#define LV_USE_DRAW_SW_COMPLEX_GRADIENTS 1 +#define LV_USE_DRAW_SW_SHADOW 1 +#define LV_USE_DRAW_SW_BLUR 1 +#define LV_USE_DRAW_SW_ARGB_TO_RGB565 1 + +#define LV_USE_ASSERT_NULL 1 +#define LV_USE_ASSERT_MALLOC 1 + +#define LV_USE_MSG 1 +#define LV_USE_FS_STDIO 0 +#define LV_USE_FS_FATFS 0 +#define LV_USE_FS_LITTLEFS 0 +#define LV_USE_LODEPNG 0 +#define LV_USE_LIBPNG 0 +#define LV_USE_BMP 0 +#define LV_USE_RLE 0 +#define LV_USE_QRCODE 0 +#define LV_USE_BARCODE 0 +#define LV_USE_TJPGD 0 +#define LV_USE_GIF 0 + +#define LV_USE_FLEX 1 +#define LV_USE_GRID 1 + +#define LV_USE_SNAPSHOT 1 +#define LV_USE_SYSMON 0 +#define LV_USE_PERF_MONITOR 0 +#define LV_USE_MEM_MONITOR 0 + +#define LV_THEME_DEFAULT_DARK 0 +#define LV_THEME_DEFAULT_GROW 1 + +#define LV_USE_THEME_DEFAULT 1 +#define LV_USE_THEME_BASIC 1 +#define LV_USE_THEME_MONO 1 + +#define LV_USE_ARC 1 +#define LV_USE_BAR 1 +#define LV_USE_BTN 1 +#define LV_USE_BTNMATRIX 1 +#define LV_USE_CALENDAR 1 +#define LV_USE_CANVAS 1 +#define LV_USE_CHART 1 +#define LV_USE_CHECKBOX 1 +#define LV_USE_DROPDOWN 1 +#define LV_USE_IMG 1 +#define LV_USE_IMGBTN 1 +#define LV_USE_KEYBOARD 1 +#define LV_USE_LABEL 1 +#define LV_LABEL_TEXT_SELECTION 1 +#define LV_LABEL_LONG_TXT_HINT 1 +#define LV_USE_LED 1 +#define LV_USE_LINE 1 +#define LV_USE_LIST 1 +#define LV_USE_MENU 1 +#define LV_USE_METER 1 +#define LV_USE_MSGBOX 1 +#define LV_USE_ROLLER 1 +#define LV_USE_SCALE 1 +#define LV_USE_SLIDER 1 +#define LV_USE_SPAN 1 +#define LV_USE_SPINBOX 1 +#define LV_USE_SPINNER 1 +#define LV_USE_SWITCH 1 +#define LV_USE_TABVIEW 1 +#define LV_USE_TABLE 1 +#define LV_USE_TEXTAREA 1 +#define LV_USE_TILEVIEW 1 +#define LV_USE_WIN 1 + +#define LV_FONT_MONTSERRAT_12 1 +#define LV_FONT_MONTSERRAT_14 1 +#define LV_FONT_MONTSERRAT_16 1 +#define LV_FONT_MONTSERRAT_18 1 +#define LV_FONT_MONTSERRAT_20 1 +#define LV_FONT_MONTSERRAT_22 1 +#define LV_FONT_MONTSERRAT_24 1 +#define LV_FONT_MONTSERRAT_26 1 +#define LV_FONT_MONTSERRAT_28 1 +#define LV_FONT_MONTSERRAT_30 1 +#define LV_FONT_MONTSERRAT_32 1 +#define LV_FONT_MONTSERRAT_34 1 +#define LV_FONT_MONTSERRAT_36 1 + +#define LV_FONT_FMT_TXT_LARGE 1 +#define LV_USE_FONT_COMPRESSED 1 +#define LV_FONT_DEFAULT &lv_font_montserrat_14 + +#define LV_TXT_ENC_UTF8 1 +#define LV_TXT_BREAK_CHARS " ,.;:-_)]}" +#define LV_TXT_LINE_BREAK_LONG_LEN 12 +#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3 +#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 3 +#define LV_TXT_COLOR_CMD "#" + +#define LV_USE_INDEV_SCROLLBAR 1 +#define LV_USE_INDEV_BUTTON 1 +#define LV_USE_INDEV_KEYPAD 1 +#define LV_USE_INDEV_ENCODER 1 + +#define LV_USE_GROUP 1 + +#define LV_USE_ANIM 1 +#define LV_ANIM_HOLD_COUNT 1 + +#define LV_GRADIENT_MAX_STOPS 2 + +#endif // LV_CONF_H diff --git a/lv_conf.h b/lv_conf.h new file mode 100644 index 0000000..1eac01b --- /dev/null +++ b/lv_conf.h @@ -0,0 +1,145 @@ +/** + * LVGL Configuration for Bitcoin Dashboard + * Target: Elecrow 7-inch HMI Display (800x480) + */ + +#ifndef LV_CONF_H +#define LV_CONF_H + +#include + +#define LV_COLOR_DEPTH 16 +#define LV_COLOR_16_SWAP 0 + +#define LV_HOR_RES_MAX 800 +#define LV_VER_RES_MAX 480 + +#define LV_DPI_DEF 100 + +#define LV_DISP_DEF_REFR_PERIOD 16 + +#define LV_TICK_CUSTOM 1 +#define LV_TICK_CUSTOM_INCLUDE "Arduino.h" +#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) + +#define LV_MEM_CUSTOM 1 +#define LV_MEM_CUSTOM_INCLUDE "Arduino.h" +#define LV_MEM_CUSTOM_ALLOC malloc +#define LV_MEM_CUSTOM_FREE free + +#define LV_MEM_SIZE (64 * 1024U) +#define LV_MEM_ADR 0 + +#define LV_DRAW_CPU_ARM_ALIGN 4 + +#define LV_USE_DRAW_MASKS 1 +#define LV_USE_DRAW_SW 1 +#define LV_DRAW_SW_DRAW_UNIT_CNT 2 +#define LV_USE_DRAW_SW_COMPLEX_GRADIENTS 1 +#define LV_USE_DRAW_SW_SHADOW 1 +#define LV_USE_DRAW_SW_BLUR 1 +#define LV_USE_DRAW_SW_ARGB_TO_RGB565 1 + +#define LV_USE_ASSERT_NULL 1 +#define LV_USE_ASSERT_MALLOC 1 + +#define LV_USE_MSG 1 +#define LV_USE_FS_STDIO 0 +#define LV_USE_FS_FATFS 0 +#define LV_USE_FS_LITTLEFS 0 +#define LV_USE_LODEPNG 0 +#define LV_USE_LIBPNG 0 +#define LV_USE_BMP 0 +#define LV_USE_RLE 0 +#define LV_USE_QRCODE 0 +#define LV_USE_BARCODE 0 +#define LV_USE_TJPGD 0 +#define LV_USE_GIF 0 + +#define LV_USE_FLEX 1 +#define LV_USE_GRID 1 + +#define LV_USE_SNAPSHOT 1 +#define LV_USE_SYSMON 0 +#define LV_USE_PERF_MONITOR 0 +#define LV_USE_MEM_MONITOR 0 + +#define LV_THEME_DEFAULT_DARK 0 +#define LV_THEME_DEFAULT_GROW 1 + +#define LV_USE_THEME_DEFAULT 1 +#define LV_USE_THEME_BASIC 1 +#define LV_USE_THEME_MONO 1 + +#define LV_USE_ARC 1 +#define LV_USE_BAR 1 +#define LV_USE_BTN 1 +#define LV_USE_BTNMATRIX 1 +#define LV_USE_CALENDAR 1 +#define LV_USE_CANVAS 1 +#define LV_USE_CHART 1 +#define LV_USE_CHECKBOX 1 +#define LV_USE_DROPDOWN 1 +#define LV_USE_IMG 1 +#define LV_USE_IMGBTN 1 +#define LV_USE_KEYBOARD 1 +#define LV_USE_LABEL 1 +#define LV_LABEL_TEXT_SELECTION 1 +#define LV_LABEL_LONG_TXT_HINT 1 +#define LV_USE_LED 1 +#define LV_USE_LINE 1 +#define LV_USE_LIST 1 +#define LV_USE_MENU 1 +#define LV_USE_METER 1 +#define LV_USE_MSGBOX 1 +#define LV_USE_ROLLER 1 +#define LV_USE_SCALE 1 +#define LV_USE_SLIDER 1 +#define LV_USE_SPAN 1 +#define LV_USE_SPINBOX 1 +#define LV_USE_SPINNER 1 +#define LV_USE_SWITCH 1 +#define LV_USE_TABVIEW 1 +#define LV_USE_TABLE 1 +#define LV_USE_TEXTAREA 1 +#define LV_USE_TILEVIEW 1 +#define LV_USE_WIN 1 + +#define LV_FONT_MONTSERRAT_12 1 +#define LV_FONT_MONTSERRAT_14 1 +#define LV_FONT_MONTSERRAT_16 1 +#define LV_FONT_MONTSERRAT_18 1 +#define LV_FONT_MONTSERRAT_20 1 +#define LV_FONT_MONTSERRAT_22 1 +#define LV_FONT_MONTSERRAT_24 1 +#define LV_FONT_MONTSERRAT_26 1 +#define LV_FONT_MONTSERRAT_28 1 +#define LV_FONT_MONTSERRAT_30 1 +#define LV_FONT_MONTSERRAT_32 1 +#define LV_FONT_MONTSERRAT_34 1 +#define LV_FONT_MONTSERRAT_36 1 + +#define LV_FONT_FMT_TXT_LARGE 1 +#define LV_USE_FONT_COMPRESSED 1 +#define LV_FONT_DEFAULT &lv_font_montserrat_14 + +#define LV_TXT_ENC_UTF8 1 +#define LV_TXT_BREAK_CHARS " ,.;:-_)]}" +#define LV_TXT_LINE_BREAK_LONG_LEN 12 +#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3 +#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 3 +#define LV_TXT_COLOR_CMD "#" + +#define LV_USE_INDEV_SCROLLBAR 1 +#define LV_USE_INDEV_BUTTON 1 +#define LV_USE_INDEV_KEYPAD 1 +#define LV_USE_INDEV_ENCODER 1 + +#define LV_USE_GROUP 1 + +#define LV_USE_ANIM 1 +#define LV_ANIM_HOLD_COUNT 1 + +#define LV_GRADIENT_MAX_STOPS 2 + +#endif // LV_CONF_H diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..b3cd5b8 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,30 @@ +; PlatformIO Project Configuration File +; Bitcoin Dashboard for Elecrow 7-inch HMI Display +; Based on working Arduino example + +[env:elecrow_7inch_hmi] +platform = espressif32@6.8.1 +board = esp32-s3-devkitc-1 +framework = arduino + +; Libraries +lib_deps = + lovyan03/LovyanGFX@^1.1.8 + +monitor_speed = 115200 + +; Critical settings for the N4R8 module (from working example) +board_upload.flash_size = 4MB +board_build.arduino.memory_type = qio_opi +board_build.f_flash = 80000000L +board_build.flash_mode = qio + +; Use Huge APP partition (3MB No OTA/1MB SPIFFS) +board_build.partitions = huge_app.csv + +; Required build flags for S3 display stability +build_flags = + -DARDUINO_USB_MODE=1 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DBOARD_HAS_PSRAM + -mfix-esp32-psram-cache-issue diff --git a/src/lv_conf.h b/src/lv_conf.h new file mode 100644 index 0000000..1eac01b --- /dev/null +++ b/src/lv_conf.h @@ -0,0 +1,145 @@ +/** + * LVGL Configuration for Bitcoin Dashboard + * Target: Elecrow 7-inch HMI Display (800x480) + */ + +#ifndef LV_CONF_H +#define LV_CONF_H + +#include + +#define LV_COLOR_DEPTH 16 +#define LV_COLOR_16_SWAP 0 + +#define LV_HOR_RES_MAX 800 +#define LV_VER_RES_MAX 480 + +#define LV_DPI_DEF 100 + +#define LV_DISP_DEF_REFR_PERIOD 16 + +#define LV_TICK_CUSTOM 1 +#define LV_TICK_CUSTOM_INCLUDE "Arduino.h" +#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) + +#define LV_MEM_CUSTOM 1 +#define LV_MEM_CUSTOM_INCLUDE "Arduino.h" +#define LV_MEM_CUSTOM_ALLOC malloc +#define LV_MEM_CUSTOM_FREE free + +#define LV_MEM_SIZE (64 * 1024U) +#define LV_MEM_ADR 0 + +#define LV_DRAW_CPU_ARM_ALIGN 4 + +#define LV_USE_DRAW_MASKS 1 +#define LV_USE_DRAW_SW 1 +#define LV_DRAW_SW_DRAW_UNIT_CNT 2 +#define LV_USE_DRAW_SW_COMPLEX_GRADIENTS 1 +#define LV_USE_DRAW_SW_SHADOW 1 +#define LV_USE_DRAW_SW_BLUR 1 +#define LV_USE_DRAW_SW_ARGB_TO_RGB565 1 + +#define LV_USE_ASSERT_NULL 1 +#define LV_USE_ASSERT_MALLOC 1 + +#define LV_USE_MSG 1 +#define LV_USE_FS_STDIO 0 +#define LV_USE_FS_FATFS 0 +#define LV_USE_FS_LITTLEFS 0 +#define LV_USE_LODEPNG 0 +#define LV_USE_LIBPNG 0 +#define LV_USE_BMP 0 +#define LV_USE_RLE 0 +#define LV_USE_QRCODE 0 +#define LV_USE_BARCODE 0 +#define LV_USE_TJPGD 0 +#define LV_USE_GIF 0 + +#define LV_USE_FLEX 1 +#define LV_USE_GRID 1 + +#define LV_USE_SNAPSHOT 1 +#define LV_USE_SYSMON 0 +#define LV_USE_PERF_MONITOR 0 +#define LV_USE_MEM_MONITOR 0 + +#define LV_THEME_DEFAULT_DARK 0 +#define LV_THEME_DEFAULT_GROW 1 + +#define LV_USE_THEME_DEFAULT 1 +#define LV_USE_THEME_BASIC 1 +#define LV_USE_THEME_MONO 1 + +#define LV_USE_ARC 1 +#define LV_USE_BAR 1 +#define LV_USE_BTN 1 +#define LV_USE_BTNMATRIX 1 +#define LV_USE_CALENDAR 1 +#define LV_USE_CANVAS 1 +#define LV_USE_CHART 1 +#define LV_USE_CHECKBOX 1 +#define LV_USE_DROPDOWN 1 +#define LV_USE_IMG 1 +#define LV_USE_IMGBTN 1 +#define LV_USE_KEYBOARD 1 +#define LV_USE_LABEL 1 +#define LV_LABEL_TEXT_SELECTION 1 +#define LV_LABEL_LONG_TXT_HINT 1 +#define LV_USE_LED 1 +#define LV_USE_LINE 1 +#define LV_USE_LIST 1 +#define LV_USE_MENU 1 +#define LV_USE_METER 1 +#define LV_USE_MSGBOX 1 +#define LV_USE_ROLLER 1 +#define LV_USE_SCALE 1 +#define LV_USE_SLIDER 1 +#define LV_USE_SPAN 1 +#define LV_USE_SPINBOX 1 +#define LV_USE_SPINNER 1 +#define LV_USE_SWITCH 1 +#define LV_USE_TABVIEW 1 +#define LV_USE_TABLE 1 +#define LV_USE_TEXTAREA 1 +#define LV_USE_TILEVIEW 1 +#define LV_USE_WIN 1 + +#define LV_FONT_MONTSERRAT_12 1 +#define LV_FONT_MONTSERRAT_14 1 +#define LV_FONT_MONTSERRAT_16 1 +#define LV_FONT_MONTSERRAT_18 1 +#define LV_FONT_MONTSERRAT_20 1 +#define LV_FONT_MONTSERRAT_22 1 +#define LV_FONT_MONTSERRAT_24 1 +#define LV_FONT_MONTSERRAT_26 1 +#define LV_FONT_MONTSERRAT_28 1 +#define LV_FONT_MONTSERRAT_30 1 +#define LV_FONT_MONTSERRAT_32 1 +#define LV_FONT_MONTSERRAT_34 1 +#define LV_FONT_MONTSERRAT_36 1 + +#define LV_FONT_FMT_TXT_LARGE 1 +#define LV_USE_FONT_COMPRESSED 1 +#define LV_FONT_DEFAULT &lv_font_montserrat_14 + +#define LV_TXT_ENC_UTF8 1 +#define LV_TXT_BREAK_CHARS " ,.;:-_)]}" +#define LV_TXT_LINE_BREAK_LONG_LEN 12 +#define LV_TXT_LINE_BREAK_LONG_PRE_MIN_LEN 3 +#define LV_TXT_LINE_BREAK_LONG_POST_MIN_LEN 3 +#define LV_TXT_COLOR_CMD "#" + +#define LV_USE_INDEV_SCROLLBAR 1 +#define LV_USE_INDEV_BUTTON 1 +#define LV_USE_INDEV_KEYPAD 1 +#define LV_USE_INDEV_ENCODER 1 + +#define LV_USE_GROUP 1 + +#define LV_USE_ANIM 1 +#define LV_ANIM_HOLD_COUNT 1 + +#define LV_GRADIENT_MAX_STOPS 2 + +#endif // LV_CONF_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0084ac9 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,384 @@ +/** + * Bitcoin Dashboard for Elecrow 7-inch HMI Display + * Simplified version with better WiFi/API debugging + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 480 + +#define TFT_WHITE 0xFFFF +#define TFT_BLACK 0x0000 +#define TFT_RED 0xF800 +#define TFT_GREEN 0x07E0 +#define TFT_BLUE 0x001F +#define TFT_ORANGE 0xFD40 +#define TFT_GRAY 0x7BEF + +class LGFX : public lgfx::LGFX_Device { +public: + lgfx::Bus_RGB _bus_instance; + lgfx::Panel_RGB _panel_instance; + lgfx::Light_PWM _light_instance; + lgfx::Touch_GT911 _touch_instance; + + LGFX(void) { + { + auto cfg = _panel_instance.config(); + cfg.memory_width = 800; + cfg.memory_height = 480; + cfg.panel_width = 800; + cfg.panel_height = 480; + cfg.offset_x = 0; + cfg.offset_y = 0; + _panel_instance.config(cfg); + } + + { + auto cfg = _bus_instance.config(); + cfg.panel = &_panel_instance; + cfg.pin_d0 = GPIO_NUM_15; + cfg.pin_d1 = GPIO_NUM_7; + cfg.pin_d2 = GPIO_NUM_6; + cfg.pin_d3 = GPIO_NUM_5; + cfg.pin_d4 = GPIO_NUM_4; + cfg.pin_d5 = GPIO_NUM_9; + cfg.pin_d6 = GPIO_NUM_46; + cfg.pin_d7 = GPIO_NUM_3; + cfg.pin_d8 = GPIO_NUM_8; + cfg.pin_d9 = GPIO_NUM_16; + cfg.pin_d10 = GPIO_NUM_1; + cfg.pin_d11 = GPIO_NUM_14; + cfg.pin_d12 = GPIO_NUM_21; + cfg.pin_d13 = GPIO_NUM_47; + cfg.pin_d14 = GPIO_NUM_48; + cfg.pin_d15 = GPIO_NUM_45; + cfg.pin_henable = GPIO_NUM_41; + cfg.pin_vsync = GPIO_NUM_40; + cfg.pin_hsync = GPIO_NUM_39; + cfg.pin_pclk = GPIO_NUM_0; + cfg.freq_write = 12000000; + cfg.hsync_polarity = 0; + cfg.hsync_front_porch = 40; + cfg.hsync_pulse_width = 48; + cfg.hsync_back_porch = 40; + cfg.vsync_polarity = 0; + cfg.vsync_front_porch = 1; + cfg.vsync_pulse_width = 31; + cfg.vsync_back_porch = 13; + cfg.pclk_active_neg = 1; + cfg.de_idle_high = 0; + cfg.pclk_idle_high = 0; + _bus_instance.config(cfg); + } + _panel_instance.setBus(&_bus_instance); + + { + auto cfg = _light_instance.config(); + cfg.pin_bl = GPIO_NUM_2; + _light_instance.config(cfg); + } + _panel_instance.light(&_light_instance); + + { + auto cfg = _touch_instance.config(); + cfg.x_min = 0; + cfg.x_max = 799; + cfg.y_min = 0; + cfg.y_max = 479; + cfg.pin_int = -1; + cfg.pin_rst = -1; + cfg.bus_shared = false; + cfg.offset_rotation = 0; + cfg.i2c_port = I2C_NUM_1; + cfg.pin_sda = GPIO_NUM_19; + cfg.pin_scl = GPIO_NUM_20; + cfg.freq = 400000; + cfg.i2c_addr = 0x14; + _touch_instance.config(cfg); + _panel_instance.setTouch(&_touch_instance); + } + setPanel(&_panel_instance); + } +}; + +static LGFX lcd; +static Preferences preferences; + +// Price data +double btcPrice = 0; +double goldPrice = 0; +double silverPrice = 0; +double mstrPrice = 0; +double strtPrice = 0; +double nasdaqPrice = 0; +double dowPrice = 0; +double spxPrice = 0; + +unsigned long lastRefresh = 0; +const unsigned long REFRESH_INTERVAL = 300000; + +bool fetchPrice(const char* symbol, double* result) { + WiFiClientSecure client; + client.setInsecure(); + + HTTPClient http; + String url = "https://query1.finance.yahoo.com/v8/finance/chart/" + String(symbol) + "?interval=1d&range=1d"; + + Serial.println("Fetching: " + String(symbol)); + Serial.println("URL: " + url); + + if (!http.begin(client, url)) { + Serial.println("HTTP begin failed"); + return false; + } + + http.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"); + http.setTimeout(15000); + + int httpCode = http.GET(); + Serial.println("HTTP code: " + String(httpCode)); + + if (httpCode != HTTP_CODE_OK) { + Serial.println("HTTP error: " + http.errorToString(httpCode)); + http.end(); + return false; + } + + String payload = http.getString(); + http.end(); + + Serial.println("Response length: " + String(payload.length())); + + // Parse regularMarketPrice from meta section + // Format: "regularMarketPrice":66647.29 + String searchStr = "\"regularMarketPrice\":"; + int idx = payload.indexOf(searchStr); + + if (idx == -1) { + Serial.println("regularMarketPrice not found"); + return false; + } + + int start = idx + searchStr.length(); + int end = payload.indexOf(",", start); + if (end == -1) end = payload.indexOf("}", start); + + if (end <= start) { + Serial.println("Could not find end of price"); + return false; + } + + String priceStr = payload.substring(start, end); + Serial.println("Price string: " + priceStr); + + *result = priceStr.toDouble(); + Serial.println("Parsed: " + String(*result)); + + return true; +} + +void updatePrices() { + Serial.println("\n=== Updating prices ==="); + Serial.println("WiFi status: " + String(WiFi.status())); + Serial.println("WiFi connected: " + String(WiFi.status() == WL_CONNECTED ? "YES" : "NO")); + + if (WiFi.status() != WL_CONNECTED) { + Serial.println("WiFi not connected, skipping update"); + return; + } + + Serial.println("IP: " + WiFi.localIP().toString()); + + fetchPrice("BTC-USD", &btcPrice); + fetchPrice("GC=F", &goldPrice); + fetchPrice("SI=F", &silverPrice); + fetchPrice("MSTR", &mstrPrice); + fetchPrice("STRC", &strtPrice); + fetchPrice("^IXIC", &nasdaqPrice); + fetchPrice("^DJI", &dowPrice); + fetchPrice("^GSPC", &spxPrice); + + Serial.println("=== Update complete ===\n"); +} + +void drawDashboard() { + lcd.fillScreen(TFT_BLACK); + + // Title bar + lcd.fillRect(0, 0, SCREEN_WIDTH, 45, TFT_ORANGE); + lcd.setTextColor(TFT_BLACK, TFT_ORANGE); + lcd.setTextSize(3); + lcd.setTextDatum(textdatum_t::middle_center); + lcd.drawString("BITCOIN DASHBOARD", SCREEN_WIDTH / 2, 22); + + // Column positions + int col1X = 20; + int col2X = 280; + int col3X = 540; + int startY = 60; + + // Column separators + lcd.drawLine(265, startY, 265, SCREEN_HEIGHT - 35, TFT_GRAY); + lcd.drawLine(525, startY, 525, SCREEN_HEIGHT - 35, TFT_GRAY); + + // Column 1: HARD MONEY + lcd.setTextColor(TFT_ORANGE, TFT_BLACK); + lcd.setTextSize(2); + lcd.setTextDatum(textdatum_t::top_left); + lcd.drawString("HARD MONEY", col1X, startY); + + lcd.setTextColor(TFT_WHITE, TFT_BLACK); + lcd.drawString("Bitcoin:", col1X, startY + 35); + lcd.drawString(btcPrice > 0 ? "$" + String((long)btcPrice) : "...", col1X, startY + 60); + + lcd.drawString("Gold:", col1X, startY + 95); + lcd.drawString(goldPrice > 0 ? "$" + String((long)goldPrice) : "...", col1X, startY + 120); + + lcd.drawString("Silver:", col1X, startY + 155); + lcd.drawString(silverPrice > 0 ? "$" + String((long)silverPrice) : "...", col1X, startY + 180); + + // Column 2: STOCKS + lcd.setTextColor(TFT_ORANGE, TFT_BLACK); + lcd.drawString("STOCKS", col2X, startY); + + lcd.setTextColor(TFT_WHITE, TFT_BLACK); + lcd.drawString("MSTR:", col2X, startY + 35); + lcd.drawString(mstrPrice > 0 ? "$" + String(mstrPrice, 2) : "...", col2X, startY + 60); + + lcd.drawString("STRC:", col2X, startY + 95); + lcd.drawString(strtPrice > 0 ? "$" + String(strtPrice, 2) : "...", col2X, startY + 120); + + // Column 3: INDICES + lcd.setTextColor(TFT_ORANGE, TFT_BLACK); + lcd.drawString("INDICES", col3X, startY); + + lcd.setTextColor(TFT_WHITE, TFT_BLACK); + lcd.drawString("NASDAQ:", col3X, startY + 35); + lcd.drawString(nasdaqPrice > 0 ? String((long)nasdaqPrice) : "...", col3X, startY + 60); + + lcd.drawString("DOW:", col3X, startY + 95); + lcd.drawString(dowPrice > 0 ? String((long)dowPrice) : "...", col3X, startY + 120); + + lcd.drawString("SPX:", col3X, startY + 155); + lcd.drawString(spxPrice > 0 ? String((long)spxPrice) : "...", col3X, startY + 180); + + // Status bar + lcd.fillRect(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 30, TFT_GRAY); + lcd.setTextColor(TFT_BLACK, TFT_GRAY); + lcd.setTextSize(1); + lcd.setTextDatum(textdatum_t::middle_left); + + String wifiStatus = WiFi.status() == WL_CONNECTED ? "Connected" : "DISCONNECTED"; + lcd.drawString("WiFi: " + wifiStatus, 10, SCREEN_HEIGHT - 15); + lcd.setTextDatum(textdatum_t::middle_center); + lcd.drawString("TOUCH TO REFRESH", SCREEN_WIDTH / 2, SCREEN_HEIGHT - 15); +} + +void setup() { + Serial.begin(115200); + delay(3000); + + Serial.println("\n\n=== STARTING ==="); + + lcd.init(); + lcd.setBrightness(255); + lcd.setRotation(0); + + lcd.fillScreen(TFT_WHITE); + delay(250); + lcd.fillScreen(TFT_RED); + delay(250); + lcd.fillScreen(TFT_GREEN); + delay(250); + lcd.fillScreen(TFT_BLUE); + delay(250); + lcd.fillScreen(TFT_BLACK); + + lcd.setTextSize(2); + lcd.setTextColor(TFT_WHITE, TFT_BLACK); + lcd.setTextDatum(textdatum_t::middle_center); + lcd.drawString("Bitcoin Dashboard", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 40); + lcd.drawString("Starting...", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + + // Load WiFi credentials + preferences.begin("wifi", true); + String ssid = preferences.getString("ssid", ""); + String pass = preferences.getString("password", ""); + preferences.end(); + + if (ssid.length() > 0) { + Serial.println("Connecting to WiFi: " + ssid); + lcd.drawString("WiFi: " + ssid, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 40); + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid.c_str(), pass.c_str()); + + int attempts = 0; + while (WiFi.status() != WL_CONNECTED && attempts < 60) { + delay(500); + Serial.print("."); + attempts++; + } + Serial.println(); + + if (WiFi.status() == WL_CONNECTED) { + Serial.println("WiFi connected!"); + Serial.println("IP: " + WiFi.localIP().toString()); + lcd.drawString("Connected! IP:", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 80); + lcd.drawString(WiFi.localIP().toString(), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 110); + + delay(1000); + updatePrices(); + lastRefresh = millis(); + drawDashboard(); + return; + } + + Serial.println("WiFi connection failed"); + lcd.drawString("WiFi failed", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 80); + delay(2000); + } + + // Show WiFi setup screen + lcd.drawString("No WiFi configured", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 40); + delay(2000); + drawDashboard(); +} + +void loop() { + int32_t x, y; + + if (lcd.getTouch(&x, &y)) { + Serial.println("Touch detected - refreshing..."); + lcd.setTextColor(TFT_WHITE, TFT_BLACK); + lcd.setTextSize(2); + lcd.setTextDatum(textdatum_t::middle_center); + lcd.fillRect(0, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH, 40, TFT_BLACK); + lcd.drawString("Refreshing...", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + + updatePrices(); + lastRefresh = millis(); + drawDashboard(); + + delay(500); + } + + // Auto-refresh every 5 minutes + if (millis() - lastRefresh > REFRESH_INTERVAL) { + updatePrices(); + lastRefresh = millis(); + drawDashboard(); + } + + delay(100); +}