Skip to content

Commit

Permalink
added clock
Browse files Browse the repository at this point in the history
  • Loading branch information
pr3y committed May 28, 2024
1 parent e915bb2 commit 4efe028
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 48 deletions.
Binary file removed Bruce3_Cardputer.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ lib_deps =
LibSSH-ESP32
PCA9554
https://github.com/bmorcelli/ESPping/
NTPClient
Timezone


[env:m5stack-cplus2]
Expand Down
69 changes: 32 additions & 37 deletions src/clients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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 =
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion src/display.cpp
Original file line number Diff line number Diff line change
@@ -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 <driver/adc.h>
Expand Down Expand Up @@ -228,7 +229,8 @@ void drawSubmenu(int index,const std::vector<std::pair<std::string, std::functio
void drawMainBorder() {
tft.fillScreen(BGCOLOR);
setTftDisplay(12, 12, FGCOLOR, 1, BGCOLOR);
tft.print("BRUCE 1.0b");

if(wifiConnected) {tft.print("timeStr");} else {tft.print("BRUCE 1.0b");}

int i=0;
if(wifiConnected) { drawWifiSmall(WIDTH - 90, 7); i++;} //Draw Wifi Symbol beside battery
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void setup() {

while(millis()<i+7000) { // boot image lasts for 5 secs
if((millis()-i>2000) && (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);

Expand Down Expand Up @@ -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(); }},
Expand Down
87 changes: 87 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
@@ -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 <NTPClient.h>
#include <WiFiUdp.h>
#include <Timezone.h>




Expand Down Expand Up @@ -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);

}
}



7 changes: 6 additions & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include <EEPROM.h>


extern char timeStr[10];

void setBrightness(int bright, bool save = true);

void getBrightness();

int gsetRotation(bool set = false);

void setBrightnessMenu();
void setBrightnessMenu();

void setClock();

void runClockLoop();
10 changes: 3 additions & 7 deletions src/wg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "globals.h"
#include "display.h"
#include "sd_functions.h"
#include "wifi_common.h"


char private_key[45];
Expand Down Expand Up @@ -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();


Expand Down

0 comments on commit 4efe028

Please sign in to comment.