From a0680884f9e852e8bcbc986bc7a8c7c84eae0f16 Mon Sep 17 00:00:00 2001
From: Debashish Sahu <debashish.sahu@gmail.com>
Date: Mon, 5 Nov 2018 16:07:30 -0500
Subject: [PATCH] Retire NeoAnimationFX & Fix #248

* Retire NeoAnimationFX
* Use DMA or UART method along with WS2812FX instead
* fix #248
---
 Arduino/McLighting/McLighting.ino     | 60 +++++++++++++--------------
 Arduino/McLighting/definitions.h      | 13 +++---
 Arduino/McLighting/request_handlers.h |  3 +-
 Arduino/McLighting/version.h          |  2 +-
 Arduino/McLighting/version_info.ino   |  5 +++
 platformio.ini                        |  3 +-
 6 files changed, 43 insertions(+), 43 deletions(-)

diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino
index c16a918..8cc26b4 100644
--- a/Arduino/McLighting/McLighting.ino
+++ b/Arduino/McLighting/McLighting.ino
@@ -64,32 +64,6 @@ WebSocketsServer webSocket = WebSocketsServer(81);
 ESP8266HTTPUpdateServer httpUpdater;
 #endif
 
-#ifdef USE_NEOANIMATIONFX
-// ***************************************************************************
-// Load libraries / Instanciate NeoAnimationFX library
-// ***************************************************************************
-// https://github.com/debsahu/NeoAnimationFX
-#include <NeoAnimationFX.h>
-#define NEOMETHOD NeoPBBGRB800
-
-NEOMETHOD neoStrip(NUMLEDS);
-NeoAnimationFX<NEOMETHOD> strip(neoStrip);
-
-// Uses Pin RX / GPIO3 (Only pin that is supported, due to hardware limitations)
-// NEOMETHOD NeoPBBGRB800 uses GRB config 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
-// NEOMETHOD NeoPBBGRB400 uses GRB config 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
-// NEOMETHOD NeoPBBRGB800 uses RGB config 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
-// NEOMETHOD NeoPBBRGB400 uses RGB config 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
-
-// Uses Pin D4 / GPIO2 (Only pin that is supported, due to hardware limitations)
-// NEOMETHOD NeoPBBGRBU800 uses GRB config 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
-// NEOMETHOD NeoPBBGRBU400 uses GRB config 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
-// NEOMETHOD NeoPBBRGBU800 uses RGB config 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
-// NEOMETHOD NeoPBBRGBU400 uses RGB config 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
-
-#endif
-
-#ifdef USE_WS2812FX
 // ***************************************************************************
 // Load libraries / Instanciate WS2812FX library
 // ***************************************************************************
@@ -109,6 +83,23 @@ WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800);
 // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
 // and minimize distance between Arduino and first pixel.  Avoid connecting
 // on a live circuit...if you must, connect GND first.
+
+#ifdef USE_WS2812FX_DMA
+  #include <NeoPixelBus.h>
+  NeoEsp8266Dma800KbpsMethod dma = NeoEsp8266Dma800KbpsMethod(NUMLEDS, 3);  //800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
+  //NeoEsp8266Dma400KbpsMethod dma = NeoEsp8266Dma400KbpsMethod(NUMLEDS, 3);  //400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
+#endif
+#ifdef USE_WS2812FX_UART
+  #include <NeoPixelBus.h>
+  NeoEsp8266Uart800KbpsMethod dma = NeoEsp8266Uart800KbpsMethod(NUMLEDS, 3);
+#endif
+#if defined(USE_WS2812FX_DMA) or defined(USE_WS2812FX_UART)
+  void DMA_Show(void) {
+    if(dma.IsReadyToUpdate()) {
+      memcpy(dma.getPixels(), strip.getPixels(), dma.getPixelsSize());
+      dma.Update();
+    }
+  }
 #endif
 
 // ***************************************************************************
@@ -265,6 +256,10 @@ void setup() {
   // Setup: Neopixel
   // ***************************************************************************
   strip.init();
+  #if defined(USE_WS2812FX_DMA) or defined(USE_WS2812FX_UART)
+    dma.Initialize();
+    strip.setCustomShow(DMA_Show);
+  #endif
   strip.setBrightness(brightness);
   strip.setSpeed(convertSpeed(ws2812fx_speed));
   //strip.setMode(FX_MODE_RAINBOW_CYCLE);
@@ -511,12 +506,15 @@ void setup() {
     json["core_version"] = ESP.getCoreVersion();
     json["cpu_freq"] = ESP.getCpuFreqMHz();
     json["chip_id"] = ESP.getFlashChipId();
-    #ifndef USE_NEOANIMATIONFX
-    json["animation_lib"] = "WS2812FX";
-    json["pin"] = PIN;
+    #if defined(USE_WS2812FX_DMA)
+      json["animation_lib"] = "WS2812FX_DMA";
+      json["pin"] = 3;
+    #elif defined(USE_WS2812FX_UART)
+      json["animation_lib"] = "WS2812FX_UART";
+      json["pin"] = 2;
     #else
-    json["animation_lib"] = "NeoAnimationFX";
-    json["pin"] = "Ignored, check NEOMETHOD";
+      json["animation_lib"] = "WS2812FX";
+      json["pin"] = PIN;
     #endif
     json["number_leds"] = NUMLEDS;
     #ifdef ENABLE_BUTTON
diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h
index e652a47..4d682f5 100644
--- a/Arduino/McLighting/definitions.h
+++ b/Arduino/McLighting/definitions.h
@@ -1,5 +1,5 @@
-//#define USE_NEOANIMATIONFX  // Uses NeoAnimationFX, PIN is ignored & set to RX/GPIO3 or UART method: D4/GPIO2, see: https://github.com/debsahu/NeoAnimationFX
-#define USE_WS2812FX          // Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX
+//#define USE_WS2812FX_DMA      // Uses PIN is ignored & set to RX/GPIO3  Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX
+#define USE_WS2812FX_UART     // Uses PIN is ignored & set to D4/GPIO2  Uses WS2812FX, see: https://github.com/kitesurfer1404/WS2812FX
 
 // Neopixel
 #define PIN D1           // PIN (14 / D5) where neopixel / WS2811 strip is attached
@@ -13,16 +13,13 @@ const char HOSTNAME[] = "McLighting01";   // Friedly hostname
 //#define ENABLE_OTA         // If defined, enable Arduino OTA code.
 #define ENABLE_AMQTT         // If defined, enable Async MQTT code, see: https://github.com/marvinroger/async-mqtt-client
 //#define ENABLE_MQTT        // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API
-#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active
+#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT or ENABLE_AMQTT must be active
 #define ENABLE_BUTTON        // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control
 //#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth
 #define ENABLE_LEGACY_ANIMATIONS
 
-#if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX)
-#error "Cant have both NeoAnimationFX and WS2812FX enabled. Choose either one."
-#endif
-#if !defined(USE_NEOANIMATIONFX) and !defined(USE_WS2812FX)
-#error "Need to either use NeoAnimationFX and WS2812FX mode."
+#if defined(USE_WS2812FX_DMA) and defined(USE_WS2812FX_UART)
+#error "Cant have both DMA and UART method."
 #endif
 #if defined(ENABLE_MQTT) and defined(ENABLE_AMQTT)
 #error "Cant have both PubSubClient and AsyncMQTT enabled. Choose either one."
diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h
index 5a9f7ce..ee2b571 100644
--- a/Arduino/McLighting/request_handlers.h
+++ b/Arduino/McLighting/request_handlers.h
@@ -410,7 +410,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
       amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
     #endif
     #ifdef ENABLE_HOMEASSISTANT
-      // stateOn = true; //Commented our because # only is ment to change color and not the state
+      stateOn = true;
       if(!ha_send_data.active())  ha_send_data.once(5, tickerSendState);
     #endif
     #ifdef ENABLE_STATE_SAVE_SPIFFS
@@ -431,6 +431,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
     }
     DBG_OUTPUT_PORT.printf("Set speed to: [%u]\n", ws2812fx_speed);
     #ifdef ENABLE_HOMEASSISTANT
+      stateOn = true;
       if(!ha_send_data.active())  ha_send_data.once(5, tickerSendState);
     #endif
     #ifdef ENABLE_MQTT
diff --git a/Arduino/McLighting/version.h b/Arduino/McLighting/version.h
index 539d5b4..b672824 100644
--- a/Arduino/McLighting/version.h
+++ b/Arduino/McLighting/version.h
@@ -1 +1 @@
-#define SKETCH_VERSION "2.1.5" 
+#define SKETCH_VERSION "2.1.6" 
diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino
index 8795eba..0a72ea9 100644
--- a/Arduino/McLighting/version_info.ino
+++ b/Arduino/McLighting/version_info.ino
@@ -20,4 +20,9 @@
  * 
  * 2 Oct 2018 v 2.1.5
  * - Try fixing #224 HA brightness causes reboot 
+ * 
+ * 5 Nov 2018 v 2.1.6
+ * - Retire NeoAnimationFX
+ * - Use DMA or UART method along with WS2812FX instead
+ * - fix #248 
  */
diff --git a/platformio.ini b/platformio.ini
index 981737b..98ef73a 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -46,8 +46,7 @@ upload_resetmethod = ${common.upload_resetmethod}
 lib_deps =
   WiFiManager@0.14
   AsyncMqttClient
-  https://github.com/bblanchon/ArduinoJson.git#v6.4.0-beta
+  https://github.com/bblanchon/ArduinoJson.git#v6.5.0-beta
   WS2812FX
-  https://github.com/debsahu/NeoAnimationFX
   NeoPixelBus
   WebSockets