From bff6bd023e3f8f0657b4adbf2b6341bfd671c0dd Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 1 Jun 2023 22:31:00 +0100 Subject: [PATCH] Unicorn: Move gamma LUTs to pimoroni_common. --- common/pimoroni_common.hpp | 4 ++-- drivers/rgbled/rgbled.cmake | 2 +- libraries/cosmic_unicorn/cosmic_unicorn.cpp | 23 +++---------------- libraries/cosmic_unicorn/cosmic_unicorn.hpp | 1 + .../galactic_unicorn/galactic_unicorn.cpp | 23 +++---------------- .../galactic_unicorn/galactic_unicorn.hpp | 1 + libraries/stellar_unicorn/stellar_unicorn.cpp | 23 +++---------------- libraries/stellar_unicorn/stellar_unicorn.hpp | 1 + 8 files changed, 15 insertions(+), 63 deletions(-) diff --git a/common/pimoroni_common.hpp b/common/pimoroni_common.hpp index 1f61712a..568ce3d8 100644 --- a/common/pimoroni_common.hpp +++ b/common/pimoroni_common.hpp @@ -78,7 +78,7 @@ namespace pimoroni { return to_ms_since_boot(get_absolute_time()); } - constexpr uint8_t GAMMA_8BIT[256] = { + inline constexpr uint8_t GAMMA_8BIT[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, @@ -98,7 +98,7 @@ namespace pimoroni { /* Moved from pico_unicorn.cpp v = (uint16_t)(powf((float)(n) / 255.0f, 2.2) * 16383.0f + 0.5f) */ - constexpr uint16_t GAMMA_14BIT[256] = { + inline constexpr uint16_t GAMMA_14BIT[256] = { 0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 16, 20, 23, 28, 32, 37, 42, 48, 54, 61, 67, 75, 82, 90, 99, 108, 117, 127, 137, 148, 159, 170, 182, 195, 207, 221, 234, 249, 263, 278, 294, 310, 326, 343, 361, 379, 397, diff --git a/drivers/rgbled/rgbled.cmake b/drivers/rgbled/rgbled.cmake index a78e1a88..65e7ea15 100644 --- a/drivers/rgbled/rgbled.cmake +++ b/drivers/rgbled/rgbled.cmake @@ -7,4 +7,4 @@ target_sources(rgbled INTERFACE target_include_directories(rgbled INTERFACE ${CMAKE_CURRENT_LIST_DIR}) # Pull in pico libraries that we need -target_link_libraries(rgbled INTERFACE pico_stdlib hardware_pwm) \ No newline at end of file +target_link_libraries(rgbled INTERFACE pico_stdlib hardware_pwm) diff --git a/libraries/cosmic_unicorn/cosmic_unicorn.cpp b/libraries/cosmic_unicorn/cosmic_unicorn.cpp index f16f3aa6..5d0dc26e 100644 --- a/libraries/cosmic_unicorn/cosmic_unicorn.cpp +++ b/libraries/cosmic_unicorn/cosmic_unicorn.cpp @@ -38,10 +38,6 @@ // // .. and back to the start -static uint16_t r_gamma_lut[256] = {0}; -static uint16_t g_gamma_lut[256] = {0}; -static uint16_t b_gamma_lut[256] = {0}; - static uint32_t dma_channel; static uint32_t dma_ctrl_channel; static uint32_t audio_dma_channel; @@ -122,19 +118,6 @@ namespace pimoroni { // Tear down the old GU instance's hardware resources partial_teardown(); } - - - // create 14-bit gamma luts - for(uint16_t v = 0; v < 256; v++) { - // gamma correct the provided 0-255 brightness value onto a - // 0-65535 range for the pwm counter - float r_gamma = 1.8f; - r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float g_gamma = 1.8f; - g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float b_gamma = 1.8f; - b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - } // for each row: // for each bcd frame: @@ -476,9 +459,9 @@ namespace pimoroni { g = (g * this->brightness) >> 8; b = (b * this->brightness) >> 8; - uint16_t gamma_r = r_gamma_lut[r]; - uint16_t gamma_g = g_gamma_lut[g]; - uint16_t gamma_b = b_gamma_lut[b]; + uint16_t gamma_r = GAMMA_14BIT[r]; + uint16_t gamma_g = GAMMA_14BIT[g]; + uint16_t gamma_b = GAMMA_14BIT[b]; // for each row: // for each bcd frame: diff --git a/libraries/cosmic_unicorn/cosmic_unicorn.hpp b/libraries/cosmic_unicorn/cosmic_unicorn.hpp index c8efd878..cc60fc0a 100644 --- a/libraries/cosmic_unicorn/cosmic_unicorn.hpp +++ b/libraries/cosmic_unicorn/cosmic_unicorn.hpp @@ -2,6 +2,7 @@ #include "hardware/pio.h" #include "pico_graphics.hpp" +#include "common/pimoroni_common.hpp" #include "../pico_synth/pico_synth.hpp" namespace pimoroni { diff --git a/libraries/galactic_unicorn/galactic_unicorn.cpp b/libraries/galactic_unicorn/galactic_unicorn.cpp index 3a9779ac..da2bc041 100644 --- a/libraries/galactic_unicorn/galactic_unicorn.cpp +++ b/libraries/galactic_unicorn/galactic_unicorn.cpp @@ -38,10 +38,6 @@ // // .. and back to the start -static uint16_t r_gamma_lut[256] = {0}; -static uint16_t g_gamma_lut[256] = {0}; -static uint16_t b_gamma_lut[256] = {0}; - static uint32_t dma_channel; static uint32_t dma_ctrl_channel; static uint32_t audio_dma_channel; @@ -122,19 +118,6 @@ namespace pimoroni { // Tear down the old GU instance's hardware resources partial_teardown(); } - - - // create 14-bit gamma luts - for(uint16_t v = 0; v < 256; v++) { - // gamma correct the provided 0-255 brightness value onto a - // 0-65535 range for the pwm counter - float r_gamma = 1.8f; - r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float g_gamma = 1.8f; - g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float b_gamma = 1.8f; - b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - } // for each row: // for each bcd frame: @@ -470,9 +453,9 @@ namespace pimoroni { g = (g * this->brightness) >> 8; b = (b * this->brightness) >> 8; - uint16_t gamma_r = r_gamma_lut[r]; - uint16_t gamma_g = g_gamma_lut[g]; - uint16_t gamma_b = b_gamma_lut[b]; + uint16_t gamma_r = GAMMA_14BIT[r]; + uint16_t gamma_g = GAMMA_14BIT[g]; + uint16_t gamma_b = GAMMA_14BIT[b]; // for each row: // for each bcd frame: diff --git a/libraries/galactic_unicorn/galactic_unicorn.hpp b/libraries/galactic_unicorn/galactic_unicorn.hpp index 3e598287..44a9a7bc 100644 --- a/libraries/galactic_unicorn/galactic_unicorn.hpp +++ b/libraries/galactic_unicorn/galactic_unicorn.hpp @@ -2,6 +2,7 @@ #include "hardware/pio.h" #include "pico_graphics.hpp" +#include "common/pimoroni_common.hpp" #include "../pico_synth/pico_synth.hpp" namespace pimoroni { diff --git a/libraries/stellar_unicorn/stellar_unicorn.cpp b/libraries/stellar_unicorn/stellar_unicorn.cpp index 37f3534d..3917aa5f 100644 --- a/libraries/stellar_unicorn/stellar_unicorn.cpp +++ b/libraries/stellar_unicorn/stellar_unicorn.cpp @@ -38,10 +38,6 @@ // // .. and back to the start -static uint16_t r_gamma_lut[256] = {0}; -static uint16_t g_gamma_lut[256] = {0}; -static uint16_t b_gamma_lut[256] = {0}; - static uint32_t dma_channel; static uint32_t dma_ctrl_channel; static uint32_t audio_dma_channel; @@ -122,19 +118,6 @@ namespace pimoroni { // Tear down the old GU instance's hardware resources partial_teardown(); } - - - // create 14-bit gamma luts - for(uint16_t v = 0; v < 256; v++) { - // gamma correct the provided 0-255 brightness value onto a - // 0-65535 range for the pwm counter - float r_gamma = 1.8f; - r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float g_gamma = 1.8f; - g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - float b_gamma = 1.8f; - b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f); - } // for each row: // for each bcd frame: @@ -467,9 +450,9 @@ namespace pimoroni { g = (g * this->brightness) >> 8; b = (b * this->brightness) >> 8; - uint16_t gamma_r = r_gamma_lut[r]; - uint16_t gamma_g = g_gamma_lut[g]; - uint16_t gamma_b = b_gamma_lut[b]; + uint16_t gamma_r = GAMMA_14BIT[r]; + uint16_t gamma_g = GAMMA_14BIT[g]; + uint16_t gamma_b = GAMMA_14BIT[b]; // for each row: // for each bcd frame: diff --git a/libraries/stellar_unicorn/stellar_unicorn.hpp b/libraries/stellar_unicorn/stellar_unicorn.hpp index 2658f667..747fb9de 100644 --- a/libraries/stellar_unicorn/stellar_unicorn.hpp +++ b/libraries/stellar_unicorn/stellar_unicorn.hpp @@ -2,6 +2,7 @@ #include "hardware/pio.h" #include "pico_graphics.hpp" +#include "common/pimoroni_common.hpp" #include "../pico_synth/pico_synth.hpp" namespace pimoroni {