Browse Source

Double buffer the LED colors and cleanup code

pull/22707/head
Hayley Hughes 4 months ago
parent
commit
7860778e41
No known key found for this signature in database GPG Key ID: D500C1F8FAAF6818
2 changed files with 75 additions and 152 deletions
  1. +1
    -1
      keyboards/ducky/one2sf/1967st/config.h
  2. +74
    -151
      keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c

+ 1
- 1
keyboards/ducky/one2sf/1967st/config.h View File

@ -24,6 +24,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define RGB_MATRIX_LED_COUNT 69
#define ENABLE_RGB_MATRIX_BREATHING
#define ENABLE_RGB_MATRIX_RAINBOW_BEACON
#define ENABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
#define ENABLE_RGB_MATRIX_TYPING_HEATMAP
>>>>>>> 41dc4aefcf (Move led driver into the keyboard)

+ 74
- 151
keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c View File

@ -20,65 +20,43 @@
* https://pc-clinic.bg/wp-content/uploads/2021/05/mbia045-datasheet-va.00-en.pdf
*/
#include <string.h>
#ifdef RGB_MATRIX_ENABLE
#include "config.h"
#include "hal.h"
#include "progmem.h"
#include "rgb_matrix.h"
# include <string.h>
#define MBIA045_CFGREG_DEFAULT 0b1000010000000000u
# include "config.h"
# include "hal.h"
# include "progmem.h"
# include "rgb_matrix.h"
#define MBIA045_DCLK PD4
#define MBIA045_LE PD3
#define MBIA045_EN PD5
# define MBIA045_CFGREG_DEFAULT 0b1000010000000000u
#define MBIA045_ROW_COUNT 5
# define MBIA045_DCLK PD4
# define MBIA045_LE PD3
# define MBIA045_EN PD5
# define MBIA045_ROW_COUNT 5
typedef struct mbi_led {
uint8_t row;
uint8_t col;
} __attribute__((packed)) mbi_led;
typedef struct mbi_color {
uint8_t r;
uint8_t g;
uint8_t b;
} __attribute__((packed)) mbi_color;
// Map physical layout to QMK led index
const mbi_led g_mbi_leds[RGB_MATRIX_LED_COUNT] = {
{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 }, { 0, 10 }, { 0, 11 }, { 0, 12 }, { 0, 13 }, { 0, 14 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 1, 8 }, { 1, 9 }, { 1, 10 }, { 1, 11 }, { 1, 12 }, { 1, 13 }, { 1, 14 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 }, { 2, 9 }, { 2, 10 }, { 2, 11 }, { 2, 13 }, { 2, 14 },
{ 3, 0 }, { 3, 2 }, { 3, 3 }, { 3, 4 }, { 3, 5 }, { 3, 6 }, { 3, 7 }, { 3, 8 }, { 3, 9 }, { 3, 10 }, { 3, 11 }, { 3, 12 }, { 3, 13 },
{ 4, 0 }, { 4, 1 }, { 4, 2 }, { 4, 3 }, { 4, 5 }, { 4, 7 }, { 4, 9 }, { 4, 10 }, { 4, 11 }, { 4, 12 }, { 4, 13 }, { 4, 14 }
};
const mbi_led g_mbi_leds[RGB_MATRIX_LED_COUNT] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0, 8}, {0, 9}, {0, 10}, {0, 11}, {0, 12}, {0, 13}, {0, 14}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {1, 10}, {1, 11}, {1, 12}, {1, 13}, {1, 14}, {2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, {2, 10}, {2, 11}, {2, 13}, {2, 14}, {3, 0}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, {3, 10}, {3, 11}, {3, 12}, {3, 13}, {4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 9}, {4, 10}, {4, 11}, {4, 12}, {4, 13}, {4, 14}};
// Lightness curve using the CIE 1931 lightness formula
// Generated by the python script provided in http://jared.geek.nz/2013/feb/linear-led-pwm
const uint16_t CIE1931_16_CURVE[] PROGMEM = {
0, 7, 14, 21, 28, 36, 43, 50, 57, 64,
71, 78, 85, 93, 100, 107, 114, 121, 128, 135,
142, 149, 157, 164, 172, 180, 189, 197, 206, 215,
225, 234, 244, 254, 265, 276, 287, 298, 310, 322,
334, 346, 359, 373, 386, 400, 414, 428, 443, 458,
474, 490, 506, 522, 539, 557, 574, 592, 610, 629,
648, 668, 688, 708, 729, 750, 771, 793, 815, 838,
861, 885, 909, 933, 958, 983, 1009, 1035, 1061, 1088,
1116, 1144, 1172, 1201, 1230, 1260, 1290, 1321, 1353, 1384,
1417, 1449, 1482, 1516, 1550, 1585, 1621, 1656, 1693, 1729,
1767, 1805, 1843, 1882, 1922, 1962, 2003, 2044, 2085, 2128,
2171, 2214, 2258, 2303, 2348, 2394, 2440, 2487, 2535, 2583,
2632, 2681, 2731, 2782, 2833, 2885, 2938, 2991, 3045, 3099,
3154, 3210, 3266, 3323, 3381, 3439, 3498, 3558, 3618, 3679,
3741, 3803, 3867, 3930, 3995, 4060, 4126, 4193, 4260, 4328,
4397, 4466, 4536, 4607, 4679, 4752, 4825, 4899, 4973, 5049,
5125, 5202, 5280, 5358, 5437, 5517, 5598, 5680, 5762, 5845,
5929, 6014, 6100, 6186, 6273, 6361, 6450, 6540, 6630, 6722,
6814, 6907, 7001, 7095, 7191, 7287, 7385, 7483, 7582, 7682,
7782, 7884, 7986, 8090, 8194, 8299, 8405, 8512, 8620, 8729,
8838, 8949, 9060, 9173, 9286, 9400, 9516, 9632, 9749, 9867,
9986, 10106, 10227, 10348, 10471, 10595, 10720, 10845, 10972, 11100,
11228, 11358, 11489, 11620, 11753, 11887, 12021, 12157, 12294, 12432,
12570, 12710, 12851, 12993, 13136, 13279, 13424, 13570, 13718, 13866,
14015, 14165, 14317, 14469, 14622, 14777, 14933, 15089, 15247, 15406,
15566, 15727, 15890, 16053, 16217, 16383,
};
0, 7, 14, 21, 28, 36, 43, 50, 57, 64, 71, 78, 85, 93, 100, 107, 114, 121, 128, 135, 142, 149, 157, 164, 172, 180, 189, 197, 206, 215, 225, 234, 244, 254, 265, 276, 287, 298, 310, 322, 334, 346, 359, 373, 386, 400, 414, 428, 443, 458, 474, 490, 506, 522, 539, 557, 574, 592, 610, 629, 648, 668, 688, 708, 729, 750, 771, 793, 815, 838, 861, 885, 909, 933, 958, 983, 1009, 1035, 1061, 1088, 1116, 1144, 1172, 1201, 1230, 1260, 1290, 1321, 1353, 1384, 1417, 1449, 1482, 1516, 1550, 1585, 1621, 1656, 1693, 1729, 1767, 1805, 1843, 1882, 1922, 1962, 2003, 2044, 2085, 2128, 2171, 2214, 2258, 2303, 2348, 2394, 2440, 2487, 2535, 2583, 2632, 2681, 2731, 2782, 2833, 2885, 2938, 2991,
3045, 3099, 3154, 3210, 3266, 3323, 3381, 3439, 3498, 3558, 3618, 3679, 3741, 3803, 3867, 3930, 3995, 4060, 4126, 4193, 4260, 4328, 4397, 4466, 4536, 4607, 4679, 4752, 4825, 4899, 4973, 5049, 5125, 5202, 5280, 5358, 5437, 5517, 5598, 5680, 5762, 5845, 5929, 6014, 6100, 6186, 6273, 6361, 6450, 6540, 6630, 6722, 6814, 6907, 7001, 7095, 7191, 7287, 7385, 7483, 7582, 7682, 7782, 7884, 7986, 8090, 8194, 8299, 8405, 8512, 8620, 8729, 8838, 8949, 9060, 9173, 9286, 9400, 9516, 9632, 9749, 9867, 9986, 10106, 10227, 10348, 10471, 10595, 10720, 10845, 10972, 11100, 11228, 11358, 11489, 11620, 11753, 11887, 12021, 12157, 12294, 12432, 12570, 12710, 12851, 12993, 13136, 13279, 13424, 13570, 13718, 13866, 14015, 14165, 14317, 14469, 14622, 14777, 14933, 15089, 15247, 15406, 15566, 15727, 15890, 16053, 16217, 16383,
};
/** The PWM buffers the full rows of 16 PWM registers in each MBI5042 driver
* The buffers are arranged in serial format
@ -87,7 +65,8 @@ const uint16_t CIE1931_16_CURVE[] PROGMEM = {
*
* g_pwm_buffer has the DCLK-able output for an "R" row, a "G" row, and a "B" row of PWM
*/
uint16_t g_pwm_buffer[MBIA045_ROW_COUNT][16 * 16];
uint16_t g_pwm_buffer[MBIA045_ROW_COUNT][16 * 16];
mbi_color g_led_buffer[RGB_MATRIX_LED_COUNT];
bool g_pwm_buffer_update_required = false;
uint8_t g_pwm_buffer_row = 0;
@ -98,11 +77,9 @@ void MBIA045_write_config_register(uint16_t regValue);
void MBIA045_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void MBIA045_update_pwm_buffers(void);
void MBIA045_write_pwm_buffers(void);
void MBIA045_planar_recode(int row, int column, uint8_t red, uint8_t green, uint8_t blue);
void MBIA045_flush(void);
void MBIA045_disable_rows(void);
void MBIA045_disable_row(int row);
void MBIA045_enable_row(int row);
void MBIA045_select_row(int row);
void MBIA045_init(void) {
/* Initialise all PWM arrays to zero.
@ -180,7 +157,7 @@ void MBIA045_init(void) {
MBIA045_disable_rows();
// Enable the LED controllers
PD5 = PAL_LOW;
MBIA045_EN = PAL_LOW;
MBIA045_set_current_gain(0b000011u);
}
@ -273,42 +250,16 @@ void MBIA045_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
/*
* @brief Pick a colour! Any colour!
*/
// led_config_t led;
mbi_led led_pos;
if (index >= 0 && index < RGB_MATRIX_LED_COUNT) {
// Convert index into row/column
led_pos = g_mbi_leds[index];
// MBIA045_planar_recode(led.matrix_co.row, 15 - (led.matrix_co.col), red, green, blue);
/* if (index == 27 && IS_HOST_LED_ON(USB_LED_CAPS_LOCK)) { */
/* MBIA045_planar_recode(led_pos.row, led_pos.col, 0xff, 0xff, 0xff); */
/* } else { */
MBIA045_planar_recode(led_pos.row, led_pos.col, red, green, blue);
/* } */
g_pwm_buffer_update_required = true;
}
g_led_buffer[index] = (mbi_color){red, green, blue};
}
void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
/*
* brief Set every led to the provided colour
*/
for (int i = 0; i < MBIA045_ROW_COUNT; i++) {
for (int j = 0; j < 16; j++) {
if (i == 2) {
if (j == 0) {
/* } */
}
} else {
MBIA045_planar_recode(i, j, red, green, blue);
}
}
for (size_t i = 0; i < RGB_MATRIX_LED_COUNT; i++) {
g_led_buffer[i] = (mbi_color){red, green, blue};
}
g_pwm_buffer_update_required = true;
}
void MBIA045_update_pwm_buffers(void) {
@ -340,6 +291,8 @@ void MBIA045_update_pwm_buffers(void) {
* Select new row (row meant for above data)
*/
if (!g_pwm_buffer_update_required) return;
uint32_t b_mask;
/* Set Mask for GPIOB */
@ -364,7 +317,7 @@ void MBIA045_update_pwm_buffers(void) {
/* Cycle DCLK */
MBIA045_DCLK = PAL_HIGH;
MBIA045_DCLK = PAL_LOW;
} // Inner Loop 16
} // Inner Loop 16
// LE Low
MBIA045_LE = PAL_LOW;
@ -388,9 +341,6 @@ void MBIA045_update_pwm_buffers(void) {
// Reset Masks
PB->DMASK = b_mask;
// Disable current row
MBIA045_disable_rows();
// Reset PWM count
// 3 DCLK cycles
for (int i = 0; i < 3; i++) {
@ -411,7 +361,7 @@ void MBIA045_update_pwm_buffers(void) {
MBIA045_LE = PAL_LOW;
// Set new row
MBIA045_enable_row(g_pwm_buffer_row);
MBIA045_select_row(g_pwm_buffer_row);
// increment row count + check
g_pwm_buffer_row++;
@ -421,47 +371,41 @@ void MBIA045_update_pwm_buffers(void) {
}
/**
* @brief Write is a zero-output routine to handle the FLUSH from the RGB LED driver calls
* @details Since the RGB data is recoded every time a colour is changed (by the relevant
* single or "all" set_color routines), there is no point at which a mass flush of RGB
* information is needed. The MBIA045 needs to be fed 16 sets of R, G, or B information
* for each row on a totally different schedule from the animations that affect the colours.
*/
void MBIA045_write_pwm_buffers(void) {}
/**
* @brief Bitwise reorder of RGB information.
* @details Recode the 8-bit standard RGB to 16-bit separated values and
* turn the 16-bit "chunky" values into 16 sequential bitwise "planes"
* @brief Send the color buffer to the PWM buffer
*/
void MBIA045_planar_recode(int row, int column, uint8_t red, uint8_t green, uint8_t blue) {
uint16_t cur_r = pgm_read_word(&CIE1931_16_CURVE[red]);
uint16_t cur_g = pgm_read_word(&CIE1931_16_CURVE[green]);
uint16_t cur_b = pgm_read_word(&CIE1931_16_CURVE[blue]);
// int row, column;
for (int i = 0; i < 16; i++) {
uint16_t tmp_r = cur_r;
uint16_t tmp_g = cur_g;
uint16_t tmp_b = cur_b;
// g_pwm_buffer[row][0][i * column] = ;
g_pwm_buffer[row][i + (column * 16)] = ((0x1u & (tmp_r >> 15)) << 14) | ((0x1u & (tmp_g >> 15)) << 13) | ((0x1u & (tmp_b >> 15)) << 12);
cur_r <<= 1;
cur_g <<= 1;
cur_b <<= 1;
void MBIA045_flush(void) {
mbi_led led_pos;
mbi_color color;
for (size_t i = 0; i < RGB_MATRIX_LED_COUNT; i++) {
led_pos = g_mbi_leds[i];
color = g_led_buffer[i];
// Convert color values from 8 to 16 bit
uint16_t cur_r = pgm_read_word(&CIE1931_16_CURVE[color.r]);
uint16_t cur_g = pgm_read_word(&CIE1931_16_CURVE[color.g]);
uint16_t cur_b = pgm_read_word(&CIE1931_16_CURVE[color.b]);
// Construct the pwm buffer
for (int i = 0; i < 16; i++) {
uint16_t tmp_r = cur_r;
uint16_t tmp_g = cur_g;
uint16_t tmp_b = cur_b;
g_pwm_buffer[led_pos.row][i + (led_pos.col * 16)] = ((0x1u & (tmp_r >> 15)) << 14) | ((0x1u & (tmp_g >> 15)) << 13) | ((0x1u & (tmp_b >> 15)) << 12);
cur_r <<= 1;
cur_g <<= 1;
cur_b <<= 1;
}
}
g_pwm_buffer_update_required = true;
}
/**
* @brief Disable all LED Rows
*/
void MBIA045_disable_rows(void) {
// Quick and dirty hardcoded row clear
// 5 rows total
// TODO: Create enum that can be configured for individual MCUs
// Row 0
PC4 = PAL_LOW;
@ -479,46 +423,24 @@ void MBIA045_disable_rows(void) {
}
/**
* @brief Disable specific LED row
* @brief Enable the specific LED row, disabling all others
*/
void MBIA045_disable_row(int row) {
switch (row) {
case 0: // Row 0
PC4 = PAL_LOW;
break;
case 1: // Row 1
PC5 = PAL_LOW;
break;
case 2: // Row 2
PB3 = PAL_LOW;
break;
case 3: // Row 3
PB2 = PAL_LOW;
break;
case 4: // Row 4
PD9 = PAL_LOW;
break;
}
}
/**
* @brief Enable specific LED row
*/
void MBIA045_enable_row(int row) {
void MBIA045_select_row(int row) {
MBIA045_disable_rows();
switch (row) {
case 0: // Row 0
case 0: // Row 0
PC4 = PAL_HIGH;
break;
case 1: // Row 1
case 1: // Row 1
PC5 = PAL_HIGH;
break;
case 2: // Row 2
case 2: // Row 2
PB3 = PAL_HIGH;
break;
case 3: // Row 3
case 3: // Row 3
PB2 = PAL_HIGH;
break;
case 4: // Row 4
case 4: // Row 4
PD9 = PAL_HIGH;
break;
}
@ -531,7 +453,6 @@ OSAL_IRQ_HANDLER(NUC123_PWMA_HANDLER) {
if ((PWMA->PIIR >> PWM_PIIR_PWMIF1_Pos) & 1) {
/* Clear interrupt flag */
PWMA->PIIR |= 1 << PWM_PIIR_PWMIF1_Pos;
MBIA045_update_pwm_buffers();
}
@ -539,8 +460,10 @@ OSAL_IRQ_HANDLER(NUC123_PWMA_HANDLER) {
}
const rgb_matrix_driver_t rgb_matrix_driver = {
.init = MBIA045_init,
.flush = MBIA045_write_pwm_buffers,
.set_color = MBIA045_set_color,
.init = MBIA045_init,
.flush = MBIA045_flush,
.set_color = MBIA045_set_color,
.set_color_all = MBIA045_set_color_all,
};
#endif

Loading…
Cancel
Save