diff --git a/Bruce3_Cardputer.bin b/Bruce3_Cardputer.bin deleted file mode 100644 index f3a99eb4c..000000000 Binary files a/Bruce3_Cardputer.bin and /dev/null differ diff --git a/platformio.ini b/platformio.ini index 72aa25c70..a267cfe18 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,6 +29,8 @@ lib_deps = LibSSH-ESP32 PCA9554 https://github.com/bmorcelli/ESPping/ + NTPClient + Timezone [env:m5stack-cplus2] diff --git a/src/clients.cpp b/src/clients.cpp index fc393aa8e..8404f3270 100644 --- a/src/clients.cpp +++ b/src/clients.cpp @@ -19,6 +19,7 @@ #include "globals.h" #include "display.h" #include "mykeyboard.h" +#include "wifi_common.h" // SSH server configuration (initialize as mpty strings) String ssh_host = ""; @@ -30,7 +31,7 @@ char* ssh_port_char; // M5Cardputer setup //M5Canvas canvas(&DISP); -String commandBuffer = "> "; +// String commandBuffer = "> "; int cursorY = 0; const int lineHeight = 32; unsigned long lastKeyPressMillis = 0; @@ -58,8 +59,8 @@ bool filterAnsiSequences = true; // Set to false to disable ANSI sequence filte void ssh_loop() { - - for(;;){ + String message = ""; + //for(;;){ #ifdef CARDPUTER if (Keyboard.isChange() && Keyboard.isPressed()) { unsigned long currentMillis = millis(); @@ -68,13 +69,13 @@ void ssh_loop() { Keyboard_Class::KeysState status = Keyboard.keysState(); for (auto i : status.word) { - commandBuffer += i; + //commandBuffer += i; tft.print(i); cursorY = tft.getCursorY(); } - if (status.del && commandBuffer.length() > 2) { - commandBuffer.remove(commandBuffer.length() - 1); + if (status.del > 2) { + //commandBuffer.remove(commandBuffer.length() - 1); tft.setCursor( tft.getCursorX() - 6, tft.getCursorY()); @@ -86,17 +87,17 @@ void ssh_loop() { } if (status.enter) { - commandBuffer.trim(); // Trim the command buffer to remove + //commandBuffer.trim(); // Trim the command buffer to remove // accidental TFT_WHITEspaces/newlines - String message = commandBuffer.substring( - 2); // Get the command part, exclude the "> " + //String message = commandBuffer.substring(2); // Get the command part, exclude the "> " + message = keyboard(message,76,"SSH Command: "); ssh_channel_write(channel_ssh, message.c_str(), message.length()); // Send the command ssh_channel_write(channel_ssh, "\r", 1); // Send exactly one carriage return (try // "\n" or "\r\n" if needed) - commandBuffer = "> "; // Reset command buffer + //commandBuffer = "> "; // Reset command buffer tft.print( '\n'); // Move to the next line on display cursorY = @@ -107,21 +108,23 @@ void ssh_loop() { #else if(checkSelPress()) { + while(checkSelPress()) { yield(); } // timerless debounce - commandBuffer = keyboard(commandBuffer,76,"SSH Command: "); + //commandBuffer = keyboard(commandBuffer,76,"SSH Command: "); + message = keyboard(message,76,"SSH Command: "); while(checkSelPress()) { yield(); } // timerless debounce - commandBuffer.trim(); // Trim the command buffer to remove + //commandBuffer.trim(); // Trim the command buffer to remove // accidental TFT_WHITEspaces/newlines - String message = ""; - if(commandBuffer.startsWith("> ")) message = commandBuffer.substring(2); // Get the command part, exclude the "> " - else message = commandBuffer; + + //if(commandBuffer.startsWith("> ")) message = commandBuffer.substring(2); // Get the command part, exclude the "> " + //else message = commandBuffer; ssh_channel_write(channel_ssh, message.c_str(), message.length()); // Send the command ssh_channel_write(channel_ssh, "\r", 1); // Send exactly one carriage return (try // "\n" or "\r\n" if needed) - commandBuffer = "> "; // Reset command buffer + //commandBuffer = "> "; // Reset command buffer tft.print('\n'); // Move to the next line on display cursorY =tft.getCursorY(); // Update cursor position } @@ -177,17 +180,17 @@ void ssh_loop() { tft.setTextColor(FGCOLOR, BGCOLOR); return; // Exit the loop upon session closure } - } + //} } void ssh_setup(){ - if (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("Connect to wifi before using wireguard"); - displayRedStripe("CONNECT TO WIFI",TFT_WHITE, TFT_RED); - delay(5000); - return; - } + if(!wifiConnected) wifiConnectMenu(); + + // Disable watchdog + disableCore0WDT(); + //disableCore1WDT(); + //disableLoopWDT(); + tft.fillScreen(BGCOLOR); tft.setCursor(0, 0); Serial.begin(115200); // Initialize serial communication for debugging @@ -201,12 +204,9 @@ void ssh_setup(){ cursorY = tft.getCursorY(); tft.setCursor(0, 0); - // Prompt for SSH host, username, and password - //tft.print("SSH Host: \n"); - // waitForInput(ssh_host); + ssh_host=keyboard("",76,"SSH HOST"); - //tft.print("SSH Port: \n"); - //waitForInput(ssh_port); + ssh_port=keyboard("",76,"SSH PORT"); ssh_port_char = stringTochar(ssh_port); @@ -388,14 +388,9 @@ void telnet_loop() { } void telnet_setup() { - if (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("Connect to wifi before using wireguard"); - displayRedStripe("CONNECT TO WIFI",TFT_WHITE, TFT_RED); - delay(5000); - return; - } - tft.fillScreen(BGCOLOR); + if(!wifiConnected) wifiConnectMenu(); + + tft.fillScreen(BGCOLOR); tft.setCursor(0, 0); Serial.begin(115200); // Initialize serial communication for debugging Serial.println("Starting Setup"); diff --git a/src/display.cpp b/src/display.cpp index c5022a512..566f350a3 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,6 +1,7 @@ #include "display.h" #include "mykeyboard.h" #include "wg.h" //for isConnectedWireguard to print wireguard lock +//#include "settings.h" //for timeStr #if defined(CARDPUTER) || defined(STICK_C_PLUS2) //Battery Calculation #include @@ -228,7 +229,8 @@ void drawSubmenu(int index,const std::vector2000) && (millis()-i)<2200) tft.fillScreen(TFT_BLACK); - if((millis()-i>2200) && (millis()-i)<2700) tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,TFT_WHITE); + if((millis()-i>2200) && (millis()-i)<2700) tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,TFT_PURPLE+0x3000); if((millis()-i>2700) && (millis()-i)<2900) tft.fillScreen(TFT_BLACK); - if((millis()-i>2900) && (millis()-i)<3400 && !change) { tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,TFT_WHITE); } + if((millis()-i>2900) && (millis()-i)<3400 && !change) { tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,TFT_PURPLE); } if((millis()-i>3400) && (millis()-i)<3600) tft.fillScreen(TFT_BLACK); if((millis()-i>3600)) tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,TFT_RED); @@ -258,6 +258,7 @@ void loop() { case 5: //Config options = { {"Brightness", [=]() { setBrightnessMenu(); }}, //settings.h + {"Clock", [=]() { setClock(); }}, {"Orientation", [=]() { gsetRotation(true); }}, //settings.h {"Main Menu", [=]() { backToMenu(); }}, {"Restart", [=]() { ESP.restart(); }}, diff --git a/src/settings.cpp b/src/settings.cpp index 15ab7dab2..5c391f061 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,6 +1,12 @@ #include "globals.h" #include "settings.h" #include "display.h" // calling loopOptions(options, true); +#include "wifi_common.h" +#include "mykeyboard.h" +#include +#include +#include + @@ -96,3 +102,84 @@ void setBrightnessMenu() { loopOptions(options, true); delay(200); } + +/********************************************************************* +** Function: setClock +** Handles Menu to set timezone to NTP +**********************************************************************/ +const char* ntpServer = "pool.ntp.org"; +//const long selectedTimezone = -3 * 3600; // GMT -3 hours (Sao Paulo timezone) +long selectedTimezone; +const int daylightOffset_sec = 0; + +TimeChangeRule BRST = {"BRST", Last, Sun, Oct, 0, -180}; +Timezone myTZ(BRST, BRST); // Create Timezone object with the same rule for standard and daylight time + +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, ntpServer, selectedTimezone, daylightOffset_sec); + + + +void setClock() { + + if(!wifiConnected) wifiConnectMenu(); + options = { + {"Brasilia", [=]() { selectedTimezone = -3 * 3600; }}, + {"New York", [=]() { selectedTimezone = -4 * 3600; }}, + {"Lisbon", [=]() { selectedTimezone = 1 * 3600; }}, + {"Hong Kong", [=]() { selectedTimezone = 8 * 3600; }}, + {"Sydney", [=]() { selectedTimezone = 10 * 3600; }}, + {"Tokyo", [=]() { selectedTimezone = 9 * 3600; }}, + }; + delay(200); + loopOptions(options, true); + delay(200); + + timeClient.begin(); + runClockLoop(); +} + +void runClockLoop() { + + + for (;;){ + + timeClient.update(); + time_t localTime = myTZ.toLocal(timeClient.getEpochTime()); // Convert NTP time to local time in Sao Paulo timezone + struct tm* timeInfo = localtime(&localTime); + + char timeStr[10]; + snprintf(timeStr, sizeof(timeStr), "%02d:%02d", timeInfo->tm_hour, timeInfo->tm_min); + + Serial.print("Current time: "); + Serial.println(timeStr); + tft.fillScreen(BGCOLOR); + tft.setCursor(60, 50); + tft.setTextSize(4); + tft.println(timeStr); + + // Checks para sair do loop + #ifndef CARDPUTER + if(checkPrevPress()) { // Apertar o botão power dos sticks + tft.fillScreen(BGCOLOR); + returnToMenu=true; + break; + //goto Exit; + } + #else + Keyboard.update(); + if(Keyboard.isKeyPressed('`')) { + tft.fillScreen(BGCOLOR); + returnToMenu=true; + break; + //goto Exit; + } // apertar ESC no Cardputer + #endif + + delay(1000); + + } +} + + + diff --git a/src/settings.h b/src/settings.h index 3c7cd0b83..8885218cb 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,6 +1,7 @@ #include +extern char timeStr[10]; void setBrightness(int bright, bool save = true); @@ -8,4 +9,8 @@ void getBrightness(); int gsetRotation(bool set = false); -void setBrightnessMenu(); \ No newline at end of file +void setBrightnessMenu(); + +void setClock(); + +void runClockLoop(); \ No newline at end of file diff --git a/src/wg.cpp b/src/wg.cpp index 857d082db..78e15046e 100644 --- a/src/wg.cpp +++ b/src/wg.cpp @@ -3,6 +3,7 @@ #include "globals.h" #include "display.h" #include "sd_functions.h" +#include "wifi_common.h" char private_key[45]; @@ -120,13 +121,8 @@ void read_and_parse_file() { **********************************************************************/ void wg_setup() { - if (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("Connect to wifi before using wireguard"); - displayRedStripe("CONNECT TO WIFI",TFT_WHITE, TFT_RED); - delay(5000); - return; - } + if(!wifiConnected) wifiConnectMenu(); + read_and_parse_file();