From c73a1fe19a2a7c21dd92888db78344002a0af800 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Tue, 28 May 2024 14:29:00 -0700 Subject: [PATCH 1/7] t-hmi first working: add touch, sdcard, remove vkbit --- README.md | 1 + platformio.ini | 3 +- src/NerdMinerV2.ino.cpp | 12 +- src/drivers/devices/lilygoT_HMI.h | 3 +- src/drivers/displays/t_hmiDisplayDriver.cpp | 204 ++++++++++++++++++-- src/drivers/storage/SDCard.cpp | 9 + src/monitor.cpp | 5 +- 7 files changed, 218 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3d30a3c..1213bf0 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Every time an stratum job notification is received miner update its current work - ESP32-C3 Devkit ([Board Info](https://docs.platformio.org/en/latest/boards/espressif32/esp32-c3-devkitm-1.html)) - ESP32-C3 Super Mini ([Board Info](https://docs.platformio.org/en/latest/boards/espressif32/seeed_xiao_esp32c3.html)) - Waveshare ESP32-S3-GEEK ([Board Info](https://www.waveshare.com/wiki/ESP32-S3-GEEK)) +- LILYGO T-HMI ([Aliexpress link\*](https://s.click.aliexpress.com/e/_oFII4s2)) / Dev support: @cosmicpsyop \*Affiliate links diff --git a/platformio.ini b/platformio.ini index 9555602..733fb38 100644 --- a/platformio.ini +++ b/platformio.ini @@ -751,10 +751,11 @@ build_flags = board_build.arduino.memory_type = qio_opi lib_deps = + https://github.com/liangyingy/arduino_xpt2046_library https://github.com/takkaO/OpenFontRender bblanchon/ArduinoJson@^6.21.2 https://github.com/tzapu/WiFiManager.git#v2.0.16-rc.2 - mathertel/OneButton @ ^2.0.3 + mathertel/OneButton @ ^2.5.0 arduino-libraries/NTPClient bodmer/TFT_eSPI @ ^2.5.31 https://github.com/achillhasler/TFT_eTouch diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index c2be3f8..48a8305 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -96,7 +96,11 @@ void setup() /******** SHOW LED INIT STATUS (devices without screen) *****/ mMonitor.NerdStatus = NM_waitingConfig; doLedStuff(0); - + +#ifdef NERDMINER_T_HMI + extern void t_hmiCheckForSDCardAndMoveToNVM(void); + t_hmiCheckForSDCardAndMoveToNVM(); +#endif /******** INIT WIFI ************/ init_WifiManager(); @@ -142,6 +146,9 @@ void app_error_fault_handler(void *arg) { // restart ESP32 esp_restart(); } +#ifdef NERDMINER_T_HMI +extern uint16_t t_hmiCheckForTouch(); +#endif void loop() { // keep watching the push buttons: @@ -153,6 +160,9 @@ void loop() { button2.tick(); #endif +#ifdef NERDMINER_T_HMI + t_hmiCheckForTouch(); +#endif wifiManagerProcess(); // avoid delays() in loop when non-blocking and other long running code vTaskDelay(50 / portTICK_PERIOD_MS); diff --git a/src/drivers/devices/lilygoT_HMI.h b/src/drivers/devices/lilygoT_HMI.h index ab384b8..556c067 100644 --- a/src/drivers/devices/lilygoT_HMI.h +++ b/src/drivers/devices/lilygoT_HMI.h @@ -4,6 +4,7 @@ #define T_HMI_DISPLAY #define PWR_EN_PIN (10) +#define PIN_ENABLE5V PWR_EN_PIN #define PWR_ON_PIN (14) #define BAT_ADC_PIN (5) #define BUTTON1_PIN (0) @@ -29,11 +30,9 @@ // sd card // 1-bit SD MMC -#ifdef DEFINE_SDMMC_1BIT #define SDMMC_CLK (12) #define SDMMC_CMD (11) #define SDMMC_D0 (13) -#endif #ifndef TFT_BL // XXX - defined in User_Setups/Setup207_LilyGo_T_HMI.h:37 diff --git a/src/drivers/displays/t_hmiDisplayDriver.cpp b/src/drivers/displays/t_hmiDisplayDriver.cpp index d3b0510..84c1ad8 100644 --- a/src/drivers/displays/t_hmiDisplayDriver.cpp +++ b/src/drivers/displays/t_hmiDisplayDriver.cpp @@ -1,7 +1,8 @@ #include "displayDriver.h" #ifdef T_HMI_DISPLAY - +#include +#include // https://github.com/liangyingy/arduino_xpt2046_library #include #include #include "media/images_320_170.h" @@ -22,15 +23,159 @@ OpenFontRender render; TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h TFT_eSprite background = TFT_eSprite(&tft); // Invoke library sprite - -SPIClass hSPI(HSPI); +#define TOUCH_ENABLE +//SPIClass hSPI(HSPI); // TFT_eTouch touch(tft, ETOUCH_CS, 0xFF, hSPI); +#ifdef TOUCH_ENABLE +XPT2046 touch = XPT2046(SPI, ETOUCH_CS, TOUCH_IRQ); +#endif + bool showbtcprice = false; extern monitor_data mMonitor; extern pool_data pData; extern DisplayDriver *currentDisplayDriver; +/*=============================================*/ +#include +#include +#include +#include "drivers/storage/nvMemory.h" +#include "drivers/storage/SDCard.h" +extern nvMemory nvMem; +extern TSettings Settings; + +void t_hmiCheckForSDCardAndMoveToNVM(void) +{ + // workaround removing use of class SDCard + // SCard SDCrd = SDCard() blows up in SDCard::initSDcard() + // Need to lower frequency to 20000 + SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); + delay(500); + bool rlst = SD_MMC.begin("/sd", true, false, 20000); + if (!rlst) + { + Serial.println("SD init failed"); + Serial.println("➸ No detected SdCard"); + } + else + { + Serial.println("SD init success"); + Serial.printf("➸ Detected SdCard insert: %.2f GB\r\n", SD_MMC.cardSize() / 1024.0 / 1024.0 / 1024.0); + + // SDCrd.SD2nvMemory(&nvMem, &Settings); + + if (SD_MMC.exists(JSON_CONFIG_FILE)) + { + // The file exists, reading and loading + File configFile = SD_MMC.open(JSON_CONFIG_FILE, "r"); + if (configFile) + { + StaticJsonDocument<512> json; + DeserializationError error = deserializeJson(json, configFile); + configFile.close(); + Serial.println("SDCard: Loading config file"); + if (!error) + { + serializeJsonPretty(json, Serial); + Serial.print('\n'); + TSettings *pSettings = &Settings; + pSettings->WifiSSID = json[JSON_KEY_SSID] | pSettings->WifiSSID; + pSettings->WifiPW = json[JSON_KEY_PASW] | pSettings->WifiPW; + pSettings->PoolAddress = json[JSON_KEY_POOLURL] | pSettings->PoolAddress; + strcpy(pSettings->PoolPassword, json[JSON_KEY_POOLPASS] | pSettings->PoolPassword); + strcpy(pSettings->BtcWallet, json[JSON_KEY_WALLETID] | pSettings->BtcWallet); + if (json.containsKey(JSON_KEY_POOLPORT)) + pSettings->PoolPort = json[JSON_KEY_POOLPORT].as(); + if (json.containsKey(JSON_KEY_TIMEZONE)) + pSettings->Timezone = json[JSON_KEY_TIMEZONE].as(); + if (json.containsKey(JSON_KEY_STATS2NV)) + pSettings->saveStats = json[JSON_KEY_STATS2NV].as(); + nvMem.saveConfig(pSettings); + WiFi.begin(pSettings->WifiSSID, pSettings->WifiPW); + Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); + ESP.restart(); + } + else + { + // Error loading JSON data + Serial.println("SDCard: Error parsing config file!"); + } + } + else + { + Serial.println("SDCard: Error opening config file!"); + } + } + else + { + Serial.println("SDCard: No config file available!"); + } + // delay(15000); + SD_MMC.end(); + Serial.println("SD close"); + } +} +/*=============================================*/ +unsigned int lower_switch = 1; +#ifdef TOUCH_ENABLE +extern void switchToNextScreen(void); + +unsigned long now = 0; +unsigned long currentTime; +bool debounce() { + if (now) now = millis(); + + unsigned long currentTime = millis(); + if (currentTime - now >= 2000) { + now = 0; + return true; + } + return false; + +}; + +uint16_t t_hmiCheckForTouch() +{ + uint16_t touch_x, touch_y, code = 0; + + if (touch.pressed()) { + touch_x = touch.RawX(); + touch_y = touch.RawY(); + // do something with the touch coordinates + /* + Serial.print("Touch coordinates: "); + Serial.print(touch_x); + Serial.print(", "); + Serial.println(touch_y); + */ + + // Perform actions based on touch coordinates + /* + if (y < y_min + (y_max - y_min) / 2) { + */ + if (touch_x < 200 + (1700 - 200) / 4) { + // bottom + code = 1; + if (debounce()) + lower_switch = 3 - lower_switch;; + } else { + // top + code = 2; + if (debounce()) + switchToNextScreen(); + } + } + if (code) { + if (code == 1) + Serial.print("Touch bottom\n"); + else + Serial.print("Touch top\n"); + } + return(code); +} +#endif + uint32_t readAdcVoltage(int pin) { esp_adc_cal_characteristics_t adc_chars; @@ -70,12 +215,32 @@ void t_hmiDisplay_Init(void) Serial.println("Initialise error"); return; } -/* XXX - Pass for first version + Serial.println(F("Initialize the touch screen")); - hSPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI, ETOUCH_CS); - TFT_eTouchBase::Calibation calibation = { 233, 3785, 3731, 120, 2 }; - touch.setCalibration(calibation); -*/ + #ifdef TOUCH_ENABLE + // different approach + //hSPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI, ETOUCH_CS); + //TFT_eTouchBase::Calibation calibation = { 233, 3785, 3731, 120, 2 }; + //touch.setCalibration(calibation); + + // Check if the screen is touched and get the coordinates + /* + if (touch.touched()) { + TS_Point p = touch.getPoint(); + Serial.print("Pressure = "); + Serial.print(p.z); + Serial.print(", x = "); + Serial.print(p.x); + Serial.print(", y = "); + Serial.print(p.y); + delay(30); + Serial.println(); + } + */ + SPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI); + touch.begin(240, 320); + #endif + Serial.println(F("Turn on the LCD backlight")); pinMode(LED_PIN, OUTPUT); pinMode(BK_LIGHT_PIN, OUTPUT); @@ -83,6 +248,7 @@ void t_hmiDisplay_Init(void) pData.bestDifficulty = "0"; pData.workersHash = "0"; pData.workersCount = 0; + lower_switch = 1; } @@ -188,7 +354,11 @@ void t_hmiDisplay_MinerScreen(unsigned long mElapsed) render.setFontSize(10); render.rdrawString(data.currentTime.c_str(), 286, 1, TFT_BLACK); - printPoolData(); + if (lower_switch == 1) + printPoolData(); + else + printMemPoolFees(mElapsed); + // Push prepared background to screen background.pushSprite(0, 0); } @@ -226,7 +396,10 @@ void t_hmiDisplay_ClockScreen(unsigned long mElapsed) background.setTextColor(0xDEDB, TFT_BLACK); background.drawString(data.currentTime.c_str(), 130, 50, GFXFF); - printMemPoolFees(mElapsed); + if (lower_switch == 1) + printMemPoolFees(mElapsed); + else + printPoolData(); // Push prepared background to screen background.pushSprite(0, 0); } @@ -286,7 +459,11 @@ void t_hmiDisplay_GlobalHashScreen(unsigned long mElapsed) background.setTextColor(TFT_BLACK); background.drawString(data.remainingBlocks.c_str(), 72, 159, FONT2); - printMemPoolFees(mElapsed); + if (lower_switch == 1) + printMemPoolFees(mElapsed); + else + printPoolData(); + // Push prepared background to screen background.pushSprite(0, 0); } @@ -326,7 +503,10 @@ void t_hmiDisplay_BTCprice(unsigned long mElapsed) background.setTextSize(1); background.setTextColor(0xDEDB, TFT_BLACK); background.drawString(data.btcPrice.c_str(), 300, 58, GFXFF); - printPoolData(); + if (lower_switch == 1) + printPoolData(); + else + printMemPoolFees(mElapsed); // Push prepared background to screen background.pushSprite(0, 0); } diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index 4552f8c..a1cda51 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -34,7 +34,9 @@ SDCard::SDCard(int ID):cardInitialized_(false),cardBusy_(false) } iSD_ = &SD; #endif // interface type +#ifndef NERDMINER_T_HMI initSDcard(); +#endif } SDCard::~SDCard() @@ -167,7 +169,14 @@ bool SDCard::initSDcard() #elif defined (BUILD_SDMMC_1) #warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); +#ifdef NERDMINER_T_HMI + // Need to lower frequency to 20000 + // Should work but blows up in SDCard::initSDcard() + // see temporary workaround t_hmiCheckForSDCardAndMoveToNVM() + cardInitialized_ = iSD_->begin("/sd", true, false, 20000); +#else cardInitialized_ = iSD_->begin("/sd", true); +#endif Serial.println("SDCard: 1-Bit Mode."); } #elif defined (BUILD_SDSPI) diff --git a/src/monitor.cpp b/src/monitor.cpp index 0d129a0..35a76fd 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -335,13 +335,12 @@ String getPoolAPIUrl(void) { else { switch (Settings.PoolPort) { case 3333: - if (Settings.PoolAddress == "pool.vkbit.com") - poolAPIUrl = "https://vkbit.com/miner/"; - else if (Settings.PoolAddress == "pool.sethforprivacy.com") + if (Settings.PoolAddress == "pool.sethforprivacy.com") poolAPIUrl = "https://pool.sethforprivacy.com/api/client/"; // Add more cases for other addresses with port 3333 if needed break; case 2018: + // Local instance of public-pool.io on Umbrel or Start9 poolAPIUrl = "http://" + Settings.PoolAddress + ":2019/api/client/"; break; default: From bc2dba69b1c58f60c427c5936872ba51b1587158 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Wed, 29 May 2024 01:17:20 -0700 Subject: [PATCH 2/7] t-hmi working with touch class --- src/NerdMinerV2.ino.cpp | 11 ++- src/drivers/devices/lilygoT_HMI.h | 2 + src/drivers/displays/t_hmiDisplayDriver.cpp | 102 +++----------------- 3 files changed, 22 insertions(+), 93 deletions(-) diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index 48a8305..b9f2f91 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -14,6 +14,7 @@ #include "drivers/displays/display.h" #include "drivers/storage/SDCard.h" #include "timeconst.h" +#include "TouchHandler.h" //3 seconds WDT #define WDT_TIMEOUT 3 @@ -28,6 +29,10 @@ OneButton button2(PIN_BUTTON_2); #endif +#ifdef TOUCH_ENABLE +extern TouchHandler touchHandler; +#endif + extern monitor_data mMonitor; SDCard SDCrd = SDCard(); @@ -159,9 +164,9 @@ void loop() { #ifdef PIN_BUTTON_2 button2.tick(); #endif - -#ifdef NERDMINER_T_HMI - t_hmiCheckForTouch(); + +#ifdef TOUCH_ENABLE + touchHandler.isTouched(); #endif wifiManagerProcess(); // avoid delays() in loop when non-blocking and other long running code diff --git a/src/drivers/devices/lilygoT_HMI.h b/src/drivers/devices/lilygoT_HMI.h index 556c067..ca5d406 100644 --- a/src/drivers/devices/lilygoT_HMI.h +++ b/src/drivers/devices/lilygoT_HMI.h @@ -34,6 +34,8 @@ #define SDMMC_CMD (11) #define SDMMC_D0 (13) +#define TOUCH_ENABLE (1) + #ifndef TFT_BL // XXX - defined in User_Setups/Setup207_LilyGo_T_HMI.h:37 #define TFT_BL (38) // LED back-light diff --git a/src/drivers/displays/t_hmiDisplayDriver.cpp b/src/drivers/displays/t_hmiDisplayDriver.cpp index 84c1ad8..a5b5285 100644 --- a/src/drivers/displays/t_hmiDisplayDriver.cpp +++ b/src/drivers/displays/t_hmiDisplayDriver.cpp @@ -12,6 +12,7 @@ #include "version.h" #include "monitor.h" #include "OpenFontRender.h" +#include "TouchHandler.h" #include #include @@ -23,15 +24,15 @@ OpenFontRender render; TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h TFT_eSprite background = TFT_eSprite(&tft); // Invoke library sprite -#define TOUCH_ENABLE -//SPIClass hSPI(HSPI); -// TFT_eTouch touch(tft, ETOUCH_CS, 0xFF, hSPI); #ifdef TOUCH_ENABLE -XPT2046 touch = XPT2046(SPI, ETOUCH_CS, TOUCH_IRQ); +TouchHandler touchHandler = TouchHandler(tft, ETOUCH_CS, TOUCH_IRQ, SPI); #endif bool showbtcprice = false; +unsigned int lowerScreen = 1; + +extern void switchToNextScreen(); extern monitor_data mMonitor; extern pool_data pData; extern DisplayDriver *currentDisplayDriver; @@ -117,64 +118,6 @@ void t_hmiCheckForSDCardAndMoveToNVM(void) } } /*=============================================*/ -unsigned int lower_switch = 1; -#ifdef TOUCH_ENABLE -extern void switchToNextScreen(void); - -unsigned long now = 0; -unsigned long currentTime; -bool debounce() { - if (now) now = millis(); - - unsigned long currentTime = millis(); - if (currentTime - now >= 2000) { - now = 0; - return true; - } - return false; - -}; - -uint16_t t_hmiCheckForTouch() -{ - uint16_t touch_x, touch_y, code = 0; - - if (touch.pressed()) { - touch_x = touch.RawX(); - touch_y = touch.RawY(); - // do something with the touch coordinates - /* - Serial.print("Touch coordinates: "); - Serial.print(touch_x); - Serial.print(", "); - Serial.println(touch_y); - */ - - // Perform actions based on touch coordinates - /* - if (y < y_min + (y_max - y_min) / 2) { - */ - if (touch_x < 200 + (1700 - 200) / 4) { - // bottom - code = 1; - if (debounce()) - lower_switch = 3 - lower_switch;; - } else { - // top - code = 2; - if (debounce()) - switchToNextScreen(); - } - } - if (code) { - if (code == 1) - Serial.print("Touch bottom\n"); - else - Serial.print("Touch top\n"); - } - return(code); -} -#endif uint32_t readAdcVoltage(int pin) { esp_adc_cal_characteristics_t adc_chars; @@ -216,29 +159,10 @@ void t_hmiDisplay_Init(void) return; } - Serial.println(F("Initialize the touch screen")); #ifdef TOUCH_ENABLE - // different approach - //hSPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI, ETOUCH_CS); - //TFT_eTouchBase::Calibation calibation = { 233, 3785, 3731, 120, 2 }; - //touch.setCalibration(calibation); - - // Check if the screen is touched and get the coordinates - /* - if (touch.touched()) { - TS_Point p = touch.getPoint(); - Serial.print("Pressure = "); - Serial.print(p.z); - Serial.print(", x = "); - Serial.print(p.x); - Serial.print(", y = "); - Serial.print(p.y); - delay(30); - Serial.println(); - } - */ - SPI.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI); - touch.begin(240, 320); + Serial.println(F("Initialize the touch screen")); + touchHandler.begin(HEIGHT, WIDTH); + touchHandler.setScreenSwitchCallback(switchToNextScreen); #endif Serial.println(F("Turn on the LCD backlight")); @@ -248,8 +172,6 @@ void t_hmiDisplay_Init(void) pData.bestDifficulty = "0"; pData.workersHash = "0"; pData.workersCount = 0; - lower_switch = 1; - } void t_hmiDisplay_AlternateScreenState(void) @@ -354,7 +276,7 @@ void t_hmiDisplay_MinerScreen(unsigned long mElapsed) render.setFontSize(10); render.rdrawString(data.currentTime.c_str(), 286, 1, TFT_BLACK); - if (lower_switch == 1) + if (lowerScreen == 1) printPoolData(); else printMemPoolFees(mElapsed); @@ -396,7 +318,7 @@ void t_hmiDisplay_ClockScreen(unsigned long mElapsed) background.setTextColor(0xDEDB, TFT_BLACK); background.drawString(data.currentTime.c_str(), 130, 50, GFXFF); - if (lower_switch == 1) + if (lowerScreen == 1) printMemPoolFees(mElapsed); else printPoolData(); @@ -459,7 +381,7 @@ void t_hmiDisplay_GlobalHashScreen(unsigned long mElapsed) background.setTextColor(TFT_BLACK); background.drawString(data.remainingBlocks.c_str(), 72, 159, FONT2); - if (lower_switch == 1) + if (lowerScreen == 1) printMemPoolFees(mElapsed); else printPoolData(); @@ -503,7 +425,7 @@ void t_hmiDisplay_BTCprice(unsigned long mElapsed) background.setTextSize(1); background.setTextColor(0xDEDB, TFT_BLACK); background.drawString(data.btcPrice.c_str(), 300, 58, GFXFF); - if (lower_switch == 1) + if (lowerScreen == 1) printPoolData(); else printMemPoolFees(mElapsed); From e8ee3b80c2856aea2d03f5e044e0a09a8c559ebe Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Wed, 29 May 2024 14:29:49 -0700 Subject: [PATCH 3/7] t-hmi fixed sdcard class for 1bit; lower freq for card detection --- src/NerdMinerV2.ino.cpp | 4 +- src/drivers/displays/t_hmiDisplayDriver.cpp | 82 --------------------- src/drivers/storage/SDCard.cpp | 4 +- src/drivers/storage/SDCard.h | 5 ++ 4 files changed, 8 insertions(+), 87 deletions(-) diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index b9f2f91..984d21f 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -103,9 +103,9 @@ void setup() doLedStuff(0); #ifdef NERDMINER_T_HMI - extern void t_hmiCheckForSDCardAndMoveToNVM(void); - t_hmiCheckForSDCardAndMoveToNVM(); + SDCrd.initSDcard(); #endif + /******** INIT WIFI ************/ init_WifiManager(); diff --git a/src/drivers/displays/t_hmiDisplayDriver.cpp b/src/drivers/displays/t_hmiDisplayDriver.cpp index a5b5285..c10f1f5 100644 --- a/src/drivers/displays/t_hmiDisplayDriver.cpp +++ b/src/drivers/displays/t_hmiDisplayDriver.cpp @@ -37,88 +37,6 @@ extern monitor_data mMonitor; extern pool_data pData; extern DisplayDriver *currentDisplayDriver; -/*=============================================*/ -#include -#include -#include -#include "drivers/storage/nvMemory.h" -#include "drivers/storage/SDCard.h" -extern nvMemory nvMem; -extern TSettings Settings; - -void t_hmiCheckForSDCardAndMoveToNVM(void) -{ - // workaround removing use of class SDCard - // SCard SDCrd = SDCard() blows up in SDCard::initSDcard() - // Need to lower frequency to 20000 - SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); - delay(500); - bool rlst = SD_MMC.begin("/sd", true, false, 20000); - if (!rlst) - { - Serial.println("SD init failed"); - Serial.println("➸ No detected SdCard"); - } - else - { - Serial.println("SD init success"); - Serial.printf("➸ Detected SdCard insert: %.2f GB\r\n", SD_MMC.cardSize() / 1024.0 / 1024.0 / 1024.0); - - // SDCrd.SD2nvMemory(&nvMem, &Settings); - - if (SD_MMC.exists(JSON_CONFIG_FILE)) - { - // The file exists, reading and loading - File configFile = SD_MMC.open(JSON_CONFIG_FILE, "r"); - if (configFile) - { - StaticJsonDocument<512> json; - DeserializationError error = deserializeJson(json, configFile); - configFile.close(); - Serial.println("SDCard: Loading config file"); - if (!error) - { - serializeJsonPretty(json, Serial); - Serial.print('\n'); - TSettings *pSettings = &Settings; - pSettings->WifiSSID = json[JSON_KEY_SSID] | pSettings->WifiSSID; - pSettings->WifiPW = json[JSON_KEY_PASW] | pSettings->WifiPW; - pSettings->PoolAddress = json[JSON_KEY_POOLURL] | pSettings->PoolAddress; - strcpy(pSettings->PoolPassword, json[JSON_KEY_POOLPASS] | pSettings->PoolPassword); - strcpy(pSettings->BtcWallet, json[JSON_KEY_WALLETID] | pSettings->BtcWallet); - if (json.containsKey(JSON_KEY_POOLPORT)) - pSettings->PoolPort = json[JSON_KEY_POOLPORT].as(); - if (json.containsKey(JSON_KEY_TIMEZONE)) - pSettings->Timezone = json[JSON_KEY_TIMEZONE].as(); - if (json.containsKey(JSON_KEY_STATS2NV)) - pSettings->saveStats = json[JSON_KEY_STATS2NV].as(); - nvMem.saveConfig(pSettings); - WiFi.begin(pSettings->WifiSSID, pSettings->WifiPW); - Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); - ESP.restart(); - } - else - { - // Error loading JSON data - Serial.println("SDCard: Error parsing config file!"); - } - } - else - { - Serial.println("SDCard: Error opening config file!"); - } - } - else - { - Serial.println("SDCard: No config file available!"); - } - // delay(15000); - SD_MMC.end(); - Serial.println("SD close"); - } -} -/*=============================================*/ - uint32_t readAdcVoltage(int pin) { esp_adc_cal_characteristics_t adc_chars; diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index a1cda51..99f88e5 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -170,9 +170,7 @@ bool SDCard::initSDcard() #warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); #ifdef NERDMINER_T_HMI - // Need to lower frequency to 20000 - // Should work but blows up in SDCard::initSDcard() - // see temporary workaround t_hmiCheckForSDCardAndMoveToNVM() + // Need to lower frequency to 20000 for proper detection cardInitialized_ = iSD_->begin("/sd", true, false, 20000); #else cardInitialized_ = iSD_->begin("/sd", true); diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index a90deb3..5727875 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -58,8 +58,13 @@ public: bool loadConfigFile(TSettings* Settings); bool cardAvailable(); bool cardBusy(); +#ifdef NERDMINER_T_HMI + bool initSDcard(); +private: +#else private: bool initSDcard(); +#endif bool cardInitialized_; bool cardBusy_; #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) From e8c2db5642dd63e4fc419549c1d9445efe121361 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Wed, 29 May 2024 14:35:49 -0700 Subject: [PATCH 4/7] t-hmi add touchhandler class --- src/TouchHandler.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/TouchHandler.h | 30 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/TouchHandler.cpp create mode 100644 src/TouchHandler.h diff --git a/src/TouchHandler.cpp b/src/TouchHandler.cpp new file mode 100644 index 0000000..b45f141 --- /dev/null +++ b/src/TouchHandler.cpp @@ -0,0 +1,66 @@ +#include "drivers/displays/display.h" +#include "TouchHandler.h" + +// Global variable declaration +extern unsigned int lowerScreen; + + +TouchHandler::~TouchHandler() { +} + +TouchHandler::TouchHandler(TFT_eSPI& tft, uint8_t csPin, uint8_t irqPin, SPIClass& spi) + : tft(tft), csPin(csPin), irqPin(irqPin), spi(spi), lastTouchTime(0), + screenSwitchCallback(nullptr), touch(spi, csPin, irqPin) { + +} + +void TouchHandler::begin(uint16_t xres, uint16_t yres) { + spi.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI); + touch.begin(xres, yres); +} + +void TouchHandler::setScreenSwitchCallback(void (*callback)()) { + screenSwitchCallback = callback; +} + +uint16_t TouchHandler::isTouched() { + // XXX - move touch_x, touch_y to private and min_x, min_y,max_x, max_y + uint16_t touch_x, touch_y, code = 0; + + if (touch.pressed()) { + touch_x = touch.RawX(); + touch_y = touch.RawY(); + + // Perform actions based on touch coordinates + // if (y < y_min + (y_max - y_min) / 4) { + if (touch_x < 200 + (1700 - 200) / 4) { + // bottom + code = 1; + if (debounce()) + lowerScreen = 3 - lowerScreen; + } else { + // top + code = 2; + if (debounce() && screenSwitchCallback) { + screenSwitchCallback(); + } + } + + if (code) { + if (code == 1) + Serial.print("Touch bottom\n"); + else + Serial.print("Touch top\n"); + } + } + return code; +} + +bool TouchHandler::debounce() { + unsigned long currentTime = millis(); + if (currentTime - lastTouchTime >= 2000) { + lastTouchTime = currentTime; + return true; + } + return false; +} diff --git a/src/TouchHandler.h b/src/TouchHandler.h new file mode 100644 index 0000000..4d3821c --- /dev/null +++ b/src/TouchHandler.h @@ -0,0 +1,30 @@ +#ifndef _TOUCHHANDLER_H_ +#define _TOUCHHANDLER_H_ + +#include // TFT display library +#include // https://github.com/liangyingy/arduino_xpt2046_library + + +class TouchHandler { +public: + TouchHandler(); + ~TouchHandler(); + TouchHandler(TFT_eSPI& tft, uint8_t csPin, uint8_t irqPin, SPIClass& spi); + void begin(uint16_t xres, uint16_t yres); + uint16_t isTouched(); + void setScreenSwitchCallback(void (*callback)()); + +private: + bool debounce(); + TFT_eSPI& tft; + XPT2046 touch; + uint8_t csPin; + uint8_t irqPin; + SPIClass& spi; + unsigned long lastTouchTime; + // unsigned int lower_switch; + void (*screenSwitchCallback)(); +}; + + +#endif \ No newline at end of file From 42b5f11d3c1de37e7fe00de73e1ae54acce31890 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Wed, 29 May 2024 15:39:53 -0700 Subject: [PATCH 5/7] t-hmi touchhandler class opt out build --- src/TouchHandler.cpp | 5 ++++- src/TouchHandler.h | 4 ++-- src/drivers/displays/t_hmiDisplayDriver.cpp | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/TouchHandler.cpp b/src/TouchHandler.cpp index b45f141..c5d73f5 100644 --- a/src/TouchHandler.cpp +++ b/src/TouchHandler.cpp @@ -1,4 +1,6 @@ -#include "drivers/displays/display.h" + +#include "drivers/devices/device.h" +#ifdef TOUCH_ENABLE #include "TouchHandler.h" // Global variable declaration @@ -64,3 +66,4 @@ bool TouchHandler::debounce() { } return false; } +#endif \ No newline at end of file diff --git a/src/TouchHandler.h b/src/TouchHandler.h index 4d3821c..00e209c 100644 --- a/src/TouchHandler.h +++ b/src/TouchHandler.h @@ -1,6 +1,6 @@ #ifndef _TOUCHHANDLER_H_ #define _TOUCHHANDLER_H_ - +#ifdef TOUCH_ENABLE #include // TFT display library #include // https://github.com/liangyingy/arduino_xpt2046_library @@ -25,6 +25,6 @@ private: // unsigned int lower_switch; void (*screenSwitchCallback)(); }; - +#endif #endif \ No newline at end of file diff --git a/src/drivers/displays/t_hmiDisplayDriver.cpp b/src/drivers/displays/t_hmiDisplayDriver.cpp index c10f1f5..1d5e1f1 100644 --- a/src/drivers/displays/t_hmiDisplayDriver.cpp +++ b/src/drivers/displays/t_hmiDisplayDriver.cpp @@ -12,8 +12,9 @@ #include "version.h" #include "monitor.h" #include "OpenFontRender.h" +#ifdef TOUCH_ENABLE #include "TouchHandler.h" - +#endif #include #include From e089634effd387e83ce873ee826910c2e32349e9 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Thu, 30 May 2024 22:38:26 -0700 Subject: [PATCH 6/7] t-hmi: implement pr suggestions; remove janky global in touchhandler class --- src/NerdMinerV2.ino.cpp | 8 ++++---- src/TouchHandler.cpp | 14 +++++++++----- src/TouchHandler.h | 3 ++- src/drivers/devices/lilygoT_HMI.h | 2 +- src/drivers/displays/t_hmiDisplayDriver.cpp | 4 ++++ src/drivers/storage/SDCard.cpp | 2 +- src/drivers/storage/SDCard.h | 2 +- 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index 984d21f..847ba32 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -14,7 +14,10 @@ #include "drivers/displays/display.h" #include "drivers/storage/SDCard.h" #include "timeconst.h" + +#ifdef TOUCH_ENABLE #include "TouchHandler.h" +#endif //3 seconds WDT #define WDT_TIMEOUT 3 @@ -102,7 +105,7 @@ void setup() mMonitor.NerdStatus = NM_waitingConfig; doLedStuff(0); -#ifdef NERDMINER_T_HMI +#ifdef SDMMC_1BIT_FIX SDCrd.initSDcard(); #endif @@ -151,9 +154,6 @@ void app_error_fault_handler(void *arg) { // restart ESP32 esp_restart(); } -#ifdef NERDMINER_T_HMI -extern uint16_t t_hmiCheckForTouch(); -#endif void loop() { // keep watching the push buttons: diff --git a/src/TouchHandler.cpp b/src/TouchHandler.cpp index c5d73f5..b671eb6 100644 --- a/src/TouchHandler.cpp +++ b/src/TouchHandler.cpp @@ -3,8 +3,6 @@ #ifdef TOUCH_ENABLE #include "TouchHandler.h" -// Global variable declaration -extern unsigned int lowerScreen; TouchHandler::~TouchHandler() { @@ -12,7 +10,7 @@ TouchHandler::~TouchHandler() { TouchHandler::TouchHandler(TFT_eSPI& tft, uint8_t csPin, uint8_t irqPin, SPIClass& spi) : tft(tft), csPin(csPin), irqPin(irqPin), spi(spi), lastTouchTime(0), - screenSwitchCallback(nullptr), touch(spi, csPin, irqPin) { + screenSwitchCallback(nullptr), screenSwitchAltCallback(nullptr), touch(spi, csPin, irqPin) { } @@ -25,6 +23,11 @@ void TouchHandler::setScreenSwitchCallback(void (*callback)()) { screenSwitchCallback = callback; } +void TouchHandler::setScreenSwitchAltCallback(void (*callback)()) { + screenSwitchAltCallback = callback; +} + + uint16_t TouchHandler::isTouched() { // XXX - move touch_x, touch_y to private and min_x, min_y,max_x, max_y uint16_t touch_x, touch_y, code = 0; @@ -38,8 +41,9 @@ uint16_t TouchHandler::isTouched() { if (touch_x < 200 + (1700 - 200) / 4) { // bottom code = 1; - if (debounce()) - lowerScreen = 3 - lowerScreen; + if (debounce() && screenSwitchAltCallback) { + screenSwitchAltCallback(); + } } else { // top code = 2; diff --git a/src/TouchHandler.h b/src/TouchHandler.h index 00e209c..8c032b6 100644 --- a/src/TouchHandler.h +++ b/src/TouchHandler.h @@ -13,7 +13,7 @@ public: void begin(uint16_t xres, uint16_t yres); uint16_t isTouched(); void setScreenSwitchCallback(void (*callback)()); - + void setScreenSwitchAltCallback(void (*callback)()); private: bool debounce(); TFT_eSPI& tft; @@ -24,6 +24,7 @@ private: unsigned long lastTouchTime; // unsigned int lower_switch; void (*screenSwitchCallback)(); + void (*screenSwitchAltCallback)(); }; #endif diff --git a/src/drivers/devices/lilygoT_HMI.h b/src/drivers/devices/lilygoT_HMI.h index ca5d406..ec1e097 100644 --- a/src/drivers/devices/lilygoT_HMI.h +++ b/src/drivers/devices/lilygoT_HMI.h @@ -35,7 +35,7 @@ #define SDMMC_D0 (13) #define TOUCH_ENABLE (1) - +#define SDMMC_1BIT_FIX (1) #ifndef TFT_BL // XXX - defined in User_Setups/Setup207_LilyGo_T_HMI.h:37 #define TFT_BL (38) // LED back-light diff --git a/src/drivers/displays/t_hmiDisplayDriver.cpp b/src/drivers/displays/t_hmiDisplayDriver.cpp index 1d5e1f1..da002f0 100644 --- a/src/drivers/displays/t_hmiDisplayDriver.cpp +++ b/src/drivers/displays/t_hmiDisplayDriver.cpp @@ -38,6 +38,9 @@ extern monitor_data mMonitor; extern pool_data pData; extern DisplayDriver *currentDisplayDriver; +void toggleBottomScreen() { lowerScreen = 3 - lowerScreen; } + + uint32_t readAdcVoltage(int pin) { esp_adc_cal_characteristics_t adc_chars; @@ -82,6 +85,7 @@ void t_hmiDisplay_Init(void) Serial.println(F("Initialize the touch screen")); touchHandler.begin(HEIGHT, WIDTH); touchHandler.setScreenSwitchCallback(switchToNextScreen); + touchHandler.setScreenSwitchAltCallback(toggleBottomScreen); #endif Serial.println(F("Turn on the LCD backlight")); diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index 99f88e5..98e58df 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -34,7 +34,7 @@ SDCard::SDCard(int ID):cardInitialized_(false),cardBusy_(false) } iSD_ = &SD; #endif // interface type -#ifndef NERDMINER_T_HMI +#ifndef SDMMC_1BIT_FIX initSDcard(); #endif } diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index 5727875..4de2e86 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -58,7 +58,7 @@ public: bool loadConfigFile(TSettings* Settings); bool cardAvailable(); bool cardBusy(); -#ifdef NERDMINER_T_HMI +#ifdef SDMMC_1BIT_FIX bool initSDcard(); private: #else From c77aea65b0a261536b1359f82d8bfdfa4ec1d7e3 Mon Sep 17 00:00:00 2001 From: cosmicpsyop Date: Tue, 4 Jun 2024 09:02:40 -0700 Subject: [PATCH 7/7] t-hmi: SD_FREQENCY for everyone --- src/drivers/devices/lilygoT_HMI.h | 1 + src/drivers/storage/SDCard.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/drivers/devices/lilygoT_HMI.h b/src/drivers/devices/lilygoT_HMI.h index ec1e097..abb631f 100644 --- a/src/drivers/devices/lilygoT_HMI.h +++ b/src/drivers/devices/lilygoT_HMI.h @@ -36,6 +36,7 @@ #define TOUCH_ENABLE (1) #define SDMMC_1BIT_FIX (1) +#define SD_FREQUENCY (20000) #ifndef TFT_BL // XXX - defined in User_Setups/Setup207_LilyGo_T_HMI.h:37 #define TFT_BL (38) // LED back-light diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index 98e58df..378d874 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -169,9 +169,9 @@ bool SDCard::initSDcard() #elif defined (BUILD_SDMMC_1) #warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes. iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); -#ifdef NERDMINER_T_HMI +#ifdef SD_FREQUENCY // Need to lower frequency to 20000 for proper detection - cardInitialized_ = iSD_->begin("/sd", true, false, 20000); + cardInitialized_ = iSD_->begin("/sd", true, false, SD_FREQUENCY); #else cardInitialized_ = iSD_->begin("/sd", true); #endif