use the Preferences library to store device config in NVM

This commit is contained in:
Kevin Matz 2020-12-11 14:36:26 -05:00
parent 73eb72a638
commit 00eac16fd6

View File

@ -28,81 +28,57 @@
#include <Arduino.h> #include <Arduino.h>
#include <WiFi.h> #include <WiFi.h>
#include "sacn.h"
#include <ArduinoOSC.h> #include <ArduinoOSC.h>
#include <NeoPixelBus.h>
#include <Preferences.h>
#include "sacn.h"
#include "strobe_esp32.h" #include "strobe_esp32.h"
#include "dmx_universe.h" #include "dmx_universe.h"
#include <NeoPixelBus.h>
/* /*
This section to be configured by Lighting Control If this pin is tied LOW at setup(), settings will be defaulted
*/ */
// ----------------------------------- const int reset_button = A1; // tie A1 to ground to reset preferances
// 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
|------+-----------+-------+------------|
*/
/* /*
This section to be configured by the Fixtures Dept. device configuration
*/ */
const bool BUTTON = true; struct config_ {
const int button = A0; // A0, use 100nF to ground String ssid;
String pwd;
wifi_mode_t mode;
const bool STROBE = true; String ip;
const int led = LED_BUILTIN; // IO13 String subnet;
String gateway;
const bool STRIP = true; String osc_host;
const uint16_t PixelCount = 8; // not less than 4 uint16_t osc_port;
const uint8_t PixelPin = 21; 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 //// device objects
Strobe *strobe = new Strobe(strobe_address); Strobe *strobe;
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin); NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> *strip;
ESPAsyncE131 *e131 = new ESPAsyncE131(); ESPAsyncE131 *e131 = new ESPAsyncE131();
//// Global button variables //// Global button variables
@ -110,17 +86,75 @@ bool pressed = false; // track button state
uint32_t change_time; // time of button press (ms) uint32_t change_time; // time of button press (ms)
/*
change pixel strip colours on DMX change
*/
void recvPixelData(Universe *universe) { 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; uint8_t r, g, b, w;
r = universe->data()->data[pixel_address + (i * 4) + 0]; r = universe->data()->data[config.strip_address + (i * 4) + 0];
g = universe->data()->data[pixel_address + (i * 4) + 1]; g = universe->data()->data[config.strip_address + (i * 4) + 1];
b = universe->data()->data[pixel_address + (i * 4) + 2]; b = universe->data()->data[config.strip_address + (i * 4) + 2];
w = universe->data()->data[pixel_address + (i * 4) + 3]; w = universe->data()->data[config.strip_address + (i * 4) + 3];
strip.SetPixelColor(i, RgbwColor(r, g, b, w)); strip->SetPixelColor(i, RgbwColor(r, g, b, w));
}
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;
} }
strip.Show();
} }
@ -130,52 +164,60 @@ void recvPixelData(Universe *universe) {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial); // wait for serial attach 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."); Serial.println("Disabling Bluetooth.");
btStop(); 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: //// start fixtures hardware:
// button // button
if (BUTTON) { if (config.button_enable) {
pinMode(button, INPUT_PULLUP); Serial.println("Initializing OSC button");
pinMode(config.button_pin, INPUT_PULLUP);
} }
// strobe // strobe
if (STROBE) { if (config.strobe_enable) {
if (!strobe->begin(led, 0)) { 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."); Serial.println("Strobe failed to configure.");
} }
if (e131->subscribe(strobe_universe, sACN_mode)) { if (e131->subscribe(config.strobe_universe)) {
e131->universe(strobe_universe)->onData(std::bind(&Strobe::recvData, e131->universe(config.strobe_universe)->onData(std::bind(&Strobe::recvData,
strobe, std::placeholders::_1)); strobe, std::placeholders::_1));
} }
} }
// pixels // pixels
if (STRIP) { if (config.strip_enable) {
strip.Begin(); Serial.println("Initializing Pixelmapped LEDs.");
strip.Show();
if (e131->subscribe(pixel_universe, sACN_mode)) { strip = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(config.strip_led_count, config.strip_data_pin);
e131->universe(pixel_universe)->onData(std::bind(&recvPixelData, strip->Begin();
strip->Show();
if (e131->subscribe(config.strip_universe)) {
e131->universe(config.strip_universe)->onData(std::bind(&recvPixelData,
std::placeholders::_1)); std::placeholders::_1));
} }
} }
Serial.println("Setup complete!");
} }
@ -191,16 +233,18 @@ void loop() {
// Be a remote trigger // Be a remote trigger
// //
bool val = !digitalRead(button); bool val = !digitalRead(config.button_pin);
if (val != pressed && BUTTON) { if (val != pressed && config.button_enable) {
pressed = val; pressed = val;
change_time = millis(); change_time = millis();
if (pressed) { if (pressed) {
Serial.println("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 { } else {
Serial.println("Released"); 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);
} }
} }
} }