Skip to content

Commit

Permalink
Unicorn: Move gamma LUTs to pimoroni_common.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Jun 4, 2023
1 parent 19c57eb commit bff6bd0
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 63 deletions.
4 changes: 2 additions & 2 deletions common/pimoroni_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion drivers/rgbled/rgbled.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
target_link_libraries(rgbled INTERFACE pico_stdlib hardware_pwm)
23 changes: 3 additions & 20 deletions libraries/cosmic_unicorn/cosmic_unicorn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions libraries/cosmic_unicorn/cosmic_unicorn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 3 additions & 20 deletions libraries/galactic_unicorn/galactic_unicorn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions libraries/galactic_unicorn/galactic_unicorn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 3 additions & 20 deletions libraries/stellar_unicorn/stellar_unicorn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions libraries/stellar_unicorn/stellar_unicorn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bff6bd0

Please sign in to comment.