2020-12-01 19:40:58 -05:00
|
|
|
/*
|
|
|
|
WiFlash_esp32 - Concept & electronics: Phil Abeyta
|
|
|
|
- System design & firmware: Kevin Matz
|
2020-12-04 14:57:57 -05:00
|
|
|
|
|
|
|
Uses a GPIO to control a lighting console over OSC, whilst also
|
|
|
|
controlling an LED strobe light with DMX data recieved over sACN.
|
|
|
|
|
|
|
|
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
2020-12-01 19:40:58 -05:00
|
|
|
*/
|
|
|
|
|
2020-12-08 17:25:05 -05:00
|
|
|
#include <Arduino.h>
|
2020-12-01 19:40:58 -05:00
|
|
|
#include <WiFi.h>
|
|
|
|
#include <ArduinoOSC.h>
|
2020-12-11 14:36:26 -05:00
|
|
|
#include <NeoPixelBus.h>
|
|
|
|
#include <Preferences.h>
|
|
|
|
#include "sacn.h"
|
2020-12-01 19:40:58 -05:00
|
|
|
#include "strobe_esp32.h"
|
2020-12-08 17:25:05 -05:00
|
|
|
#include "dmx_universe.h"
|
2020-12-01 19:40:58 -05:00
|
|
|
|
|
|
|
/*
|
2020-12-11 14:36:26 -05:00
|
|
|
If this pin is tied LOW at setup(), settings will be defaulted
|
2020-12-04 14:57:57 -05:00
|
|
|
*/
|
2020-12-11 14:36:26 -05:00
|
|
|
const int reset_button = A1; // tie A1 to ground to reset preferances
|
2020-12-04 14:57:57 -05:00
|
|
|
|
|
|
|
/*
|
2020-12-11 14:36:26 -05:00
|
|
|
device configuration
|
2020-12-04 14:57:57 -05:00
|
|
|
*/
|
2020-12-11 14:36:26 -05:00
|
|
|
struct config_ {
|
|
|
|
String ssid;
|
|
|
|
String pwd;
|
|
|
|
wifi_mode_t mode;
|
|
|
|
|
|
|
|
String ip;
|
|
|
|
String subnet;
|
|
|
|
String gateway;
|
|
|
|
|
|
|
|
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;
|
2020-12-01 19:40:58 -05:00
|
|
|
|
|
|
|
|
2020-12-04 09:44:58 -05:00
|
|
|
//// device objects
|
2020-12-11 14:36:26 -05:00
|
|
|
Strobe *strobe;
|
|
|
|
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> *strip;
|
2020-12-04 09:44:58 -05:00
|
|
|
ESPAsyncE131 *e131 = new ESPAsyncE131();
|
|
|
|
|
2020-12-01 19:40:58 -05:00
|
|
|
//// Global button variables
|
|
|
|
bool pressed = false; // track button state
|
|
|
|
uint32_t change_time; // time of button press (ms)
|
|
|
|
|
2020-12-08 17:25:05 -05:00
|
|
|
|
2020-12-11 14:36:26 -05:00
|
|
|
/*
|
|
|
|
change pixel strip colours on DMX change
|
|
|
|
*/
|
2020-12-08 17:25:05 -05:00
|
|
|
void recvPixelData(Universe *universe) {
|
2020-12-11 14:36:26 -05:00
|
|
|
for (int i = 0; i < config.strip_led_count; i++) {
|
2020-12-08 17:25:05 -05:00
|
|
|
uint8_t r, g, b, w;
|
2020-12-11 14:36:26 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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();
|
2020-12-08 17:25:05 -05:00
|
|
|
}
|
2020-12-11 14:36:26 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-12-08 17:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-01 19:40:58 -05:00
|
|
|
/*
|
2020-12-04 14:57:57 -05:00
|
|
|
Arduino powerup
|
|
|
|
*/
|
2020-12-01 19:40:58 -05:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
2020-12-08 17:23:56 -05:00
|
|
|
while (!Serial); // wait for serial attach
|
2020-12-11 14:36:26 -05:00
|
|
|
Serial.println("Serial started!");
|
2020-12-01 19:40:58 -05:00
|
|
|
|
2020-12-11 14:36:26 -05:00
|
|
|
Serial.println("Loading Prefferences from NVM.");
|
|
|
|
loadConfig();
|
2020-12-06 14:09:42 -05:00
|
|
|
|
2020-12-04 14:57:57 -05:00
|
|
|
//// start WiFi:
|
2020-12-01 19:40:58 -05:00
|
|
|
// set IPv4
|
2020-12-11 14:36:26 -05:00
|
|
|
Serial.println("Starting WiFi.");
|
2020-12-01 19:40:58 -05:00
|
|
|
WiFi.mode(WIFI_STA);
|
2020-12-11 14:36:26 -05:00
|
|
|
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();
|
|
|
|
|
2020-12-01 19:40:58 -05:00
|
|
|
|
2020-12-04 14:57:57 -05:00
|
|
|
//// start fixtures hardware:
|
2020-12-01 19:40:58 -05:00
|
|
|
// button
|
2020-12-11 14:36:26 -05:00
|
|
|
if (config.button_enable) {
|
|
|
|
Serial.println("Initializing OSC button");
|
|
|
|
pinMode(config.button_pin, INPUT_PULLUP);
|
2020-12-01 19:40:58 -05:00
|
|
|
}
|
2020-12-09 10:48:22 -05:00
|
|
|
|
|
|
|
// strobe
|
2020-12-11 14:36:26 -05:00
|
|
|
if (config.strobe_enable) {
|
|
|
|
strobe = new Strobe(config.strobe_address);
|
|
|
|
Serial.println("Initializing DMX Strobe");
|
|
|
|
if (!strobe->begin(config.strobe_led_pin, 0)) {
|
2020-12-09 10:48:22 -05:00
|
|
|
Serial.println("Strobe failed to configure.");
|
|
|
|
}
|
2020-12-11 14:36:26 -05:00
|
|
|
if (e131->subscribe(config.strobe_universe)) {
|
|
|
|
e131->universe(config.strobe_universe)->onData(std::bind(&Strobe::recvData,
|
|
|
|
strobe, std::placeholders::_1));
|
2020-12-09 10:48:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 17:25:05 -05:00
|
|
|
// pixels
|
2020-12-11 14:36:26 -05:00
|
|
|
if (config.strip_enable) {
|
|
|
|
Serial.println("Initializing Pixelmapped LEDs.");
|
|
|
|
|
|
|
|
strip = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(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));
|
2020-12-09 10:48:22 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 14:36:26 -05:00
|
|
|
|
|
|
|
Serial.println("Setup complete!");
|
2020-12-01 19:40:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-04 14:57:57 -05:00
|
|
|
/*
|
|
|
|
Arduino process loop
|
|
|
|
*/
|
2020-12-01 19:40:58 -05:00
|
|
|
void loop() {
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
|
|
Serial.println("Waiting for WiFi...");
|
|
|
|
delay(1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be a remote trigger
|
|
|
|
//
|
2020-12-11 14:36:26 -05:00
|
|
|
bool val = !digitalRead(config.button_pin);
|
|
|
|
if (val != pressed && config.button_enable) {
|
2020-12-01 19:40:58 -05:00
|
|
|
pressed = val;
|
|
|
|
change_time = millis();
|
|
|
|
if (pressed) {
|
|
|
|
Serial.println("Pressed");
|
2020-12-11 14:36:26 -05:00
|
|
|
OscWiFi.send(config.osc_host, config.osc_port,
|
|
|
|
config.osc_pressed_addr, config.osc_pressed_value);
|
2020-12-01 19:40:58 -05:00
|
|
|
} else {
|
|
|
|
Serial.println("Released");
|
2020-12-11 14:36:26 -05:00
|
|
|
OscWiFi.send(config.osc_host, config.osc_port,
|
|
|
|
config.osc_released_addr, config.osc_released_value);
|
2020-12-01 19:40:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|