Skip to content

Commit bff6bd0

Browse files
committed
Unicorn: Move gamma LUTs to pimoroni_common.
1 parent 19c57eb commit bff6bd0

File tree

8 files changed

+15
-63
lines changed

8 files changed

+15
-63
lines changed

common/pimoroni_common.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace pimoroni {
7878
return to_ms_since_boot(get_absolute_time());
7979
}
8080

81-
constexpr uint8_t GAMMA_8BIT[256] = {
81+
inline constexpr uint8_t GAMMA_8BIT[256] = {
8282
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8383
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
8484
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
@@ -98,7 +98,7 @@ namespace pimoroni {
9898

9999
/* Moved from pico_unicorn.cpp
100100
v = (uint16_t)(powf((float)(n) / 255.0f, 2.2) * 16383.0f + 0.5f) */
101-
constexpr uint16_t GAMMA_14BIT[256] = {
101+
inline constexpr uint16_t GAMMA_14BIT[256] = {
102102
0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 16, 20, 23, 28, 32,
103103
37, 42, 48, 54, 61, 67, 75, 82, 90, 99, 108, 117, 127, 137, 148, 159,
104104
170, 182, 195, 207, 221, 234, 249, 263, 278, 294, 310, 326, 343, 361, 379, 397,

drivers/rgbled/rgbled.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ target_sources(rgbled INTERFACE
77
target_include_directories(rgbled INTERFACE ${CMAKE_CURRENT_LIST_DIR})
88

99
# Pull in pico libraries that we need
10-
target_link_libraries(rgbled INTERFACE pico_stdlib hardware_pwm)
10+
target_link_libraries(rgbled INTERFACE pico_stdlib hardware_pwm)

libraries/cosmic_unicorn/cosmic_unicorn.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
//
3939
// .. and back to the start
4040

41-
static uint16_t r_gamma_lut[256] = {0};
42-
static uint16_t g_gamma_lut[256] = {0};
43-
static uint16_t b_gamma_lut[256] = {0};
44-
4541
static uint32_t dma_channel;
4642
static uint32_t dma_ctrl_channel;
4743
static uint32_t audio_dma_channel;
@@ -122,19 +118,6 @@ namespace pimoroni {
122118
// Tear down the old GU instance's hardware resources
123119
partial_teardown();
124120
}
125-
126-
127-
// create 14-bit gamma luts
128-
for(uint16_t v = 0; v < 256; v++) {
129-
// gamma correct the provided 0-255 brightness value onto a
130-
// 0-65535 range for the pwm counter
131-
float r_gamma = 1.8f;
132-
r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
133-
float g_gamma = 1.8f;
134-
g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
135-
float b_gamma = 1.8f;
136-
b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
137-
}
138121

139122
// for each row:
140123
// for each bcd frame:
@@ -476,9 +459,9 @@ namespace pimoroni {
476459
g = (g * this->brightness) >> 8;
477460
b = (b * this->brightness) >> 8;
478461

479-
uint16_t gamma_r = r_gamma_lut[r];
480-
uint16_t gamma_g = g_gamma_lut[g];
481-
uint16_t gamma_b = b_gamma_lut[b];
462+
uint16_t gamma_r = GAMMA_14BIT[r];
463+
uint16_t gamma_g = GAMMA_14BIT[g];
464+
uint16_t gamma_b = GAMMA_14BIT[b];
482465

483466
// for each row:
484467
// for each bcd frame:

libraries/cosmic_unicorn/cosmic_unicorn.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "hardware/pio.h"
44
#include "pico_graphics.hpp"
5+
#include "common/pimoroni_common.hpp"
56
#include "../pico_synth/pico_synth.hpp"
67

78
namespace pimoroni {

libraries/galactic_unicorn/galactic_unicorn.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
//
3939
// .. and back to the start
4040

41-
static uint16_t r_gamma_lut[256] = {0};
42-
static uint16_t g_gamma_lut[256] = {0};
43-
static uint16_t b_gamma_lut[256] = {0};
44-
4541
static uint32_t dma_channel;
4642
static uint32_t dma_ctrl_channel;
4743
static uint32_t audio_dma_channel;
@@ -122,19 +118,6 @@ namespace pimoroni {
122118
// Tear down the old GU instance's hardware resources
123119
partial_teardown();
124120
}
125-
126-
127-
// create 14-bit gamma luts
128-
for(uint16_t v = 0; v < 256; v++) {
129-
// gamma correct the provided 0-255 brightness value onto a
130-
// 0-65535 range for the pwm counter
131-
float r_gamma = 1.8f;
132-
r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
133-
float g_gamma = 1.8f;
134-
g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
135-
float b_gamma = 1.8f;
136-
b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
137-
}
138121

139122
// for each row:
140123
// for each bcd frame:
@@ -470,9 +453,9 @@ namespace pimoroni {
470453
g = (g * this->brightness) >> 8;
471454
b = (b * this->brightness) >> 8;
472455

473-
uint16_t gamma_r = r_gamma_lut[r];
474-
uint16_t gamma_g = g_gamma_lut[g];
475-
uint16_t gamma_b = b_gamma_lut[b];
456+
uint16_t gamma_r = GAMMA_14BIT[r];
457+
uint16_t gamma_g = GAMMA_14BIT[g];
458+
uint16_t gamma_b = GAMMA_14BIT[b];
476459

477460
// for each row:
478461
// for each bcd frame:

libraries/galactic_unicorn/galactic_unicorn.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "hardware/pio.h"
44
#include "pico_graphics.hpp"
5+
#include "common/pimoroni_common.hpp"
56
#include "../pico_synth/pico_synth.hpp"
67

78
namespace pimoroni {

libraries/stellar_unicorn/stellar_unicorn.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
//
3939
// .. and back to the start
4040

41-
static uint16_t r_gamma_lut[256] = {0};
42-
static uint16_t g_gamma_lut[256] = {0};
43-
static uint16_t b_gamma_lut[256] = {0};
44-
4541
static uint32_t dma_channel;
4642
static uint32_t dma_ctrl_channel;
4743
static uint32_t audio_dma_channel;
@@ -122,19 +118,6 @@ namespace pimoroni {
122118
// Tear down the old GU instance's hardware resources
123119
partial_teardown();
124120
}
125-
126-
127-
// create 14-bit gamma luts
128-
for(uint16_t v = 0; v < 256; v++) {
129-
// gamma correct the provided 0-255 brightness value onto a
130-
// 0-65535 range for the pwm counter
131-
float r_gamma = 1.8f;
132-
r_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, r_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
133-
float g_gamma = 1.8f;
134-
g_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, g_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
135-
float b_gamma = 1.8f;
136-
b_gamma_lut[v] = (uint16_t)(powf((float)(v) / 255.0f, b_gamma) * (float(1U << (BCD_FRAME_COUNT)) - 1.0f) + 0.5f);
137-
}
138121

139122
// for each row:
140123
// for each bcd frame:
@@ -467,9 +450,9 @@ namespace pimoroni {
467450
g = (g * this->brightness) >> 8;
468451
b = (b * this->brightness) >> 8;
469452

470-
uint16_t gamma_r = r_gamma_lut[r];
471-
uint16_t gamma_g = g_gamma_lut[g];
472-
uint16_t gamma_b = b_gamma_lut[b];
453+
uint16_t gamma_r = GAMMA_14BIT[r];
454+
uint16_t gamma_g = GAMMA_14BIT[g];
455+
uint16_t gamma_b = GAMMA_14BIT[b];
473456

474457
// for each row:
475458
// for each bcd frame:

libraries/stellar_unicorn/stellar_unicorn.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "hardware/pio.h"
44
#include "pico_graphics.hpp"
5+
#include "common/pimoroni_common.hpp"
56
#include "../pico_synth/pico_synth.hpp"
67

78
namespace pimoroni {

0 commit comments

Comments
 (0)