2020-12-01 00:33:47 +01:00
|
|
|
#ifndef BusManager_h
|
|
|
|
#define BusManager_h
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class for addressing various light types
|
|
|
|
*/
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
#include "const.h"
|
2021-01-16 00:50:43 +01:00
|
|
|
#include "pin_manager.h"
|
2020-12-07 01:39:42 +01:00
|
|
|
#include "bus_wrapper.h"
|
2021-01-17 00:34:34 +01:00
|
|
|
#include <Arduino.h>
|
2020-12-01 00:33:47 +01:00
|
|
|
|
2021-01-30 20:51:36 +01:00
|
|
|
//temporary struct for passing bus configuration to bus
|
|
|
|
struct BusConfig {
|
|
|
|
uint8_t type = TYPE_WS2812_RGB;
|
|
|
|
uint16_t count = 1;
|
|
|
|
uint16_t start = 0;
|
|
|
|
uint8_t colorOrder = COL_ORDER_GRB;
|
|
|
|
bool reversed = false;
|
|
|
|
uint8_t pins[5] = {LEDPIN, 255, 255, 255, 255};
|
|
|
|
BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false) {
|
|
|
|
type = busType; count = len; start = pstart; colorOrder = pcolorOrder; reversed = rev;
|
|
|
|
uint8_t nPins = 1;
|
|
|
|
if (type > 47) nPins = 2;
|
|
|
|
else if (type > 41 && type < 46) nPins = NUM_PWM_PINS(type);
|
|
|
|
for (uint8_t i = 0; i < nPins; i++) pins[i] = ppins[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
//parent class of BusDigital and BusPwm
|
|
|
|
class Bus {
|
|
|
|
public:
|
2021-01-16 19:53:08 +01:00
|
|
|
Bus(uint8_t type, uint16_t start) {
|
2020-12-01 00:33:47 +01:00
|
|
|
_type = type;
|
2021-01-16 19:53:08 +01:00
|
|
|
_start = start;
|
2020-12-01 00:33:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual void show() {}
|
2021-01-15 15:43:11 +01:00
|
|
|
virtual bool canShow() { return true; }
|
2020-12-01 00:33:47 +01:00
|
|
|
|
|
|
|
virtual void setPixelColor(uint16_t pix, uint32_t c) {};
|
|
|
|
|
2021-01-16 21:40:04 +01:00
|
|
|
virtual void setBrightness(uint8_t b) {};
|
2020-12-01 00:33:47 +01:00
|
|
|
|
|
|
|
virtual uint32_t getPixelColor(uint16_t pix) { return 0; };
|
|
|
|
|
2021-01-16 19:53:08 +01:00
|
|
|
virtual void cleanup() {};
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
virtual ~Bus() { //throw the bus under the bus
|
2021-01-18 20:51:32 +01:00
|
|
|
//Serial.println("Destructor!");
|
2020-12-01 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:03 +01:00
|
|
|
virtual uint8_t getPins(uint8_t* pinArray) { return 0; }
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
uint16_t getStart() {
|
|
|
|
return _start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setStart(uint16_t start) {
|
|
|
|
_start = start;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
virtual uint16_t getLength() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
virtual void setColorOrder() {}
|
|
|
|
|
2021-01-16 19:53:08 +01:00
|
|
|
virtual uint8_t getColorOrder() {
|
|
|
|
return COL_ORDER_RGB;
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
uint8_t getType() {
|
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2020-12-07 01:39:42 +01:00
|
|
|
bool isOk() {
|
|
|
|
return _valid;
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:03 +01:00
|
|
|
bool reversed = false;
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
protected:
|
|
|
|
uint8_t _type = TYPE_NONE;
|
2020-12-07 01:39:42 +01:00
|
|
|
uint8_t _bri = 255;
|
2021-01-16 00:50:43 +01:00
|
|
|
uint16_t _start = 0;
|
2020-12-07 01:39:42 +01:00
|
|
|
bool _valid = false;
|
2020-12-01 00:33:47 +01:00
|
|
|
};
|
|
|
|
|
2020-12-07 01:39:42 +01:00
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
class BusDigital : public Bus {
|
|
|
|
public:
|
2021-01-30 20:51:36 +01:00
|
|
|
BusDigital(BusConfig &bc, uint8_t nr) : Bus(bc.type, bc.start) {
|
|
|
|
if (!IS_DIGITAL(bc.type) || !bc.count) return;
|
|
|
|
_pins[0] = bc.pins[0];
|
2021-01-16 19:53:08 +01:00
|
|
|
if (!pinManager.allocatePin(_pins[0])) return;
|
2021-01-30 20:51:36 +01:00
|
|
|
if (IS_2PIN(bc.type)) {
|
|
|
|
_pins[1] = bc.pins[1];
|
2021-01-16 19:53:08 +01:00
|
|
|
if (!pinManager.allocatePin(_pins[1])) {
|
|
|
|
cleanup(); return;
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 20:51:36 +01:00
|
|
|
_len = bc.count;
|
|
|
|
reversed = bc.reversed;
|
|
|
|
_iType = PolyBus::getI(bc.type, _pins, nr);
|
2020-12-07 01:39:42 +01:00
|
|
|
if (_iType == I_NONE) return;
|
2021-01-16 19:53:08 +01:00
|
|
|
_busPtr = PolyBus::create(_iType, _pins, _len);
|
2021-01-16 00:50:43 +01:00
|
|
|
_valid = (_busPtr != nullptr);
|
2021-01-30 20:51:36 +01:00
|
|
|
_colorOrder = bc.colorOrder;
|
2021-01-18 20:51:32 +01:00
|
|
|
//Serial.printf("Successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n",nr, len, type, pins[0],pins[1],_iType);
|
2020-12-01 00:33:47 +01:00
|
|
|
};
|
|
|
|
|
2020-12-07 01:39:42 +01:00
|
|
|
void show() {
|
|
|
|
PolyBus::show(_busPtr, _iType);
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
bool canShow() {
|
|
|
|
return PolyBus::canShow(_busPtr, _iType);
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:50:43 +01:00
|
|
|
void setBrightness(uint8_t b) {
|
2021-01-19 17:22:37 +01:00
|
|
|
//Fix for turning off onboard LED breaking bus
|
2021-01-17 00:34:34 +01:00
|
|
|
#ifdef LED_BUILTIN
|
2021-01-19 16:51:03 +01:00
|
|
|
if (_bri == 0 && b > 0) {
|
2021-02-17 18:03:57 +01:00
|
|
|
if (_pins[0] == LED_BUILTIN || _pins[1] == LED_BUILTIN) PolyBus::begin(_busPtr, _iType, _pins);
|
2021-01-19 16:51:03 +01:00
|
|
|
}
|
2021-01-17 00:34:34 +01:00
|
|
|
#endif
|
2021-01-16 21:40:04 +01:00
|
|
|
_bri = b;
|
2021-01-16 00:50:43 +01:00
|
|
|
PolyBus::setBrightness(_busPtr, _iType, b);
|
|
|
|
}
|
|
|
|
|
2020-12-07 01:39:42 +01:00
|
|
|
void setPixelColor(uint16_t pix, uint32_t c) {
|
2021-01-19 16:51:03 +01:00
|
|
|
if (reversed) pix = _len - pix -1;
|
2021-01-16 17:11:23 +01:00
|
|
|
PolyBus::setPixelColor(_busPtr, _iType, pix, c, _colorOrder);
|
2021-01-15 15:43:11 +01:00
|
|
|
}
|
2020-12-07 01:39:42 +01:00
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
uint32_t getPixelColor(uint16_t pix) {
|
2021-01-19 16:51:03 +01:00
|
|
|
if (reversed) pix = _len - pix -1;
|
2021-01-16 17:11:23 +01:00
|
|
|
return PolyBus::getPixelColor(_busPtr, _iType, pix, _colorOrder);
|
2020-12-07 01:39:42 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
uint8_t getColorOrder() {
|
|
|
|
return _colorOrder;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
uint16_t getLength() {
|
|
|
|
return _len;
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:03 +01:00
|
|
|
uint8_t getPins(uint8_t* pinArray) {
|
|
|
|
uint8_t numPins = IS_2PIN(_type) ? 2 : 1;
|
|
|
|
for (uint8_t i = 0; i < numPins; i++) pinArray[i] = _pins[i];
|
|
|
|
return numPins;
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
void setColorOrder(uint8_t colorOrder) {
|
|
|
|
if (colorOrder > 5) return;
|
|
|
|
_colorOrder = colorOrder;
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:22:37 +01:00
|
|
|
void reinit() {
|
2021-02-17 18:03:57 +01:00
|
|
|
PolyBus::begin(_busPtr, _iType, _pins);
|
2021-01-19 17:22:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 19:53:08 +01:00
|
|
|
void cleanup() {
|
2021-01-18 20:51:32 +01:00
|
|
|
//Serial.println("Digital Cleanup");
|
2021-01-16 19:53:08 +01:00
|
|
|
PolyBus::cleanup(_busPtr, _iType);
|
|
|
|
_iType = I_NONE;
|
|
|
|
_valid = false;
|
|
|
|
_busPtr = nullptr;
|
|
|
|
pinManager.deallocatePin(_pins[0]);
|
|
|
|
pinManager.deallocatePin(_pins[1]);
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:51:32 +01:00
|
|
|
~BusDigital() {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
private:
|
|
|
|
uint8_t _colorOrder = COL_ORDER_GRB;
|
2020-12-07 01:39:42 +01:00
|
|
|
uint8_t _pins[2] = {255, 255};
|
|
|
|
uint8_t _iType = I_NONE;
|
|
|
|
uint16_t _len = 0;
|
|
|
|
void * _busPtr = nullptr;
|
2020-12-01 00:33:47 +01:00
|
|
|
};
|
|
|
|
|
2020-12-07 01:39:42 +01:00
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
class BusPwm : public Bus {
|
|
|
|
public:
|
2021-01-30 20:51:36 +01:00
|
|
|
BusPwm(BusConfig &bc) : Bus(bc.type, bc.start) {
|
|
|
|
if (!IS_PWM(bc.type)) return;
|
|
|
|
uint8_t numPins = NUM_PWM_PINS(bc.type);
|
2020-12-07 01:39:42 +01:00
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
analogWriteRange(255); //same range as one RGB channel
|
|
|
|
analogWriteFreq(WLED_PWM_FREQ_ESP8266);
|
|
|
|
#else
|
|
|
|
_ledcStart = pinManager.allocateLedc(numPins);
|
|
|
|
if (_ledcStart == 255) { //no more free LEDC channels
|
|
|
|
deallocatePins(); return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
for (uint8_t i = 0; i < numPins; i++) {
|
2021-01-30 20:51:36 +01:00
|
|
|
_pins[i] = bc.pins[i];
|
2020-12-07 01:39:42 +01:00
|
|
|
if (!pinManager.allocatePin(_pins[i])) {
|
|
|
|
deallocatePins(); return;
|
|
|
|
}
|
|
|
|
#ifdef ESP8266
|
|
|
|
pinMode(_pins[i], OUTPUT);
|
|
|
|
#else
|
|
|
|
ledcSetup(_ledcStart + i, WLED_PWM_FREQ_ESP32, 8);
|
|
|
|
ledcAttachPin(_pins[i], _ledcStart + i);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
_valid = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
void setPixelColor(uint16_t pix, uint32_t c) {
|
|
|
|
if (pix != 0 || !_valid) return; //only react to first pixel
|
|
|
|
uint8_t r = c >> 16;
|
|
|
|
uint8_t g = c >> 8;
|
|
|
|
uint8_t b = c ;
|
|
|
|
uint8_t w = c >> 24;
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
case TYPE_ANALOG_1CH: //one channel (white), use highest RGBW value
|
|
|
|
_data[0] = max(r, max(g, max(b, w))); break;
|
|
|
|
|
|
|
|
case TYPE_ANALOG_2CH: //warm white + cold white, we'll need some nice handling here, for now just R+G channels
|
|
|
|
case TYPE_ANALOG_3CH: //standard dumb RGB
|
|
|
|
case TYPE_ANALOG_4CH: //RGBW
|
|
|
|
case TYPE_ANALOG_5CH: //we'll want the white handling from 2CH here + RGB
|
|
|
|
_data[0] = r; _data[1] = g; _data[2] = b; _data[3] = w; _data[4] = 0; break;
|
|
|
|
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//does no index check
|
|
|
|
uint32_t getPixelColor(uint16_t pix) {
|
|
|
|
return ((_data[3] << 24) | (_data[0] << 16) | (_data[1] << 8) | (_data[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void show() {
|
|
|
|
uint8_t numPins = NUM_PWM_PINS(_type);
|
|
|
|
for (uint8_t i = 0; i < numPins; i++) {
|
|
|
|
uint8_t scaled = (_data[i] * _bri) / 255;
|
|
|
|
#ifdef ESP8266
|
|
|
|
analogWrite(_pins[i], scaled);
|
|
|
|
#else
|
|
|
|
ledcWrite(_ledcStart + i, scaled);
|
|
|
|
#endif
|
2020-12-01 00:33:47 +01:00
|
|
|
}
|
2020-12-07 01:39:42 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 21:40:04 +01:00
|
|
|
void setBrightness(uint8_t b) {
|
|
|
|
_bri = b;
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:03 +01:00
|
|
|
uint8_t getPins(uint8_t* pinArray) {
|
|
|
|
uint8_t numPins = NUM_PWM_PINS(_type);
|
|
|
|
for (uint8_t i = 0; i < numPins; i++) pinArray[i] = _pins[i];
|
|
|
|
return numPins;
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:53:08 +01:00
|
|
|
void cleanup() {
|
2020-12-07 01:39:42 +01:00
|
|
|
deallocatePins();
|
2021-01-16 19:53:08 +01:00
|
|
|
}
|
2020-12-07 01:39:42 +01:00
|
|
|
|
2020-12-01 00:33:47 +01:00
|
|
|
private:
|
2021-01-16 19:53:08 +01:00
|
|
|
uint8_t _pins[5] = {255, 255, 255, 255, 255};
|
2020-12-07 01:39:42 +01:00
|
|
|
uint8_t _data[5] = {255, 255, 255, 255, 255};
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
uint8_t _ledcStart = 255;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void deallocatePins() {
|
|
|
|
uint8_t numPins = NUM_PWM_PINS(_type);
|
|
|
|
for (uint8_t i = 0; i < numPins; i++) {
|
|
|
|
if (!pinManager.isPinOk(_pins[i])) continue;
|
|
|
|
#ifdef ESP8266
|
|
|
|
digitalWrite(_pins[i], LOW); //turn off PWM interrupt
|
|
|
|
#else
|
2021-01-16 19:53:08 +01:00
|
|
|
if (_ledcStart < 16) ledcDetachPin(_pins[i]);
|
2020-12-07 01:39:42 +01:00
|
|
|
#endif
|
|
|
|
pinManager.deallocatePin(_pins[i]);
|
|
|
|
}
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
pinManager.deallocateLedc(_ledcStart, numPins);
|
|
|
|
#endif
|
|
|
|
}
|
2020-12-01 00:33:47 +01:00
|
|
|
};
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
class BusManager {
|
|
|
|
public:
|
|
|
|
BusManager() {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-01-30 20:51:36 +01:00
|
|
|
int add(BusConfig &bc) {
|
2021-01-15 15:43:11 +01:00
|
|
|
if (numBusses >= WLED_MAX_BUSSES) return -1;
|
2021-01-30 20:51:36 +01:00
|
|
|
if (IS_DIGITAL(bc.type)) {
|
|
|
|
busses[numBusses] = new BusDigital(bc, numBusses);
|
2021-01-15 15:43:11 +01:00
|
|
|
} else {
|
2021-01-30 20:51:36 +01:00
|
|
|
busses[numBusses] = new BusPwm(bc);
|
2021-01-15 15:43:11 +01:00
|
|
|
}
|
|
|
|
numBusses++;
|
|
|
|
return numBusses -1;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:21:16 +01:00
|
|
|
//do not call this method from system context (network callback)
|
2021-01-15 15:43:11 +01:00
|
|
|
void removeAll() {
|
2021-01-18 20:51:32 +01:00
|
|
|
//Serial.println("Removing all.");
|
2021-01-21 01:21:16 +01:00
|
|
|
//prevents crashes due to deleting busses while in use.
|
|
|
|
while (!canAllShow()) yield();
|
2021-01-15 15:43:11 +01:00
|
|
|
for (uint8_t i = 0; i < numBusses; i++) delete busses[i];
|
|
|
|
numBusses = 0;
|
|
|
|
}
|
|
|
|
//void remove(uint8_t id);
|
|
|
|
|
|
|
|
void show() {
|
|
|
|
for (uint8_t i = 0; i < numBusses; i++) {
|
|
|
|
busses[i]->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPixelColor(uint16_t pix, uint32_t c) {
|
|
|
|
for (uint8_t i = 0; i < numBusses; i++) {
|
|
|
|
Bus* b = busses[i];
|
|
|
|
uint16_t bstart = b->getStart();
|
2021-01-18 20:51:32 +01:00
|
|
|
if (pix < bstart || pix >= bstart + b->getLength()) continue;
|
2021-01-15 15:43:11 +01:00
|
|
|
busses[i]->setPixelColor(pix - bstart, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBrightness(uint8_t b) {
|
|
|
|
for (uint8_t i = 0; i < numBusses; i++) {
|
|
|
|
busses[i]->setBrightness(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getPixelColor(uint16_t pix) {
|
|
|
|
for (uint8_t i = 0; i < numBusses; i++) {
|
|
|
|
Bus* b = busses[i];
|
|
|
|
uint16_t bstart = b->getStart();
|
|
|
|
if (pix < bstart || pix >= bstart + b->getLength()) continue;
|
|
|
|
return b->getPixelColor(pix - bstart);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool canAllShow() {
|
|
|
|
for (uint8_t i = 0; i < numBusses; i++) {
|
2021-01-18 20:51:32 +01:00
|
|
|
if (!busses[i]->canShow()) return false;
|
2021-01-15 15:43:11 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:53:08 +01:00
|
|
|
Bus* getBus(uint8_t busNr) {
|
|
|
|
if (busNr >= numBusses) return nullptr;
|
|
|
|
return busses[busNr];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t getNumBusses() {
|
|
|
|
return numBusses;
|
|
|
|
}
|
|
|
|
|
2021-01-21 01:21:16 +01:00
|
|
|
static bool isRgbw(uint8_t type) {
|
|
|
|
if (type == TYPE_SK6812_RGBW || type == TYPE_TM1814) return true;
|
|
|
|
if (type > TYPE_ONOFF && type <= TYPE_ANALOG_5CH && type != TYPE_ANALOG_3CH) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:43:11 +01:00
|
|
|
private:
|
|
|
|
uint8_t numBusses = 0;
|
|
|
|
Bus* busses[WLED_MAX_BUSSES];
|
|
|
|
};
|
2021-01-16 19:53:08 +01:00
|
|
|
#endif
|