From 63faf519c19bbd1ab79ac7e9cd1f7d6b4c6e34f2 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sat, 7 Oct 2023 02:29:08 -0700 Subject: [PATCH] Working on adding a display. --- include/Display.h | 34 +++++++++++++ include/internal/DisplayAdafruitOLED.h | 26 ++++++++++ include/internal/DisplayNone.h | 20 ++++++++ include/prefs.h | 4 +- platformio.ini | 33 +++++++++++-- src/DisplayAdafruitOLED.cc | 68 ++++++++++++++++++++++++++ src/DisplayNone.cc | 22 +++++++++ src/WiFiMgr.cc | 30 +++++++++++- src/main.cc | 8 ++- src/prefs.cc | 10 +++- 10 files changed, 244 insertions(+), 11 deletions(-) create mode 100644 include/Display.h create mode 100644 include/internal/DisplayAdafruitOLED.h create mode 100644 include/internal/DisplayNone.h create mode 100644 src/DisplayAdafruitOLED.cc create mode 100644 src/DisplayNone.cc diff --git a/include/Display.h b/include/Display.h new file mode 100644 index 0000000..d8eff6c --- /dev/null +++ b/include/Display.h @@ -0,0 +1,34 @@ +#ifndef KIMODEM_DISPLAY_H +#define KIMODEM_DISPLAY_H + +#include + + +#define ANSI_CLEAR_SCREEN "\33[2J" + +/* +class Display { +public: + Display(); + + void Clear(); + void Draw(); + bool HasDisplay(); + + void Print(const char *); + void Println(const char *); + void SetCursor(int16_t x, int16_t y); +}; +*/ + +#if defined(ADAFRUIT_OLED) +#include "internal/DisplayAdafruitOLED.h" +#else +#include "internal/DisplayNone.h" +#endif + + +static Display display; + + +#endif \ No newline at end of file diff --git a/include/internal/DisplayAdafruitOLED.h b/include/internal/DisplayAdafruitOLED.h new file mode 100644 index 0000000..365416f --- /dev/null +++ b/include/internal/DisplayAdafruitOLED.h @@ -0,0 +1,26 @@ +#ifndef KIMODEM_ADAFRUIT_OLED_H +#define KIMODEM_ADAFRUIT_OLED_H + +#include +#include +#include +#include + +class Display { +public: + Display(); + + void Clear(); + void Draw(); + bool HasDisplay(); + + void Print(const char *); + void Println(const char *); + void SetCursor(int16_t x, int16_t y); +private: + Adafruit_SSD1306 oled; +}; + + + +#endif \ No newline at end of file diff --git a/include/internal/DisplayNone.h b/include/internal/DisplayNone.h new file mode 100644 index 0000000..d6f12fd --- /dev/null +++ b/include/internal/DisplayNone.h @@ -0,0 +1,20 @@ +#ifndef KIMODEM_DISPLAY_SERIAL +#define KIMODEM_DISPLAY_SERIAL + +#include + + +class Display { +public: + Display() {}; + + void Clear(); + void Draw(); + bool HasDisplay(); + + void Print(const char *); + void Println(const char *); + void SetCursor(int16_t x, int16_t y); +}; + +#endif \ No newline at end of file diff --git a/include/prefs.h b/include/prefs.h index 2fca6f5..36cb5c8 100644 --- a/include/prefs.h +++ b/include/prefs.h @@ -6,8 +6,8 @@ namespace config { // Networking -size_t LastSSID(char *ssid); -void SetLastSSID(const char *ssid, size_t ssidlen); +size_t LastSSID(char *ssid, size_t ssidlen); +void SetLastSSID(const char *ssid); bool StartTryConnecting(); void SetStartTryConnecting(bool opt); diff --git a/platformio.ini b/platformio.ini index 0d37b26..9deb2de 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,11 +8,34 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[platformio] - -[env:sparkfun_esp32micromod] -platform = espressif32 -board = sparkfun_esp32micromod +[env] framework = arduino +platform = espressif32 monitor_speed = 115200 extra_scripts = scripts/download_fs.py + +[env:featheresp32] +board = featheresp32 +build_flags = +; -DDISPLAY_NONE + -DADAFRUIT_OLED +lib_deps = + adafruit/Adafruit GFX Library@^1.11.8 + adafruit/Adafruit SSD1306@^2.5.7 + +[env:sparkfun_esp32micromod] +board = sparkfun_esp32micromod +build_flags = + -DSPARKFUN_HYPERDISPLAY + -DDISPLAY_NONE + +[env:d1mini] +board = wemos_d1_mini32 +build_flags = + -DDISPLAY_NONE + +[env:huzzah] +platform = espressif8266 +board = huzzah +build_flags = + -DDISPLAY_NONE diff --git a/src/DisplayAdafruitOLED.cc b/src/DisplayAdafruitOLED.cc new file mode 100644 index 0000000..fe6661b --- /dev/null +++ b/src/DisplayAdafruitOLED.cc @@ -0,0 +1,68 @@ +#if defined(ADAFRUIT_OLED) +#include +#include +#include +#include +#include + +#include "internal/DisplayAdafruitOLED.h" + + +static Adafruit_SSD1306 oled(SSD1306_SWITCHCAPVCC, 0x3C); + + + +Display::Display() + : oled(oled) +{ + Serial.println("initialized display"); + oled.setTextSize(1); + oled.setTextColor(SSD1306_WHITE); +} + + +void +Display::Clear() +{ + this->oled.clearDisplay(); + this->SetCursor(0, 0); + this->Draw(); +} + + +void +Display::Draw() +{ + this->oled.display(); +} + + +bool +Display::HasDisplay() +{ + return true; +} + + +void +Display::Print(const char *s) +{ + this->oled.print(s); +} + + +void +Display::Println(const char *s) +{ + this->oled.println(s); +} + + +void +Display::SetCursor(int16_t x, int16_t y) +{ + this->oled.setCursor(x, y); +} + + +#endif \ No newline at end of file diff --git a/src/DisplayNone.cc b/src/DisplayNone.cc new file mode 100644 index 0000000..fa6204c --- /dev/null +++ b/src/DisplayNone.cc @@ -0,0 +1,22 @@ +#if defined(NO_DISPLAY) + +#include "Display.h" +#include "internal/DisplayNone.h" + +void +Display::Clear() +{ + Serial.println("\33[2J"); +} + + +void Display::Draw() {} +bool Display::HasDisplay() { return false; } + + +void Display::Print(const char *s) {}; +void Display::Println(const char *) {}; +void Display::SetCursor(int16_t x, int16_t y) {}; + + +#endif \ No newline at end of file diff --git a/src/WiFiMgr.cc b/src/WiFiMgr.cc index cfa542a..daf4e8f 100644 --- a/src/WiFiMgr.cc +++ b/src/WiFiMgr.cc @@ -3,9 +3,13 @@ #include #include "Dictionary.h" +#include "Display.h" #include "TLV.h" +#include "prefs.h" + #include "WiFiMgr.h" + #define MAX_WAIT 10000 #define CONN_WAIT 250 @@ -37,6 +41,14 @@ tryConnect(const char *ssid, const char *wpakey) if (WiFi.status() == WL_CONNECTED) { Serial.print("MODEM ADDR "); Serial.println(WiFi.localIP()); + + display.Clear(); + display.Print("SSID: "); + display.Println(ssid); + display.Print("IP: "); + display.Println(WiFi.localIP().toString().c_str()); + display.Draw(); + return true; } @@ -52,6 +64,18 @@ TryConnect(Dictionary &pb) TLV::Record wpakey; uint8_t *cursor; + if ((ssid.Len = config::LastSSID(ssid.Val, 32)) != 0) { + Serial.print("LAST SSID: "); + Serial.println(ssid.Val); + if (pb.Lookup(ssid.Val, ssid.Len, wpakey)) { + if (tryConnect((const char *)ssid.Val, (const char *)wpakey.Val)) { + return true; + } + } else { + Serial.println("WPAKEY NOT FOUND"); + } + } + while ((cursor != NULL) && (cursor[0] != TAG_EMPTY)) { TLV::ReadFromMemory(ssid, cursor); TLV::SkipRecord(ssid, cursor); @@ -80,13 +104,15 @@ tryConnect(Dictionary &pb, int network) } Serial.print("MODEM: CHECK "); - Serial.println(ssid); + Serial.print(ssid); + Serial.print(": "); if (!pb.Lookup(ssid, uint8_t(ssidLen), password)) { - Serial.println("SSID NOT IN PHONEBOOK"); + Serial.println("NOT FOUND"); return false; } + Serial.println("CONNECTING"); return tryConnect(ssid, password.Val); } diff --git a/src/main.cc b/src/main.cc index e76e0ec..35065e2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,6 +3,7 @@ #include "Arena.h" #include "Dictionary.h" +#include "Display.h" #include "WiFiMgr.h" #include "prefs.h" #include "homenet.h" @@ -33,6 +34,8 @@ setupPhonebook() } pbFile.close(); + config::SetLastSSID(HOME_SSID); + return ok; } fileSize = fileSize > PHONEBOOK_SIZE ? PHONEBOOK_SIZE : fileSize; @@ -55,10 +58,14 @@ void setup() { Serial.begin(115200); while (!Serial) ; + display.Clear(); + Serial.println("MODEM BOOT"); InitializeArena(arena); NewStaticArena(arena, phonebookBuffer, PHONEBOOK_SIZE); + while (true); + if (!SPIFFS.begin(true) && !SPIFFS.begin(false)) { Serial.println("SPIFFS BEGIN FAIL"); while (true) ; @@ -71,7 +78,6 @@ void setup() { } } - Serial.println("MODEM READY"); } diff --git a/src/prefs.cc b/src/prefs.cc index df29094..38e122c 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -24,8 +24,16 @@ checkReady() size_t LastSSID(char *ssid, size_t ssidlen) { + size_t len; checkReady(); - return preferences.getString("SSID", ssid, ssidlen); + len = preferences.getString("SSID", ssid, ssidlen); + if (len > 0) { + // preferences.getString includes the null byte in + // the result. c.f. DQTNA. + len--; + } + + return len; }