From 00eac16fd6bc1667a4e95af427681797f6dd9b05 Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Fri, 11 Dec 2020 14:36:26 -0500 Subject: [PATCH] use the Preferences library to store device config in NVM --- wiflash_esp32/wiflash_esp32.ino | 244 +++++++++++++++++++------------- 1 file changed, 144 insertions(+), 100 deletions(-) diff --git a/wiflash_esp32/wiflash_esp32.ino b/wiflash_esp32/wiflash_esp32.ino index e35c63d..e889207 100644 --- a/wiflash_esp32/wiflash_esp32.ino +++ b/wiflash_esp32/wiflash_esp32.ino @@ -28,81 +28,57 @@ #include #include -#include "sacn.h" #include +#include +#include +#include "sacn.h" #include "strobe_esp32.h" #include "dmx_universe.h" -#include /* - This section to be configured by Lighting Control + If this pin is tied LOW at setup(), settings will be defaulted */ -// ----------------------------------- -// Configure the WiFi network -// ----------------------------------- -const char ssid[] = "........"; -const char pwd[] = "........"; - -// ----------------------------------- -// Configure the IPv4 network -// ----------------------------------- -const IPAddress ip(127, 0, 0, 1); -const IPAddress subnet(255, 255, 255, 0); -const IPAddress gateway(127, 0, 0, 1); - -// ----------------------------------- -// Configure OSC -// ----------------------------------- -const String host = "127.0.0.1"; -const uint16_t port = 7001; -const String pressed_addr = "/hog/playback/go/0"; -const String pressed_value = "99.1"; -const String released_addr = "/hog/playback/go/0"; -const String released_value = "99.2"; - -// ----------------------------------- -// Configure E1.31 sACN -// ----------------------------------- -const e131_listen_t sACN_mode = E131_UNICAST; // (uni|multi)cast -const uint16_t pixel_universe = 1; -const uint16_t pixel_address = 001; -const uint16_t strobe_universe = 1; -const uint16_t strobe_address = 501; - -/* DMX Value - | chan | Command | range | range | - |------+-----------+-------+------------| - | 1 | Intensity | 0- | 0-100% | 15 bit 2.4kHz PWM - | 2 | fine | 65535 | | - |------+-----------+-------+------------| - | 3 | Duration | 0-255 | 0.02-0.5s | 0.002s resolution - |------+-----------+-------+------------| - | 4 | Rate | 0-255 | 0.5-25hz | 0.1 hz resolution - |------+-----------+-------+------------| -*/ - +const int reset_button = A1; // tie A1 to ground to reset preferances /* - This section to be configured by the Fixtures Dept. + device configuration */ -const bool BUTTON = true; -const int button = A0; // A0, use 100nF to ground +struct config_ { + String ssid; + String pwd; + wifi_mode_t mode; -const bool STROBE = true; -const int led = LED_BUILTIN; // IO13 + String ip; + String subnet; + String gateway; -const bool STRIP = true; -const uint16_t PixelCount = 8; // not less than 4 -const uint8_t PixelPin = 21; + String osc_host; + uint16_t osc_port; + String osc_pressed_addr; + String osc_pressed_value; + String osc_released_addr; + String osc_released_value; + bool button_enable; + int button_pin; + + bool strobe_enable; + int strobe_led_pin; + uint16_t strobe_universe; + uint16_t strobe_address; + + bool strip_enable; + uint16_t strip_led_count; + uint8_t strip_data_pin; + uint16_t strip_universe; + uint16_t strip_address; +}; +struct config_ config; -/* - Change nothing else unless you're really sure. -*/ //// device objects -Strobe *strobe = new Strobe(strobe_address); -NeoPixelBus strip(PixelCount, PixelPin); +Strobe *strobe; +NeoPixelBus *strip; ESPAsyncE131 *e131 = new ESPAsyncE131(); //// Global button variables @@ -110,17 +86,75 @@ bool pressed = false; // track button state uint32_t change_time; // time of button press (ms) - +/* + change pixel strip colours on DMX change +*/ void recvPixelData(Universe *universe) { - for (int i = 0; i < PixelCount; i++) { + for (int i = 0; i < config.strip_led_count; i++) { uint8_t r, g, b, w; - r = universe->data()->data[pixel_address + (i * 4) + 0]; - g = universe->data()->data[pixel_address + (i * 4) + 1]; - b = universe->data()->data[pixel_address + (i * 4) + 2]; - w = universe->data()->data[pixel_address + (i * 4) + 3]; - strip.SetPixelColor(i, RgbwColor(r, g, b, w)); + r = universe->data()->data[config.strip_address + (i * 4) + 0]; + g = universe->data()->data[config.strip_address + (i * 4) + 1]; + b = universe->data()->data[config.strip_address + (i * 4) + 2]; + w = universe->data()->data[config.strip_address + (i * 4) + 3]; + strip->SetPixelColor(i, RgbwColor(r, g, b, w)); } - strip.Show(); + strip->Show(); +} + + +/* + Change a setting in NVM +*/ +bool changeSetting(String key, String val) { + return true; +} + +/* + Load device configuration from NVM +*/ +bool loadConfig() { + prefs.begin("wiflash", false); // open RW; + + Serial.print("Jump pin "); + Serial.print(reset_button); + Serial.println(" to GROUND to reset settings."); + pinMode(reset_button, INPUT_PULLUP); + if (prefs.getBool("reset_on_reboot") || + !digitalRead(reset_button)) { + prefs.clear(); + } + + config.ssid = prefs.getString("ssid", "WiFlash"); + config.pwd = prefs.getString("pwd", "technologyismagic"); + config.mode = (wifi_mode_t)prefs.getChar("wifi_mode", WIFI_AP); + + config.ip = prefs.getString("ip", "192.168.1.1"); + config.subnet = prefs.getString("subnet", "255.255.255.0"); + config.gateway = prefs.getString("gateway", ""); + + config.button_enable = prefs.getBool("button_enable", true); + config.button_pin = prefs.getChar("butto_pin", A0); // A0, use 100nF to ground + + config.osc_host = prefs.getString("osc_host", "192.168.1.1"); + config.osc_port = prefs.getShort("osc_port", 7001); + config.osc_pressed_addr = prefs.getString("osc_pressed_addr", "/hog/playback/go/0"); + config.osc_pressed_value = prefs.getString("osc_pressed_value", "99.1"); + config.osc_released_addr = prefs.getString("osc_released_addr", "/hog/playback/go/0"); + config.osc_released_value = prefs.getString("osc_released_value", "99.2"); + + config.strobe_enable = prefs.getBool("strobe_enable", true); + config.strobe_led_pin = prefs.getChar("strobe_led_pin", LED_BUILTIN); // IO13 + config.strobe_universe = prefs.getShort("strobe_universe", 1); + config.strobe_address = prefs.getShort("strobe_address", 001); + + config.strip_enable = prefs.getBool("strip_enable", false); + config.strip_led_count = prefs.getShort("strip_led_count", 8); // not less than 4 + config.strip_data_pin = prefs.getChar("strip_data_pin", 21); + config.strip_universe = prefs.getShort("strip_universe", 1); + config.strip_address = prefs.getShort("strip_address", 001); + + return true; +} } @@ -130,52 +164,60 @@ void recvPixelData(Universe *universe) { void setup() { Serial.begin(115200); while (!Serial); // wait for serial attach + Serial.println("Serial started!"); + + Serial.println("Loading Prefferences from NVM."); + loadConfig(); + + //// start WiFi: + // set IPv4 + Serial.println("Starting WiFi."); + WiFi.mode(WIFI_STA); + IPAddress ip_, gateway_, subnet_; + ip_.fromString(config.ip); + gateway_.fromString(config.gateway); + subnet_.fromString(config.subnet); + WiFi.config(ip_, gateway_, subnet_); + WiFi.begin(config.ssid.c_str(), config.pwd.c_str()); Serial.println("Disabling Bluetooth."); btStop(); - //// start WiFi: - // set IPv4 - WiFi.mode(WIFI_STA); - WiFi.config(ip, gateway, subnet); - WiFi.begin(ssid, pwd); - - // join 802.11 - if (WiFi.config(ip, gateway, subnet)) { - Serial.print("Connected to "); - Serial.println(ssid); - Serial.print("IP address is "); - Serial.println(WiFi.localIP()); - } else { - Serial.println("STA Failed to configure"); - } //// start fixtures hardware: // button - if (BUTTON) { - pinMode(button, INPUT_PULLUP); + if (config.button_enable) { + Serial.println("Initializing OSC button"); + pinMode(config.button_pin, INPUT_PULLUP); } // strobe - if (STROBE) { - if (!strobe->begin(led, 0)) { + if (config.strobe_enable) { + strobe = new Strobe(config.strobe_address); + Serial.println("Initializing DMX Strobe"); + if (!strobe->begin(config.strobe_led_pin, 0)) { Serial.println("Strobe failed to configure."); } - if (e131->subscribe(strobe_universe, sACN_mode)) { - e131->universe(strobe_universe)->onData(std::bind(&Strobe::recvData, - strobe, std::placeholders::_1)); + if (e131->subscribe(config.strobe_universe)) { + e131->universe(config.strobe_universe)->onData(std::bind(&Strobe::recvData, + strobe, std::placeholders::_1)); } } // pixels - if (STRIP) { - strip.Begin(); - strip.Show(); - if (e131->subscribe(pixel_universe, sACN_mode)) { - e131->universe(pixel_universe)->onData(std::bind(&recvPixelData, - std::placeholders::_1)); + if (config.strip_enable) { + Serial.println("Initializing Pixelmapped LEDs."); + + strip = new NeoPixelBus(config.strip_led_count, config.strip_data_pin); + strip->Begin(); + strip->Show(); + if (e131->subscribe(config.strip_universe)) { + e131->universe(config.strip_universe)->onData(std::bind(&recvPixelData, + std::placeholders::_1)); } } + + Serial.println("Setup complete!"); } @@ -191,16 +233,18 @@ void loop() { // Be a remote trigger // - bool val = !digitalRead(button); - if (val != pressed && BUTTON) { + bool val = !digitalRead(config.button_pin); + if (val != pressed && config.button_enable) { pressed = val; change_time = millis(); if (pressed) { Serial.println("Pressed"); - OscWiFi.send(host, port, pressed_addr, pressed_value); + OscWiFi.send(config.osc_host, config.osc_port, + config.osc_pressed_addr, config.osc_pressed_value); } else { Serial.println("Released"); - OscWiFi.send(host, port, released_addr, released_value); + OscWiFi.send(config.osc_host, config.osc_port, + config.osc_released_addr, config.osc_released_value); } } }