From 39a773805ea1e92c25115a5b35facbdce56db592 Mon Sep 17 00:00:00 2001 From: Debashish Sahu Date: Fri, 25 Jan 2019 14:15:39 -0500 Subject: [PATCH] Make PIN configurable via REST API - new "REST API": /pixels?pin=GPIO_NO to change PIN# (Allowed GPIO values: 16/5/4/0/2/14/12/13/15/3/1) --- Arduino/McLighting/McLighting.ino | 48 +++++++++++++-- Arduino/McLighting/definitions.h | 6 +- Arduino/McLighting/request_handlers.h | 84 +++++++++++++++++++++++++++ Arduino/McLighting/version_info.ino | 1 + README.md | 11 ++-- 5 files changed, 136 insertions(+), 14 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index eb4613c..f4a99a1 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -114,6 +114,7 @@ void initDMA(uint16_t stripSize = NUMLEDS){ #endif dma->Initialize(); } + void DMA_Show(void) { if(dma->IsReadyToUpdate()) { memcpy(dma->getPixels(), strip->getPixels(), dma->getPixelsSize()); @@ -228,15 +229,16 @@ void saveConfigCallback () { // *************************************************************************** #include "request_handlers.h" -neoPixelType RGBOrderOnStrip = NEO_GRB; -// -void initStrip(uint16_t stripSize = NUMLEDS, neoPixelType RGBOrder = NEO_GRB){ +// function to Initialize the strip +void initStrip(uint16_t stripSize = WS2812FXStripSettings.stripSize, neoPixelType RGBOrder = WS2812FXStripSettings.RGBOrder, uint8_t pin = WS2812FXStripSettings.pin){ if (strip) { - if (strip->getLength() == stripSize && RGBOrderOnStrip == RGBOrder) return; + if (strip->getLength() == stripSize && WS2812FXStripSettings.RGBOrder == RGBOrder && WS2812FXStripSettings.pin == pin) return; delete strip; - RGBOrderOnStrip = RGBOrder; + WS2812FXStripSettings.stripSize = stripSize; + WS2812FXStripSettings.RGBOrder = RGBOrder; + WS2812FXStripSettings.pin = pin; } - strip = new WS2812FX(stripSize, PIN, RGBOrder + NEO_KHZ800); + strip = new WS2812FX(stripSize, pin, RGBOrder + NEO_KHZ800); // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: @@ -260,6 +262,7 @@ void initStrip(uint16_t stripSize = NUMLEDS, neoPixelType RGBOrder = NEO_GRB){ //strip->setMode(FX_MODE_RAINBOW_CYCLE); strip->setColor(main_color.red, main_color.green, main_color.blue); strip->start(); + saveWS2812FXStripSettings.once(3, writeStripConfigFS); } // *************************************************************************** @@ -309,6 +312,7 @@ void setup() { // *************************************************************************** // Setup: Neopixel // *************************************************************************** + readStripConfigFS(); // Read config from FS first initStrip(); // *************************************************************************** @@ -778,6 +782,38 @@ void setup() { DBG_OUTPUT_PORT.println("invalid input!"); } } + if(server.hasArg("pin")){ + uint8_t pin = server.arg("pin").toInt(); + DBG_OUTPUT_PORT.print("/pixels: GPIO used# "); + #if defined(USE_WS2812FX_DMA) or defined(USE_WS2812FX_UART1) or defined(USE_WS2812FX_UART2) + #ifdef USE_WS2812FX_DMA + DBG_OUTPUT_PORT.println("3"); + #endif + #ifdef USE_WS2812FX_UART1 + DBG_OUTPUT_PORT.println("2"); + #endif + #ifdef USE_WS2812FX_UART2 + DBG_OUTPUT_PORT.println("1"); + #endif + #else + if (pin == 16 or pin == 5 or pin == 4 or pin == 0 or pin == 2 or pin == 14 or pin == 12 or pin == 13 or pin == 15 or pin == 3 or pin == 1) { + initStrip(NULL, NULL, pin); + DBG_OUTPUT_PORT.println(pin); + } else { + DBG_OUTPUT_PORT.println("invalid input!"); + } + #endif + } + + DynamicJsonDocument jsonBuffer; + JsonObject json = jsonBuffer.to(); + json["pixel_pount"] = WS2812FXStripSettings.stripSize; + json["rgb_order"] = WS2812FXStripSettings.RGBOrder; + json["pin"] = WS2812FXStripSettings.pin; + String json_str; + serializeJson(json, json_str); + server.sendHeader("Access-Control-Allow-Origin", "*"); + server.send(200, "application/json", json_str); }); server.on("/off", []() { diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 74c0a38..cf6d8e7 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -1,6 +1,6 @@ -//#define USE_WS2812FX_DMA // Uses PIN is ignored & set to RX/GPIO3 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX -//#define USE_WS2812FX_UART1 // Uses PIN is ignored & set to D4/GPIO2 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX -//#define USE_WS2812FX_UART2 // Uses PIN is ignored & set to TX/GPIO1 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX +//#define USE_WS2812FX_DMA // PIN is ignored & set to RX/GPIO3 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX +//#define USE_WS2812FX_UART1 // PIN is ignored & set to D4/GPIO2 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX +//#define USE_WS2812FX_UART2 // PIN is ignored & set to TX/GPIO1 Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX // Neopixel #define PIN 14 // PIN (14 / D5) where neopixel / WS2811 strip is attached diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 479ccbc..96cf4b3 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -1422,3 +1422,87 @@ bool readStateFS() { return false; } #endif + +//Strip Config +struct +{ + uint16_t stripSize = NUMLEDS; + uint16_t RGBOrder = NEO_GRB; + #if defined(USE_WS2812FX_DMA) or defined(USE_WS2812FX_UART1) or defined(USE_WS2812FX_UART2) + #ifdef USE_WS2812FX_DMA + uint8_t pin = 3; + #endif + #ifdef USE_WS2812FX_UART1 + uint8_t pin = 2; + #endif + #ifdef USE_WS2812FX_UART2 + uint8_t pin = 1; + #endif + #else + uint8_t pin = PIN; + #endif +} WS2812FXStripSettings; + +Ticker saveWS2812FXStripSettings; + +bool readStripConfigFS(void) { + //read stripconfiguration from FS JSON + updateFS = true; + if (SPIFFS.exists("/neoconfig.json")) { + //file exists, reading and loading + DBG_OUTPUT_PORT.print("Reading neoconfig file... "); + File configFile = SPIFFS.open("/neoconfig.json", "r"); + if (configFile) { + DBG_OUTPUT_PORT.println("Opened!"); + size_t size = configFile.size(); + std::unique_ptr buf(new char[size]); + configFile.readBytes(buf.get(), size); + DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(4)+300); + DeserializationError error = deserializeJson(jsonBuffer, buf.get()); + DBG_OUTPUT_PORT.print("Config: "); + if (!error) { + DBG_OUTPUT_PORT.println(" Parsed!"); + JsonObject json = jsonBuffer.as(); + serializeJson(json, DBG_OUTPUT_PORT); + WS2812FXStripSettings.stripSize = json["pixel_pount"]; + WS2812FXStripSettings.RGBOrder = json["rgb_order"]; + WS2812FXStripSettings.pin = json["pin"]; + updateFS = false; + return true; + } else { + DBG_OUTPUT_PORT.print("Failed to load json config: "); + DBG_OUTPUT_PORT.println(error.c_str()); + } + } else { + DBG_OUTPUT_PORT.println("Failed to open /config.json"); + } + } else { + DBG_OUTPUT_PORT.println("Coudnt find config.json"); + } + //end read + updateFS = false; + return false; +} + +void writeStripConfigFS(void){ + updateFS = true; + //save the strip config to FS JSON + DBG_OUTPUT_PORT.print("Saving cfg: "); + DynamicJsonDocument jsonBuffer; + JsonObject json = jsonBuffer.to(); + json["pixel_pount"] = WS2812FXStripSettings.stripSize; + json["rgb_order"] = WS2812FXStripSettings.RGBOrder; + json["pin"] = WS2812FXStripSettings.pin; + + //SPIFFS.remove("/neoconfig.json") ? DBG_OUTPUT_PORT.println("removed file") : DBG_OUTPUT_PORT.println("failed removing file"); + File configFile = SPIFFS.open("/neoconfig.json", "w"); + if (!configFile) { + DBG_OUTPUT_PORT.println("Failed!"); + updateFS = false; + } + serializeJson(json, DBG_OUTPUT_PORT); + serializeJson(json, configFile); + configFile.close(); + updateFS = false; + //end save +} \ No newline at end of file diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index c2ec5d5..776b466 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -68,4 +68,5 @@ * - Pointers added for WS2812FX & NeoPixelBus * - new "REST API": /pixels?ct=xxx to change length of LED strip * - new "REST API": /pixels?rgbo=xxx to change RGB order + * - new "REST API": /pixels?pin=GPIO_NO to change PIN# (Allowed GPIO values: 16/5/4/0/2/14/12/13/15/3/1) */ diff --git a/README.md b/README.md index c25f95a..763bbef 100644 --- a/README.md +++ b/README.md @@ -61,17 +61,18 @@ I hope I didn't miss any sources and mentioned every author. In case I forgot so ## Todos - [ ] Support multiple strips and control them separately or together [Issue](https://github.com/toblum/McLighting/issues/118) -- [ ] Make number of pixels, MQTT and PIN configurable via front end [Issue](https://github.com/toblum/McLighting/issues/93) and [Issue](https://github.com/toblum/McLighting/issues/93) -- [ ] Bundle webpages instead of SPIFFS [Issue](https://github.com/toblum/McLighting/issues/93) - [ ] Remove old / wrong EEPROM settings completely [Issue] - [ ] Customer profile to define segments of (in)active areas on the strip [Issue](https://github.com/toblum/McLighting/issues/37) - [ ] Additional clients - [ ] If no wifi, at least enable button mode. - [ ] Also enable McLighting in Wifi AP mode. -- [ ] IR remote support [issue](https://github.com/toblum/McLightingUI/issues/3) - [ ] Multiple buttons/GPIO Inputs. [Issue](https://github.com/toblum/McLighting/issues/119) -- [ ] Music visualizer / Bring back ArtNet [Issue](https://github.com/toblum/McLighting/issues/111) -- [ ] Display version and parameters (Number of LEDs, definition settings, ..) in the web UI [Issue](https://github.com/toblum/McLighting/issues/150) +- [ ] IR remote support [issue](https://github.com/toblum/McLightingUI/issues/3) +- [ ] Make number of pixels, MQTT and PIN configurable via front end [Issue](https://github.com/toblum/McLighting/issues/93) and [Issue](https://github.com/toblum/McLighting/issues/272) +- [x] Make number of pixels, RGB Order and PIN configurable via REST API +- [x] Bundle webpages instead of SPIFFS [Issue](https://github.com/toblum/McLighting/issues/93) +- [x] Music visualizer / Bring back ArtNet [Issue](https://github.com/toblum/McLighting/issues/111) +- [x] Display version and parameters (Number of LEDs, definition settings, ..) in the web UI [Issue](https://github.com/toblum/McLighting/issues/150) - [x] MQTT support - [x] Save favourite effects? [Issue](https://github.com/toblum/McLighting/issues/35)(https://github.com/toblum/McLighting/issues/101) - [x] OTA update [Issue](https://github.com/toblum/McLighting/issues/92)