aboutsummaryrefslogtreecommitdiffstats
path: root/keyboards/model01/leds.c
blob: 0abc1f0da80ee02e5801c22c53385553c1546696 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright 2018 James Laird-Wah
 *
 * 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/>.
 */
#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

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
  };
  i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
  i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
  _delay_us(10);
}

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;
  i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
  _delay_us(10);
}

#ifdef RGB_MATRIX_ENABLE

__attribute__ ((weak))
rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = {
  {{0x73}, {  3, 35}, 4},
  {{0x72}, {  0, 26}, 4},
  {{0x71}, {  0, 17}, 4},
  {{0x70}, {  0,  6}, 4},
  {{0x60}, { 14,  5}, 4},
  {{0x61}, { 15, 16}, 4},
  {{0x62}, { 16, 25}, 4},
  {{0x63}, { 17, 34}, 4},
  {{0x53}, { 31, 29}, 4},
  {{0x52}, { 31, 19}, 4},
  {{0x51}, { 30, 11}, 4},
  {{0x50}, { 30,  1}, 4},
  {{0x40}, { 45,  0}, 4},
  {{0x41}, { 45,  8}, 4},
  {{0x42}, { 46, 17}, 4},
  {{0x43}, { 46, 27}, 4},
  {{0x33}, { 60, 27}, 4},
  {{0x32}, { 60, 18}, 4},
  {{0x31}, { 60,  9}, 4},
  {{0x30}, { 60,  0}, 4},
  {{0x20}, { 74,  2}, 4},
  {{0x21}, { 74, 11}, 4},
  {{0x22}, { 75, 20}, 4},
  {{0x23}, { 74, 28}, 4},
  {{0x12}, { 89, 30}, 4},
  {{0x11}, { 89, 19}, 4},
  {{0x10}, { 89,  7}, 4},
  {{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}, 4},
  {{0x65}, {134, 19}, 4},
  {{0x66}, {134, 30}, 4},
  {{0x57}, {149, 28}, 4},
  {{0x56}, {148, 20}, 4},
  {{0x55}, {149, 11}, 4},
  {{0x54}, {149,  2}, 4},
  {{0x44}, {163,  0}, 4},
  {{0x45}, {163,  9}, 4},
  {{0x46}, {163, 18}, 4},
  {{0x47}, {163, 27}, 4},
  {{0x37}, {177, 27}, 4},
  {{0x36}, {177, 17}, 4},
  {{0x35}, {178,  8}, 4},
  {{0x34}, {178,  0}, 4},
  {{0x24}, {193,  1}, 4},
  {{0x25}, {193, 11}, 4},
  {{0x26}, {192, 19}, 4},
  {{0x27}, {192, 29}, 4},
  {{0x17}, {206, 34}, 4},
  {{0x16}, {207, 25}, 4},
  {{0x15}, {208, 16}, 4},
  {{0x14}, {209,  5}, 4},
  {{0x04}, {224,  6}, 4},
  {{0x05}, {223, 17}, 4},
  {{0x06}, {223, 26}, 4},
  {{0x07}, {220, 35}, 4},
};

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: */