diff options
author | Drashna Jaelre <drashna@live.com> | 2019-11-14 12:00:51 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-14 12:00:51 -0800 |
commit | 1cf63a193be50b14ecd33ee1c6eb5e470640e084 (patch) | |
tree | 59b7f70cdd02f8ca767de9400499b9c86686ab91 | |
parent | 8dc9764f31ce3326b2f5fd1a3bea216e86cf4734 (diff) | |
download | qmk_firmware-1cf63a193be50b14ecd33ee1c6eb5e470640e084.tar.gz qmk_firmware-1cf63a193be50b14ecd33ee1c6eb5e470640e084.zip |
Move Ergodox EZ RGB Light code to custom driver (#7309)
* Move Ergodox EZ RGB code to custom driver
Also implements full addressing of Ergodox EZ's LED Strip, as written by seebs
Co-authored-by: Seebs <seebs@seebs.net>
* Make Clipping range accessible for custom drivers
* Remove RGBW_BB_TWI from driver and docs
* Revert changes to clipping range support
* Use just rgblight_set instead of full custom driver
* Convert to i2c_master commands
* Rename rgblight driver and clean up includes
-rw-r--r-- | drivers/avr/ws2812.c | 129 | ||||
-rw-r--r-- | keyboards/ergodox_ez/config.h | 12 | ||||
-rw-r--r-- | keyboards/ergodox_ez/led_i2c.c | 86 | ||||
-rw-r--r-- | keyboards/ergodox_ez/post_config.h | 20 | ||||
-rw-r--r-- | keyboards/ergodox_ez/rules.mk | 5 |
5 files changed, 120 insertions, 132 deletions
diff --git a/drivers/avr/ws2812.c b/drivers/avr/ws2812.c index dc7e8d48a..82d985c20 100644 --- a/drivers/avr/ws2812.c +++ b/drivers/avr/ws2812.c | |||
@@ -36,108 +36,6 @@ | |||
36 | void ws2812_sendarray(uint8_t *array, uint16_t length); | 36 | void ws2812_sendarray(uint8_t *array, uint16_t length); |
37 | void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); | 37 | void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); |
38 | 38 | ||
39 | #ifdef RGBW_BB_TWI | ||
40 | |||
41 | // Port for the I2C | ||
42 | # define I2C_DDR DDRD | ||
43 | # define I2C_PIN PIND | ||
44 | # define I2C_PORT PORTD | ||
45 | |||
46 | // Pins to be used in the bit banging | ||
47 | # define I2C_CLK 0 | ||
48 | # define I2C_DAT 1 | ||
49 | |||
50 | # define I2C_DATA_HI() \ | ||
51 | I2C_DDR &= ~(1 << I2C_DAT); \ | ||
52 | I2C_PORT |= (1 << I2C_DAT); | ||
53 | # define I2C_DATA_LO() \ | ||
54 | I2C_DDR |= (1 << I2C_DAT); \ | ||
55 | I2C_PORT &= ~(1 << I2C_DAT); | ||
56 | |||
57 | # define I2C_CLOCK_HI() \ | ||
58 | I2C_DDR &= ~(1 << I2C_CLK); \ | ||
59 | I2C_PORT |= (1 << I2C_CLK); | ||
60 | # define I2C_CLOCK_LO() \ | ||
61 | I2C_DDR |= (1 << I2C_CLK); \ | ||
62 | I2C_PORT &= ~(1 << I2C_CLK); | ||
63 | |||
64 | # define I2C_DELAY 1 | ||
65 | |||
66 | void I2C_WriteBit(unsigned char c) { | ||
67 | if (c > 0) { | ||
68 | I2C_DATA_HI(); | ||
69 | } else { | ||
70 | I2C_DATA_LO(); | ||
71 | } | ||
72 | |||
73 | I2C_CLOCK_HI(); | ||
74 | _delay_us(I2C_DELAY); | ||
75 | |||
76 | I2C_CLOCK_LO(); | ||
77 | _delay_us(I2C_DELAY); | ||
78 | |||
79 | if (c > 0) { | ||
80 | I2C_DATA_LO(); | ||
81 | } | ||
82 | |||
83 | _delay_us(I2C_DELAY); | ||
84 | } | ||
85 | |||
86 | // Inits bitbanging port, must be called before using the functions below | ||
87 | // | ||
88 | void I2C_Init(void) { | ||
89 | I2C_PORT &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
90 | |||
91 | I2C_CLOCK_HI(); | ||
92 | I2C_DATA_HI(); | ||
93 | |||
94 | _delay_us(I2C_DELAY); | ||
95 | } | ||
96 | |||
97 | // Send a START Condition | ||
98 | // | ||
99 | void I2C_Start(void) { | ||
100 | // set both to high at the same time | ||
101 | I2C_DDR &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
102 | _delay_us(I2C_DELAY); | ||
103 | |||
104 | I2C_DATA_LO(); | ||
105 | _delay_us(I2C_DELAY); | ||
106 | |||
107 | I2C_CLOCK_LO(); | ||
108 | _delay_us(I2C_DELAY); | ||
109 | } | ||
110 | |||
111 | // Send a STOP Condition | ||
112 | // | ||
113 | void I2C_Stop(void) { | ||
114 | I2C_CLOCK_HI(); | ||
115 | _delay_us(I2C_DELAY); | ||
116 | |||
117 | I2C_DATA_HI(); | ||
118 | _delay_us(I2C_DELAY); | ||
119 | } | ||
120 | |||
121 | // write a byte to the I2C slave device | ||
122 | // | ||
123 | unsigned char I2C_Write(unsigned char c) { | ||
124 | for (char i = 0; i < 8; i++) { | ||
125 | I2C_WriteBit(c & 128); | ||
126 | |||
127 | c <<= 1; | ||
128 | } | ||
129 | |||
130 | I2C_WriteBit(0); | ||
131 | _delay_us(I2C_DELAY); | ||
132 | _delay_us(I2C_DELAY); | ||
133 | |||
134 | // _delay_us(I2C_DELAY); | ||
135 | // return I2C_ReadBit(); | ||
136 | return 0; | ||
137 | } | ||
138 | |||
139 | #endif | ||
140 | |||
141 | // Setleds for standard RGB | 39 | // Setleds for standard RGB |
142 | void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { | 40 | void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { |
143 | // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); | 41 | // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); |
@@ -145,38 +43,15 @@ void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { | |||
145 | } | 43 | } |
146 | 44 | ||
147 | void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) { | 45 | void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) { |
148 | #ifdef RGBW_BB_TWI | ||
149 | uint8_t sreg_prev, twcr_prev; | ||
150 | sreg_prev = SREG; | ||
151 | twcr_prev = TWCR; | ||
152 | cli(); | ||
153 | TWCR &= ~(1 << TWEN); | ||
154 | I2C_Init(); | ||
155 | I2C_Start(); | ||
156 | I2C_Write(0x84); | ||
157 | uint16_t datlen = leds << 2; | ||
158 | uint8_t curbyte; | ||
159 | uint8_t *data = (uint8_t *)ledarray; | ||
160 | while (datlen--) { | ||
161 | curbyte = *data++; | ||
162 | I2C_Write(curbyte); | ||
163 | } | ||
164 | I2C_Stop(); | ||
165 | SREG = sreg_prev; | ||
166 | TWCR = twcr_prev; | ||
167 | #endif | ||
168 | // ws2812_DDRREG |= pinmask; // Enable DDR | ||
169 | // new universal format (DDR) | 46 | // new universal format (DDR) |
170 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask; | 47 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask; |
171 | 48 | ||
172 | ws2812_sendarray_mask((uint8_t *)ledarray, leds * sizeof(LED_TYPE), pinmask); | 49 | ws2812_sendarray_mask((uint8_t *)ledarray, leds * sizeof(LED_TYPE), pinmask); |
173 | 50 | ||
174 | #ifndef RGBW_BB_TWI | 51 | #ifdef RGBW |
175 | # ifdef RGBW | ||
176 | _delay_us(80); | 52 | _delay_us(80); |
177 | # else | 53 | #else |
178 | _delay_us(50); | 54 | _delay_us(50); |
179 | # endif | ||
180 | #endif | 55 | #endif |
181 | } | 56 | } |
182 | 57 | ||
diff --git a/keyboards/ergodox_ez/config.h b/keyboards/ergodox_ez/config.h index bb51ec321..e60101e5e 100644 --- a/keyboards/ergodox_ez/config.h +++ b/keyboards/ergodox_ez/config.h | |||
@@ -75,17 +75,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
75 | /* ws2812 RGB LED */ | 75 | /* ws2812 RGB LED */ |
76 | #define RGB_DI_PIN D7 | 76 | #define RGB_DI_PIN D7 |
77 | #define RGBLIGHT_ANIMATIONS | 77 | #define RGBLIGHT_ANIMATIONS |
78 | #define RGBLED_NUM 15 // Number of LEDs | ||
79 | #define RGBLIGHT_HUE_STEP 12 | 78 | #define RGBLIGHT_HUE_STEP 12 |
80 | #define RGBLIGHT_SAT_STEP 255 | 79 | #define RGBLIGHT_SAT_STEP 255 |
81 | #define RGBLIGHT_VAL_STEP 12 | 80 | #define RGBLIGHT_VAL_STEP 12 |
82 | 81 | ||
82 | // Pick one of the modes | ||
83 | // Defaults to 15 mirror, for legacy behavior | ||
84 | |||
85 | // #define ERGODOX_LED_15 // Addresses 15 LEDs, but same position on both halves | ||
86 | // #define ERGODOX_LED_15_MIRROR // Addresses 15 LEDs, but are mirrored | ||
87 | // #define ERGODOX_LED_30 // Addresses all 30 LED individually | ||
88 | |||
83 | /* fix space cadet rollover issue */ | 89 | /* fix space cadet rollover issue */ |
84 | #define DISABLE_SPACE_CADET_ROLLOVER | 90 | #define DISABLE_SPACE_CADET_ROLLOVER |
85 | 91 | ||
86 | #define RGBW_BB_TWI | 92 | #define RGBW |
87 | |||
88 | #define RGBW 1 | ||
89 | 93 | ||
90 | #define RGBLIGHT_SLEEP | 94 | #define RGBLIGHT_SLEEP |
91 | 95 | ||
diff --git a/keyboards/ergodox_ez/led_i2c.c b/keyboards/ergodox_ez/led_i2c.c new file mode 100644 index 000000000..3e75a8cd0 --- /dev/null +++ b/keyboards/ergodox_ez/led_i2c.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * light weight WS2812 lib V2.0b | ||
3 | * | ||
4 | * Controls WS2811/WS2812/WS2812B RGB-LEDs | ||
5 | * Author: Tim (cpldcpu@gmail.com) | ||
6 | * | ||
7 | * Jan 18th, 2014 v2.0b Initial Version | ||
8 | * Nov 29th, 2015 v2.3 Added SK6812RGBW support | ||
9 | * | ||
10 | * This program is free software: you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation, either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
22 | */ | ||
23 | #ifdef RGBLIGHT_ENABLE | ||
24 | |||
25 | # include "ws2812.c" | ||
26 | # include "ergodox_ez.h" | ||
27 | |||
28 | extern rgblight_config_t rgblight_config; | ||
29 | |||
30 | /* | ||
31 | * Forward declare internal functions | ||
32 | * | ||
33 | * The functions take a byte-array and send to the data output as WS2812 bitstream. | ||
34 | * The length is the number of bytes to send - three per LED. | ||
35 | */ | ||
36 | |||
37 | void ws2812_sendarray(uint8_t *array, uint16_t length); | ||
38 | void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); | ||
39 | |||
40 | |||
41 | |||
42 | |||
43 | void rgblight_set(void) { | ||
44 | if (!rgblight_config.enable) { | ||
45 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | ||
46 | led[i].r = 0; | ||
47 | led[i].g = 0; | ||
48 | led[i].b = 0; | ||
49 | #ifdef RGBW | ||
50 | led[i].w = 0; | ||
51 | #endif | ||
52 | } | ||
53 | } | ||
54 | |||
55 | |||
56 | uint8_t led_num = RGBLED_NUM; | ||
57 | i2c_init(); | ||
58 | i2c_start(0x84, ERGODOX_EZ_I2C_TIMEOUT); | ||
59 | int i = 0; | ||
60 | # if defined(ERGODOX_LED_30) | ||
61 | // prevent right-half code from trying to bitbang all 30 | ||
62 | // so with 30 LEDs, we count from 29 to 15 here, and the | ||
63 | // other half does 0 to 14. | ||
64 | led_num = RGBLED_NUM / 2; | ||
65 | for (i = led_num + led_num - 1; i >= led_num; --i) | ||
66 | # elif defined(ERGODOX_LED_15_MIRROR) | ||
67 | for (i = 0; i < led_num; ++i) | ||
68 | # else // ERGDOX_LED_15 non-mirrored | ||
69 | for (i = led_num - 1; i >= 0; --i) | ||
70 | # endif | ||
71 | { | ||
72 | uint8_t *data = (uint8_t *)(led + i); | ||
73 | i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT); | ||
74 | i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT); | ||
75 | i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT); | ||
76 | #ifdef RGBW | ||
77 | i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT); | ||
78 | #endif | ||
79 | } | ||
80 | i2c_stop(); | ||
81 | |||
82 | ws2812_setleds(led, RGBLED_NUM); | ||
83 | } | ||
84 | |||
85 | |||
86 | #endif // RGBLIGHT_ENABLE | ||
diff --git a/keyboards/ergodox_ez/post_config.h b/keyboards/ergodox_ez/post_config.h new file mode 100644 index 000000000..526cc8c41 --- /dev/null +++ b/keyboards/ergodox_ez/post_config.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #if !defined(ERGODOX_LED_15) && !defined(ERGODOX_LED_30) | ||
4 | // if no value is defined, assume previous behavior | ||
5 | // # define ERGODOX_LED_15 | ||
6 | // # define ERGODOX_LED_30 | ||
7 | # define ERGODOX_LED_15_MIRROR | ||
8 | #endif | ||
9 | |||
10 | #if (defined(ERGODOX_LED_30) + defined(ERGODOX_LED_15) + defined(ERGODOX_LED_15_MIRROR)) != 1 | ||
11 | # error "You must only define one of the ERGODOX_LED options." | ||
12 | #endif | ||
13 | |||
14 | #ifdef ERGODOX_LED_30 | ||
15 | // If using 30 LEDs, then define that many | ||
16 | # define RGBLED_NUM 30 // Number of LEDs | ||
17 | #else | ||
18 | // If not, then only define 15 | ||
19 | # define RGBLED_NUM 15 // Number of LEDs | ||
20 | #endif | ||
diff --git a/keyboards/ergodox_ez/rules.mk b/keyboards/ergodox_ez/rules.mk index dbc35f683..fd8f5722d 100644 --- a/keyboards/ergodox_ez/rules.mk +++ b/keyboards/ergodox_ez/rules.mk | |||
@@ -31,11 +31,14 @@ SWAP_HANDS_ENABLE= yes # Allow swapping hands of keyboard | |||
31 | SLEEP_LED_ENABLE = no | 31 | SLEEP_LED_ENABLE = no |
32 | API_SYSEX_ENABLE = no | 32 | API_SYSEX_ENABLE = no |
33 | RGBLIGHT_ENABLE = yes | 33 | RGBLIGHT_ENABLE = yes |
34 | RGBLIGHT_CUSTOM_DRIVER = yes | ||
35 | |||
34 | RGB_MATRIX_ENABLE = no # enable later | 36 | RGB_MATRIX_ENABLE = no # enable later |
35 | DEBOUNCE_TYPE = eager_pr | 37 | DEBOUNCE_TYPE = eager_pr |
36 | 38 | ||
37 | # project specific files | 39 | # project specific files |
38 | SRC += matrix.c | 40 | SRC += matrix.c \ |
41 | led_i2c.c | ||
39 | QUANTUM_LIB_SRC += i2c_master.c | 42 | QUANTUM_LIB_SRC += i2c_master.c |
40 | 43 | ||
41 | LAYOUTS = ergodox | 44 | LAYOUTS = ergodox |