Added tones and octopint
This commit is contained in:
parent
8334c5115c
commit
ec6af93cfe
112
src/functions.h
Normal file
112
src/functions.h
Normal file
@ -0,0 +1,112 @@
|
||||
#include <Wire.h>
|
||||
#include "config.h"
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
unsigned long middle_long_press_started = 0;
|
||||
unsigned long right_long_press_started = 0;
|
||||
bool is_display_off = false;
|
||||
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
|
||||
// Initialize display, buttons and buzzer
|
||||
void initSystems() {
|
||||
|
||||
pinMode(PIN_BTN_L, INPUT);
|
||||
pinMode(PIN_BTN_M, INPUT);
|
||||
pinMode(PIN_BTN_R, INPUT);
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
// Initialize the SSD1306 display
|
||||
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust I2C address if needed
|
||||
for(;;); // Don't proceed, loop forever
|
||||
}
|
||||
|
||||
// Rotate the display 180 degrees
|
||||
display.setRotation(2);
|
||||
|
||||
// Clear the display buffer
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.display();
|
||||
}
|
||||
|
||||
// Connect to WiFi
|
||||
void connectToWifi() {
|
||||
|
||||
Serial.println("Connecting to WiFi...");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("Connecting to WiFi...");
|
||||
display.display();
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
|
||||
int attemptCount = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attemptCount < 20) { // Timeout after 20 attempts
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
display.print(".");
|
||||
display.display();
|
||||
attemptCount++;
|
||||
}
|
||||
|
||||
// Check if connected
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("\nConnected to WiFi!");
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Display connection success and IP address
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connected!");
|
||||
display.setCursor(0, 10);
|
||||
display.print("IP: ");
|
||||
display.print(WiFi.localIP());
|
||||
display.display();
|
||||
} else {
|
||||
Serial.println("\nFailed to connect to WiFi.");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connection");
|
||||
display.setCursor(0, 10);
|
||||
display.print("Failed");
|
||||
display.display();
|
||||
}
|
||||
}
|
||||
|
||||
void commonButtonHandler() {
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH);
|
||||
bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH);
|
||||
|
||||
// Handle middle + left button long press for reset
|
||||
if (middleLeftPressed) {
|
||||
if ((currentMillis - middle_long_press_started) > 2000) {
|
||||
ESP.restart(); // Restart if the buttons are pressed for more than 2 seconds
|
||||
}
|
||||
} else {
|
||||
middle_long_press_started = currentMillis; // Reset timer if the buttons are not pressed
|
||||
}
|
||||
|
||||
if (rightPressed) {
|
||||
if ((currentMillis - right_long_press_started) > 2000) {
|
||||
if (!is_display_off) {
|
||||
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
|
||||
is_display_off = true;
|
||||
}
|
||||
} else if (is_display_off) {
|
||||
display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display
|
||||
is_display_off = false;
|
||||
}
|
||||
} else {
|
||||
right_long_press_started = currentMillis;
|
||||
}
|
||||
}
|
||||
|
69
src/main.cpp
69
src/main.cpp
@ -1,73 +1,20 @@
|
||||
#include "config.h"
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "config.h"
|
||||
// Initialize display with defined dimensions and reset pin
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
#include "functions.h"
|
||||
#include "octoprint.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Power On");
|
||||
|
||||
// Initialize the SSD1306 display
|
||||
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust I2C address if needed
|
||||
Serial.println("SSD1306 allocation failed");
|
||||
for(;;); // Don't proceed, loop forever
|
||||
}
|
||||
initSystems();
|
||||
playTune(1);
|
||||
connectToWifi();
|
||||
displayOctoPrintVersion(display);
|
||||
|
||||
// Rotate the display 180 degrees
|
||||
display.setRotation(2);
|
||||
|
||||
// Clear the display buffer
|
||||
display.clearDisplay();
|
||||
|
||||
// Display initial message
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.print("Connecting to WiFi...");
|
||||
display.display();
|
||||
|
||||
// Connect to WiFi
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
Serial.print("Connecting to WiFi");
|
||||
|
||||
int attemptCount = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attemptCount < 20) { // Timeout after 20 attempts
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
display.print(".");
|
||||
display.display();
|
||||
attemptCount++;
|
||||
}
|
||||
|
||||
// Check if connected
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("\nConnected to WiFi!");
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Display connection success and IP address
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connected!");
|
||||
display.setCursor(0, 10);
|
||||
display.print("IP: ");
|
||||
display.print(WiFi.localIP());
|
||||
display.display();
|
||||
} else {
|
||||
Serial.println("\nFailed to connect to WiFi.");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connection");
|
||||
display.setCursor(0, 10);
|
||||
display.print("Failed");
|
||||
display.display();
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Nothing to do here for now
|
||||
commonButtonHandler();
|
||||
}
|
||||
|
48
src/octoprint.h
Normal file
48
src/octoprint.h
Normal file
@ -0,0 +1,48 @@
|
||||
#include "config.h"
|
||||
#include <OctoPrintAPI.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include "tunes.h"
|
||||
|
||||
void playTune(int tuneNumber);
|
||||
|
||||
WiFiClient client;
|
||||
|
||||
String printerOperational;
|
||||
String printerPaused;
|
||||
String printerPrinting;
|
||||
String printerReady;
|
||||
String printerText;
|
||||
String printerHotend;
|
||||
String printerTarget;
|
||||
String payload;
|
||||
|
||||
OctoprintApi api(client, (char*)OCTOPRINT_HOST, OCTOPRINT_PORT, OCTOPRINT_API_KEY);
|
||||
|
||||
// Function to fetch and display OctoPrint version
|
||||
void displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
||||
if (api.getOctoprintVersion()) {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
|
||||
display.print("OctoPrint Version:");
|
||||
display.setCursor(0, 10);
|
||||
display.print(api.octoprintVer.octoprintServer);
|
||||
|
||||
display.display(); // Update the display
|
||||
} else {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.print("Failed to get");
|
||||
display.setCursor(0, 10);
|
||||
display.print("OctoPrint version.");
|
||||
display.display();
|
||||
playTune(3);
|
||||
}
|
||||
}
|
45
src/tunes.h
Normal file
45
src/tunes.h
Normal file
@ -0,0 +1,45 @@
|
||||
#include "config.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
// Define melodies and their durations
|
||||
int melody1[] = { 440, 523, 659, 587 }; // Happy tune
|
||||
int noteDuration1[] = { 100, 50, 100, 150 };
|
||||
|
||||
int melody2[] = { 587, 659, 740, 659 }; // Tune 2
|
||||
int noteDuration2[] = { 150, 100, 150, 100 };
|
||||
|
||||
int melody3[] = { 440, 392 }; // Sad Tune
|
||||
int noteDuration3[] = { 200, 150 };
|
||||
|
||||
// Function to play a specified tune
|
||||
void playTune(int tuneIndex) {
|
||||
int* melody = nullptr; // Use pointer instead of array
|
||||
int* noteDuration = nullptr; // Use pointer instead of array
|
||||
int melodySize = 0; // To store the size of the melody
|
||||
|
||||
switch(tuneIndex) {
|
||||
case 1:
|
||||
melody = melody1;
|
||||
noteDuration = noteDuration1;
|
||||
melodySize = sizeof(melody1) / sizeof(melody1[0]);
|
||||
break;
|
||||
case 2:
|
||||
melody = melody2;
|
||||
noteDuration = noteDuration2;
|
||||
melodySize = sizeof(melody2) / sizeof(melody2[0]);
|
||||
break;
|
||||
case 3:
|
||||
melody = melody3;
|
||||
noteDuration = noteDuration3;
|
||||
melodySize = sizeof(melody3) / sizeof(melody3[0]);
|
||||
break;
|
||||
default:
|
||||
return; // Invalid tune index
|
||||
}
|
||||
|
||||
for (int i = 0; i < melodySize; i++) {
|
||||
tone(PIN_BUZZER, melody[i], noteDuration[i]);
|
||||
delay(noteDuration[i] + 20);
|
||||
noTone(PIN_BUZZER); // Stop the tone
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user