Added extended preferences

This commit is contained in:
Dionysus 2024-05-29 17:14:53 -04:00
parent 4afe369d27
commit 77a9144837
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -16,7 +16,6 @@
#include "pins.h" #include "pins.h"
// Constants // Constants
#define CHAR_HEIGHT 10 #define CHAR_HEIGHT 10
#define LINE_SPACING 0 #define LINE_SPACING 0
@ -37,13 +36,7 @@ struct WiFiNetwork {
Pangodream_18650_CL BL(BOARD_BAT_ADC, CONV_FACTOR, READS); Pangodream_18650_CL BL(BOARD_BAT_ADC, CONV_FACTOR, READS);
Preferences preferences; Preferences preferences;
TFT_eSPI tft = TFT_eSPI(); TFT_eSPI tft = TFT_eSPI();
WiFiClientSecure client; WiFiClient* client;
// Initialize variables
String inputBuffer = "";
String ssid = "";
String password = "";
String nick = "";
// Memory vectors & maps // Memory vectors & maps
std::map<String, uint32_t> nickColors; std::map<String, uint32_t> nickColors;
@ -51,7 +44,7 @@ std::vector<String> lines; // Possible rename to bufferLines ?
std::vector<bool> mentions; std::vector<bool> mentions;
std::vector<WiFiNetwork> wifiNetworks; std::vector<WiFiNetwork> wifiNetworks;
// Global variables to cache preferences // Global variables to cache preferences and buffers
String irc_nickname; String irc_nickname;
String irc_username; String irc_username;
String irc_realname; String irc_realname;
@ -62,17 +55,11 @@ String irc_channel;
String irc_nickserv; String irc_nickserv;
String wifi_ssid; String wifi_ssid;
String wifi_password; String wifi_password;
String inputBuffer = "";
// IRC connection (These will eventually be set dynamically when we have a settings menu) // Leftover crack variables (will be removed when preferences are done)
const char* server = "irc.supernets.org";
const int port = 6697;
bool useSSL = true;
const char* channel = "#comms"; const char* channel = "#comms";
// IRC identity
const char* user = "tdeck";
const char* realname = "ACID DROP Firmware v0.1.0b"; // Need to eventually set this up to use a variable
// Timing variables // Timing variables
unsigned long infoScreenStartTime = 0; unsigned long infoScreenStartTime = 0;
unsigned long configScreenStartTime = 0; unsigned long configScreenStartTime = 0;
@ -127,30 +114,21 @@ void setup() {
tft.invertDisplay(1); tft.invertDisplay(1);
Serial.println("TFT initialized"); Serial.println("TFT initialized");
// Display the boot screen for 3 seconds // Display the boot screen
displayXBM(); displayXBM();
delay(3000); delay(3000);
// Load the WiFi preferences if they exist // Initialize the preferences
preferences.begin("wifi", false); setDefaultPreferences();
ssid = preferences.getString("ssid", ""); loadPreferences();
password = preferences.getString("password", "");
preferences.end();
// Connect to WiFi if credentials are stored, otherwise scan for networks // Connect to WiFi if credentials are stored, otherwise scan for networks
if (ssid.length() > 0 && password.length() > 0) { if (wifi_ssid.length() > 0 && wifi_password.length() > 0) {
Serial.println("Stored WiFi credentials found"); Serial.println("Stored WiFi credentials found");
connectToWiFi(); connectToWiFi(wifi_ssid, wifi_password);
if (WiFi.status() != WL_CONNECTED) // ISSUE: If we are out of range of the last stored network, it is still going to attempt to connect to it 10 times...
scanWiFiNetworks();
} else { } else {
scanWiFiNetworks(); scanWiFiNetworks();
} }
// Initialize the random seed for random nick generation
randomSeed(analogRead(0));
int randomNum = random(1000, 10000);
nick = "ACID_" + String(randomNum);
} }
@ -170,7 +148,7 @@ void loop() {
} }
} else { } else {
// Handle keyboard input for WiFi if the SSID is empty still (aka not connected) // Handle keyboard input for WiFi if the SSID is empty still (aka not connected)
if (ssid.isEmpty()) { if (wifi_ssid.length() == 0) {
char incoming = getKeyboardInput(); char incoming = getKeyboardInput();
if (incoming != 0) { if (incoming != 0) {
handleWiFiSelection(incoming); handleWiFiSelection(incoming);
@ -184,24 +162,27 @@ void loop() {
} }
// Handle IRC data // Handle IRC data
if (client.connected()) { if (client && client->connected()) {
handleIRC(); handleIRC();
} else { } else {
// Connect to IRC if not connected (or reconnect to wifi if disconnected) // Connect to IRC if not connected (or reconnect to wifi if disconnected)
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
displayCenteredText("CONNECTING TO " + String(server)); displayCenteredText("CONNECTING TO " + String(irc_server));
if (connectToIRC()) { if (connectToIRC()) {
delay(2000); // Delay to allow the connection to establish
Serial.println("Connected to IRC!"); Serial.println("Connected to IRC!");
displayCenteredText("CONNECTED TO " + String(server)); displayCenteredText("CONNECTED TO " + String(irc_server));
sendIRC("NICK " + String(nick)); Serial.println("Connecting to IRC with " + irc_nickname + " (" + irc_username + ") [" + irc_realname + "]");
sendIRC("USER " + String(user) + " 0 * :" + String(realname)); sendIRC("NICK " + String(irc_nickname));
sendIRC("USER " + String(irc_username) + " 0 * :" + String(irc_realname));
} else { } else {
Serial.println("Failed to connect to IRC");
displayCenteredText("CONNECTION FAILED"); displayCenteredText("CONNECTION FAILED");
delay(1000); delay(1000);
} }
} else { } else {
displayCenteredText("RECONNECTING TO WIFI"); displayCenteredText("RECONNECTING TO WIFI");
WiFi.begin(ssid.c_str(), password.c_str()); WiFi.begin(wifi_ssid.c_str(), wifi_password.c_str()); // Need to handle this better
} }
} }
@ -228,12 +209,72 @@ void loop() {
} }
} }
void setDefaultPreferences() {
preferences.begin("config", false);
// IRC preferences
if (!preferences.isKey("irc_nickname"))
preferences.putString("irc_nickname", "ACID_" + String(random(1000, 10000)));
if (!preferences.isKey("irc_username"))
preferences.putString("irc_username", "tdeck");
if (!preferences.isKey("irc_realname"))
preferences.putString("irc_realname", "ACID DROP Firmware");
if (!preferences.isKey("irc_server"))
preferences.putString("irc_server", "irc.supernets.org");
if (!preferences.isKey("irc_port"))
preferences.putInt("irc_port", 6667);
if (!preferences.isKey("irc_tls"))
preferences.putBool("irc_tls", false);
if (!preferences.isKey("irc_channel"))
preferences.putString("irc_channel", "#comms");
if (!preferences.isKey("irc_nickserv"))
preferences.putString("irc_nickserv", "");
// WiFi preferences
if (!preferences.isKey("wifi_ssid"))
preferences.putString("wifi_ssid", "");
if (!preferences.isKey("wifi_password"))
preferences.putString("wifi_password", "");
preferences.end();
}
void loadPreferences() {
preferences.begin("config", true);
// IRC preferences
irc_nickname = preferences.getString("irc_nickname");
irc_username = preferences.getString("irc_username");
irc_realname = preferences.getString("irc_realname");
irc_server = preferences.getString("irc_server");
irc_port = preferences.getInt("irc_port");
irc_tls = preferences.getBool("irc_tls");
irc_channel = preferences.getString("irc_channel");
irc_nickserv = preferences.getString("irc_nickserv");
// WiFi preferences
wifi_ssid = preferences.getString("wifi_ssid");
wifi_password = preferences.getString("wifi_password");
preferences.end();
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// WiFi functions --------------------------------------------------------------------------------- // WiFi functions ---------------------------------------------------------------------------------
void connectToWiFi() { void connectToWiFi(String ssid, String password) {
Serial.println("Connecting to WiFi network: " + ssid); Serial.println("Connecting to WiFi network: " + ssid);
WiFi.begin(ssid.c_str(), password.c_str()); WiFi.begin(ssid.c_str(), password.c_str());
@ -254,17 +295,29 @@ void connectToWiFi() {
updateTimeFromNTP(); updateTimeFromNTP();
// Store the WiFi credentials upon successful connection // Store the WiFi credentials upon successful connection
preferences.putString("ssid", ssid); preferences.begin("config", false);
preferences.putString("password", password); preferences.putString("wifi_ssid", ssid);
preferences.putString("wifi_password", password);
preferences.end();
Serial.println("Stored WiFi credentials updated"); Serial.println("Stored WiFi credentials updated");
// Cache the WiFi credentials
wifi_ssid = ssid;
wifi_password = password;
} else { } else {
Serial.println("Failed to connect to WiFi network: " + ssid); Serial.println("Failed to connect to WiFi network: " + ssid);
displayCenteredText("WIFI CONNECTION FAILED"); displayCenteredText("WIFI CONNECTION FAILED");
// Clear stored credentials on failure // Clear stored credentials on failure
preferences.remove("ssid"); preferences.begin("config", false);
preferences.remove("password"); preferences.putString("wifi_ssid", "");
Serial.println("Stored WiFi credentials removed"); preferences.putString("wifi_password", "");
preferences.end();
Serial.println("Stored WiFi credentials removed");\
// Clear the cached WiFi credentials
wifi_ssid = "";
wifi_password = "";
scanWiFiNetworks(); // Rescan for networks scanWiFiNetworks(); // Rescan for networks
} }
@ -307,9 +360,9 @@ void scanWiFiNetworks() {
void handlePasswordInput(char key) { void handlePasswordInput(char key) {
if (key == '\n' || key == '\r') { // Enter if (key == '\n' || key == '\r') { // Enter
password = inputBuffer; wifi_password = inputBuffer;
inputBuffer = ""; inputBuffer = "";
connectToWiFi(); connectToWiFi(wifi_ssid, wifi_password);
} else if (key == '\b') { // Backspace } else if (key == '\b') { // Backspace
if (inputBuffer.length() > 0) { if (inputBuffer.length() > 0) {
inputBuffer.remove(inputBuffer.length() - 1); inputBuffer.remove(inputBuffer.length() - 1);
@ -393,7 +446,7 @@ void handleWiFiSelection(char key) {
} else if (key == 'd') { } else if (key == 'd') {
updateSelectedNetwork(1); updateSelectedNetwork(1);
} else if (key == '\n' || key == '\r') { } else if (key == '\n' || key == '\r') {
ssid = wifiNetworks[selectedNetworkIndex].ssid; wifi_ssid = wifiNetworks[selectedNetworkIndex].ssid;
if (wifiNetworks[selectedNetworkIndex].encryption == "Secured") { if (wifiNetworks[selectedNetworkIndex].encryption == "Secured") {
inputBuffer = ""; inputBuffer = "";
tft.fillScreen(TFT_BLACK); tft.fillScreen(TFT_BLACK);
@ -413,8 +466,8 @@ void handleWiFiSelection(char key) {
} }
} }
} else { } else {
password = ""; wifi_password = "";
connectToWiFi(); connectToWiFi(wifi_ssid, wifi_password);
} }
} }
} }
@ -425,14 +478,15 @@ void handleWiFiSelection(char key) {
// IRC functions ---------------------------------------------------------------------------------- // IRC functions ----------------------------------------------------------------------------------
bool connectToIRC() { bool connectToIRC() {
if (useSSL) { if (irc_tls) {
Serial.println("Connecting to IRC with TLS: " + String(server) + ":" + String(port)); Serial.println("Connecting to IRC with TLS: " + String(irc_server) + ":" + String(irc_port));
client.setInsecure(); client = new WiFiClientSecure();
return client.connect(server, port); static_cast<WiFiClientSecure*>(client)->setInsecure();
return static_cast<WiFiClientSecure*>(client)->connect(irc_server.c_str(), irc_port);
} else { } else {
Serial.println("Connecting to IRC: " + String(server) + ":" + String(port)); Serial.println("Connecting to IRC: " + String(irc_server) + ":" + String(irc_port));
WiFiClient nonSecureClient; client = new WiFiClient();
return nonSecureClient.connect(server, port); return client->connect(irc_server.c_str(), irc_port);
} }
} }
@ -443,11 +497,12 @@ void sendIRC(String command) {
return; return;
} }
if (client.connected()) { if (client->connected()) {
if (client.println(command)) if (client->println(command)) {
Serial.println("IRC: >>> " + command); Serial.println("IRC: >>> " + command);
else } else {
Serial.println("Failed to send: " + command); Serial.println("Failed to send: " + command);
}
} else { } else {
Serial.println("Failed to send: Not connected to IRC"); Serial.println("Failed to send: Not connected to IRC");
} }
@ -455,13 +510,14 @@ void sendIRC(String command) {
void handleIRC() { void handleIRC() {
while (client.available()) { while (client->available()) {
String line = client.readStringUntil('\n'); String line = client->readStringUntil('\n');
// This is an anomaly, but it can happen and I wanted debug output for if it does // This is an anomaly, but it can happen and I wanted debug output for if it does
if (line.length() > 512) if (line.length() > 512) {
Serial.println("WARNING: IRC line length exceeds 512 characters!"); Serial.println("WARNING: IRC line length exceeds 512 characters!");
line = line.substring(0, 512); // Truncate the line to 512 characters anyways line = line.substring(0, 512); // Truncate the line to 512 characters anyways
}
Serial.println("IRC: " + line); Serial.println("IRC: " + line);
@ -712,6 +768,11 @@ int renderFormattedMessage(String message, int cursorY, int lineHeight, bool hig
bool underline = false; bool underline = false;
bool nickHighlighted = false; // Track if the nick has been highlighted bool nickHighlighted = false; // Track if the nick has been highlighted
int nickPos = -1;
if (highlightNick) {
nickPos = message.indexOf(irc_nickname);
}
for (unsigned int i = 0; i < message.length(); i++) { for (unsigned int i = 0; i < message.length(); i++) {
char c = message[i]; char c = message[i];
if (c == '\x02') { // Bold if (c == '\x02') { // Bold
@ -761,9 +822,9 @@ int renderFormattedMessage(String message, int cursorY, int lineHeight, bool hig
tft.setTextColor(fgColor, bgColor); tft.setTextColor(fgColor, bgColor);
tft.setTextFont(1); tft.setTextFont(1);
} else { } else {
if (highlightNick && !nickHighlighted && message.substring(i).startsWith(nick)) { if (highlightNick && !nickHighlighted && nickPos != -1 && i == nickPos) {
tft.setTextColor(TFT_YELLOW, bgColor); // Set both foreground and background color tft.setTextColor(TFT_YELLOW, bgColor); // Set both foreground and background color
for (char nc : nick) { for (char nc : irc_nickname) {
tft.print(nc); tft.print(nc);
i++; i++;
} }
@ -806,6 +867,7 @@ int renderFormattedMessage(String message, int cursorY, int lineHeight, bool hig
} }
void displayLines() { void displayLines() {
tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK); tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK);
@ -878,7 +940,7 @@ void displayLines() {
tft.print(kicker); tft.print(kicker);
cursorY += CHAR_HEIGHT; cursorY += CHAR_HEIGHT;
} else if (line.startsWith("MODE ")) { } else if (line.startsWith("MODE ")) {
tft.setTextColor(TFT_YELLOW); tft.setTextColor(TFT_BLUE);
tft.print("MODE "); tft.print("MODE ");
String modeChange = line.substring(5); String modeChange = line.substring(5);
tft.setTextColor(TFT_WHITE); tft.setTextColor(TFT_WHITE);
@ -919,7 +981,7 @@ void displayLines() {
} }
void addLine(String senderNick, String message, String type, uint16_t errorColor = TFT_WHITE, uint16_t reasonColor = TFT_WHITE) { void addLine(String senderNick, String message, String type, bool mention = false, uint16_t errorColor = TFT_WHITE, uint16_t reasonColor = TFT_WHITE) {
if (type != "error" && nickColors.find(senderNick) == nickColors.end()) if (type != "error" && nickColors.find(senderNick) == nickColors.end())
nickColors[senderNick] = generateRandomColor(); nickColors[senderNick] = generateRandomColor();
@ -942,6 +1004,7 @@ void addLine(String senderNick, String message, String type, uint16_t errorColor
formattedMessage = "KICK " + senderNick + message; formattedMessage = "KICK " + senderNick + message;
} else if (type == "mode") { } else if (type == "mode") {
formattedMessage = "MODE " + message; formattedMessage = "MODE " + message;
tft.setTextColor(TFT_BLUE);
} else if (type == "action") { } else if (type == "action") {
formattedMessage = "* " + senderNick + " " + message; formattedMessage = "* " + senderNick + " " + message;
} else if (type == "error") { } else if (type == "error") {
@ -963,7 +1026,7 @@ void addLine(String senderNick, String message, String type, uint16_t errorColor
mentions.push_back(false); mentions.push_back(false);
} else { } else {
lines.push_back(formattedMessage); lines.push_back(formattedMessage);
mentions.push_back(false); mentions.push_back(mention);
} }
displayLines(); displayLines();
@ -984,7 +1047,7 @@ void parseAndDisplay(String line) {
int colonPos = line.indexOf(':', thirdSpace); int colonPos = line.indexOf(':', thirdSpace);
String message = line.substring(colonPos + 1); String message = line.substring(colonPos + 1);
String senderNick = line.substring(1, line.indexOf('!')); String senderNick = line.substring(1, line.indexOf('!'));
bool mention = message.indexOf(nick) != -1; bool mention = message.indexOf(irc_nickname) != -1;
if (message.startsWith(String("\x01") + "ACTION ") && message.endsWith("\x01")) { if (message.startsWith(String("\x01") + "ACTION ") && message.endsWith("\x01")) {
String actionMessage = message.substring(8, message.length() - 1); String actionMessage = message.substring(8, message.length() - 1);
@ -1006,14 +1069,15 @@ void parseAndDisplay(String line) {
String prefix = line.startsWith(":") ? line.substring(1, firstSpace) : ""; String prefix = line.startsWith(":") ? line.substring(1, firstSpace) : "";
String newNick = line.substring(line.lastIndexOf(':') + 1); String newNick = line.substring(line.lastIndexOf(':') + 1);
if (prefix == "") { // Our own NICK changes if (prefix.indexOf('!') == -1) { // Our own NICK changes
addLine(nick, " -> " + newNick, "nick"); addLine(irc_nickname, " -> " + newNick, "nick");
nick = newNick; irc_nickname = newNick; // Update global nick when we get confirmation
} else { // Other peoples NICK change } else { // Other peoples NICK change
String oldNick = prefix.substring(0, prefix.indexOf('!')); String oldNick = prefix.substring(0, prefix.indexOf('!'));
if (oldNick == nick)
nick = newNick; // Update global nick when we get confirmation
addLine(oldNick, " -> " + newNick, "nick"); addLine(oldNick, " -> " + newNick, "nick");
if (oldNick == irc_nickname) {
irc_nickname = newNick; // Update global nick when we get confirmation
}
} }
} else if (command == "KICK") { } else if (command == "KICK") {
int thirdSpace = line.indexOf(' ', secondSpace + 1); int thirdSpace = line.indexOf(' ', secondSpace + 1);
@ -1056,11 +1120,11 @@ void handleKeyboardInput(char key) {
} else if (inputBuffer.startsWith("/me ")) { } else if (inputBuffer.startsWith("/me ")) {
String actionMessage = inputBuffer.substring(4); String actionMessage = inputBuffer.substring(4);
sendIRC("PRIVMSG " + String(channel) + " :\001ACTION " + actionMessage + "\001"); sendIRC("PRIVMSG " + String(channel) + " :\001ACTION " + actionMessage + "\001");
addLine(nick, actionMessage, "action"); addLine(irc_nickname, actionMessage, "action");
inputBuffer = ""; inputBuffer = "";
} else { } else {
sendIRC("PRIVMSG " + String(channel) + " :" + inputBuffer); sendIRC("PRIVMSG " + String(channel) + " :" + inputBuffer);
addLine(nick, inputBuffer, "message"); addLine(irc_nickname, inputBuffer, "message");
} }
inputBuffer = ""; inputBuffer = "";
displayInputLine(); displayInputLine();