Skip to content

Commit

Permalink
Split RGB Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
XScorpion2 committed Dec 3, 2020
1 parent d5c15b0 commit bf43328
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 54 deletions.
2 changes: 2 additions & 0 deletions docs/feature_rgb_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ These are defined in [`rgblight_list.h`](https://github.com/qmk/qmk_firmware/blo
#define RGB_MATRIX_STARTUP_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS // Sets the default brightness value, if none has been set
#define RGB_MATRIX_STARTUP_SPD 127 // Sets the default animation speed, if none has been set
#define RGB_MATRIX_DISABLE_KEYCODES // disables control of rgb matrix by keycodes (must use code functions to control the feature)
#define RGB_MATRIX_SPLIT { X, Y } // (Optional) For split keyboards, the number of LEDs connected on each half. X = left, Y = Right.
// If RGB_MATRIX_KEYPRESSES or RGB_MATRIX_KEYRELEASES is enabled, you also will want to enable SPLIT_TRANSPORT_MIRROR
```

## EEPROM storage :id=eeprom-storage
Expand Down
1 change: 0 additions & 1 deletion docs/ja/understanding_qmk.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
* [`bool process_haptic(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/2cee371bf125a6ec541dd7c5a809573facc7c456/drivers/haptic/haptic.c#L216)
* [`bool process_record_kb(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/keyboards/clueboard/card/card.c#L20)
* [`bool process_record_user(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/keyboards/clueboard/card/keymaps/default/keymap.c#L58)
* [`bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/rgb_matrix.c#L139)
* [`bool process_midi(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_midi.c#L81)
* [`bool process_audio(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_audio.c#L19)
* [`bool process_steno(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_steno.c#L160)
Expand Down
1 change: 0 additions & 1 deletion docs/understanding_qmk.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ The `process_record()` function itself is deceptively simple, but hidden within
* [`bool process_haptic(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/2cee371bf125a6ec541dd7c5a809573facc7c456/drivers/haptic/haptic.c#L216)
* [`bool process_record_kb(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/keyboards/clueboard/card/card.c#L20)
* [`bool process_record_user(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/keyboards/clueboard/card/keymaps/default/keymap.c#L58)
* [`bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/rgb_matrix.c#L139)
* [`bool process_midi(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_midi.c#L81)
* [`bool process_audio(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_audio.c#L19)
* [`bool process_steno(uint16_t keycode, keyrecord_t *record)`](https://github.com/qmk/qmk_firmware/blob/e1203a222bb12ab9733916164a000ef3ac48da93/quantum/process_keycode/process_steno.c#L160)
Expand Down
10 changes: 0 additions & 10 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ bool process_record_quantum(keyrecord_t *record) {
#ifdef HAPTIC_ENABLE
process_haptic(keycode, record) &&
#endif // HAPTIC_ENABLE
#if defined(RGB_MATRIX_ENABLE)
process_rgb_matrix(keycode, record) &&
#endif
#if defined(VIA_ENABLE)
process_record_via(keycode, record) &&
#endif
Expand Down Expand Up @@ -622,9 +619,6 @@ void matrix_init_quantum() {
#ifdef AUDIO_ENABLE
audio_init();
#endif
#ifdef RGB_MATRIX_ENABLE
rgb_matrix_init();
#endif
#if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
unicode_input_mode_init();
#endif
Expand Down Expand Up @@ -659,10 +653,6 @@ void matrix_scan_quantum() {
led_matrix_task();
#endif

#ifdef RGB_MATRIX_ENABLE
rgb_matrix_task();
#endif

#ifdef WPM_ENABLE
decay_wpm();
#endif
Expand Down
54 changes: 39 additions & 15 deletions quantum/rgb_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ last_hit_t g_last_hit_tracker;
// internals
static uint8_t rgb_last_enable = UINT8_MAX;
static uint8_t rgb_last_effect = UINT8_MAX;
static effect_params_t rgb_effect_params = {0, 0xFF};
static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false};
static rgb_task_states rgb_task_state = SYNCING;
#if RGB_DISABLE_TIMEOUT > 0
static uint32_t rgb_anykey_timer;
Expand All @@ -143,6 +143,11 @@ static uint32_t rgb_timer_buffer;
static last_hit_t last_hit_buffer;
#endif // RGB_MATRIX_KEYREACTIVE_ENABLED

// split rgb matrix
#ifdef RGB_MATRIX_SPLIT
const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
#endif

void eeconfig_read_rgb_matrix(void) { eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }

void eeconfig_update_rgb_matrix(void) { eeprom_update_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
Expand All @@ -153,6 +158,7 @@ void eeconfig_update_rgb_matrix_default(void) {
rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
rgb_matrix_config.hsv = (HSV){RGB_MATRIX_STARTUP_HUE, RGB_MATRIX_STARTUP_SAT, RGB_MATRIX_STARTUP_VAL};
rgb_matrix_config.speed = RGB_MATRIX_STARTUP_SPD;
rgb_matrix_config.flags = LED_FLAG_ALL;
eeconfig_update_rgb_matrix();
}

Expand All @@ -164,6 +170,7 @@ void eeconfig_debug_rgb_matrix(void) {
dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
dprintf("rgb_matrix_config.flags = %d\n", rgb_matrix_config.flags);
}

__attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
Expand All @@ -180,28 +187,43 @@ uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *l

void rgb_matrix_update_pwm_buffers(void) { rgb_matrix_driver.flush(); }

void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color(index, red, green, blue); }
void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
#ifdef RGB_MATRIX_SPLIT
if (!is_keyboard_left() && index >= k_rgb_matrix_split[0])
rgb_matrix_driver.set_color(index - k_rgb_matrix_split[0], red, green, blue);
else if (is_keyboard_left() && index < k_rgb_matrix_split[0])
#endif
rgb_matrix_driver.set_color(index, red, green, blue);
}

void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color_all(red, green, blue); }
void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
#ifdef RGB_MATRIX_SPLIT
for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++)
rgb_matrix_set_color(i, red, green, blue);
#else
rgb_matrix_driver.set_color_all(red, green, blue);
#endif
}

bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed) {
#ifndef RGB_MATRIX_SPLIT
if (!is_keyboard_master()) return;
#endif
#if RGB_DISABLE_TIMEOUT > 0
if (record->event.pressed) {
rgb_anykey_timer = 0;
}
rgb_anykey_timer = 0;
#endif // RGB_DISABLE_TIMEOUT > 0

#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
uint8_t led[LED_HITS_TO_REMEMBER];
uint8_t led_count = 0;

# if defined(RGB_MATRIX_KEYRELEASES)
if (!record->event.pressed)
if (!pressed)
# elif defined(RGB_MATRIX_KEYPRESSES)
if (record->event.pressed)
if (pressed)
# endif // defined(RGB_MATRIX_KEYRELEASES)
{
led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
led_count = rgb_matrix_map_row_column_to_led(row, col, led);
}

if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
Expand All @@ -224,11 +246,9 @@ bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {

#if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
process_rgb_matrix_typing_heatmap(record);
process_rgb_matrix_typing_heatmap(row, col);
}
#endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)

return true;
}

void rgb_matrix_test(void) {
Expand Down Expand Up @@ -316,6 +336,10 @@ static void rgb_task_start(void) {
static void rgb_task_render(uint8_t effect) {
bool rendering = false;
rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
if (rgb_effect_params.flags != rgb_matrix_config.flags) {
rgb_effect_params.flags = rgb_matrix_config.flags;
rgb_matrix_set_color_all(0, 0, 0);
}

// each effect can opt to do calculations
// and/or request PWM buffer updates.
Expand Down Expand Up @@ -619,6 +643,6 @@ void rgb_matrix_decrease_speed_helper(bool write_to_eeprom) { rgb_matrix_set_spe
void rgb_matrix_decrease_speed_noeeprom(void) { rgb_matrix_decrease_speed_helper(false); }
void rgb_matrix_decrease_speed(void) { rgb_matrix_decrease_speed_helper(true); }

led_flags_t rgb_matrix_get_flags(void) { return rgb_effect_params.flags; }
led_flags_t rgb_matrix_get_flags(void) { return rgb_matrix_config.flags; }

void rgb_matrix_set_flags(led_flags_t flags) { rgb_effect_params.flags = flags; }
void rgb_matrix_set_flags(led_flags_t flags) { rgb_matrix_config.flags = flags; }
2 changes: 1 addition & 1 deletion quantum/rgb_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *l
void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue);

bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record);
void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed);

void rgb_matrix_task(void);

Expand Down
4 changes: 1 addition & 3 deletions quantum/rgb_matrix_animations/typing_heatmap_anim.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
RGB_MATRIX_EFFECT(TYPING_HEATMAP)
# ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS

void process_rgb_matrix_typing_heatmap(keyrecord_t* record) {
uint8_t row = record->event.key.row;
uint8_t col = record->event.key.col;
void process_rgb_matrix_typing_heatmap(uint8_t row, uint8_t col) {
uint8_t m_row = row - 1;
uint8_t p_row = row + 1;
uint8_t m_col = col - 1;
Expand Down
1 change: 1 addition & 0 deletions quantum/rgb_matrix_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typedef union {
uint8_t mode : 6;
HSV hsv;
uint8_t speed; // EECONFIG needs to be increased to support this
led_flags_t flags;
};
} rgb_config_t;

Expand Down
27 changes: 27 additions & 0 deletions quantum/split_common/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ static pin_t encoders_pad[] = ENCODERS_PAD_A;
# define NUMBER_OF_ENCODERS (sizeof(encoders_pad) / sizeof(pin_t))
#endif

#ifdef RGB_MATRIX_SPLIT
# include "rgb_matrix.h"
#endif

#if defined(USE_I2C)

# include "i2c_master.h"
Expand All @@ -45,6 +49,9 @@ typedef struct _I2C_slave_buffer_t {
# ifdef WPM_ENABLE
uint8_t current_wpm;
# endif
# ifdef RGB_MATRIX_SPLIT
rgb_config_t rgb_matrix;
# endif
} I2C_slave_buffer_t;

static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg;
Expand All @@ -56,6 +63,7 @@ static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_re
# define I2C_KEYMAP_SLAVE_START offsetof(I2C_slave_buffer_t, smatrix)
# define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state)
# define I2C_WPM_START offsetof(I2C_slave_buffer_t, current_wpm)
# define I2C_RGB_MATRIX_START offsetof(I2C_slave_buffer_t, rgb_matrix)

# define TIMEOUT 100

Expand Down Expand Up @@ -104,6 +112,10 @@ bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[])
}
# endif

# ifdef RGB_MATRIX_SPLIT
i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_MATRIX_START, (void *)rgb_matrix_config, sizeof(i2c_buffer->rgb_matrix), TIMEOUT);
# endif

# ifndef DISABLE_SYNC_TIMER
i2c_buffer->sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET;
i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_SYNC_TIME_START, (void *)&i2c_buffer->sync_timer, sizeof(i2c_buffer->sync_timer), TIMEOUT);
Expand Down Expand Up @@ -141,6 +153,10 @@ void transport_slave(matrix_row_t matrix[]) {
# ifdef WPM_ENABLE
set_current_wpm(i2c_buffer->current_wpm);
# endif

# ifdef RGB_MATRIX_SPLIT
memcpy((void*)i2c_buffer->rgb_matrix, (void *)rgb_matrix_config, sizeof(i2c_buffer->rgb_matrix));
# endif
}

void transport_master_init(void) { i2c_init(); }
Expand Down Expand Up @@ -174,6 +190,9 @@ typedef struct _Serial_m2s_buffer_t {
# ifdef WPM_ENABLE
uint8_t current_wpm;
# endif
# ifdef RGB_MATRIX_SPLIT
rgb_config_t rgb_matrix;
# endif
} Serial_m2s_buffer_t;

# if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
Expand Down Expand Up @@ -284,6 +303,10 @@ bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[])
serial_m2s_buffer.current_wpm = get_current_wpm();
# endif

# ifdef RGB_MATRIX_SPLIT
serial_m2s_buffer.rgb_matrix = rgb_matrix_config;
# endif

# ifndef DISABLE_SYNC_TIMER
serial_m2s_buffer.sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET;
# endif
Expand Down Expand Up @@ -314,6 +337,10 @@ void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[])
# ifdef WPM_ENABLE
set_current_wpm(serial_m2s_buffer.current_wpm);
# endif

# ifdef RGB_MATRIX_SPLIT
rgb_matrix_config = serial_m2s_buffer.rgb_matrix;
# endif
}

#endif
2 changes: 1 addition & 1 deletion tmk_core/common/eeconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void eeconfig_init_quantum(void) {
eeprom_update_dword(EECONFIG_HAPTIC, 0);
eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
eeprom_update_dword(EECONFIG_RGB_MATRIX, 0);
eeprom_update_byte(EECONFIG_RGB_MATRIX_SPEED, 0);
eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0);

// TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS
// within the emulated eeprom via dfu-util or another tool
Expand Down
7 changes: 4 additions & 3 deletions tmk_core/common/eeconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#define EECONFIG_HAPTIC (uint32_t *)24
#define EECONFIG_RGB_MATRIX (uint32_t *)28
#define EECONFIG_RGB_MATRIX_SPEED (uint8_t *)32
// Speed & Flags
#define EECONFIG_RGB_MATRIX_EXTENDED (uint16_t *)32
// TODO: Combine these into a single word and single block of EEPROM
#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)33
#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)34
// Size of EEPROM being used, other code can refer to this for available EEPROM
#define EECONFIG_SIZE 34
#define EECONFIG_SIZE 35
/* debug bit */
#define EECONFIG_DEBUG_ENABLE (1 << 0)
#define EECONFIG_DEBUG_MATRIX (1 << 1)
Expand Down
Loading

0 comments on commit bf43328

Please sign in to comment.