OctoCube/src/functions.h

114 lines
3.1 KiB
C
Raw Normal View History

2024-11-01 17:45:17 +00:00
#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();
}
2024-11-01 18:03:31 +00:00
delay(1000);
2024-11-01 17:45:17 +00:00
}
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;
}
}