]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/model01/leds.c
rgb_led struct conversion (aka: Per led (key) type rgb matrix effects - part 2) ...
[qmk_firmware.git] / keyboards / model01 / leds.c
1 /* Copyright 2018 James Laird-Wah
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include <quantum.h>
17 #include <i2c_master.h>
18 #include <led_tables.h>
19 #include <rgb_matrix.h>
20 #include <string.h>
21 #include "model01.h"
22
23 #define I2C_TIMEOUT     1000
24
25 void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
26   uint8_t buf[] = {
27     TWI_CMD_LED_SET_ALL_TO,
28     b, g, r
29   };
30   i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
31   i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
32   _delay_us(10);
33 }
34
35 void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b) {
36   uint8_t buf[] = {
37     TWI_CMD_LED_SET_ONE_TO,
38     led & 0x1f,
39     b, g, r
40   };
41   int hand = (led >= 32) ? RIGHT : LEFT;
42   i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
43   _delay_us(10);
44 }
45
46 #ifdef RGB_MATRIX_ENABLE
47
48 __attribute__ ((weak))
49 led_config_t g_led_config = { {
50     {  27,  26,  20,  19,  12,  11,   4,   3 },
51     {  28,  25,  21,  18,  13,  10,   5,   2 },
52     {  29,  24,  22,  17,  14,   9,   6,   1 },
53     {  30,  31,  23,  16,  15,   8,   7,   0 },
54     {  60,  59,  52,  51,  44,  43,  37,  36 },
55     {  61,  58,  53,  50,  45,  42,  38,  35 },
56     {  62,  57,  54,  49,  46,  41,  39,  34 },
57     {  63,  56,  55,  48,  47,  40,  32,  33 }
58 }, {
59     {   3,  35 }, {   0,  26 }, {   0,  17 }, {   0,   6 }, {  14,   5 }, {  15,  16 }, {  16,  25 }, {  17,  34 },
60     {  31,  29 }, {  31,  19 }, {  30,  11 }, {  30,   1 }, {  45,   0 }, {  45,   8 }, {  46,  17 }, {  46,  27 },
61     {  60,  27 }, {  60,  18 }, {  60,   9 }, {  60,   0 }, {  74,   2 }, {  74,  11 }, {  75,  20 }, {  74,  28 },
62     {  89,  30 }, {  89,  19 }, {  89,   7 }, {  70,  38 }, {  82,  41 }, {  93,  45 }, { 104,  50 }, {  74,  64 },
63     { 149,  64 }, { 119,  50 }, { 130,  45 }, { 141,  41 }, { 153,  38 }, { 134,   7 }, { 134,  19 }, { 134,  30 },
64     { 149,  28 }, { 148,  20 }, { 149,  11 }, { 149,   2 }, { 163,   0 }, { 163,   9 }, { 163,  18 }, { 163,  27 },
65     { 177,  27 }, { 177,  17 }, { 178,   8 }, { 178,   0 }, { 193,   1 }, { 193,  11 }, { 192,  19 }, { 192,  29 },
66     { 206,  34 }, { 207,  25 }, { 208,  16 }, { 209,   5 }, { 224,   6 }, { 223,  17 }, { 223,  26 }, { 220,  35 }
67 }, {
68     4, 4, 4, 4, 4, 4, 4, 4,
69     4, 4, 4, 4, 4, 4, 4, 4,
70     4, 4, 4, 4, 4, 4, 4, 4,
71     4, 4, 4, 1, 1, 1, 1, 1,
72     1, 1, 1, 1, 1, 4, 4, 4,
73     4, 4, 4, 4, 4, 4, 4, 4,
74     4, 4, 4, 4, 4, 4, 4, 4,
75     4, 4, 4, 4, 4, 4, 4, 4
76 } };
77
78
79 static struct {
80   uint8_t b;
81   uint8_t g;
82   uint8_t r;
83 } __attribute__((packed)) led_state[64];
84
85 static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
86   led_state[index].r = r;
87   led_state[index].g = g;
88   led_state[index].b = b;
89 }
90
91 static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
92   for (int i=0; i<DRIVER_LED_TOTAL; i++)
93     set_color(i, r, g, b);
94 }
95
96 static void init(void) {
97   // Enable high current pathway to LEDs - this does violate the USB spec though! (1.6 amps...)
98   DDRE |= _BV(6);
99   PORTE &= ~_BV(6);
100
101   // Overcurrent check input
102   DDRB &= ~_BV(4);
103   PORTB &= ~_BV(4);
104 }
105
106 static void flush(void) {
107   uint8_t *bank_data = (uint8_t*)&led_state[0];
108   uint8_t command[1 + 8*3];
109   for (int hand=0; hand<2; hand++) {
110     int addr = I2C_ADDR(hand);
111
112     for (int bank=0; bank<4; bank++) {
113       command[0] = TWI_CMD_LED_BASE + bank;
114       memcpy(&command[1], bank_data, 8*3);
115       i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
116       _delay_us(100);
117
118       bank_data += 8*3;
119     }
120   }
121 }
122
123 const rgb_matrix_driver_t rgb_matrix_driver = {
124   .init = init,
125   .flush = flush,
126   .set_color = set_color,
127   .set_color_all = set_color_all
128 };
129
130 #endif
131
132 /* vim: set ts=2 sw=2 et: */