Permanent double buffer.

This commit is contained in:
Blaz Kristan 2021-09-27 16:29:38 +02:00
parent 8af953e20d
commit 5d1efd84a4

View File

@ -373,47 +373,50 @@ class BusNetwork : public Bus {
public: public:
BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start) { BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start) {
_valid = false; _valid = false;
/* // switch (bc.type) {
switch (bc.type) { // case TYPE_NET_ARTNET_RGB:
case TYPE_NET_ARTNET_RGB: // _rgbw = false;
case TYPE_NET_E131_RGB: // _UDPtype = 2;
case TYPE_NET_DDP_RGB: // break;
default: // case TYPE_NET_E131_RGB:
_rgbw = false; // _rgbw = false;
break; // _UDPtype = 1;
} // break;
*/ // case TYPE_NET_DDP_RGB:
// _rgbw = false;
// _UDPtype = 0;
// break;
// default:
_rgbw = false; _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 //_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; if (_data == nullptr) return;
memset(_data, 0, bc.count * (_rgbw ? 4 : 3)); memset(_data, 0, bc.count * _UDPchannels);
_len = bc.count; _len = bc.count;
_colorOrder = bc.colorOrder; _colorOrder = bc.colorOrder;
_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]);
_broadcastLock = false; _broadcastLock = false;
_valid = true; _valid = true;
_data2 = (byte *)malloc(_len * _UDPchannels);
}; };
void setPixelColor(uint16_t pix, uint32_t c) { void setPixelColor(uint16_t pix, uint32_t c) {
if (!_valid || pix >= _len) return; if (!_valid || pix >= _len) return;
uint16_t offset = pix*(_rgbw?4:3); uint16_t offset = pix * _UDPchannels;
_data[offset] = 0xFF & (c >> 16); _data[offset] = 0xFF & (c >> 16);
_data[offset+1] = 0xFF & (c >> 8); _data[offset+1] = 0xFF & (c >> 8);
_data[offset+2] = 0xFF & (c ); _data[offset+2] = 0xFF & (c );
if (_rgbw) _data[offset+3] = 0xFF & (c >> 24); 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) { uint32_t getPixelColor(uint16_t pix) {
if (!_valid || pix >= _len) return 0; if (!_valid || pix >= _len) return 0;
uint16_t offset = pix*(_rgbw?4:3); uint16_t offset = pix * _UDPchannels;
// behave as NeoPixelBus
return ( return (
(_rgbw ? (scale8(_data[offset+3], _bri) << 24) : 0) (_rgbw ? (scale8(_data[offset+3], _bri) << 24) : 0)
| (scale8(_data[offset] , _bri) << 16) | (scale8(_data[offset] , _bri) << 16)
@ -423,35 +426,27 @@ class BusNetwork : public Bus {
} }
void show() { void show() {
uint8_t type; if (!_valid || !canShow()) return;
if (!_valid || _broadcastLock) return;
_broadcastLock = true; _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 // apply brightness to second buffer
byte *_data2 = (byte *)malloc(_len * (_rgbw ? 4 : 3));
if (_data2 == nullptr) { if (_data2 == nullptr) {
// but display original buffer if memory allocation failed // but display original buffer if memory allocation failed
realtimeBroadcast(type, _client, _len, _data, _rgbw); realtimeBroadcast(_UDPtype, _client, _len, _data, _rgbw);
} else { } else {
for (uint16_t pix=0; pix<_len; pix++) { 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 ] = scale8(_data[offset ], _bri);
_data2[offset+1] = scale8(_data[offset+1], _bri); _data2[offset+1] = scale8(_data[offset+1], _bri);
_data2[offset+2] = scale8(_data[offset+2], _bri); _data2[offset+2] = scale8(_data[offset+2], _bri);
if (_rgbw) _data2[offset+3] = scale8(_data[offset+3], _bri); if (_rgbw) _data2[offset+3] = scale8(_data[offset+3], _bri);
} }
realtimeBroadcast(type, _client, _len, _data2, _rgbw); realtimeBroadcast(_UDPtype, _client, _len, _data2, _rgbw);
free(_data2);
} }
_broadcastLock = false; _broadcastLock = false;
} }
inline bool canShow() { inline bool canShow() {
// this should be a return value from UDP routine if it is still sending data out
return !_broadcastLock; return !_broadcastLock;
} }
@ -479,6 +474,8 @@ class BusNetwork : public Bus {
_valid = false; _valid = false;
if (_data != nullptr) free(_data); if (_data != nullptr) free(_data);
_data = nullptr; _data = nullptr;
if (_data2 != nullptr) free(_data2);
_data2 = nullptr;
} }
~BusNetwork() { ~BusNetwork() {
@ -490,9 +487,11 @@ class BusNetwork : public Bus {
uint16_t _len = 0; uint16_t _len = 0;
uint8_t _colorOrder; uint8_t _colorOrder;
uint8_t _bri = 255; uint8_t _bri = 255;
uint8_t _UDPtype;
uint8_t _UDPchannels;
bool _rgbw; bool _rgbw;
bool _broadcastLock; bool _broadcastLock;
byte* _data; byte *_data, *_data2;
}; };