summaryrefslogtreecommitdiffstats
path: root/keyboards/system76
diff options
context:
space:
mode:
authorlokher <lokher@gmail.com>2022-12-06 17:04:10 +0800
committerlokher <lokher@gmail.com>2022-12-06 17:04:10 +0800
commit27fc28fd2ff52e079a5bc58d6aaea4c752420615 (patch)
tree7ac943fb1ba4f430a7220efd18f66f6a77205c30 /keyboards/system76
parente736133392fe6427cfb995da0787337189828272 (diff)
parent2709b6ed616f8012ff4cfd3ee69a822a8d188351 (diff)
Merge upstream master
Diffstat (limited to 'keyboards/system76')
-rw-r--r--keyboards/system76/launch_1/config.h111
-rw-r--r--keyboards/system76/launch_1/rgb_matrix_kb.inc157
-rw-r--r--keyboards/system76/system76_ec.c420
3 files changed, 688 insertions, 0 deletions
diff --git a/keyboards/system76/launch_1/config.h b/keyboards/system76/launch_1/config.h
new file mode 100644
index 0000000000..0431b9ab17
--- /dev/null
+++ b/keyboards/system76/launch_1/config.h
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2021 System76
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "config_common.h"
+
+// Key matrix size
+#define MATRIX_ROWS 6
+#define MATRIX_COLS 14
+
+/*
+ * Key matrix pins
+ * ROWS: AVR pins used for rows, top to bottom
+ * COLS: AVR pins used for columns, left to right
+ */
+#define MATRIX_ROW_PINS { F0, F1, F4, F5, F6, F7 }
+#define MATRIX_COL_PINS { D7, C7, C6, B6, B5, B4, D6, D4, E6, D5, D3, D2, B7, B0 }
+
+/*
+ * Diode Direction
+ * COL2ROW = COL => Anode (+), ROW => Cathode (-)
+ * ROW2COL = ROW => Anode (+), COL => Cathode (-)
+ */
+#define DIODE_DIRECTION COL2ROW
+
+// Set 0 if debouncing isn't needed
+#define DEBOUNCE 5
+
+#ifdef RGB_MATRIX_ENABLE
+# define RGB_DI_PIN E2
+# define RGB_MATRIX_LED_COUNT 84
+# define RGB_MATRIX_KEYPRESSES // Reacts to keypresses
+// # define RGB_MATRIX_KEYRELEASES // Reacts to keyreleases (instead of keypresses)
+// # define RGB_MATRIX_FRAMEBUFFER_EFFECTS // Enables framebuffer effects
+# define RGB_DISABLE_WHEN_USB_SUSPENDED // Turns off effects when suspended
+// Limit brightness to support USB-A at 0.5 A
+// TODO: Do this dynamically based on power source
+# define RGB_MATRIX_MAXIMUM_BRIGHTNESS 176 // Limits maximum brightness of LEDs to 176 out of 255. If not defined, maximum brightness is set to 255
+# define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_RAINBOW_MOVING_CHEVRON // Sets the default mode, if none has been set
+# define RGB_MATRIX_DEFAULT_HUE 142 // Sets the default hue value, if none has been set
+# define RGB_MATRIX_DEFAULT_SAT 255 // Sets the default saturation value, if none has been set
+# define RGB_MATRIX_DEFAULT_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS // Sets the default brightness value, if none has been set
+# define RGB_MATRIX_DEFAULT_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 ENABLE_RGB_MATRIX_CYCLE_ALL
+# define ENABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
+# define ENABLE_RGB_MATRIX_CYCLE_UP_DOWN
+# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN
+# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN_DUAL
+# define ENABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
+# define ENABLE_RGB_MATRIX_CYCLE_PINWHEEL
+# define ENABLE_RGB_MATRIX_CYCLE_SPIRAL
+# define ENABLE_RGB_MATRIX_RAINDROPS
+# define ENABLE_RGB_MATRIX_SPLASH
+# define ENABLE_RGB_MATRIX_MULTISPLASH
+#endif // RGB_MATRIX_ENABLE
+
+// Mechanical locking support; use KC_LCAP, KC_LNUM, or KC_LSCR instead in keymap
+#define LOCKING_SUPPORT_ENABLE
+
+// Locking resynchronize hack
+#define LOCKING_RESYNC_ENABLE
+
+// I2C {
+#define F_SCL 100000UL // Run I2C bus at 100 kHz
+#define I2C_START_RETRY_COUNT 20
+#define I2C_TIMEOUT 100 // milliseconds
+// } I2C
+
+// EEPROM {
+#define EEPROM_SIZE 1024
+// TODO: Refactor with new user EEPROM code (coming soon)
+#define EEPROM_MAGIC 0x76EC
+#define EEPROM_MAGIC_ADDR 64
+// Bump this every time we change what we store
+// This will automatically reset the EEPROM with defaults
+// and avoid loading invalid data from the EEPROM
+#define EEPROM_VERSION 0x02
+#define EEPROM_VERSION_ADDR (EEPROM_MAGIC_ADDR + 2)
+// } EEPROM
+
+// Dynamic keymap {
+#define DYNAMIC_KEYMAP_LAYER_COUNT 4
+#define DYNAMIC_KEYMAP_MACRO_COUNT 0
+// Dynamic keymap starts after EEPROM version
+#define DYNAMIC_KEYMAP_EEPROM_ADDR (EEPROM_VERSION_ADDR + 1)
+// Dynamic macro starts after dynamic keymaps, it is disabled
+#define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
+#define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE 0
+// } Dynamic keymap
+
+// System76 EC {
+#define SYSTEM76_EC_EEPROM_ADDR (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE)
+#define SYSTEM76_EC_EEPROM_SIZE (EEPROM_SIZE - SYSTEM76_EC_EEPROM_ADDR)
+// } System76 EC
diff --git a/keyboards/system76/launch_1/rgb_matrix_kb.inc b/keyboards/system76/launch_1/rgb_matrix_kb.inc
new file mode 100644
index 0000000000..02de10ed50
--- /dev/null
+++ b/keyboards/system76/launch_1/rgb_matrix_kb.inc
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2021 System76
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+RGB_MATRIX_EFFECT(active_keys)
+RGB_MATRIX_EFFECT(raw_rgb)
+RGB_MATRIX_EFFECT(unlocked)
+
+#ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
+
+#include "dynamic_keymap.h"
+
+static bool active_keys_initialized = false;
+static uint8_t active_keys_table[RGB_MATRIX_LED_COUNT] = {0};
+
+static void active_keys_initialize(void) {
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ for (uint8_t col = 0; col < MATRIX_COLS; col++) {
+ uint8_t led = g_led_config.matrix_co[row][col];
+ if (led < RGB_MATRIX_LED_COUNT && row < 16 && col < 16) {
+ active_keys_table[led] = (row << 4) | col;
+ }
+ }
+ }
+ active_keys_initialized = true;
+}
+
+static bool active_keys(effect_params_t* params) {
+ if (!active_keys_initialized) {
+ active_keys_initialize();
+ }
+
+ RGB_MATRIX_USE_LIMITS(led_min, led_max);
+ uint8_t layer = get_highest_layer(layer_state);
+ RGB rgb = hsv_to_rgb(rgb_matrix_config.hsv);
+
+ for (uint8_t i = led_min; i < led_max; i++) {
+ RGB_MATRIX_TEST_LED_FLAGS();
+
+ uint8_t rowcol = active_keys_table[i];
+ uint8_t row = rowcol >> 4;
+ uint8_t col = rowcol & 0xF;
+ uint16_t keycode = dynamic_keymap_get_keycode(layer, row, col);
+ switch (keycode) {
+ case KC_NO:
+ case KC_TRNS:
+ rgb_matrix_set_color(i, 0, 0, 0);
+ break;
+ default:
+ rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
+ break;
+ }
+ }
+
+ return led_max < RGB_MATRIX_LED_COUNT;
+}
+
+RGB raw_rgb_data[RGB_MATRIX_LED_COUNT] = {0};
+
+static uint8_t normalize_component(uint8_t component) {
+ uint16_t x = (uint16_t)component;
+ x *= rgb_matrix_config.hsv.v; // Multiply by current brightness
+ x /= 255; // Divide by maximum brightness
+ return (uint8_t)x;
+}
+
+static RGB normalize_index(uint8_t i) {
+ RGB raw = raw_rgb_data[i];
+ RGB rgb = {
+ .r = normalize_component(raw.r),
+ .g = normalize_component(raw.g),
+ .b = normalize_component(raw.b),
+ };
+ return rgb;
+}
+
+static bool raw_rgb(effect_params_t* params) {
+ RGB_MATRIX_USE_LIMITS(led_min, led_max);
+ for (uint8_t i = led_min; i < led_max; i++) {
+ RGB_MATRIX_TEST_LED_FLAGS();
+ RGB rgb = normalize_index(i);
+ rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
+ }
+ return led_max < RGB_MATRIX_LED_COUNT;
+}
+
+static uint8_t unlocked_keys[8][2] = {
+ {2, 7}, // U
+ {4, 6}, // N
+ {3, 9}, // L
+ {2, 9}, // O
+ {4, 3}, // C
+ {3, 8}, // K
+ {2, 3}, // E
+ {3, 3}, // D
+};
+
+static uint8_t unlocked_ticks = 0;
+static uint8_t unlocked_i = 0;
+static uint8_t unlocked_leds_count = 0;
+static uint8_t unlocked_leds[2] = {0, 0};
+
+static bool unlocked(effect_params_t* params) {
+ RGB_MATRIX_USE_LIMITS(led_min, led_max);
+
+ unlocked_ticks++;
+
+ if (params->init) {
+ unlocked_ticks = 0;
+ unlocked_i = 0;
+ }
+
+ if (unlocked_ticks == 0) {
+ if (unlocked_i == 8) {
+ unlocked_leds_count = 0;
+ unlocked_i = 0;
+ } else {
+ unlocked_leds_count = rgb_matrix_map_row_column_to_led(unlocked_keys[unlocked_i][0], unlocked_keys[unlocked_i][1], unlocked_leds);
+ unlocked_i++;
+ }
+ }
+
+ for (uint8_t i = led_min; i < led_max; i++) {
+ RGB_MATRIX_TEST_LED_FLAGS();
+
+ HSV hsv = {
+ .h = i + unlocked_ticks,
+ .s = 0xFF,
+ .v = 0x70,
+ };
+ for (uint8_t j = 0; j < unlocked_leds_count; j++) {
+ if (i == unlocked_leds[j]) {
+ hsv.s = 0;
+ hsv.v = 0xFF;
+ }
+ }
+
+ RGB rgb = hsv_to_rgb(hsv);
+ rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
+ }
+ return led_max < RGB_MATRIX_LED_COUNT;
+}
+
+#endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
diff --git a/keyboards/system76/system76_ec.c b/keyboards/system76/system76_ec.c
new file mode 100644
index 0000000000..6301659072
--- /dev/null
+++ b/keyboards/system76/system76_ec.c
@@ -0,0 +1,420 @@
+/*
+ * Copyright (C) 2021 System76
+ * Copyright (C) 2021 Jimmy Cassis <KernelOops@outlook.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "dynamic_keymap.h"
+#include "raw_hid.h"
+#include "rgb_matrix.h"
+#include "version.h"
+
+enum Command {
+ CMD_PROBE = 1, // Probe for System76 EC protocol
+ CMD_BOARD = 2, // Read board string
+ CMD_VERSION = 3, // Read version string
+ CMD_RESET = 6, // Reset to bootloader
+ CMD_KEYMAP_GET = 9, // Get keyboard map index
+ CMD_KEYMAP_SET = 10, // Set keyboard map index
+ CMD_LED_GET_VALUE = 11, // Get LED value by index
+ CMD_LED_SET_VALUE = 12, // Set LED value by index
+ CMD_LED_GET_COLOR = 13, // Get LED color by index
+ CMD_LED_SET_COLOR = 14, // Set LED color by index
+ CMD_LED_GET_MODE = 15, // Get LED matrix mode and speed
+ CMD_LED_SET_MODE = 16, // Set LED matrix mode and speed
+ CMD_MATRIX_GET = 17, // Get currently pressed keys
+ CMD_LED_SAVE = 18, // Save LED settings to ROM
+ CMD_SET_NO_INPUT = 19, // Enable/disable no input mode
+};
+
+bool input_disabled = false;
+
+#define CMD_LED_INDEX_ALL 0xFF
+
+static bool keymap_get(uint8_t layer, uint8_t output, uint8_t input, uint16_t *value) {
+ if (layer < dynamic_keymap_get_layer_count()) {
+ if (output < MATRIX_ROWS) {
+ if (input < MATRIX_COLS) {
+ *value = dynamic_keymap_get_keycode(layer, output, input);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+static bool keymap_set(uint8_t layer, uint8_t output, uint8_t input, uint16_t value) {
+ if (layer < dynamic_keymap_get_layer_count()) {
+ if (output < MATRIX_ROWS) {
+ if (input < MATRIX_COLS) {
+ dynamic_keymap_set_keycode(layer, output, input, value);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+static bool bootloader_reset = false;
+static bool bootloader_unlocked = false;
+
+void system76_ec_unlock(void) {
+#ifdef RGB_MATRIX_CUSTOM_KB
+ rgb_matrix_mode_noeeprom(RGB_MATRIX_CUSTOM_unlocked);
+#endif
+#ifdef SYSTEM76_EC
+ bootloader_unlocked = true;
+#endif
+}
+
+bool system76_ec_is_unlocked(void) { return bootloader_unlocked; }
+
+#ifdef RGB_MATRIX_CUSTOM_KB
+enum Mode {
+ MODE_SOLID_COLOR = 0,
+ MODE_PER_KEY,
+ #ifndef DISABLE_RGB_MATRIX_ANIMATIONS
+ MODE_CYCLE_ALL,
+ MODE_CYCLE_LEFT_RIGHT,
+ MODE_CYCLE_UP_DOWN,
+ MODE_CYCLE_OUT_IN,
+ MODE_CYCLE_OUT_IN_DUAL,
+ MODE_RAINBOW_MOVING_CHEVRON,
+ MODE_CYCLE_PINWHEEL,
+ MODE_CYCLE_SPIRAL,
+ MODE_RAINDROPS,
+ MODE_SPLASH,
+ MODE_MULTISPLASH,
+ #endif // DISABLE_RGB_MATRIX_ANIMATIONS
+ MODE_ACTIVE_KEYS,
+ MODE_DISABLED,
+ MODE_LAST,
+};
+
+// clang-format off
+static enum rgb_matrix_effects mode_map[] = {
+ RGB_MATRIX_SOLID_COLOR,
+ RGB_MATRIX_CUSTOM_raw_rgb,
+ #ifndef DISABLE_RGB_MATRIX_ANIMATIONS
+ RGB_MATRIX_CYCLE_ALL,
+ RGB_MATRIX_CYCLE_LEFT_RIGHT,
+ RGB_MATRIX_CYCLE_UP_DOWN,
+ RGB_MATRIX_CYCLE_OUT_IN,
+ RGB_MATRIX_CYCLE_OUT_IN_DUAL,
+ RGB_MATRIX_RAINBOW_MOVING_CHEVRON,
+ RGB_MATRIX_CYCLE_PINWHEEL,
+ RGB_MATRIX_CYCLE_SPIRAL,
+ RGB_MATRIX_RAINDROPS,
+ RGB_MATRIX_SPLASH,
+ RGB_MATRIX_MULTISPLASH,
+ #endif // DISABLE_RGB_MATRIX_ANIMATIONS
+ RGB_MATRIX_CUSTOM_active_keys,
+ RGB_MATRIX_NONE,
+};
+// clang-format on
+
+_Static_assert(sizeof(mode_map) == MODE_LAST, "mode_map_length");
+
+RGB raw_rgb_data[RGB_MATRIX_LED_COUNT];
+
+// clang-format off
+rgb_config_t layer_rgb[DYNAMIC_KEYMAP_LAYER_COUNT] = {
+ // Layer 0
+ {
+ .enable = 1,
+ .mode = RGB_MATRIX_DEFAULT_MODE,
+ .hsv = {
+ .h = RGB_MATRIX_DEFAULT_HUE,
+ .s = RGB_MATRIX_DEFAULT_SAT,
+ .v = RGB_MATRIX_DEFAULT_VAL,
+ },
+ .speed = RGB_MATRIX_DEFAULT_SPD,
+ .flags = LED_FLAG_KEYLIGHT,
+ },
+ // Layer 1
+ {
+ .enable = 1,
+ .mode = RGB_MATRIX_CUSTOM_active_keys,
+ .hsv = {
+ .h = RGB_MATRIX_DEFAULT_HUE,
+ .s = RGB_MATRIX_DEFAULT_SAT,
+ .v = RGB_MATRIX_DEFAULT_VAL,
+ },
+ .speed = RGB_MATRIX_DEFAULT_SPD,
+ .flags = LED_FLAG_KEYLIGHT,
+ },
+ // Layer 2
+ {
+ .enable = 1,
+ .mode = RGB_MATRIX_CUSTOM_active_keys,
+ .hsv = {
+ .h = RGB_MATRIX_DEFAULT_HUE,
+ .s = RGB_MATRIX_DEFAULT_SAT,
+ .v = RGB_MATRIX_DEFAULT_VAL,
+ },
+ .speed = RGB_MATRIX_DEFAULT_SPD,
+ .flags = LED_FLAG_KEYLIGHT,
+ },
+ // Layer 3
+ {
+ .enable = 1,
+ .mode = RGB_MATRIX_CUSTOM_active_keys,
+ .hsv = {
+ .h = RGB_MATRIX_DEFAULT_HUE,
+ .s = RGB_MATRIX_DEFAULT_SAT,
+ .v = RGB_MATRIX_DEFAULT_VAL,
+ },
+ .speed = RGB_MATRIX_DEFAULT_SPD,
+ .flags = LED_FLAG_KEYLIGHT,
+ },
+};
+// clang-format on
+
+// Read or write EEPROM data with checks for being inside System76 EC region.
+static bool system76_ec_eeprom_op(void *buf, uint16_t size, uint16_t offset, bool write) {
+ uint16_t addr = SYSTEM76_EC_EEPROM_ADDR + offset;
+ uint16_t end = addr + size;
+ // Check for overflow and zero size
+ if ((end > addr) && (addr >= SYSTEM76_EC_EEPROM_ADDR) && (end <= (SYSTEM76_EC_EEPROM_ADDR + SYSTEM76_EC_EEPROM_SIZE))) {
+ if (write) {
+ eeprom_update_block((const void *)buf, (void *)addr, size);
+ } else {
+ eeprom_read_block((void *)buf, (const void *)addr, size);
+ }
+ return true;
+ } else {
+ return false;
+ }
+}
+
+// Read or write EEPROM RGB parameters.
+void system76_ec_rgb_eeprom(bool write) {
+ uint16_t layer_rgb_size = sizeof(layer_rgb);
+ system76_ec_eeprom_op((void *)layer_rgb, layer_rgb_size, 0, write);
+ system76_ec_eeprom_op((void *)raw_rgb_data, sizeof(raw_rgb_data), layer_rgb_size, write);
+}
+
+// Update RGB parameters on layer change.
+void system76_ec_rgb_layer(layer_state_t layer_state) {
+ if (!bootloader_unlocked) {
+ uint8_t layer = get_highest_layer(layer_state);
+ if (layer < DYNAMIC_KEYMAP_LAYER_COUNT) {
+ rgb_matrix_config = layer_rgb[layer];
+ }
+ }
+}
+#endif // RGB_MATRIX_CUSTOM_KB
+
+void raw_hid_receive(uint8_t *data, uint8_t length) {
+ // Error response by default, set to success by commands
+ data[1] = 1;
+
+ switch (data[0]) {
+ case CMD_PROBE:
+ // Signature
+ data[2] = 0x76;
+ data[3] = 0xEC;
+ // Version
+ data[4] = 0x01;
+ data[1] = 0;
+ break;
+ case CMD_BOARD:
+ strncpy((char *)&data[2], QMK_KEYBOARD, length - 2);
+ data[1] = 0;
+ break;
+ case CMD_VERSION:
+ strncpy((char *)&data[2], QMK_VERSION, length - 2);
+ data[1] = 0;
+ break;
+ case CMD_RESET:
+ if (bootloader_unlocked) {
+ data[1] = 0;
+ bootloader_reset = true;
+ }
+ break;
+ case CMD_KEYMAP_GET: {
+ uint16_t value = 0;
+ if (keymap_get(data[2], data[3], data[4], &value)) {
+ data[5] = (uint8_t)value;
+ data[6] = (uint8_t)(value >> 8);
+ data[1] = 0;
+ }
+ } break;
+ case CMD_KEYMAP_SET: {
+ uint16_t value = ((uint16_t)data[5]) | (((uint16_t)data[6]) << 8);
+ if (keymap_set(data[2], data[3], data[4], value)) {
+ data[1] = 0;
+ }
+ } break;
+#ifdef RGB_MATRIX_CUSTOM_KB
+ case CMD_LED_GET_VALUE:
+ if (!bootloader_unlocked) {
+ uint8_t index = data[2];
+ for (uint8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
+ if (index == (0xF0 | layer)) {
+ data[3] = layer_rgb[layer].hsv.v;
+ data[4] = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
+ data[1] = 0;
+ break;
+ }
+ }
+ }
+ break;
+ case CMD_LED_SET_VALUE:
+ if (!bootloader_unlocked) {
+ uint8_t index = data[2];
+ uint8_t value = data[3];
+ if (value >= RGB_MATRIX_MAXIMUM_BRIGHTNESS) {
+ value = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
+ }
+ for (uint8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
+ if (index == (0xF0 | layer)) {
+ layer_rgb[layer].hsv.v = value;
+ data[1] = 0;
+ system76_ec_rgb_layer(layer_state);
+ break;
+ }
+ }
+ }
+ break;
+ case CMD_LED_GET_COLOR:
+ if (!bootloader_unlocked) {
+ uint8_t index = data[2];
+ if (index < RGB_MATRIX_LED_COUNT) {
+ data[3] = raw_rgb_data[index].r;
+ data[4] = raw_rgb_data[index].g;
+ data[5] = raw_rgb_data[index].b;
+ data[1] = 0;
+ } else {
+ for (uint8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
+ if (index == (0xF0 | layer)) {
+ data[3] = layer_rgb[layer].hsv.h;
+ data[4] = layer_rgb[layer].hsv.s;
+ data[5] = 0;
+ data[1] = 0;
+ break;
+ }
+ }
+ }
+ }
+ break;
+ case CMD_LED_SET_COLOR:
+ if (!bootloader_unlocked) {
+ uint8_t index = data[2];
+
+ RGB rgb = {
+ .r = data[3],
+ .g = data[4],
+ .b = data[5],
+ };
+
+ if (index < RGB_MATRIX_LED_COUNT) {
+ raw_rgb_data[index] = rgb;
+ data[1] = 0;
+ } else {
+ for (uint8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
+ if (index == (0xF0 | layer)) {
+ layer_rgb[layer].hsv.h = rgb.r;
+ layer_rgb[layer].hsv.s = rgb.g;
+ // Ignore rgb.b
+ data[1] = 0;
+ system76_ec_rgb_layer(layer_state);
+ break;
+ }
+ }
+ }
+ }
+ break;
+ case CMD_LED_GET_MODE:
+ if (!bootloader_unlocked) {
+ uint8_t layer = data[2];
+ if (layer < DYNAMIC_KEYMAP_LAYER_COUNT) {
+ enum rgb_matrix_effects mode = layer_rgb[layer].mode;
+ for (uint8_t i = 0; i < MODE_LAST; i++) {
+ if (mode_map[i] == mode) {
+ data[3] = i;
+ data[4] = layer_rgb[layer].speed;
+ data[1] = 0;
+ break;
+ }
+ }
+ }
+ }
+ break;
+ case CMD_LED_SET_MODE:
+ if (!bootloader_unlocked) {
+ uint8_t layer = data[2];
+ uint8_t mode = data[3];
+ uint8_t speed = data[4];
+ if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && mode < MODE_LAST) {
+ layer_rgb[layer].mode = mode_map[mode];
+ layer_rgb[layer].speed = speed;
+ data[1] = 0;
+ system76_ec_rgb_layer(layer_state);
+ }
+ }
+ break;
+ case CMD_LED_SAVE:
+ if (!bootloader_unlocked) {
+ system76_ec_rgb_eeprom(true);
+ data[1] = 0;
+ }
+ break;
+#endif // RGB_MATRIX_CUSTOM_KB
+ case CMD_MATRIX_GET: {
+ // TODO: Improve performance?
+ data[2] = matrix_rows();
+ data[3] = matrix_cols();
+
+ uint8_t byte = 4;
+ uint8_t bit = 0;
+
+ for (uint8_t row = 0; row < matrix_rows(); row++) {
+ for (uint8_t col = 0; col < matrix_cols(); col++) {
+ if (byte < length) {
+ if (matrix_is_on(row, col)) {
+ data[byte] |= (1 << bit);
+ } else {
+ data[byte] &= ~(1 << bit);
+ }
+ }
+
+ bit++;
+ if (bit >= 8) {
+ byte++;
+ bit = 0;
+ }
+ }
+ }
+ data[1] = 0;
+ } break;
+ case CMD_SET_NO_INPUT: {
+ clear_keyboard();
+ input_disabled = data[2] != 0;
+ data[1] = 0;
+ } break;
+ }
+
+ raw_hid_send(data, length);
+
+ if (bootloader_reset) {
+ // Give host time to read response
+ wait_ms(100);
+ // Jump to the bootloader
+ bootloader_jump();
+ }
+}