acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

main.ino (5097B)

      1 // Standard includes
      2 #include <time.h>
      3 
      4 // Local includes
      5 #include "Display.h"
      6 #include "IRC.h"
      7 #include "Lora.h"
      8 #include "Network.h"
      9 #include "pins.h"
     10 #include "Speaker.h"
     11 #include "Storage.h"
     12 #include "Utilities.h"
     13 
     14 // Timing constants
     15 const unsigned long STATUS_UPDATE_INTERVAL = 15000; // 15 seconds
     16 const unsigned long INACTIVITY_TIMEOUT     = 30000; // 30 seconds
     17 
     18 
     19 // Main functions ---------------------------------------------------------------------------------
     20 void setup() {
     21     // Initialize serial communication
     22     Serial.begin(115200);
     23     Serial.println("Booting device...");
     24 
     25     // Give power to the board peripherals
     26     pinMode(BOARD_POWERON, OUTPUT); 
     27     digitalWrite(BOARD_POWERON, HIGH);
     28 
     29     // Start the I2C bus for the keyboard
     30     Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
     31 
     32     // Initialize the display
     33     setupScreen();
     34     displayXBM();
     35 
     36     // Load preferences from storage
     37     loadPreferences();
     38 
     39     // Give power to the SD card
     40     //setupSD();
     41     //mountSD();
     42     
     43     // Turn on power to the radio
     44     //setupRadio();
     45 
     46     // Setup the WiFi
     47     initializeNetwork();
     48 
     49     // Initialize the speaker
     50     setupI2S(); // Do we want to keep this open or uninstall after each use to keep resources free?
     51     const char* rtttl_boot = "TakeOnMe:d=4,o=4,b=500:8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5,8f#5,8e5,8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5";
     52     playRTTTL(rtttl_boot);
     53 
     54 
     55     // Connect to WiFi if credentials are stored, otherwise scan for networks
     56     if (wifi_ssid.length() > 0 && wifi_password.length() > 0) {
     57         Serial.println("Stored WiFi credentials found");
     58         connectToWiFi(wifi_ssid, wifi_password);
     59     } else {
     60         scanWiFiNetworks();
     61     }
     62 }
     63 
     64 
     65 void loop() {
     66     // Handle the info screen if it is active (from /info command)
     67     if (infoScreen) {
     68         if (millis() - infoScreenStartTime > 10000) { // 10 seconds
     69             infoScreen = false;
     70             tft.fillScreen(TFT_BLACK);
     71             displayLines(); // Redraw the previous buffer
     72         }
     73     } else if (configScreen) {
     74         if (millis() - configScreenStartTime > 10000) { // 10 seconds
     75             configScreen = false;
     76             tft.fillScreen(TFT_BLACK);
     77             displayLines(); // Redraw the previous buffer
     78         }
     79     } else {
     80         // Handle keyboard input for WiFi if the SSID is empty still (aka not connected)
     81         if (wifi_ssid.length() == 0) {
     82             char incoming = getKeyboardInput();
     83             if (incoming != 0) {
     84                 handleWiFiSelection(incoming);
     85                 lastActivityTime = millis(); // Reset activity timer
     86             }
     87         } else {
     88             // Handle status bar updating on an interval
     89             if (millis() - lastStatusUpdateTime > STATUS_UPDATE_INTERVAL) {
     90                 updateStatusBar();
     91                 lastStatusUpdateTime = millis();
     92             }
     93 
     94             // Handle IRC data
     95             if (client && client->connected()) {
     96                 handleIRC();
     97             } else {
     98                 // Connect to IRC if not connected (or reconnect to wifi if disconnected)
     99                 if (WiFi.status() == WL_CONNECTED) {
    100                     displayCenteredText("CONNECTING TO " + String(irc_server));
    101                     if (connectToIRC()) {
    102                         delay(2000); // Delay to allow the connection to establish
    103                         Serial.println("Connected to IRC!");
    104                         displayCenteredText("CONNECTED TO " + String(irc_server));
    105                         Serial.println("Connecting to IRC with " + irc_nickname + " (" + irc_username + ") [" + irc_realname + "]");
    106                         sendIRC("NICK " + String(irc_nickname));
    107                         sendIRC("USER " + String(irc_username) + " 0 * :" + String(irc_realname));
    108                     } else {
    109                         Serial.println("Failed to connect to IRC");
    110                         displayCenteredText("CONNECTION FAILED");
    111                         delay(1000);
    112                     }
    113                 } else {
    114                     displayCenteredText("RECONNECTING TO WIFI");
    115                     WiFi.begin(wifi_ssid.c_str(), wifi_password.c_str()); // Need to handle this better
    116                 }
    117             }
    118 
    119             // Join channel after a delay
    120             if (readyToJoinChannel && millis() >= joinChannelTime) {
    121                 tft.fillScreen(TFT_BLACK);
    122                 updateStatusBar();
    123                 sendIRC("JOIN " + String(irc_channel));
    124                 readyToJoinChannel = false;
    125             }
    126 
    127             // Handle keyboard input (for IRC)
    128             char incoming = getKeyboardInput();
    129             if (incoming != 0) {
    130                 handleKeyboardInput(incoming);
    131                 lastActivityTime = millis();
    132             }
    133 
    134             // Handle inactivity timeout
    135             if (screenOn && millis() - lastActivityTime > INACTIVITY_TIMEOUT)
    136                 turnOffScreen();
    137         }
    138     }
    139 }