From b779fd0fac07e95242af8ae944d9f38fb964873a Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Wed, 23 Oct 2019 23:53:45 +0300 Subject: [PATCH] Adafruit NeoPixel library used instead fastLed (allow define [pin#,num,LEDorder] in configuration. Use FASTLED directive to revert FASTLed back --- lighthub/item.cpp | 15 +++++ lighthub/lighthub.ino.cpp | 17 +++++ lighthub/main.h | 6 +- lighthub/modules/out_spiled.cpp | 112 +++++++++++++++++++++++++++----- lighthub/modules/out_spiled.h | 14 +++- lighthub/options.h | 35 +++++----- platformio.ini | 12 ++++ 7 files changed, 174 insertions(+), 37 deletions(-) create mode 100644 lighthub/lighthub.ino.cpp diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 69e2d08..efde561 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -26,8 +26,12 @@ e-mail anklimov@gmail.com #ifdef _dmxout #include "dmx.h" +#ifdef ADAFRUIT_LED +#include +#else #include "FastLED.h" #endif +#endif #ifndef MODBUS_DISABLE #include @@ -364,8 +368,10 @@ debugSerial<0) { + #ifdef ADAFRUIT_LED + Adafruit_NeoPixel strip(0, 0, 0); + uint32_t rgb = strip.ColorHSV(map(Par[0], 0, 365, 0, 655535), rgbSaturation, rgbValue); + DmxWrite(iaddr, (rgb >> 16)& 0xFF); + DmxWrite(iaddr + 1, (rgb >> 8) & 0xFF); + DmxWrite(iaddr + 2, rgb & 0xFF); + #else CRGB rgb = CHSV(map(Par[0], 0, 365, 0, 255), rgbSaturation, rgbValue); DmxWrite(iaddr, rgb.r); DmxWrite(iaddr + 1, rgb.g); DmxWrite(iaddr + 2, rgb.b); + #endif break; } case CH_WHITE: diff --git a/lighthub/lighthub.ino.cpp b/lighthub/lighthub.ino.cpp new file mode 100644 index 0000000..3217e17 --- /dev/null +++ b/lighthub/lighthub.ino.cpp @@ -0,0 +1,17 @@ +# 1 "/var/folders/kt/8psth65x03v6tw_phdhbj12r0000gn/T/tmpwztWO8" +#include +# 1 "/Users/andrey/Documents/Arduino/lighthub/lighthub/lighthub.ino" +#include "main.h" +void setup(); +void loop(); +#line 2 "/Users/andrey/Documents/Arduino/lighthub/lighthub/lighthub.ino" +void setup(){ + + setup_main(); + + +} +void loop(){ + + loop_main(); +} \ No newline at end of file diff --git a/lighthub/main.h b/lighthub/main.h index 354eb20..88ce2c4 100644 --- a/lighthub/main.h +++ b/lighthub/main.h @@ -121,9 +121,9 @@ #include #endif -#ifndef DMX_DISABLE -#include "FastLED.h" -#endif +//#ifndef DMX_DISABLE +//#include "FastLED.h" +//#endif #ifdef _owire #include "owTerm.h" diff --git a/lighthub/modules/out_spiled.cpp b/lighthub/modules/out_spiled.cpp index 09423cf..386e69d 100644 --- a/lighthub/modules/out_spiled.cpp +++ b/lighthub/modules/out_spiled.cpp @@ -4,23 +4,63 @@ #include "Arduino.h" #include "options.h" #include "Streaming.h" -#include "FastLED.h" + #include "item.h" -#define NUM_LEDS 43 -#define DATA_PIN 4 +#ifdef ADAFRUIT_LED -//static CRGB leds[NUM_LEDS]; +#include +#ifdef __AVR__ + #include // Required for 16 MHz Adafruit Trinket +#endif +Adafruit_NeoPixel *leds = NULL; + +#else +#include "FastLED.h" static CRGB *leds = NULL; + +#endif + +#define NUM_LEDS 43 + static int driverStatus = CST_UNKNOWN; +void out_SPILed::getConfig() +{ + pin=item->getArg(0); + if(pin<=0) pin=3; + + numLeds=item->getArg(1); + if (numLeds<=0) numLeds=NUM_LEDS; + + ledsType=item->getArg(2); + #ifdef ADAFRUIT_LED + if (ledsType<=0) ledsType= NEO_BRG + NEO_KHZ800; + #endif +} + + int out_SPILed::Setup() { +getConfig(); Serial.println("SPI-LED Init"); -leds = new CRGB [NUM_LEDS]; //Allocate dynamic memory for LED objects +if (!leds) +{ +#ifdef ADAFRUIT_LED +#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) + clock_prescale_set(clock_div_1); +#endif +leds = new Adafruit_NeoPixel(numLeds, pin, ledsType); +leds->begin(); +#else +leds = new CRGB [numLeds]; //Allocate dynamic memory for LED objects +//template< CONTROLLER = TM1809, uint8_t DATA_PIN = pin, EOrder ORDER = BRG > +//FastLED.addLeds(leds, numLeds); +FastLED.addLeds(leds, numLeds); +#endif +} -FastLED.addLeds(leds, NUM_LEDS); driverStatus = CST_INITIALIZED; return 1; } @@ -29,9 +69,15 @@ int out_SPILed::Stop() { Serial.println("SPI-LED De-Init"); //FastLED.addLeds(leds, NUM_LEDS); +#ifdef ADAFRUIT_LED +leds->clear(); +delete leds; +#else FastLED.clear(true); -driverStatus = CST_UNKNOWN; delete [] leds; +#endif +driverStatus = CST_UNKNOWN; + return 1; } @@ -55,52 +101,88 @@ return 1; int out_SPILed::getChanType() { - return CH_RGB; + if ((ledsType>>4) == (ledsType>>6)) + return CH_RGB; + else + return CH_RGBW; } int out_SPILed::PixelCtrl(CHstore *st, short cmd, int from, int to, bool show, bool rgb) { //debugSerial<s, 0, 100, 0, 255); int Value = map(st->v, 0, 100, 0, 255); + +#ifdef ADAFRUIT_LED + uint16_t Hue = map(st->h, 0, 365, 0, 655535); + pixel = leds->ColorHSV(Hue, Saturation, Value); +#else int Hue = map(st->h, 0, 365, 0, 255); pixel = CHSV(Hue, Saturation, Value); +#endif + debugSerial<h<s<v<NUM_LEDS-1) to=NUM_LEDS-1; + if (to>numLeds-1) to=numLeds-1; if (from<0) from=0; for (int i=from;i<=to;i++) { switch (cmd) { case CMD_ON: + + #ifdef ADAFRUIT_LED + if (!leds->getPixelColor(i)) leds->setPixelColor(i, leds->Color(255, 255, 255)); + #else if (!leds[i].r && !leds[i].g &&!leds[i].b) leds[i] = CRGB::White; - // FastLED.show(); + #endif + + break; case CMD_OFF: - //pixel = leds[i]; + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, leds->Color(0, 0, 0)); + #else leds[i] = CRGB::Black; - //FastLED.show(); - //leds[i] = pixel; + #endif + break; default: if (rgb) { + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, leds->Color(st->r,st->g,st->b)); + #else leds[i] = CRGB(st->r,st->g,st->b); + #endif } else { + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, pixel); + #else leds[i] = pixel; + #endif + } } } //for if (show) { + #ifdef ADAFRUIT_LED + leds->show(); + #else FastLED.show(); + #endif + debugSerial<0); CHstore st; if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it -int from=0, to=NUM_LEDS-1; //All LEDs on the strip by default +int from=0, to=numLeds-1; //All LEDs on the strip by default // retrive LEDs range from suffix if (subItem) { //Just single LED to control todo - range diff --git a/lighthub/modules/out_spiled.h b/lighthub/modules/out_spiled.h index 57f7ff3..b05292e 100644 --- a/lighthub/modules/out_spiled.h +++ b/lighthub/modules/out_spiled.h @@ -1,13 +1,20 @@ #pragma once +#include "options.h" #ifndef SPILED_DISABLE #include #include +#ifdef ADAFRUIT_LED + +#include +#else +#include "FastLED.h" +#endif class out_SPILed : public abstractOut { public: - out_SPILed(Item * _item):abstractOut(_item){}; + out_SPILed(Item * _item):abstractOut(_item){getConfig();}; int Setup() override; int Poll() override; int Stop() override; @@ -17,8 +24,9 @@ public: int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; int PixelCtrl(CHstore *st, short cmd, int from =0 , int to = 1024, bool show = 1, bool rgb = 0); int numLeds; - int pin; + int8_t pin; + int ledsType; protected: - + void getConfig(); }; #endif diff --git a/lighthub/options.h b/lighthub/options.h index 7765091..1e9ec7c 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -1,13 +1,29 @@ // Configuration of drivers enabled -#ifndef PIO_SRC_REV -#define PIO_SRC_REV v0.999 + +#ifndef FASTLED +#define ADAFRUIT_LED +#endif +// ADAFRUIT library allow to dynamically configure SPI LED Strip Parameters + +// If not defined ADAFRUIT_LED - FastLED library will be used instead +// And strip type, pin, order must defined on compilation time +#ifndef CONTROLLER +#define CONTROLLER TM1809 +#endif + +#ifndef DATA_PIN +#define DATA_PIN 4 +#endif + +#ifndef ORDER +#define ORDER BRG #endif #define TXEnablePin 13 #define ESP_EEPROM_SIZE 2048 #ifndef AVR_DMXOUT_PIN -#define AVR_DMXOUT_PIN 3 +#define AVR_DMXOUT_PIN 18 #endif #define T_ATTEMPTS 200 @@ -78,19 +94,6 @@ #ifndef HOMETOPIC #define HOMETOPIC "myhome" #endif -/* -#ifndef OUTTOPIC -#define OUTTOPIC "/myhome/s_out/" -#endif - -#ifndef CMDTOPIC -#define CMDTOPIC "/myhome/in/command/" -#endif - -#ifndef INTOPIC -#define INTOPIC "/myhome/in/" -#endif -*/ //Default output topic #ifndef OUTTOPIC diff --git a/platformio.ini b/platformio.ini index 81a366f..19150ad 100644 --- a/platformio.ini +++ b/platformio.ini @@ -101,6 +101,7 @@ lib_deps = DHT sensor library Streaming https://github.com/anklimov/NRFFlashStorage + Adafruit NeoPixel ; https://github.com/livello/PrintEx#is-select-redecl @@ -150,6 +151,7 @@ lib_deps = ;SparkFun CCS811 Arduino Library https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library.git M5Stack + Adafruit NeoPixel [env:esp32-wifi] platform = espressif32 @@ -197,6 +199,7 @@ lib_deps = ClosedCube HDC1080 ;SparkFun CCS811 Arduino Library https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library.git + Adafruit NeoPixel [env:due-5100] platform = atmelsam framework = arduino @@ -238,6 +241,7 @@ lib_deps = Streaming ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:mega2560slim-5100] platform = atmelavr @@ -277,6 +281,7 @@ lib_deps = Streaming ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:mega2560-5500] platform = atmelavr @@ -315,6 +320,7 @@ lib_deps = Streaming ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:esp8266-wifi] @@ -361,6 +367,7 @@ lib_deps = ESP_EEPROM ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:mega2560-5100] platform = atmelavr @@ -401,6 +408,7 @@ lib_deps = Streaming ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:due-5500] platform = atmelsam @@ -446,6 +454,7 @@ lib_deps = https://github.com/livello/PrintEx#is-select-redecl ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:lighthub21] platform = atmelsam @@ -490,6 +499,7 @@ lib_deps = https://github.com/livello/PrintEx#is-select-redecl ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:controllino] @@ -531,6 +541,7 @@ lib_deps = https://github.com/livello/PrintEx#is-select-redecl ClosedCube HDC1080 SparkFun CCS811 Arduino Library + Adafruit NeoPixel [env:stm32-enc2860] platform = ststm32 @@ -576,3 +587,4 @@ lib_deps = Streaming UIPEthernet https://github.com/anklimov/NRFFlashStorage + Adafruit NeoPixel