]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/whitefox/drivers/gdisp/IS31FL3731C/board_IS31FL3731C.h
Whitefox LED control (#1432)
[qmk_firmware.git] / keyboards / whitefox / drivers / gdisp / IS31FL3731C / board_IS31FL3731C.h
1 /*
2 Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef _GDISP_LLD_BOARD_H
19 #define _GDISP_LLD_BOARD_H
20
21 static const I2CConfig i2ccfg = {
22   400000 // clock speed (Hz); 400kHz max for IS31
23 };
24
25 #define GDISP_SCREEN_WIDTH  16
26 #define GDISP_SCREEN_HEIGHT 5
27
28 static const uint8_t led_mask[] = {
29         0xFF, 0x00, /* C1-1 -> C1-16 */
30         0xFF, 0x00, /* C2-1 -> C2-16 */
31         0xFF, 0x00, /* C3-1 -> C3-16 */
32         0xFF, 0x00, /* C4-1 -> C4-16 */
33         0xFF, 0x00, /* C5-1 -> C5-16 */
34         0xFF, 0x00, /* C6-1 -> C6-16 */
35         0xFF, 0x00, /* C7-1 -> C7-16 */
36         0xFF, 0x00, /* C8-1 -> C8-16 */
37         0xFE, 0x00, /* C9-1 -> C9-16 */
38 };
39
40 // The address of the LED
41 #define LA(c, r) (c + r * 16 )
42 // Need to be an address that is not mapped, but inside the range of the controller matrix
43 #define NA LA(8, 8)
44
45 // The numbers in the comments are the led numbers DXX on the PCB
46 // The mapping is taken from the schematic of left hand side
47 static const uint8_t led_mapping[GDISP_SCREEN_HEIGHT][GDISP_SCREEN_WIDTH] = {
48 //   1         2         3         4         5         6         7         8         9         10        11        12        13        14        15        16
49    { LA(0, 0), LA(1, 0), LA(2, 0), LA(3, 0), LA(4, 0), LA(5, 0), LA(6, 0), LA(7, 0), LA(0, 1), LA(1, 1), LA(2, 1), LA(3, 1), LA(4, 1), LA(5, 1), LA(6, 1), LA(7, 1)},
50 //   17        18        19        20        21        22        23        24        25        26                  27        28        29        30        31
51    { LA(0, 2), LA(1, 2), LA(2, 2), LA(3, 2), LA(4, 2), LA(5, 2), LA(6, 2), LA(7, 2), LA(0, 3), LA(1, 3), NA,       LA(2, 3), LA(3, 3), LA(4, 3), LA(5, 3), LA(6, 3)},
52 //   32        33        34        35        36        37        38        39        40        41                  42        43        44        45        46
53    { LA(7, 3), LA(0, 4), LA(1, 4), LA(2, 4), LA(3, 4), LA(4, 4), LA(5, 4), LA(6, 4), LA(7, 4), LA(0, 5), NA,       LA(1, 5), LA(2, 5), LA(3, 5), LA(4, 5), LA(5, 5)},
54 //   47        48        49        50        51        52        53        54        55        56                  57        58        59        60        61
55    { LA(6, 5), LA(7, 5), LA(0, 6), LA(1, 6), LA(2, 6), LA(3, 6), LA(4, 6), LA(5, 6), LA(6, 6), LA(7, 6), NA,       LA(0, 7), LA(1, 7), LA(2, 7), LA(3, 7), LA(4, 7)},
56 //   62        63        64                                      65                                      66        67        68        69        70        71
57    { LA(5, 7), LA(6, 7), LA(7, 7), NA,       NA,       NA,       LA(0, 8), NA,       NA,       NA,       LA(1, 8), LA(2, 8), LA(3, 8), LA(4, 8), LA(5, 8), LA(6, 8)},
58 };
59
60
61 #define IS31_ADDR_DEFAULT 0x74 // AD connected to GND
62 #define IS31_TIMEOUT 5000
63
64 static GFXINLINE void init_board(GDisplay *g) {
65     (void) g;
66     /* I2C pins */
67     palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
68     palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
69     palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
70     palClearPad(GPIOB, 16);
71     /* start I2C */
72     i2cStart(&I2CD1, &i2ccfg);
73     // try high drive (from kiibohd)
74     I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
75     // try glitch fixing (from kiibohd)
76     I2CD1.i2c->FLT = 4;
77 }
78
79 static GFXINLINE void post_init_board(GDisplay *g) {
80         (void) g;
81 }
82
83 static GFXINLINE const uint8_t* get_led_mask(GDisplay* g) {
84     (void) g;
85     return led_mask;
86 }
87
88 static GFXINLINE uint8_t get_led_address(GDisplay* g, uint16_t x, uint16_t y)
89 {
90     (void) g;
91     return led_mapping[y][x];
92 }
93
94 static GFXINLINE void set_hardware_shutdown(GDisplay* g, bool shutdown) {
95     (void) g;
96     if(!shutdown) {
97         palSetPad(GPIOB, 16);
98     }
99     else {
100         palClearPad(GPIOB, 16);
101     }
102 }
103
104 static GFXINLINE void write_data(GDisplay *g, uint8_t* data, uint16_t length) {
105         (void) g;
106         i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, data, length, 0, 0, US2ST(IS31_TIMEOUT));
107 }
108
109 #endif /* _GDISP_LLD_BOARD_H */