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