Add button press handle, added print time calc
This commit is contained in:
parent
d3d74938d3
commit
3299c3d66a
100
src/functions.h
100
src/functions.h
@ -4,6 +4,9 @@
|
|||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
|
void playTune(int tuneNumber);
|
||||||
|
void beep();
|
||||||
|
|
||||||
unsigned long middle_long_press_started = 0;
|
unsigned long middle_long_press_started = 0;
|
||||||
unsigned long right_long_press_started = 0;
|
unsigned long right_long_press_started = 0;
|
||||||
bool is_display_off = false;
|
bool is_display_off = false;
|
||||||
@ -38,10 +41,9 @@ void initSystems() {
|
|||||||
// Connect to WiFi
|
// Connect to WiFi
|
||||||
void connectToWifi() {
|
void connectToWifi() {
|
||||||
|
|
||||||
Serial.println("Connecting to WiFi...");
|
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.print("Connecting to WiFi...");
|
display.println("Connecting to WiFi");
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||||
@ -49,7 +51,6 @@ void connectToWifi() {
|
|||||||
int attemptCount = 0;
|
int attemptCount = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && attemptCount < 20) { // Timeout after 20 attempts
|
while (WiFi.status() != WL_CONNECTED && attemptCount < 20) { // Timeout after 20 attempts
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
|
||||||
display.print(".");
|
display.print(".");
|
||||||
display.display();
|
display.display();
|
||||||
attemptCount++;
|
attemptCount++;
|
||||||
@ -69,6 +70,7 @@ void connectToWifi() {
|
|||||||
display.print("IP: ");
|
display.print("IP: ");
|
||||||
display.print(WiFi.localIP());
|
display.print(WiFi.localIP());
|
||||||
display.display();
|
display.display();
|
||||||
|
playTune(2);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("\nFailed to connect to WiFi.");
|
Serial.println("\nFailed to connect to WiFi.");
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
@ -82,32 +84,96 @@ void connectToWifi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void commonButtonHandler() {
|
void commonButtonHandler() {
|
||||||
|
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH);
|
static unsigned long leftPressStart = 0;
|
||||||
|
static unsigned long middlePressStart = 0;
|
||||||
|
static unsigned long rightPressStart = 0;
|
||||||
|
|
||||||
|
static bool leftBeeped = false;
|
||||||
|
static bool middleBeeped = false;
|
||||||
|
static bool rightBeeped = false;
|
||||||
|
|
||||||
|
bool leftPressed = (digitalRead(PIN_BTN_L) == HIGH);
|
||||||
|
bool middlePressed = (digitalRead(PIN_BTN_M) == HIGH);
|
||||||
bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH);
|
bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH);
|
||||||
|
|
||||||
// Handle middle + left button long press for reset
|
// Short press detection
|
||||||
if (middleLeftPressed) {
|
if (leftPressed) {
|
||||||
if ((currentMillis - middle_long_press_started) > 2000) {
|
if ((currentMillis - leftPressStart > 50)) { // Debounce delay
|
||||||
ESP.restart(); // Restart if the buttons are pressed for more than 2 seconds
|
if (!leftBeeped) {
|
||||||
|
beep(); // Play beep sound
|
||||||
|
leftBeeped = true; // Set beeped state
|
||||||
|
// Handle left short press action here
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
middle_long_press_started = currentMillis; // Reset timer if the buttons are not pressed
|
leftPressStart = currentMillis; // Reset the timer if button is not pressed
|
||||||
|
leftBeeped = false; // Reset beeped state
|
||||||
|
}
|
||||||
|
|
||||||
|
if (middlePressed) {
|
||||||
|
if ((currentMillis - middlePressStart > 50)) { // Debounce delay
|
||||||
|
if (!middleBeeped) {
|
||||||
|
beep(); // Play beep sound
|
||||||
|
middleBeeped = true; // Set beeped state
|
||||||
|
// Handle middle short press action here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
middlePressStart = currentMillis; // Reset the timer if button is not pressed
|
||||||
|
middleBeeped = false; // Reset beeped state
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rightPressed) {
|
if (rightPressed) {
|
||||||
if ((currentMillis - right_long_press_started) > 2000) {
|
if ((currentMillis - rightPressStart > 50)) { // Debounce delay
|
||||||
if (!is_display_off) {
|
if (!rightBeeped) {
|
||||||
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
|
beep(); // Play beep sound
|
||||||
is_display_off = true;
|
rightBeeped = true; // Set beeped state
|
||||||
}
|
if (is_display_off) {
|
||||||
} else if (is_display_off) {
|
|
||||||
display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display
|
display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display
|
||||||
is_display_off = false;
|
is_display_off = false;
|
||||||
|
beep();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
right_long_press_started = currentMillis;
|
rightPressStart = currentMillis; // Reset the timer if button is not pressed
|
||||||
|
rightBeeped = false; // Reset beeped state
|
||||||
|
}
|
||||||
|
|
||||||
|
// Long press detection
|
||||||
|
if (leftPressed && (currentMillis - leftPressStart > 2000)) {
|
||||||
|
if (!leftBeeped) {
|
||||||
|
beep(); // Play beep sound
|
||||||
|
leftBeeped = true; // Set beeped state
|
||||||
|
// Handle left long press action here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (middlePressed && (currentMillis - middlePressStart > 2000)) {
|
||||||
|
if (!middleBeeped) {
|
||||||
|
beep(); // Play beep sound
|
||||||
|
middleBeeped = true; // Set beeped state
|
||||||
|
// Handle middle long press action here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rightPressed && (currentMillis - rightPressStart > 2000)) {
|
||||||
|
if (!is_display_off) {
|
||||||
|
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
|
||||||
|
is_display_off = true;
|
||||||
|
playTune(3); // Play beep sound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combination of Left and Middle long press
|
||||||
|
if (leftPressed && middlePressed &&
|
||||||
|
(currentMillis - leftPressStart > 2000) &&
|
||||||
|
(currentMillis - middlePressStart > 2000)) {
|
||||||
|
if (!leftBeeped && !middleBeeped) {
|
||||||
|
beep(); // Play beep sound
|
||||||
|
ESP.restart();
|
||||||
|
// Handle left and middle long press action here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -17,4 +17,5 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
commonButtonHandler();
|
commonButtonHandler();
|
||||||
|
fetchPrintingStatus(display);
|
||||||
}
|
}
|
||||||
|
@ -7,19 +7,11 @@
|
|||||||
#include "tunes.h"
|
#include "tunes.h"
|
||||||
|
|
||||||
void playTune(int tuneNumber);
|
void playTune(int tuneNumber);
|
||||||
|
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
|
|
||||||
String printerOperational;
|
|
||||||
String printerPaused;
|
|
||||||
String printerPrinting;
|
|
||||||
String printerReady;
|
|
||||||
String printerText;
|
|
||||||
String printerHotend;
|
|
||||||
String printerTarget;
|
|
||||||
String payload;
|
|
||||||
|
|
||||||
OctoprintApi api(client, OCTOPRINT_HOST, OCTOPRINT_PORT, OCTOPRINT_API_KEY);
|
OctoprintApi api(client, OCTOPRINT_HOST, OCTOPRINT_PORT, OCTOPRINT_API_KEY);
|
||||||
|
unsigned long api_mtbs = OCTOPRINT_API_REFRESH_INTERVAL;
|
||||||
|
unsigned long api_lasttime = 0;
|
||||||
|
|
||||||
bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
@ -27,23 +19,20 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
|||||||
display.setTextSize(1);
|
display.setTextSize(1);
|
||||||
display.setTextColor(WHITE);
|
display.setTextColor(WHITE);
|
||||||
display.print("Fetching OctoPrint Version...");
|
display.print("Fetching OctoPrint Version...");
|
||||||
display.display(); // Show loading message
|
display.display();
|
||||||
|
|
||||||
// Attempt to fetch the OctoPrint version
|
|
||||||
if (api.getOctoprintVersion()) {
|
if (api.getOctoprintVersion()) {
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.setTextSize(1);
|
display.setTextSize(1);
|
||||||
display.setTextColor(WHITE);
|
display.setTextColor(WHITE);
|
||||||
|
|
||||||
display.print("OctoPrint Version:");
|
display.print("OctoPrint Version:");
|
||||||
display.setCursor(0, 10);
|
display.setCursor(0, 10);
|
||||||
display.setTextSize(2); // Larger text for version
|
display.setTextSize(2);
|
||||||
display.print(api.octoprintVer.octoprintServer);
|
display.print(api.octoprintVer.octoprintServer);
|
||||||
|
display.display();
|
||||||
display.display(); // Update the display
|
|
||||||
playTune(2);
|
playTune(2);
|
||||||
return true; // Indicate success
|
return true;
|
||||||
} else {
|
} else {
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
@ -53,10 +42,44 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
|||||||
display.setCursor(0, 10);
|
display.setCursor(0, 10);
|
||||||
display.print("OctoPrint version.");
|
display.print("OctoPrint version.");
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
// Optionally, add a short delay to show the error
|
|
||||||
playTune(3);
|
playTune(3);
|
||||||
return false; // Indicate failure
|
return false;
|
||||||
}
|
}
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool fetchPrintingStatus(Adafruit_SSD1306& display) {
|
||||||
|
if (millis() - api_lasttime > api_mtbs || api_lasttime == 0) {
|
||||||
|
if (api.getPrinterStatistics()) {
|
||||||
|
display.clearDisplay();
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setTextColor(WHITE);
|
||||||
|
display.println(api.printerStats.printerState);
|
||||||
|
display.print("Nozzle: ");
|
||||||
|
display.println(api.printerStats.printerTool0TempActual);
|
||||||
|
display.print("Bed: ");
|
||||||
|
display.println(api.printerStats.printerBedTempActual);
|
||||||
|
|
||||||
|
//Print time left (if printing) in human readable time HH:MM:SS
|
||||||
|
int runHours= api.printJob.progressPrintTimeLeft/3600;
|
||||||
|
int secsRemaining=api.printJob.progressPrintTimeLeft%3600;
|
||||||
|
int runMinutes=secsRemaining/60;
|
||||||
|
int runSeconds=secsRemaining%60;
|
||||||
|
char buf[31];
|
||||||
|
sprintf(buf,"Print time left:\t%02d:%02d:%02d",runHours,runMinutes,runSeconds);
|
||||||
|
display.println(buf);
|
||||||
|
|
||||||
|
const float temp_percent = floor(api.printJob.progressCompletion*100)/100;
|
||||||
|
display.print(temp_percent);
|
||||||
|
display.println("%");
|
||||||
|
display.display();
|
||||||
|
api_lasttime = millis(); // Update the last fetch time
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to fetch printer statistics.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // No fetch was done (within the time interval)
|
||||||
|
}
|
||||||
|
@ -44,3 +44,9 @@ void playTune(int tuneIndex) {
|
|||||||
noTone(PIN_BUZZER); // Stop the tone
|
noTone(PIN_BUZZER); // Stop the tone
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void beep() {
|
||||||
|
tone(PIN_BUZZER, 1000, 100);
|
||||||
|
delay(100);
|
||||||
|
noTone(PIN_BUZZER);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user