From 3661d983a416dfe77b9ee87a2c6539f1c465b743 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 12 May 2022 15:22:08 -0700 Subject: [PATCH] Sm168xx settings refactoring (#570) * refactor gains * const refactoring --- src/internal/NeoEspBitBangMethod.cpp | 15 --- src/internal/NeoSettings.h | 14 +-- src/internal/NeoSm168xxColorFeatures.h | 139 +++++++++++++++---------- 3 files changed, 91 insertions(+), 77 deletions(-) diff --git a/src/internal/NeoEspBitBangMethod.cpp b/src/internal/NeoEspBitBangMethod.cpp index 7cb9003..27f1bac 100644 --- a/src/internal/NeoEspBitBangMethod.cpp +++ b/src/internal/NeoEspBitBangMethod.cpp @@ -159,21 +159,6 @@ void IRAM_ATTR NeoEspBitBangBase_send_pixels_inv(uint8_t* pixels, uint8_t* end, uint32_t cyclesStart = 0; // trigger emediately uint32_t cyclesNext = 0; -#if defined(ARDUINO_ARCH_ESP8266) - // compensation for if (pin == ...) - t0h -= 3; - t1h -= 3; - - uint32_t gpio_clear = 0; - uint32_t gpio_set = 0; - if (pin == 16) - { - // reading and writing RTC_GPIO_OUT is too slow inside the loop - gpio_clear = (READ_PERI_REG(RTC_GPIO_OUT) & (uint32)0xfffffffe); - gpio_set = gpio_clear | 1; - } -#endif - for (;;) { // do the checks here while we are waiting on time to pass diff --git a/src/internal/NeoSettings.h b/src/internal/NeoSettings.h index e85d115..008dd29 100644 --- a/src/internal/NeoSettings.h +++ b/src/internal/NeoSettings.h @@ -39,9 +39,9 @@ public: { } - uint16_t RedTenthMilliAmpere; // in 1/10th ma - uint16_t GreenTenthMilliAmpere; // in 1/10th ma - uint16_t BlueTenthMilliAmpere; // in 1/10th ma + const uint16_t RedTenthMilliAmpere; // in 1/10th ma + const uint16_t GreenTenthMilliAmpere; // in 1/10th ma + const uint16_t BlueTenthMilliAmpere; // in 1/10th ma }; class NeoRgbwCurrentSettings @@ -55,8 +55,8 @@ public: { } - uint16_t RedTenthMilliAmpere; // in 1/10th ma - uint16_t GreenTenthMilliAmpere; // in 1/10th ma - uint16_t BlueTenthMilliAmpere; // in 1/10th ma - uint16_t WhiteTenthMilliAmpere; // in 1/10th ma + const uint16_t RedTenthMilliAmpere; // in 1/10th ma + const uint16_t GreenTenthMilliAmpere; // in 1/10th ma + const uint16_t BlueTenthMilliAmpere; // in 1/10th ma + const uint16_t WhiteTenthMilliAmpere; // in 1/10th ma }; \ No newline at end of file diff --git a/src/internal/NeoSm168xxColorFeatures.h b/src/internal/NeoSm168xxColorFeatures.h index 2ad1306..e97b9e6 100644 --- a/src/internal/NeoSm168xxColorFeatures.h +++ b/src/internal/NeoSm168xxColorFeatures.h @@ -39,29 +39,40 @@ SM16824E 60~350mA class NeoSm168x3SettingsBase : public NeoRgbCurrentSettings { public: - NeoSm168x3SettingsBase() : - NeoRgbCurrentSettings(0,0,0), - Encoded{0} {} + NeoSm168x3SettingsBase(uint8_t redGain, + uint8_t greenGain, + uint8_t blueGain, + uint16_t redCurrent, + uint16_t greenCurrent, + uint16_t blueCurrent) : + NeoRgbCurrentSettings(redCurrent, greenCurrent, blueCurrent), + RedGain(redGain & 0x0f), + GreenGain(greenGain & 0x0f), + BlueGain(blueGain & 0x0f) {} - uint8_t Encoded[2]; + const uint8_t RedGain : 4; + const uint8_t GreenGain : 4; + const uint8_t BlueGain : 4; }; class NeoSm16803pbSettings : public NeoSm168x3SettingsBase { public: - NeoSm16803pbSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain) + NeoSm16803pbSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain) : + NeoSm168x3SettingsBase(redGain, + greenGain, + blueGain, + CurrentLookup[redGain], + CurrentLookup[greenGain], + CurrentLookup[blueGain]) { - redGain &= 0x0f; - greenGain &= 0x0f; - blueGain &= 0x0f; - - RedTenthMilliAmpere = CurrentLookup[redGain]; - GreenTenthMilliAmpere = CurrentLookup[greenGain]; - BlueTenthMilliAmpere = CurrentLookup[blueGain]; + } + void Encode(uint8_t* encoded) const + { // 0RGB 4 bits each - Encoded[0] = redGain; - Encoded[1] = greenGain << 4 | blueGain; + encoded[0] = RedGain; + encoded[1] = GreenGain << 4 | BlueGain; } protected: @@ -74,19 +85,21 @@ class NeoSm16823eSettings : public NeoSm168x3SettingsBase { public: NeoSm16823eSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain, uint16_t resisterOhms) : + NeoSm168x3SettingsBase(redGain, + greenGain, + blueGain, + calcCurrent(resisterOhms, redGain), + calcCurrent(resisterOhms, greenGain), + calcCurrent(resisterOhms, blueGain)), extROhms(resisterOhms) { - redGain &= 0x0f; - greenGain &= 0x0f; - blueGain &= 0x0f; - - RedTenthMilliAmpere = calcCurrent(extROhms, redGain); - GreenTenthMilliAmpere = calcCurrent(extROhms, greenGain); - BlueTenthMilliAmpere = calcCurrent(extROhms, blueGain); + } + void Encode(uint8_t* encoded) const + { // RGB0 4 bits each - Encoded[0] = redGain << 4 | greenGain; - Encoded[1] = blueGain << 4; + encoded[0] = RedGain << 4 | GreenGain; + encoded[1] = BlueGain << 4; } protected: @@ -97,7 +110,6 @@ protected: uint16_t mA = (967 * (240 + (gain * 32)) / ohms); // from spec sheet, gain 0-15 instead return mA * 10; // return tenths of mA } - }; // RGBW versions @@ -105,31 +117,46 @@ protected: class NeoSm168x4SettingsBase : public NeoRgbwCurrentSettings { public: - NeoSm168x4SettingsBase() : - NeoRgbwCurrentSettings(0,0,0,0), - Encoded{ 0 } {} + NeoSm168x4SettingsBase(uint8_t redGain, + uint8_t greenGain, + uint8_t blueGain, + uint8_t whiteGain, + uint16_t redCurrent, + uint16_t greenCurrent, + uint16_t blueCurrent, + uint16_t whiteCurrent) : + NeoRgbwCurrentSettings(redCurrent, greenCurrent, blueCurrent, whiteCurrent), + RedGain(redGain & 0x0f), + GreenGain(greenGain & 0x0f), + BlueGain(blueGain & 0x0f), + WhiteGain(whiteGain & 0x0f) {} - uint8_t Encoded[2]; + const uint8_t RedGain : 4; + const uint8_t GreenGain : 4; + const uint8_t BlueGain : 4; + const uint8_t WhiteGain : 4; }; class NeoSm16804ebSettings : public NeoSm168x4SettingsBase { public: - NeoSm16804ebSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain, uint8_t whiteGain) + NeoSm16804ebSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain, uint8_t whiteGain) : + NeoSm168x4SettingsBase(redGain, + greenGain, + blueGain, + whiteGain, + CurrentLookup[redGain], + CurrentLookup[greenGain], + CurrentLookup[blueGain], + CurrentLookup[whiteGain]) { - redGain &= 0x0f; - greenGain &= 0x0f; - blueGain &= 0x0f; - whiteGain &= 0x0f; - - RedTenthMilliAmpere = CurrentLookup[redGain]; - GreenTenthMilliAmpere = CurrentLookup[greenGain]; - BlueTenthMilliAmpere = CurrentLookup[blueGain]; - WhiteTenthMilliAmpere = CurrentLookup[whiteGain]; + } + void Encode(uint8_t* encoded) const + { // RGBW 4 bits each - Encoded[0] = redGain << 4 | greenGain; - Encoded[1] = blueGain << 4 | whiteGain; + encoded[0] = RedGain << 4 | GreenGain; + encoded[1] = BlueGain << 4 | WhiteGain; } protected: @@ -142,21 +169,23 @@ class NeoSm16824eSettings : public NeoSm168x4SettingsBase { public: NeoSm16824eSettings(uint8_t redGain, uint8_t greenGain, uint8_t blueGain, uint8_t whiteGain, uint16_t resisterOhms) : + NeoSm168x4SettingsBase(redGain, + greenGain, + blueGain, + whiteGain, + calcCurrent(resisterOhms, redGain), + calcCurrent(resisterOhms, greenGain), + calcCurrent(resisterOhms, blueGain), + calcCurrent(resisterOhms, whiteGain)), extROhms(resisterOhms) { - redGain &= 0x0f; - greenGain &= 0x0f; - blueGain &= 0x0f; - whiteGain &= 0x0f; - - RedTenthMilliAmpere = calcCurrent(extROhms, redGain); - GreenTenthMilliAmpere = calcCurrent(extROhms, greenGain); - BlueTenthMilliAmpere = calcCurrent(extROhms, blueGain); - WhiteTenthMilliAmpere = calcCurrent(extROhms, whiteGain); + } + void Encode(uint8_t* encoded) const + { // RGBW 4 bits each - Encoded[0] = redGain << 4 | greenGain; - Encoded[1] = blueGain << 4 | whiteGain; + encoded[0] = RedGain << 4 | GreenGain; + encoded[1] = BlueGain << 4 | WhiteGain; } protected: @@ -180,8 +209,8 @@ public: { // settings are at the end of the data stream uint8_t* pDest = pData + sizeData - SettingsSize; - // copy by bytes to avoid endianess - memcpy(pDest, &settings.Encoded, SettingsSize); + + settings.Encode(pDest); } static uint8_t* pixels([[maybe_unused]] uint8_t* pData, [[maybe_unused]] size_t sizeData) @@ -241,8 +270,8 @@ public: { // settings are at the end of the data stream uint8_t* pDest = pData + sizeData - SettingsSize; - // copy by bytes to avoid endianess - memcpy(pDest, &settings.Encoded, SettingsSize); + + settings.Encode(pDest); } static uint8_t* pixels([[maybe_unused]] uint8_t* pData, [[maybe_unused]] size_t sizeData)