1 /* Copyright 2018 James Laird-Wah
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.
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.
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/>.
17 #include <i2c_master.h>
18 #include <led_tables.h>
19 #include <rgb_matrix.h>
23 #define I2C_TIMEOUT 1000
25 void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
27 TWI_CMD_LED_SET_ALL_TO,
30 i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
31 i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
35 void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b) {
37 TWI_CMD_LED_SET_ONE_TO,
41 int hand = (led >= 32) ? RIGHT : LEFT;
42 i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
46 #ifdef RGB_MATRIX_ENABLE
48 __attribute__ ((weak))
49 const rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = {
50 {{0x73}, { 3, 35}, 0},
51 {{0x72}, { 0, 26}, 0},
52 {{0x71}, { 0, 17}, 0},
54 {{0x60}, { 14, 5}, 0},
55 {{0x61}, { 15, 16}, 0},
56 {{0x62}, { 16, 25}, 0},
57 {{0x63}, { 17, 34}, 0},
58 {{0x53}, { 31, 29}, 0},
59 {{0x52}, { 31, 19}, 0},
60 {{0x51}, { 30, 11}, 0},
61 {{0x50}, { 30, 1}, 0},
62 {{0x40}, { 45, 0}, 0},
63 {{0x41}, { 45, 8}, 0},
64 {{0x42}, { 46, 17}, 0},
65 {{0x43}, { 46, 27}, 0},
66 {{0x33}, { 60, 27}, 0},
67 {{0x32}, { 60, 18}, 0},
68 {{0x31}, { 60, 9}, 0},
69 {{0x30}, { 60, 0}, 0},
70 {{0x20}, { 74, 2}, 0},
71 {{0x21}, { 74, 11}, 0},
72 {{0x22}, { 75, 20}, 0},
73 {{0x23}, { 74, 28}, 0},
74 {{0x12}, { 89, 30}, 0},
75 {{0x11}, { 89, 19}, 0},
76 {{0x10}, { 89, 7}, 0},
77 {{0x00}, { 70, 38}, 1},
78 {{0x01}, { 82, 41}, 1},
79 {{0x02}, { 93, 45}, 1},
80 {{0x03}, {104, 50}, 1},
81 {{0x13}, { 74, 64}, 1},
82 {{0x67}, {149, 64}, 1},
83 {{0x77}, {119, 50}, 1},
84 {{0x76}, {130, 45}, 1},
85 {{0x75}, {141, 41}, 1},
86 {{0x74}, {153, 38}, 1},
87 {{0x64}, {134, 7}, 0},
88 {{0x65}, {134, 19}, 0},
89 {{0x66}, {134, 30}, 0},
90 {{0x57}, {149, 28}, 0},
91 {{0x56}, {148, 20}, 0},
92 {{0x55}, {149, 11}, 0},
93 {{0x54}, {149, 2}, 0},
94 {{0x44}, {163, 0}, 0},
95 {{0x45}, {163, 9}, 0},
96 {{0x46}, {163, 18}, 0},
97 {{0x47}, {163, 27}, 0},
98 {{0x37}, {177, 27}, 0},
99 {{0x36}, {177, 17}, 0},
100 {{0x35}, {178, 8}, 0},
101 {{0x34}, {178, 0}, 0},
102 {{0x24}, {193, 1}, 0},
103 {{0x25}, {193, 11}, 0},
104 {{0x26}, {192, 19}, 0},
105 {{0x27}, {192, 29}, 0},
106 {{0x17}, {206, 34}, 0},
107 {{0x16}, {207, 25}, 0},
108 {{0x15}, {208, 16}, 0},
109 {{0x14}, {209, 5}, 0},
110 {{0x04}, {224, 6}, 0},
111 {{0x05}, {223, 17}, 0},
112 {{0x06}, {223, 26}, 0},
113 {{0x07}, {220, 35}, 0},
120 } __attribute__((packed)) led_state[64];
122 static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
123 led_state[index].r = r;
124 led_state[index].g = g;
125 led_state[index].b = b;
128 static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
129 for (int i=0; i<DRIVER_LED_TOTAL; i++)
130 set_color(i, r, g, b);
133 static void init(void) {
134 // Enable high current pathway to LEDs - this does violate the USB spec though! (1.6 amps...)
138 // Overcurrent check input
143 static void flush(void) {
144 uint8_t *bank_data = (uint8_t*)&led_state[0];
145 uint8_t command[1 + 8*3];
146 for (int hand=0; hand<2; hand++) {
147 int addr = I2C_ADDR(hand);
149 for (int bank=0; bank<4; bank++) {
150 command[0] = TWI_CMD_LED_BASE + bank;
151 memcpy(&command[1], bank_data, 8*3);
152 i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
160 const rgb_matrix_driver_t rgb_matrix_driver = {
163 .set_color = set_color,
164 .set_color_all = set_color_all
169 /* vim: set ts=2 sw=2 et: */