aboutsummaryrefslogtreecommitdiff
path: root/keyboards/wilba_tech/wt_mono_backlight.c
diff options
context:
space:
mode:
authorWilba <Jason.S.Williams@gmail.com>2019-10-12 15:37:03 +1100
committerDrashna Jaelre <drashna@live.com>2019-10-11 21:37:03 -0700
commite47ab6a5752e16bd3b4288153798c12af1bee3d5 (patch)
tree47fd56bfb7be699cc6b1ca84c3c22b8804000f5c /keyboards/wilba_tech/wt_mono_backlight.c
parent22aa2ce6b2f3bda51aca84d8f707c17f0301ff3b (diff)
downloadqmk_firmware-e47ab6a5752e16bd3b4288153798c12af1bee3d5.tar.gz
qmk_firmware-e47ab6a5752e16bd3b4288153798c12af1bee3d5.zip
[Keyboard] wilba.tech PCB refactoring (#6982)
* Cleanup * Refactor VIA rules.mk * WT mono backlight refactor, VIA support * Added WT75-C * Fixed compile error * Cleanup rules.mk * Review changes * Review changes
Diffstat (limited to 'keyboards/wilba_tech/wt_mono_backlight.c')
-rw-r--r--keyboards/wilba_tech/wt_mono_backlight.c341
1 files changed, 333 insertions, 8 deletions
diff --git a/keyboards/wilba_tech/wt_mono_backlight.c b/keyboards/wilba_tech/wt_mono_backlight.c
index bf485bce1..69d82e582 100644
--- a/keyboards/wilba_tech/wt_mono_backlight.c
+++ b/keyboards/wilba_tech/wt_mono_backlight.c
@@ -14,20 +14,43 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16 16
17#include "quantum.h"
17#include "wt_mono_backlight.h" 18#include "wt_mono_backlight.h"
19#include "wt_rgb_backlight_api.h" // reuse these for now
20#include "wt_rgb_backlight_keycodes.h" // reuse these for now
21
18#include "drivers/avr/i2c_master.h" 22#include "drivers/avr/i2c_master.h"
19#include "drivers/issi/is31fl3736.h" 23#include "drivers/issi/is31fl3736.h"
20
21#include <avr/interrupt.h> 24#include <avr/interrupt.h>
22 25
26#include "progmem.h"
27#include "quantum/color.h"
28
23#define ISSI_ADDR_DEFAULT 0x50 29#define ISSI_ADDR_DEFAULT 0x50
24 30
31#define BACKLIGHT_EFFECT_MAX 3
32
33#ifndef MONO_BACKLIGHT_COLOR_1
34#define MONO_BACKLIGHT_COLOR_1 { .h = 0, .s = 255 }
35#endif
36
37backlight_config g_config = {
38 .disable_when_usb_suspended = MONO_BACKLIGHT_DISABLE_WHEN_USB_SUSPENDED,
39 .disable_after_timeout = MONO_BACKLIGHT_DISABLE_AFTER_TIMEOUT,
40 .brightness = MONO_BACKLIGHT_BRIGHTNESS,
41 .effect = MONO_BACKLIGHT_EFFECT,
42 .effect_speed = MONO_BACKLIGHT_EFFECT_SPEED,
43 .color_1 = MONO_BACKLIGHT_COLOR_1,
44};
45
25bool g_suspend_state = false; 46bool g_suspend_state = false;
47uint8_t g_indicator_state = 0;
26 48
27// Global tick at 20 Hz 49// Global tick at 20 Hz
28uint32_t g_tick = 0; 50uint32_t g_tick = 0;
29uint8_t g_config_effect_speed = 0; 51
30uint8_t g_config_brightness = 255; 52// Ticks since any key was last hit.
53uint32_t g_any_key_hit = 0;
31 54
32void backlight_init_drivers(void) 55void backlight_init_drivers(void)
33{ 56{
@@ -41,6 +64,10 @@ void backlight_init_drivers(void)
41 IS31FL3736_update_led_control_registers( ISSI_ADDR_DEFAULT, 0x00 ); 64 IS31FL3736_update_led_control_registers( ISSI_ADDR_DEFAULT, 0x00 );
42} 65}
43 66
67void backlight_set_key_hit(uint8_t row, uint8_t column)
68{
69 g_any_key_hit = 0;
70}
44 71
45// This is (F_CPU/1024) / 20 Hz 72// This is (F_CPU/1024) / 20 Hz
46// = 15625 Hz / 20 Hz 73// = 15625 Hz / 20 Hz
@@ -82,13 +109,63 @@ void backlight_set_suspend_state(bool state)
82 g_suspend_state = state; 109 g_suspend_state = state;
83} 110}
84 111
112void backlight_set_indicator_state(uint8_t state)
113{
114 g_indicator_state = state;
115}
116
117void backlight_set_brightness_all( uint8_t value )
118{
119 IS31FL3736_mono_set_brightness_all( value );
120}
121
122void backlight_effect_all_off(void)
123{
124 IS31FL3736_mono_set_brightness_all( 0 );
125}
126
127void backlight_effect_all_on(void)
128{
129 IS31FL3736_mono_set_brightness_all( g_config.brightness );
130}
131
132void backlight_effect_raindrops(bool initialize)
133{
134 // Change one LED every tick
135 uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % 96 : 255;
136
137 for ( int i=0; i<96; i++ )
138 {
139 // If initialize, all get set to random brightness
140 // If not, all but one will stay the same as before.
141 if ( initialize || i == led_to_change )
142 {
143 IS31FL3736_mono_set_brightness(i, rand() & 0xFF );
144 }
145 }
146}
147
85void backlight_effect_cycle_all(void) 148void backlight_effect_cycle_all(void)
86{ 149{
87 uint8_t offset = ( g_tick << g_config_effect_speed ) & 0xFF; 150 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
88 151
89 backlight_set_brightness_all( offset ); 152 backlight_set_brightness_all( offset );
90} 153}
91 154
155// This runs after another backlight effect and replaces
156// colors already set
157void backlight_effect_indicators(void)
158{
159#if defined(MONO_BACKLIGHT_WT75_A)
160 HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
161 RGB rgb = hsv_to_rgb( hsv );
162 // G8, H8, I8 -> (6*8+7) (7*8+7), (8*8+7)
163 IS31FL3736_mono_set_brightness(55, rgb.r);
164 IS31FL3736_mono_set_brightness(63, rgb.g);
165 IS31FL3736_mono_set_brightness(71, rgb.b);
166#endif // MONO_BACKLIGHT_WT75_A
167}
168
92ISR(TIMER3_COMPA_vect) 169ISR(TIMER3_COMPA_vect)
93{ 170{
94 // delay 1 second before driving LEDs or doing anything else 171 // delay 1 second before driving LEDs or doing anything else
@@ -100,8 +177,154 @@ ISR(TIMER3_COMPA_vect)
100 177
101 g_tick++; 178 g_tick++;
102 179
103 //backlight_effect_cycle_all(); 180 if ( g_any_key_hit < 0xFFFFFFFF )
104 backlight_set_brightness_all( 255 ); 181 {
182 g_any_key_hit++;
183 }
184
185 // Ideally we would also stop sending zeros to the LED driver PWM buffers
186 // while suspended and just do a software shutdown. This is a cheap hack for now.
187 bool suspend_backlight = ((g_suspend_state && g_config.disable_when_usb_suspended) ||
188 (g_config.disable_after_timeout > 0 && g_any_key_hit > g_config.disable_after_timeout * 60 * 20));
189 uint8_t effect = suspend_backlight ? 0 : g_config.effect;
190
191 // Keep track of the effect used last time,
192 // detect change in effect, so each effect can
193 // have an optional initialization.
194 static uint8_t effect_last = 255;
195 bool initialize = effect != effect_last;
196 effect_last = effect;
197
198 // this gets ticked at 20 Hz.
199 // each effect can opt to do calculations
200 // and/or request PWM buffer updates.
201 switch ( effect )
202 {
203 case 0:
204 backlight_effect_all_off();
205 break;
206 case 1:
207 backlight_effect_all_on();;
208 break;
209 case 2:
210 backlight_effect_raindrops(initialize);
211 break;
212 default:
213 backlight_effect_all_off();
214 break;
215 }
216
217 if ( ! suspend_backlight )
218 {
219 backlight_effect_indicators();
220 }
221}
222
223// Some helpers for setting/getting HSV
224void _set_color( HS *color, uint8_t *data )
225{
226 color->h = data[0];
227 color->s = data[1];
228}
229
230void _get_color( HS *color, uint8_t *data )
231{
232 data[0] = color->h;
233 data[1] = color->s;
234}
235
236void backlight_config_set_value( uint8_t *data )
237{
238 bool reinitialize = false;
239 uint8_t *value_id = &(data[0]);
240 uint8_t *value_data = &(data[1]);
241 switch ( *value_id )
242 {
243 case id_disable_when_usb_suspended:
244 {
245 g_config.disable_when_usb_suspended = (bool)*value_data;
246 break;
247 }
248 case id_disable_after_timeout:
249 {
250 g_config.disable_after_timeout = *value_data;
251 break;
252 }
253 case id_brightness:
254 {
255 g_config.brightness = *value_data;
256 break;
257 }
258 case id_effect:
259 {
260 g_config.effect = *value_data;
261 break;
262 }
263 case id_effect_speed:
264 {
265 g_config.effect_speed = *value_data;
266 break;
267 }
268 case id_color_1:
269 {
270 _set_color( &(g_config.color_1), value_data );
271 break;
272 }
273 }
274
275 if ( reinitialize )
276 {
277 backlight_init_drivers();
278 }
279}
280
281void backlight_config_get_value( uint8_t *data )
282{
283 uint8_t *value_id = &(data[0]);
284 uint8_t *value_data = &(data[1]);
285 switch ( *value_id )
286 {
287 case id_disable_when_usb_suspended:
288 {
289 *value_data = ( g_config.disable_when_usb_suspended ? 1 : 0 );
290 break;
291 }
292 case id_disable_after_timeout:
293 {
294 *value_data = g_config.disable_after_timeout;
295 break;
296 }
297 case id_brightness:
298 {
299 *value_data = g_config.brightness;
300 break;
301 }
302 case id_effect:
303 {
304 *value_data = g_config.effect;
305 break;
306 }
307 case id_effect_speed:
308 {
309 *value_data = g_config.effect_speed;
310 break;
311 }
312 case id_color_1:
313 {
314 _get_color( &(g_config.color_1), value_data );
315 break;
316 }
317 }
318}
319
320void backlight_config_load(void)
321{
322 eeprom_read_block( &g_config, ((void*)MONO_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
323}
324
325void backlight_config_save(void)
326{
327 eeprom_update_block( &g_config, ((void*)MONO_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
105} 328}
106 329
107void backlight_update_pwm_buffers(void) 330void backlight_update_pwm_buffers(void)
@@ -109,8 +332,110 @@ void backlight_update_pwm_buffers(void)
109 IS31FL3736_update_pwm_buffers(ISSI_ADDR_DEFAULT,0x00); 332 IS31FL3736_update_pwm_buffers(ISSI_ADDR_DEFAULT,0x00);
110} 333}
111 334
112void backlight_set_brightness_all( uint8_t value ) 335bool process_record_backlight(uint16_t keycode, keyrecord_t *record)
113{ 336{
114 IS31FL3736_mono_set_brightness_all( value ); 337 // Record keypresses for backlight effects
338 if ( record->event.pressed )
339 {
340 backlight_set_key_hit( record->event.key.row, record->event.key.col );
341 }
342
343 switch(keycode)
344 {
345 case BR_INC:
346 if (record->event.pressed)
347 {
348 backlight_brightness_increase();
349 }
350 return false;
351 break;
352 case BR_DEC:
353 if (record->event.pressed)
354 {
355 backlight_brightness_decrease();
356 }
357 return false;
358 break;
359 case EF_INC:
360 if (record->event.pressed)
361 {
362 backlight_effect_increase();
363 }
364 return false;
365 break;
366 case EF_DEC:
367 if (record->event.pressed)
368 {
369 backlight_effect_decrease();
370 }
371 return false;
372 break;
373 case ES_INC:
374 if (record->event.pressed)
375 {
376 backlight_effect_speed_increase();
377 }
378 return false;
379 break;
380 case ES_DEC:
381 if (record->event.pressed)
382 {
383 backlight_effect_speed_decrease();
384 }
385 return false;
386 break;
387 }
388
389 return true;
390}
391
392// Deals with the messy details of incrementing an integer
393uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
394{
395 int16_t new_value = value;
396 new_value += step;
397 return MIN( MAX( new_value, min ), max );
115} 398}
116 399
400uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
401{
402 int16_t new_value = value;
403 new_value -= step;
404 return MIN( MAX( new_value, min ), max );
405}
406
407void backlight_effect_increase(void)
408{
409 g_config.effect = increment( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
410 backlight_config_save();
411}
412
413void backlight_effect_decrease(void)
414{
415 g_config.effect = decrement( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
416 backlight_config_save();
417}
418
419void backlight_effect_speed_increase(void)
420{
421 g_config.effect_speed = increment( g_config.effect_speed, 1, 0, 3 );
422 backlight_config_save();
423}
424
425void backlight_effect_speed_decrease(void)
426{
427 g_config.effect_speed = decrement( g_config.effect_speed, 1, 0, 3 );
428 backlight_config_save();
429}
430
431void backlight_brightness_increase(void)
432{
433 g_config.brightness = increment( g_config.brightness, 8, 0, 255 );
434 backlight_config_save();
435}
436
437void backlight_brightness_decrease(void)
438{
439 g_config.brightness = decrement( g_config.brightness, 8, 0, 255 );
440 backlight_config_save();
441}