mirror of
https://github.com/wled/WLED.git
synced 2025-07-09 11:56:35 +00:00
Move _data and allocation to derived class
- as suggested by @TripleWhy - minimum length guard Conflicts: wled00/bus_manager.cpp wled00/bus_manager.h
This commit is contained in:
parent
1db3359b84
commit
c43d09c8b1
@ -118,16 +118,6 @@ uint32_t Bus::autoWhiteCalc(uint32_t c) const {
|
|||||||
return RGBW32(r, g, b, w);
|
return RGBW32(r, g, b, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *Bus::allocateData(size_t size) {
|
|
||||||
freeData(); // should not happen, but for safety
|
|
||||||
return _data = (uint8_t *)(size>0 ? calloc(size, sizeof(uint8_t)) : nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Bus::freeData() {
|
|
||||||
if (_data) free(_data);
|
|
||||||
_data = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
BusDigital::BusDigital(const BusConfig &bc, uint8_t nr)
|
BusDigital::BusDigital(const BusConfig &bc, uint8_t nr)
|
||||||
: Bus(bc.type, bc.start, bc.autoWhite, bc.count, bc.reversed, (bc.refreshReq || bc.type == TYPE_TM1814))
|
: Bus(bc.type, bc.start, bc.autoWhite, bc.count, bc.reversed, (bc.refreshReq || bc.type == TYPE_TM1814))
|
||||||
@ -153,8 +143,10 @@ BusDigital::BusDigital(const BusConfig &bc, uint8_t nr)
|
|||||||
_hasRgb = hasRGB(bc.type);
|
_hasRgb = hasRGB(bc.type);
|
||||||
_hasWhite = hasWhite(bc.type);
|
_hasWhite = hasWhite(bc.type);
|
||||||
_hasCCT = hasCCT(bc.type);
|
_hasCCT = hasCCT(bc.type);
|
||||||
if (bc.doubleBuffer && !allocateData(bc.count * Bus::getNumberOfChannels(bc.type))) return;
|
if (bc.doubleBuffer) {
|
||||||
//_buffering = bc.doubleBuffer;
|
_data = (uint8_t*)d_calloc(_len, Bus::getNumberOfChannels(_type));
|
||||||
|
if (!_data) DEBUG_PRINTLN(F("Bus: Buffer allocation failed!"));
|
||||||
|
}
|
||||||
uint16_t lenToCreate = bc.count;
|
uint16_t lenToCreate = bc.count;
|
||||||
if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus
|
if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus
|
||||||
_busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr);
|
_busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr);
|
||||||
@ -285,7 +277,7 @@ void BusDigital::show() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PolyBus::show(_busPtr, _iType, !_data); // faster if buffer consistency is not important (use !_buffering this causes 20% FPS drop)
|
PolyBus::show(_busPtr, _iType, !_data); // faster if buffer consistency is not important
|
||||||
// restore bus brightness to its original value
|
// restore bus brightness to its original value
|
||||||
// this is done right after show, so this is only OK if LED updates are completed before show() returns
|
// this is done right after show, so this is only OK if LED updates are completed before show() returns
|
||||||
// or async show has a separate buffer (ESP32 RMT and I2S are ok)
|
// or async show has a separate buffer (ESP32 RMT and I2S are ok)
|
||||||
@ -434,10 +426,11 @@ void BusDigital::begin() {
|
|||||||
void BusDigital::cleanup() {
|
void BusDigital::cleanup() {
|
||||||
DEBUG_PRINTLN(F("Digital Cleanup."));
|
DEBUG_PRINTLN(F("Digital Cleanup."));
|
||||||
PolyBus::cleanup(_busPtr, _iType);
|
PolyBus::cleanup(_busPtr, _iType);
|
||||||
|
free(_data);
|
||||||
|
_data = nullptr;
|
||||||
_iType = I_NONE;
|
_iType = I_NONE;
|
||||||
_valid = false;
|
_valid = false;
|
||||||
_busPtr = nullptr;
|
_busPtr = nullptr;
|
||||||
if (_data != nullptr) freeData();
|
|
||||||
PinManager::deallocatePin(_pins[1], PinOwner::BusDigital);
|
PinManager::deallocatePin(_pins[1], PinOwner::BusDigital);
|
||||||
PinManager::deallocatePin(_pins[0], PinOwner::BusDigital);
|
PinManager::deallocatePin(_pins[0], PinOwner::BusDigital);
|
||||||
}
|
}
|
||||||
@ -509,7 +502,6 @@ BusPwm::BusPwm(const BusConfig &bc)
|
|||||||
_hasRgb = hasRGB(bc.type);
|
_hasRgb = hasRGB(bc.type);
|
||||||
_hasWhite = hasWhite(bc.type);
|
_hasWhite = hasWhite(bc.type);
|
||||||
_hasCCT = hasCCT(bc.type);
|
_hasCCT = hasCCT(bc.type);
|
||||||
_data = _pwmdata; // avoid malloc() and use stack
|
|
||||||
_valid = true;
|
_valid = true;
|
||||||
DEBUG_PRINTF_P(PSTR("%successfully inited PWM strip with type %u, frequency %u, bit depth %u and pins %u,%u,%u,%u,%u\n"), _valid?"S":"Uns", bc.type, _frequency, _depth, _pins[0], _pins[1], _pins[2], _pins[3], _pins[4]);
|
DEBUG_PRINTF_P(PSTR("%successfully inited PWM strip with type %u, frequency %u, bit depth %u and pins %u,%u,%u,%u,%u\n"), _valid?"S":"Uns", bc.type, _frequency, _depth, _pins[0], _pins[1], _pins[2], _pins[3], _pins[4]);
|
||||||
}
|
}
|
||||||
@ -678,7 +670,7 @@ void BusPwm::deallocatePins() {
|
|||||||
|
|
||||||
BusOnOff::BusOnOff(const BusConfig &bc)
|
BusOnOff::BusOnOff(const BusConfig &bc)
|
||||||
: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed)
|
: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed)
|
||||||
, _onoffdata(0)
|
, _data(0)
|
||||||
{
|
{
|
||||||
if (!Bus::isOnOff(bc.type)) return;
|
if (!Bus::isOnOff(bc.type)) return;
|
||||||
|
|
||||||
@ -691,7 +683,6 @@ BusOnOff::BusOnOff(const BusConfig &bc)
|
|||||||
_hasRgb = false;
|
_hasRgb = false;
|
||||||
_hasWhite = false;
|
_hasWhite = false;
|
||||||
_hasCCT = false;
|
_hasCCT = false;
|
||||||
_data = &_onoffdata; // avoid malloc() and use stack
|
|
||||||
_valid = true;
|
_valid = true;
|
||||||
DEBUG_PRINTF_P(PSTR("%successfully inited On/Off strip with pin %u\n"), _valid?"S":"Uns", _pin);
|
DEBUG_PRINTF_P(PSTR("%successfully inited On/Off strip with pin %u\n"), _valid?"S":"Uns", _pin);
|
||||||
}
|
}
|
||||||
@ -703,17 +694,17 @@ void BusOnOff::setPixelColor(unsigned pix, uint32_t c) {
|
|||||||
uint8_t g = G(c);
|
uint8_t g = G(c);
|
||||||
uint8_t b = B(c);
|
uint8_t b = B(c);
|
||||||
uint8_t w = W(c);
|
uint8_t w = W(c);
|
||||||
_data[0] = bool(r|g|b|w) && bool(_bri) ? 0xFF : 0;
|
_data = bool(r|g|b|w) && bool(_bri) ? 0xFF : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BusOnOff::getPixelColor(unsigned pix) const {
|
uint32_t BusOnOff::getPixelColor(unsigned pix) const {
|
||||||
if (!_valid) return 0;
|
if (!_valid) return 0;
|
||||||
return RGBW32(_data[0], _data[0], _data[0], _data[0]);
|
return RGBW32(_data, _data, _data, _data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BusOnOff::show() {
|
void BusOnOff::show() {
|
||||||
if (!_valid) return;
|
if (!_valid) return;
|
||||||
digitalWrite(_pin, _reversed ? !(bool)_data[0] : (bool)_data[0]);
|
digitalWrite(_pin, _reversed ? !(bool)_data : (bool)_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BusOnOff::getPins(uint8_t* pinArray) const {
|
size_t BusOnOff::getPins(uint8_t* pinArray) const {
|
||||||
@ -752,7 +743,8 @@ BusNetwork::BusNetwork(const BusConfig &bc)
|
|||||||
_hasCCT = false;
|
_hasCCT = false;
|
||||||
_UDPchannels = _hasWhite + 3;
|
_UDPchannels = _hasWhite + 3;
|
||||||
_client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]);
|
_client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]);
|
||||||
_valid = (allocateData(_len * _UDPchannels) != nullptr) && bc.count > 0;
|
_data = (uint8_t*)d_calloc(_len, _UDPchannels);
|
||||||
|
_valid = (_data != nullptr);
|
||||||
DEBUG_PRINTF_P(PSTR("%successfully inited virtual strip with type %u and IP %u.%u.%u.%u\n"), _valid?"S":"Uns", bc.type, bc.pins[0], bc.pins[1], bc.pins[2], bc.pins[3]);
|
DEBUG_PRINTF_P(PSTR("%successfully inited virtual strip with type %u and IP %u.%u.%u.%u\n"), _valid?"S":"Uns", bc.type, bc.pins[0], bc.pins[1], bc.pins[2], bc.pins[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -801,9 +793,11 @@ std::vector<LEDType> BusNetwork::getLEDTypes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BusNetwork::cleanup() {
|
void BusNetwork::cleanup() {
|
||||||
|
DEBUG_PRINTLN(F("Virtual Cleanup."));
|
||||||
|
free(_data);
|
||||||
|
_data = nullptr;
|
||||||
_type = I_NONE;
|
_type = I_NONE;
|
||||||
_valid = false;
|
_valid = false;
|
||||||
freeData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -83,16 +83,15 @@ class Bus {
|
|||||||
: _type(type)
|
: _type(type)
|
||||||
, _bri(255)
|
, _bri(255)
|
||||||
, _start(start)
|
, _start(start)
|
||||||
, _len(len)
|
, _len(std::max(len,(uint16_t)1))
|
||||||
, _reversed(reversed)
|
, _reversed(reversed)
|
||||||
, _valid(false)
|
, _valid(false)
|
||||||
, _needsRefresh(refresh)
|
, _needsRefresh(refresh)
|
||||||
, _data(nullptr) // keep data access consistent across all types of buses
|
|
||||||
{
|
{
|
||||||
_autoWhiteMode = Bus::hasWhite(type) ? aw : RGBW_MODE_MANUAL_ONLY;
|
_autoWhiteMode = Bus::hasWhite(type) ? aw : RGBW_MODE_MANUAL_ONLY;
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual ~Bus() {} //throw the bus under the bus (derived class needs to freeData())
|
virtual ~Bus() {} //throw the bus under the bus
|
||||||
|
|
||||||
virtual void begin() {};
|
virtual void begin() {};
|
||||||
virtual void show() = 0;
|
virtual void show() = 0;
|
||||||
@ -191,7 +190,6 @@ class Bus {
|
|||||||
bool _hasCCT;// : 1;
|
bool _hasCCT;// : 1;
|
||||||
//} __attribute__ ((packed));
|
//} __attribute__ ((packed));
|
||||||
uint8_t _autoWhiteMode;
|
uint8_t _autoWhiteMode;
|
||||||
uint8_t *_data;
|
|
||||||
// global Auto White Calculation override
|
// global Auto White Calculation override
|
||||||
static uint8_t _gAWM;
|
static uint8_t _gAWM;
|
||||||
// _cct has the following menaings (see calculateCCT() & BusManager::setSegmentCCT()):
|
// _cct has the following menaings (see calculateCCT() & BusManager::setSegmentCCT()):
|
||||||
@ -206,8 +204,6 @@ class Bus {
|
|||||||
static uint8_t _cctBlend;
|
static uint8_t _cctBlend;
|
||||||
|
|
||||||
uint32_t autoWhiteCalc(uint32_t c) const;
|
uint32_t autoWhiteCalc(uint32_t c) const;
|
||||||
uint8_t *allocateData(size_t size = 1);
|
|
||||||
void freeData();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -244,7 +240,8 @@ class BusDigital : public Bus {
|
|||||||
uint16_t _frequencykHz;
|
uint16_t _frequencykHz;
|
||||||
uint8_t _milliAmpsPerLed;
|
uint8_t _milliAmpsPerLed;
|
||||||
uint16_t _milliAmpsMax;
|
uint16_t _milliAmpsMax;
|
||||||
void * _busPtr;
|
uint8_t *_data;
|
||||||
|
void *_busPtr;
|
||||||
|
|
||||||
static uint16_t _milliAmpsTotal; // is overwitten/recalculated on each show()
|
static uint16_t _milliAmpsTotal; // is overwitten/recalculated on each show()
|
||||||
|
|
||||||
@ -274,13 +271,13 @@ class BusPwm : public Bus {
|
|||||||
uint16_t getFrequency() const override { return _frequency; }
|
uint16_t getFrequency() const override { return _frequency; }
|
||||||
size_t getBusSize() const override { return sizeof(BusPwm); }
|
size_t getBusSize() const override { return sizeof(BusPwm); }
|
||||||
void show() override;
|
void show() override;
|
||||||
inline void cleanup() { deallocatePins(); _data = nullptr; }
|
inline void cleanup() { deallocatePins(); }
|
||||||
|
|
||||||
static std::vector<LEDType> getLEDTypes();
|
static std::vector<LEDType> getLEDTypes();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _pins[OUTPUT_MAX_PINS];
|
uint8_t _pins[OUTPUT_MAX_PINS];
|
||||||
uint8_t _pwmdata[OUTPUT_MAX_PINS];
|
uint8_t _data[OUTPUT_MAX_PINS];
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
uint8_t _ledcStart;
|
uint8_t _ledcStart;
|
||||||
#endif
|
#endif
|
||||||
@ -301,13 +298,13 @@ class BusOnOff : public Bus {
|
|||||||
size_t getPins(uint8_t* pinArray) const override;
|
size_t getPins(uint8_t* pinArray) const override;
|
||||||
size_t getBusSize() const override { return sizeof(BusOnOff); }
|
size_t getBusSize() const override { return sizeof(BusOnOff); }
|
||||||
void show() override;
|
void show() override;
|
||||||
inline void cleanup() { PinManager::deallocatePin(_pin, PinOwner::BusOnOff); _data = nullptr; }
|
inline void cleanup() { PinManager::deallocatePin(_pin, PinOwner::BusOnOff); }
|
||||||
|
|
||||||
static std::vector<LEDType> getLEDTypes();
|
static std::vector<LEDType> getLEDTypes();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _pin;
|
uint8_t _pin;
|
||||||
uint8_t _onoffdata;
|
uint8_t _data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -331,6 +328,7 @@ class BusNetwork : public Bus {
|
|||||||
uint8_t _UDPtype;
|
uint8_t _UDPtype;
|
||||||
uint8_t _UDPchannels;
|
uint8_t _UDPchannels;
|
||||||
bool _broadcastLock;
|
bool _broadcastLock;
|
||||||
|
uint8_t *_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -351,7 +349,7 @@ struct BusConfig {
|
|||||||
uint16_t milliAmpsMax;
|
uint16_t milliAmpsMax;
|
||||||
|
|
||||||
BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT)
|
BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT)
|
||||||
: count(len)
|
: count(std::max(len,(uint16_t)1))
|
||||||
, start(pstart)
|
, start(pstart)
|
||||||
, colorOrder(pcolorOrder)
|
, colorOrder(pcolorOrder)
|
||||||
, reversed(rev)
|
, reversed(rev)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user