]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Keyboard: keyboardio model01: rgbmatrix support (#3989)
authorJames Laird-Wah <james@laird-wah.net>
Fri, 28 Sep 2018 03:53:36 +0000 (13:53 +1000)
committerDrashna Jaelre <drashna@live.com>
Fri, 28 Sep 2018 03:53:36 +0000 (20:53 -0700)
* model01: implement RGB matrix feature

LEDs can still be individually controlled if this is switched off.

* model01: use fast banked LED setting commands

* model01: update default keymap to use RGB matrix

* model01: update RGB matrix support to match common_features.mk

* rgb_matrix: include <string.h>

This was missing after commit 4d5705ea6ceeb11cd2adc018644ec87c51af4e86
which introduces a use of memset().

* model01: make g_rgb_leds weak

This allows users to override which LEDs are configured as
modifiers, or tweak the x/y coordinates, should they so choose.

keyboards/model01/config.h
keyboards/model01/keymaps/default/keymap.c
keyboards/model01/leds.c
keyboards/model01/leds.h
keyboards/model01/rules.mk
quantum/rgb_matrix.c

index 796653f459cdac2270df5b6703b969dde48c812e..167310731f8e243f5d849b3aa6f133cf850fd2de 100644 (file)
@@ -33,3 +33,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 /* The scanners already debounce for us */
 #define DEBOUNCING_DELAY 0
+
+/* RGB matrix constants */
+#define DRIVER_LED_TOTAL 64
index cdc050cd90b7d8f7725b33d9c4dd7928836b65e7..a6e5303fe83dd1d8e20a8a990800deced060bdc3 100644 (file)
@@ -26,7 +26,7 @@ enum {
 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 [DEF] = LAYOUT(
   RESET  , KC_1   , KC_2   , KC_3   , KC_4   , KC_5   ,                      KC_6   , KC_7   , KC_8   , KC_9   , KC_0   , TG(NUM),
-  KC_GRV , KC_Q   , KC_W   , KC_E   , KC_R   , KC_T   , _______,    _______, KC_Y   , KC_U   , KC_I   , KC_O   , KC_P   , KC_EQL ,
+  KC_GRV , KC_Q   , KC_W   , KC_E   , KC_R   , KC_T   , RGB_MOD,    _______, KC_Y   , KC_U   , KC_I   , KC_O   , KC_P   , KC_EQL ,
   KC_PGUP, KC_A   , KC_S   , KC_D   , KC_F   , KC_G   , KC_TAB ,    KC_ENT , KC_H   , KC_J   , KC_K   , KC_L   , KC_SCLN, KC_QUOT,
   KC_PGDN, KC_Z   , KC_X   , KC_C   , KC_V   , KC_B   , KC_ESC ,    _______, KC_N   , KC_M   , KC_COMM, KC_DOT , KC_SLSH, KC_MINS,
                                          KC_LCTL,                                 KC_RCTL,
@@ -48,7 +48,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
   ),
 [FUN] = LAYOUT(
   _______, KC_F1  , KC_F2  , KC_F3  , KC_F4  , KC_F5  ,                      KC_F6  , KC_F7  , KC_F8  , KC_F9  , KC_F10 , KC_F11 ,
-  KC_TAB , _______, KC_MS_U, _______, KC_BTN3, _______, _______,    KC_MPRV, KC_MNXT, KC_LCBR, KC_RCBR, KC_LBRC, KC_RBRC, KC_F12 ,
+  KC_TAB , _______, KC_MS_U, _______, KC_BTN3, _______, RGB_TOG,    KC_MPRV, KC_MNXT, KC_LCBR, KC_RCBR, KC_LBRC, KC_RBRC, KC_F12 ,
   KC_HOME, KC_MS_L, KC_MS_D, KC_MS_R, KC_BTN1, _______, _______,    KC_MPLY, KC_LEFT, KC_DOWN, KC_UP  , KC_RGHT, _______, _______,
   KC_END , KC_PSCR, KC_INS , _______, KC_BTN2, _______, _______,    _______, KC_MUTE, KC_VOLD, KC_VOLU, _______, KC_BSLS, KC_PIPE,
                                          _______,                                 _______,
@@ -73,21 +73,52 @@ LAYOUT(
   )
 */
 
+static void set_numpad_colours(int on, void (*write)(int, uint8_t, uint8_t, uint8_t)) {
+  if (!on) {
+    for (int i=44; i<=60; i++)
+      write(i, 0, 0, 0);
+    write(63, 0, 0, 0);
+    return;
+  }
+
+  /* main number keys */
+  for (int i=44; i<=47; i++)
+    write(i, 255, 0, 0);
+  for (int i=49; i<=54; i++)
+    write(i, 255, 0, 0);
+
+  /* accessory keys */
+  write(48, 128, 128, 0);
+  for (int i=55; i<=59; i++)
+    write(i, 128, 128, 0);
+
+  // enter
+  write(63, 0, 128, 0);
+
+  // num key
+  write(60, 128, 0, 128);
+}
+
+#ifdef RGB_MATRIX_ENABLE
+/* the RGB matrix effects will overwrite the numpad indicator.
+ * this handy mechanism allows to override the matrix effects.
+ */
+void rgb_matrix_indicators_user(void) {
+  if (layer_state & (1<<NUM)) {
+    set_numpad_colours(1, &rgb_matrix_set_color);
+  }
+}
+#else   /* no RGB matrix support */
+
 uint32_t layer_state_set_user(uint32_t state) {
-  switch (biton32(state)) {
-    case DEF:
-      set_all_leds_to(0,0,0);
-      break;
-    case NUM:
-      /* highlight the numpad keys when numlock is on */
-      for (int i=44; i<=60; i++) {
-        set_led_to(i, 128,0,0);
-      }
-      set_led_to(63, 128, 0, 0);
-      break;
+  if (state & (1<<NUM)) {
+    set_numpad_colours(1, &set_led_to);
+  } else {
+    set_numpad_colours(0, &set_led_to);
   }
 
   return state;
 }
+#endif
 
 /* vim: set ts=2 sw=2 et: */
index b80a749a75d07795dd84910add1c506474fd9f00..7a62c7424c66bfbda28d0d8804c9ce2c34ae4fe5 100644 (file)
 #include <quantum.h>
 #include <i2c_master.h>
 #include <led_tables.h>
+#include <rgb_matrix.h>
+#include <string.h>
 #include "model01.h"
 
 #define I2C_TIMEOUT     1000
 
-#define LINCOR(i) pgm_read_byte(&CIE1931_CURVE[i])
-
-int set_all_leds_to_raw(uint8_t r, uint8_t g, uint8_t b) {
+void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
   uint8_t buf[] = {
     TWI_CMD_LED_SET_ALL_TO,
     b, g, r
   };
-  int ret = 0;
-  ret |= i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
-  ret |= i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
+  i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
+  i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
   _delay_us(10);
-  return ret;
-}
-
-int set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
-  return set_all_leds_to_raw(LINCOR(r), LINCOR(g), LINCOR(b));
 }
 
-int set_led_to_raw(uint8_t led, uint8_t r, uint8_t g, uint8_t b) {
+void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b) {
   uint8_t buf[] = {
     TWI_CMD_LED_SET_ONE_TO,
     led & 0x1f,
     b, g, r
   };
   int hand = (led >= 32) ? RIGHT : LEFT;
-  int ret = i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
+  i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
   _delay_us(10);
-  return ret;
 }
 
-int set_led_to(uint8_t led, uint8_t r, uint8_t g, uint8_t b) {
-  return set_led_to_raw(led, LINCOR(r), LINCOR(g), LINCOR(b));
+#ifdef RGB_MATRIX_ENABLE
+
+__attribute__ ((weak))
+const rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = {
+  {{0x73}, {  3, 35}, 0},
+  {{0x72}, {  0, 26}, 0},
+  {{0x71}, {  0, 17}, 0},
+  {{0x70}, {  0,  6}, 0},
+  {{0x60}, { 14,  5}, 0},
+  {{0x61}, { 15, 16}, 0},
+  {{0x62}, { 16, 25}, 0},
+  {{0x63}, { 17, 34}, 0},
+  {{0x53}, { 31, 29}, 0},
+  {{0x52}, { 31, 19}, 0},
+  {{0x51}, { 30, 11}, 0},
+  {{0x50}, { 30,  1}, 0},
+  {{0x40}, { 45,  0}, 0},
+  {{0x41}, { 45,  8}, 0},
+  {{0x42}, { 46, 17}, 0},
+  {{0x43}, { 46, 27}, 0},
+  {{0x33}, { 60, 27}, 0},
+  {{0x32}, { 60, 18}, 0},
+  {{0x31}, { 60,  9}, 0},
+  {{0x30}, { 60,  0}, 0},
+  {{0x20}, { 74,  2}, 0},
+  {{0x21}, { 74, 11}, 0},
+  {{0x22}, { 75, 20}, 0},
+  {{0x23}, { 74, 28}, 0},
+  {{0x12}, { 89, 30}, 0},
+  {{0x11}, { 89, 19}, 0},
+  {{0x10}, { 89,  7}, 0},
+  {{0x00}, { 70, 38}, 1},
+  {{0x01}, { 82, 41}, 1},
+  {{0x02}, { 93, 45}, 1},
+  {{0x03}, {104, 50}, 1},
+  {{0x13}, { 74, 64}, 1},
+  {{0x67}, {149, 64}, 1},
+  {{0x77}, {119, 50}, 1},
+  {{0x76}, {130, 45}, 1},
+  {{0x75}, {141, 41}, 1},
+  {{0x74}, {153, 38}, 1},
+  {{0x64}, {134,  7}, 0},
+  {{0x65}, {134, 19}, 0},
+  {{0x66}, {134, 30}, 0},
+  {{0x57}, {149, 28}, 0},
+  {{0x56}, {148, 20}, 0},
+  {{0x55}, {149, 11}, 0},
+  {{0x54}, {149,  2}, 0},
+  {{0x44}, {163,  0}, 0},
+  {{0x45}, {163,  9}, 0},
+  {{0x46}, {163, 18}, 0},
+  {{0x47}, {163, 27}, 0},
+  {{0x37}, {177, 27}, 0},
+  {{0x36}, {177, 17}, 0},
+  {{0x35}, {178,  8}, 0},
+  {{0x34}, {178,  0}, 0},
+  {{0x24}, {193,  1}, 0},
+  {{0x25}, {193, 11}, 0},
+  {{0x26}, {192, 19}, 0},
+  {{0x27}, {192, 29}, 0},
+  {{0x17}, {206, 34}, 0},
+  {{0x16}, {207, 25}, 0},
+  {{0x15}, {208, 16}, 0},
+  {{0x14}, {209,  5}, 0},
+  {{0x04}, {224,  6}, 0},
+  {{0x05}, {223, 17}, 0},
+  {{0x06}, {223, 26}, 0},
+  {{0x07}, {220, 35}, 0},
+};
+
+static struct {
+  uint8_t b;
+  uint8_t g;
+  uint8_t r;
+} __attribute__((packed)) led_state[64];
+
+static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
+  led_state[index].r = r;
+  led_state[index].g = g;
+  led_state[index].b = b;
+}
+
+static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
+  for (int i=0; i<DRIVER_LED_TOTAL; i++)
+    set_color(i, r, g, b);
 }
 
+static void init(void) {
+  // Enable high current pathway to LEDs - this does violate the USB spec though! (1.6 amps...)
+  DDRE |= _BV(6);
+  PORTE &= ~_BV(6);
+
+  // Overcurrent check input
+  DDRB &= ~_BV(4);
+  PORTB &= ~_BV(4);
+}
+
+static void flush(void) {
+  uint8_t *bank_data = (uint8_t*)&led_state[0];
+  uint8_t command[1 + 8*3];
+  for (int hand=0; hand<2; hand++) {
+    int addr = I2C_ADDR(hand);
+
+    for (int bank=0; bank<4; bank++) {
+      command[0] = TWI_CMD_LED_BASE + bank;
+      memcpy(&command[1], bank_data, 8*3);
+      i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
+      _delay_us(100);
+
+      bank_data += 8*3;
+    }
+  }
+}
+
+const rgb_matrix_driver_t rgb_matrix_driver = {
+  .init = init,
+  .flush = flush,
+  .set_color = set_color,
+  .set_color_all = set_color_all
+};
+
+#endif
+
 /* vim: set ts=2 sw=2 et: */
index 0aac781dd5d5c324b894e4922b6d1f8d56f03d7c..405bdf328a25fef7e550e91a614513b96e08bb3f 100644 (file)
 #pragma once
 
 #include <quantum.h>
+#include <rgb_matrix.h>
 
-int set_all_leds_to(uint8_t r, uint8_t g, uint8_t b);
-int set_led_to(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
-
-/* Raw (gamma uncorrected) LED values */
-int set_all_leds_to_raw(uint8_t r, uint8_t g, uint8_t b);
-int set_led_to_raw(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
+void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b);
+void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b);
index cab7065ff8fff315329de90c6b5ae5cd5dc890d3..49ab981d148c22e2fb5c8a4c168468963a05f0c5 100644 (file)
@@ -59,4 +59,6 @@ MIDI_ENABLE = no            # MIDI support (+2400 to 4200, depending on config)
 UNICODE_ENABLE = no         # Unicode
 
 CUSTOM_MATRIX = yes
-CIE1931_CURVE = yes
+
+# You can set RGB_MATRIX_ENABLE = no in your rules.mk to disable this and save the Flash
+RGB_MATRIX_ENABLE = custom     # Enable RGB matrix effects (+10000).
index 807e4d62d1917c2e258e8bb86a4c0e73d36f8403..b282284a138e4b33dd0c84d61167f49fd8db860b 100644 (file)
@@ -21,6 +21,7 @@
 #include "progmem.h"
 #include "config.h"
 #include "eeprom.h"
+#include <string.h>
 #include <math.h>
 
 rgb_config_t rgb_matrix_config;