2018-04-13 00:28:29 +02:00
|
|
|
//this code is a modified version of https://github.com/Makuna/NeoPixelBus/issues/103
|
2018-09-04 15:51:38 +02:00
|
|
|
#ifndef NpbWrapper_h
|
|
|
|
#define NpbWrapper_h
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-18 00:31:45 +01:00
|
|
|
//#define WORKAROUND_ESP32_BITBANG
|
2018-04-13 00:28:29 +02:00
|
|
|
//see https://github.com/Aircoookie/WLED/issues/2 for flicker free ESP32 support
|
|
|
|
|
2018-11-18 00:31:45 +01:00
|
|
|
//PIN CONFIGURATION
|
|
|
|
#define LEDPIN 2 //strip pin. Any for ESP32, gpio2 is recommended for ESP8266
|
|
|
|
#define BTNPIN 0 //button pin. Needs to have pullup (gpio0 recommended)
|
|
|
|
#define IR_PIN 4 //infrared pin.
|
|
|
|
#define AUXPIN 15 //unused auxiliary output pin
|
|
|
|
|
2018-09-04 15:51:38 +02:00
|
|
|
|
2018-04-13 00:28:29 +02:00
|
|
|
//uncomment this if red and green are swapped
|
|
|
|
//#define SWAPRG
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-04-13 00:28:29 +02:00
|
|
|
//automatically uses the right driver method for each platform
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2018-11-09 17:00:36 +01:00
|
|
|
#ifdef WORKAROUND_ESP32_BITBANG
|
|
|
|
#define PIXELMETHOD NeoEsp32BitBangWs2813Method
|
|
|
|
#pragma message "Software BitBang is used because of your NeoPixelBus version. Look in NpbWrapper.h for instructions on how to mitigate flickering."
|
|
|
|
#else
|
|
|
|
#define PIXELMETHOD NeoEsp32RmtWS2813_V3Method
|
|
|
|
#endif
|
2018-09-04 15:51:38 +02:00
|
|
|
#else //esp8266
|
2018-11-09 17:00:36 +01:00
|
|
|
//autoselect the right method depending on strip pin
|
|
|
|
#if LEDPIN == 2
|
|
|
|
#define PIXELMETHOD NeoEsp8266Uart800KbpsMethod
|
|
|
|
#elif LEDPIN == 3
|
|
|
|
#define PIXELMETHOD NeoEsp8266Dma800KbpsMethod
|
|
|
|
#else
|
|
|
|
#define PIXELMETHOD NeoEsp8266BitBang800KbpsMethod
|
|
|
|
#pragma message "Software BitBang will be used because of your selected LED pin. This may cause flicker. Use GPIO 2 or 3 for best results."
|
|
|
|
#endif
|
2018-04-13 00:28:29 +02:00
|
|
|
#endif
|
2018-09-04 15:51:38 +02:00
|
|
|
|
2018-04-13 00:28:29 +02:00
|
|
|
//handle swapping Red and Green automatically
|
|
|
|
#ifdef SWAPRG
|
2018-11-09 17:00:36 +01:00
|
|
|
#define PIXELFEATURE3 NeoRgbFeature
|
|
|
|
#define PIXELFEATURE4 NeoRgbwFeature
|
2018-04-13 00:28:29 +02:00
|
|
|
#else
|
2018-11-09 17:00:36 +01:00
|
|
|
#define PIXELFEATURE3 NeoGrbFeature
|
|
|
|
#define PIXELFEATURE4 NeoGrbwFeature
|
2018-04-13 00:28:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <NeoPixelBrightnessBus.h>
|
|
|
|
|
|
|
|
enum NeoPixelType
|
|
|
|
{
|
|
|
|
NeoPixelType_None = 0,
|
|
|
|
NeoPixelType_Grb = 1,
|
|
|
|
NeoPixelType_Grbw = 2,
|
|
|
|
NeoPixelType_End = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
class NeoPixelWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NeoPixelWrapper() :
|
2018-11-09 17:00:36 +01:00
|
|
|
// initialize each member to null
|
|
|
|
_pGrb(NULL),
|
|
|
|
_pGrbw(NULL),
|
|
|
|
_type(NeoPixelType_None)
|
2018-04-13 00:28:29 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~NeoPixelWrapper()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:51:38 +02:00
|
|
|
void Begin(NeoPixelType type, uint16_t countPixels)
|
2018-04-13 00:28:29 +02:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
_type = type;
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
switch (_type)
|
|
|
|
{
|
2018-04-13 00:28:29 +02:00
|
|
|
case NeoPixelType_Grb:
|
2018-09-04 15:51:38 +02:00
|
|
|
_pGrb = new NeoPixelBrightnessBus<PIXELFEATURE3,PIXELMETHOD>(countPixels, LEDPIN);
|
2018-04-13 00:28:29 +02:00
|
|
|
_pGrb->Begin();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NeoPixelType_Grbw:
|
2018-09-04 15:51:38 +02:00
|
|
|
_pGrbw = new NeoPixelBrightnessBus<PIXELFEATURE4,PIXELMETHOD>(countPixels, LEDPIN);
|
2018-04-13 00:28:29 +02:00
|
|
|
_pGrbw->Begin();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Show()
|
|
|
|
{
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2018-11-09 17:00:36 +01:00
|
|
|
#ifdef WORKAROUND_ESP32_BITBANG
|
|
|
|
delay(1);
|
|
|
|
portDISABLE_INTERRUPTS(); //this is a workaround to prevent flickering (see https://github.com/adafruit/Adafruit_NeoPixel/issues/139)
|
|
|
|
#endif
|
2018-04-13 00:28:29 +02:00
|
|
|
#endif
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
switch (_type)
|
|
|
|
{
|
2018-04-13 00:28:29 +02:00
|
|
|
case NeoPixelType_Grb: _pGrb->Show(); break;
|
|
|
|
case NeoPixelType_Grbw: _pGrbw->Show(); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2018-11-09 17:00:36 +01:00
|
|
|
#ifdef WORKAROUND_ESP32_BITBANG
|
|
|
|
portENABLE_INTERRUPTS();
|
|
|
|
#endif
|
2018-04-13 00:28:29 +02:00
|
|
|
#endif
|
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-04-13 00:28:29 +02:00
|
|
|
bool CanShow() const
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: _pGrb->CanShow(); break;
|
|
|
|
case NeoPixelType_Grbw: _pGrbw->CanShow(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
void SetPixelColor(uint16_t indexPixel, RgbColor color)
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: _pGrb->SetPixelColor(indexPixel, color); break;
|
|
|
|
case NeoPixelType_Grbw:_pGrbw->SetPixelColor(indexPixel, color); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
void SetPixelColor(uint16_t indexPixel, RgbwColor color)
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: _pGrb->SetPixelColor(indexPixel, RgbColor(color.R,color.G,color.B)); break;
|
|
|
|
case NeoPixelType_Grbw: _pGrbw->SetPixelColor(indexPixel, color); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
void SetBrightness(byte b)
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: _pGrb->SetBrightness(b); break;
|
|
|
|
case NeoPixelType_Grbw:_pGrbw->SetBrightness(b); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
RgbColor GetPixelColor(uint16_t indexPixel) const
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: return _pGrb->GetPixelColor(indexPixel); break;
|
|
|
|
case NeoPixelType_Grbw: /*doesn't support it so we don't return it*/ break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
// NOTE: Due to feature differences, some support RGBW but the method name
|
|
|
|
// here needs to be unique, thus GetPixeColorRgbw
|
|
|
|
RgbwColor GetPixelColorRgbw(uint16_t indexPixel) const
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: return _pGrb->GetPixelColor(indexPixel); break;
|
|
|
|
case NeoPixelType_Grbw: return _pGrbw->GetPixelColor(indexPixel); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
void ClearTo(RgbColor color)
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: _pGrb->ClearTo(color); break;
|
|
|
|
case NeoPixelType_Grbw:_pGrbw->ClearTo(color); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
void ClearTo(RgbwColor color)
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: break;
|
|
|
|
case NeoPixelType_Grbw:_pGrbw->ClearTo(color); break;
|
2018-04-13 00:28:29 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2018-04-13 00:28:29 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
NeoPixelType _type;
|
|
|
|
|
|
|
|
// have a member for every possible type
|
|
|
|
NeoPixelBrightnessBus<PIXELFEATURE3,PIXELMETHOD>* _pGrb;
|
|
|
|
NeoPixelBrightnessBus<PIXELFEATURE4,PIXELMETHOD>* _pGrbw;
|
|
|
|
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
switch (_type) {
|
|
|
|
case NeoPixelType_Grb: delete _pGrb ; _pGrb = NULL; break;
|
|
|
|
case NeoPixelType_Grbw: delete _pGrbw; _pGrbw = NULL; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-09-04 15:51:38 +02:00
|
|
|
#endif
|