summary refs log tree commit diff
path: root/quantum
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-02-14 12:15:08 +1100
committerGitHub <noreply@github.com>2021-02-14 12:15:08 +1100
commit7ce5ba645abcbf3d859a418d4456a1cc80c73e64 (patch)
tree32de70337d61f36be88d142314a78ba6e8df6300 /quantum
parentde8caf708c1a9a80527a04be620ed3969262e50b (diff)
LED Matrix: add led_matrix_types.h and implement g_led_config (#11741)
* LED Matrix: add led_matrix_types.h and implement g_led_config

* Set correct flags for non-"modifier" LEDs

* Clean up docs a little

* Add license headers for [led,rgb]_matrix_types.h
Diffstat (limited to 'quantum')
-rw-r--r--quantum/led_matrix.c132
-rw-r--r--quantum/led_matrix.h42
-rw-r--r--quantum/led_matrix_types.h69
-rw-r--r--quantum/rgb_matrix_types.h16
4 files changed, 153 insertions, 106 deletions
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c
index eb523990a6..c3538e94df 100644
--- a/quantum/led_matrix.c
+++ b/quantum/led_matrix.c
@@ -27,7 +27,7 @@
 #include <string.h>
 #include <math.h>
 
-led_config_t led_matrix_config;
+led_eeconfig_t led_matrix_eeconfig;
 
 #ifndef MAX
 #    define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
@@ -70,40 +70,32 @@ void eeconfig_update_led_matrix(uint32_t config_value) { eeprom_update_dword(EEC
 
 void eeconfig_update_led_matrix_default(void) {
     dprintf("eeconfig_update_led_matrix_default\n");
-    led_matrix_config.enable = 1;
-    led_matrix_config.mode   = LED_MATRIX_UNIFORM_BRIGHTNESS;
-    led_matrix_config.val    = 128;
-    led_matrix_config.speed  = 0;
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.enable = 1;
+    led_matrix_eeconfig.mode   = LED_MATRIX_UNIFORM_BRIGHTNESS;
+    led_matrix_eeconfig.val    = 128;
+    led_matrix_eeconfig.speed  = 0;
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void eeconfig_debug_led_matrix(void) {
-    dprintf("led_matrix_config eeprom\n");
-    dprintf("led_matrix_config.enable = %d\n", led_matrix_config.enable);
-    dprintf("led_matrix_config.mode = %d\n", led_matrix_config.mode);
-    dprintf("led_matrix_config.val = %d\n", led_matrix_config.val);
-    dprintf("led_matrix_config.speed = %d\n", led_matrix_config.speed);
+    dprintf("led_matrix_eeconfig eeprom\n");
+    dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
+    dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
+    dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
+    dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
 }
 
-// Last led hit
-#ifndef LED_HITS_TO_REMEMBER
-#    define LED_HITS_TO_REMEMBER 8
-#endif
 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
 uint8_t g_last_led_count                     = 0;
 
-void map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
-    led_matrix led;
-    *led_count = 0;
-
-    for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) {
-        // map_index_to_led(i, &led);
-        led = g_leds[i];
-        if (row == led.matrix_co.row && column == led.matrix_co.col) {
-            led_i[*led_count] = i;
-            (*led_count)++;
-        }
+uint8_t map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
+    uint8_t led_count = 0;
+    uint8_t led_index = g_led_config.matrix_co[row][column];
+    if (led_index != NO_LED) {
+        led_i[led_count] = led_index;
+        led_count++;
     }
+    return led_count;
 }
 
 void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
@@ -114,8 +106,8 @@ void led_matrix_set_index_value_all(uint8_t value) { led_matrix_driver.set_value
 
 bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
     if (record->event.pressed) {
-        uint8_t led[8], led_count;
-        map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
+        uint8_t led[8];
+        uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
         if (led_count > 0) {
             for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
                 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
@@ -127,8 +119,8 @@ bool process_led_matrix(uint16_t keycode, keyrecord_t *record) {
         g_any_key_hit = 0;
     } else {
 #ifdef LED_MATRIX_KEYRELEASES
-        uint8_t led[8], led_count;
-        map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
+        uint8_t led[8];
+        uint8_t led_count = map_row_column_to_led(record->event.key.row, record->event.key.col, led);
         for (uint8_t i = 0; i < led_count; i++) g_key_hit[led[i]] = 255;
 
         g_any_key_hit = 255;
@@ -143,12 +135,12 @@ void led_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
 void led_matrix_all_off(void) { led_matrix_set_index_value_all(0); }
 
 // Uniform brightness
-void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_config.val); }
+void led_matrix_uniform_brightness(void) { led_matrix_set_index_value_all(LED_MATRIX_MAXIMUM_BRIGHTNESS / BACKLIGHT_LEVELS * led_matrix_eeconfig.val); }
 
 void led_matrix_custom(void) {}
 
 void led_matrix_task(void) {
-    if (!led_matrix_config.enable) {
+    if (!led_matrix_eeconfig.enable) {
         led_matrix_all_off();
         led_matrix_indicators();
         return;
@@ -170,7 +162,7 @@ void led_matrix_task(void) {
     // Ideally we would also stop sending zeros to the LED driver PWM buffers
     // while suspended and just do a software shutdown. This is a cheap hack for now.
     bool    suspend_backlight = ((g_suspend_state && LED_DISABLE_WHEN_USB_SUSPENDED) || (LED_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > LED_DISABLE_AFTER_TIMEOUT * 60 * 20));
-    uint8_t effect            = suspend_backlight ? 0 : led_matrix_config.mode;
+    uint8_t effect            = suspend_backlight ? 0 : led_matrix_eeconfig.mode;
 
     // this gets ticked at 20 Hz.
     // each effect can opt to do calculations
@@ -211,8 +203,8 @@ __attribute__((weak)) void led_matrix_indicators_user(void) {}
 //  else
 //  {
 //      // This needs updated to something like
-//      // uint8_t led[8], led_count;
-//      // map_row_column_to_led(row,column,led,&led_count);
+//      // uint8_t led[8];
+//      // uint8_t led_count = map_row_column_to_led(row, column, led);
 //      // for(uint8_t i = 0; i < led_count; i++)
 //      map_row_column_to_led(row, column, index);
 //  }
@@ -235,12 +227,12 @@ void led_matrix_init(void) {
         eeconfig_update_led_matrix_default();
     }
 
-    led_matrix_config.raw = eeconfig_read_led_matrix();
+    led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
 
-    if (!led_matrix_config.mode) {
-        dprintf("led_matrix_init_drivers led_matrix_config.mode = 0. Write default values to EEPROM.\n");
+    if (!led_matrix_eeconfig.mode) {
+        dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
         eeconfig_update_led_matrix_default();
-        led_matrix_config.raw = eeconfig_read_led_matrix();
+        led_matrix_eeconfig.raw = eeconfig_read_led_matrix();
     }
 
     eeconfig_debug_led_matrix();  // display current eeprom values
@@ -270,8 +262,8 @@ static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max)
 // }
 
 // void backlight_set_key_value(uint8_t row, uint8_t column, uint8_t value) {
-//     uint8_t led[8], led_count;
-//     map_row_column_to_led(row,column,led,&led_count);
+//     uint8_t led[8];
+//     uint8_t led_count = map_row_column_to_led(row, column, led);
 //     for(uint8_t i = 0; i < led_count; i++) {
 //         if (led[i] < LED_DRIVER_LED_COUNT) {
 //             void *address = backlight_get_custom_key_value_eeprom_address(led[i]);
@@ -283,74 +275,74 @@ static uint8_t decrement(uint8_t value, uint8_t step, uint8_t min, uint8_t max)
 uint32_t led_matrix_get_tick(void) { return g_tick; }
 
 void led_matrix_toggle(void) {
-    led_matrix_config.enable ^= 1;
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.enable ^= 1;
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void led_matrix_enable(void) {
-    led_matrix_config.enable = 1;
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.enable = 1;
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
-void led_matrix_enable_noeeprom(void) { led_matrix_config.enable = 1; }
+void led_matrix_enable_noeeprom(void) { led_matrix_eeconfig.enable = 1; }
 
 void led_matrix_disable(void) {
-    led_matrix_config.enable = 0;
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.enable = 0;
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
-void led_matrix_disable_noeeprom(void) { led_matrix_config.enable = 0; }
+void led_matrix_disable_noeeprom(void) { led_matrix_eeconfig.enable = 0; }
 
 void led_matrix_step(void) {
-    led_matrix_config.mode++;
-    if (led_matrix_config.mode >= LED_MATRIX_EFFECT_MAX) {
-        led_matrix_config.mode = 1;
+    led_matrix_eeconfig.mode++;
+    if (led_matrix_eeconfig.mode >= LED_MATRIX_EFFECT_MAX) {
+        led_matrix_eeconfig.mode = 1;
     }
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void led_matrix_step_reverse(void) {
-    led_matrix_config.mode--;
-    if (led_matrix_config.mode < 1) {
-        led_matrix_config.mode = LED_MATRIX_EFFECT_MAX - 1;
+    led_matrix_eeconfig.mode--;
+    if (led_matrix_eeconfig.mode < 1) {
+        led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
     }
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void led_matrix_increase_val(void) {
-    led_matrix_config.val = increment(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.val = increment(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void led_matrix_decrease_val(void) {
-    led_matrix_config.val = decrement(led_matrix_config.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    led_matrix_eeconfig.val = decrement(led_matrix_eeconfig.val, 8, 0, LED_MATRIX_MAXIMUM_BRIGHTNESS);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void led_matrix_increase_speed(void) {
-    led_matrix_config.speed = increment(led_matrix_config.speed, 1, 0, 3);
-    eeconfig_update_led_matrix(led_matrix_config.raw);  // EECONFIG needs to be increased to support this
+    led_matrix_eeconfig.speed = increment(led_matrix_eeconfig.speed, 1, 0, 3);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);  // EECONFIG needs to be increased to support this
 }
 
 void led_matrix_decrease_speed(void) {
-    led_matrix_config.speed = decrement(led_matrix_config.speed, 1, 0, 3);
-    eeconfig_update_led_matrix(led_matrix_config.raw);  // EECONFIG needs to be increased to support this
+    led_matrix_eeconfig.speed = decrement(led_matrix_eeconfig.speed, 1, 0, 3);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);  // EECONFIG needs to be increased to support this
 }
 
 void led_matrix_mode(uint8_t mode, bool eeprom_write) {
-    led_matrix_config.mode = mode;
+    led_matrix_eeconfig.mode = mode;
     if (eeprom_write) {
-        eeconfig_update_led_matrix(led_matrix_config.raw);
+        eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
     }
 }
 
-uint8_t led_matrix_get_mode(void) { return led_matrix_config.mode; }
+uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
 
-void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_config.val = val; }
+void led_matrix_set_value_noeeprom(uint8_t val) { led_matrix_eeconfig.val = val; }
 
 void led_matrix_set_value(uint8_t val) {
     led_matrix_set_value_noeeprom(val);
-    eeconfig_update_led_matrix(led_matrix_config.raw);
+    eeconfig_update_led_matrix(led_matrix_eeconfig.raw);
 }
 
 void backlight_set(uint8_t val) { led_matrix_set_value(val); }
diff --git a/quantum/led_matrix.h b/quantum/led_matrix.h
index 7dcdf1d482..85bae43c15 100644
--- a/quantum/led_matrix.h
+++ b/quantum/led_matrix.h
@@ -19,46 +19,12 @@
 
 #pragma once
 
+#include "led_matrix_types.h"
+
 #ifndef BACKLIGHT_ENABLE
 #    error You must define BACKLIGHT_ENABLE with LED_MATRIX_ENABLE
 #endif
 
-typedef struct Point {
-    uint8_t x;
-    uint8_t y;
-} __attribute__((packed)) Point;
-
-typedef struct led_matrix {
-    union {
-        uint8_t raw;
-        struct {
-            uint8_t row : 4;  // 16 max
-            uint8_t col : 4;  // 16 max
-        };
-    } matrix_co;
-    Point   point;
-    uint8_t modifier : 1;
-} __attribute__((packed)) led_matrix;
-
-extern const led_matrix g_leds[LED_DRIVER_LED_COUNT];
-
-typedef struct {
-    uint8_t index;
-    uint8_t value;
-} led_indicator;
-
-typedef union {
-    uint32_t raw;
-    struct {
-        bool    enable : 1;
-        uint8_t mode : 6;
-        uint8_t hue : 8;  // Unused by led_matrix
-        uint8_t sat : 8;  // Unused by led_matrix
-        uint8_t val : 8;
-        uint8_t speed : 8;  // EECONFIG needs to be increased to support this
-    };
-} led_config_t;
-
 enum led_matrix_effects {
     LED_MATRIX_UNIFORM_BRIGHTNESS = 1,
     // All new effects go above this line
@@ -122,3 +88,7 @@ typedef struct {
 } led_matrix_driver_t;
 
 extern const led_matrix_driver_t led_matrix_driver;
+
+extern led_eeconfig_t led_matrix_eeconfig;
+
+extern led_config_t g_led_config;
diff --git a/quantum/led_matrix_types.h b/quantum/led_matrix_types.h
new file mode 100644
index 0000000000..2602bf2bf6
--- /dev/null
+++ b/quantum/led_matrix_types.h
@@ -0,0 +1,69 @@
+/* Copyright 2021
+ *
+ * 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 2 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 <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#if defined(__GNUC__)
+#    define PACKED __attribute__((__packed__))
+#else
+#    define PACKED
+#endif
+
+#if defined(_MSC_VER)
+#    pragma pack(push, 1)
+#endif
+
+// Last led hit
+#ifndef LED_HITS_TO_REMEMBER
+#    define LED_HITS_TO_REMEMBER 8
+#endif  // LED_HITS_TO_REMEMBER
+
+typedef struct PACKED {
+    uint8_t x;
+    uint8_t y;
+} point_t;
+
+#define LED_FLAG_ALL 0xFF
+#define LED_FLAG_NONE 0x00
+#define LED_FLAG_MODIFIER 0x01
+#define LED_FLAG_KEYLIGHT 0x04
+#define LED_FLAG_INDICATOR 0x08
+
+#define NO_LED 255
+
+typedef struct PACKED {
+    uint8_t matrix_co[MATRIX_ROWS][MATRIX_COLS];
+    point_t point[LED_DRIVER_LED_COUNT];
+    uint8_t flags[LED_DRIVER_LED_COUNT];
+} led_config_t;
+
+typedef union {
+    uint32_t raw;
+    struct PACKED {
+        uint8_t  enable : 2;
+        uint8_t  mode : 6;
+        uint16_t reserved;
+        uint8_t  val;
+        uint8_t  speed;  // EECONFIG needs to be increased to support this
+    };
+} led_eeconfig_t;
+
+#if defined(_MSC_VER)
+#    pragma pack(pop)
+#endif
diff --git a/quantum/rgb_matrix_types.h b/quantum/rgb_matrix_types.h
index f7ebec1d5d..7b8171fb23 100644
--- a/quantum/rgb_matrix_types.h
+++ b/quantum/rgb_matrix_types.h
@@ -1,3 +1,19 @@
+/* Copyright 2021
+ *
+ * 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 2 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 <http://www.gnu.org/licenses/>.
+ */
+
 #pragma once
 
 #include <stdint.h>