From 5d1efd84a4cb3281b76098cd3facc261f90e3ce7 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 27 Sep 2021 16:29:38 +0200 Subject: [PATCH] Permanent double buffer. --- wled00/bus_manager.h | 69 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 21c6d203..e086ead2 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -373,47 +373,50 @@ class BusNetwork : public Bus { public: BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start) { _valid = false; - /* - switch (bc.type) { - case TYPE_NET_ARTNET_RGB: - case TYPE_NET_E131_RGB: - case TYPE_NET_DDP_RGB: - default: +// switch (bc.type) { +// case TYPE_NET_ARTNET_RGB: +// _rgbw = false; +// _UDPtype = 2; +// break; +// case TYPE_NET_E131_RGB: +// _rgbw = false; +// _UDPtype = 1; +// break; +// case TYPE_NET_DDP_RGB: +// _rgbw = false; +// _UDPtype = 0; +// break; +// default: _rgbw = false; - break; - } - */ - _rgbw = false; + _UDPtype = bc.type - TYPE_NET_DDP_RGB; +// break; +// } + _UDPchannels = _rgbw ? 4 : 3; //_rgbw |= bc.rgbwOverride; // RGBW override in bit 7 or can have a special type - _data = (byte *)malloc(bc.count * (_rgbw ? 4 : 3)); + _data = (byte *)malloc(bc.count * _UDPchannels); if (_data == nullptr) return; - memset(_data, 0, bc.count * (_rgbw ? 4 : 3)); + memset(_data, 0, bc.count * _UDPchannels); _len = bc.count; _colorOrder = bc.colorOrder; _client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]); _broadcastLock = false; _valid = true; + _data2 = (byte *)malloc(_len * _UDPchannels); }; void setPixelColor(uint16_t pix, uint32_t c) { if (!_valid || pix >= _len) return; - uint16_t offset = pix*(_rgbw?4:3); + uint16_t offset = pix * _UDPchannels; _data[offset] = 0xFF & (c >> 16); _data[offset+1] = 0xFF & (c >> 8); _data[offset+2] = 0xFF & (c ); if (_rgbw) _data[offset+3] = 0xFF & (c >> 24); - /* - // not using double buffer - _data[offset] = scale8(0xFF & (c >> 16), _bri); - _data[offset+1] = scale8(0xFF & (c >> 8), _bri); - _data[offset+2] = scale8(0xFF & (c ), _bri); - if (_rgbw) _data[offset+3] = scale8(0xFF & (c >> 24), _bri); - */ } uint32_t getPixelColor(uint16_t pix) { if (!_valid || pix >= _len) return 0; - uint16_t offset = pix*(_rgbw?4:3); + uint16_t offset = pix * _UDPchannels; + // behave as NeoPixelBus return ( (_rgbw ? (scale8(_data[offset+3], _bri) << 24) : 0) | (scale8(_data[offset] , _bri) << 16) @@ -423,35 +426,27 @@ class BusNetwork : public Bus { } void show() { - uint8_t type; - if (!_valid || _broadcastLock) return; + if (!_valid || !canShow()) return; _broadcastLock = true; - switch (_type) { - case TYPE_NET_ARTNET_RGB: type = 2; break; - case TYPE_NET_E131_RGB: type = 1; break; - case TYPE_NET_DDP_RGB: - default: type = 0; break; - } // apply brightness to second buffer - byte *_data2 = (byte *)malloc(_len * (_rgbw ? 4 : 3)); if (_data2 == nullptr) { // but display original buffer if memory allocation failed - realtimeBroadcast(type, _client, _len, _data, _rgbw); + realtimeBroadcast(_UDPtype, _client, _len, _data, _rgbw); } else { for (uint16_t pix=0; pix<_len; pix++) { - uint16_t offset = pix*(_rgbw?4:3); + uint16_t offset = pix * _UDPchannels; _data2[offset ] = scale8(_data[offset ], _bri); _data2[offset+1] = scale8(_data[offset+1], _bri); _data2[offset+2] = scale8(_data[offset+2], _bri); if (_rgbw) _data2[offset+3] = scale8(_data[offset+3], _bri); } - realtimeBroadcast(type, _client, _len, _data2, _rgbw); - free(_data2); + realtimeBroadcast(_UDPtype, _client, _len, _data2, _rgbw); } _broadcastLock = false; } inline bool canShow() { + // this should be a return value from UDP routine if it is still sending data out return !_broadcastLock; } @@ -479,6 +474,8 @@ class BusNetwork : public Bus { _valid = false; if (_data != nullptr) free(_data); _data = nullptr; + if (_data2 != nullptr) free(_data2); + _data2 = nullptr; } ~BusNetwork() { @@ -490,9 +487,11 @@ class BusNetwork : public Bus { uint16_t _len = 0; uint8_t _colorOrder; uint8_t _bri = 255; + uint8_t _UDPtype; + uint8_t _UDPchannels; bool _rgbw; bool _broadcastLock; - byte* _data; + byte *_data, *_data2; };