]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lfkeyboards/lfkpad/lfkpad.c
Lfkeyboards updates (#2421)
[qmk_firmware.git] / keyboards / lfkeyboards / lfkpad / lfkpad.c
1 #include <avr/sfr_defs.h>
2 #include <avr/timer_avr.h>
3 #include <avr/wdt.h>
4 #include "lfkpad.h"
5 #include "keymap.h"
6 #include "issi.h"
7 #include "TWIlib.h"
8 #include "lighting.h"
9 #include "debug.h"
10 #include "quantum.h"
11
12 uint16_t click_hz = CLICK_HZ;
13 uint16_t click_time = CLICK_MS;
14 uint8_t click_toggle = CLICK_ENABLED;
15
16
17 void matrix_init_kb(void)
18 {
19     matrix_init_user();
20 #ifndef AUDIO_ENABLE
21     // If we're not using the audio pin, drive it low
22     sbi(DDRC, 6);
23     cbi(PORTC, 6);
24 #endif
25
26 #ifdef ISSI_ENABLE
27     issi_init();
28 #endif
29 #ifdef WATCHDOG_ENABLE
30     // This is done after turning the layer LED red, if we're caught in a loop
31     // we should get a flashing red light
32     wdt_enable(WDTO_500MS);
33 #endif
34
35 }
36
37 void matrix_scan_kb(void)
38 {
39 #ifdef WATCHDOG_ENABLE
40     wdt_reset();
41 #endif
42 #ifdef ISSI_ENABLE
43     // switch/underglow lighting update
44     static uint32_t issi_device = 0;
45     static uint32_t twi_last_ready = 0;
46     if(twi_last_ready > 1000){
47         // Its been way too long since the last ISSI update, reset the I2C bus and start again
48         dprintf("TWI failed to recover, TWI re-init\n");
49         twi_last_ready = 0;
50         TWIInit();
51         force_issi_refresh();
52     }
53     if(isTWIReady()){
54         twi_last_ready = 0;
55         // If the i2c bus is available, kick off the issi update, alternate between devices
56         update_issi(issi_device, issi_device);
57         if(issi_device){
58             issi_device = 0;
59         }else{
60             issi_device = 3;
61         }
62     }else{
63         twi_last_ready++;
64     }
65 #endif
66     matrix_scan_user();
67 }
68
69 void click(uint16_t freq, uint16_t duration){
70 #ifdef AUDIO_ENABLE
71     if(freq >= 100 && freq <= 20000 && duration < 100){
72         play_note(freq, 10);
73         for (uint16_t i = 0; i < duration; i++){
74             _delay_ms(1);
75         }
76         stop_all_notes();
77     }
78 #endif
79 }
80
81 bool process_record_kb(uint16_t keycode, keyrecord_t* record)
82 {
83     if (click_toggle && record->event.pressed){
84         click(click_hz, click_time);
85     }
86     if (keycode == RESET) {
87         reset_keyboard_kb();
88     } else {
89     }
90     return process_record_user(keycode, record);
91 }
92
93 void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
94 {
95 #ifdef AUDIO_ENABLE
96     int8_t sign = 1;
97 #endif
98     if(id == LFK_ESC_TILDE){
99         // Send ~ on shift-esc
100         void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
101         uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
102         if(layer_state == 0){
103             method(shifted ? KC_GRAVE : KC_ESCAPE);
104         }else{
105             method(shifted ? KC_ESCAPE : KC_GRAVE);
106         }
107         send_keyboard_report();
108     }else if(event->event.pressed){
109         switch(id){
110             case LFK_SET_DEFAULT_LAYER:
111                 // set/save the current base layer to eeprom, falls through to LFK_CLEAR
112                 eeconfig_update_default_layer(1UL << opt);
113                 default_layer_set(1UL << opt);
114             case LFK_CLEAR:
115                 // Go back to default layer
116                 layer_clear();
117                 break;
118 #ifdef AUDIO_ENABLE
119             case LFK_CLICK_FREQ_LOWER:
120                 sign = -1;  // continue to next statement
121             case LFK_CLICK_FREQ_HIGHER:
122                 click_hz += sign * 100;
123                 click(click_hz, click_time);
124                 break;
125             case LFK_CLICK_TOGGLE:
126                 if(click_toggle){
127                     click_toggle = 0;
128                     click(4000, 100);
129                     click(1000, 100);
130                 }else{
131                     click_toggle = 1;
132                     click(1000, 100);
133                     click(4000, 100);
134                 }
135                 break;
136             case LFK_CLICK_TIME_SHORTER:
137                 sign = -1;  // continue to next statement
138             case LFK_CLICK_TIME_LONGER:
139                 click_time += sign;
140                 click(click_hz, click_time);
141                 break;
142 #endif
143         }
144     }
145 }
146
147 void reset_keyboard_kb(){
148 #ifdef WATCHDOG_ENABLE
149     MCUSR = 0;
150     wdt_disable();
151     wdt_reset();
152 #endif
153     reset_keyboard();
154 }
155
156 void led_set_kb(uint8_t usb_led)
157 {
158     led_set_user(usb_led);
159 }
160
161 // LFK lighting info
162 const uint8_t rgb_matrices[] = {0, 1};
163 const uint8_t rgb_sequence[] = {
164     32,  1,  2,  3,
165     31, 30,  5,  6,
166     28, 27,  7,  8,
167     17, 18,  9,
168     19, 21, 11, 12,
169       22,   14,
170
171     16,         26,
172      4,         25,
173     13,         24,
174           20
175
176 };