]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/meira/meira.c
Fix keyboard not working if I2C pullups not enabled.
[qmk_firmware.git] / keyboards / meira / meira.c
1 /* Copyright 2017 Cole Markham, WoodKeys.click
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "meira.h"
17 #include "issi.h"
18 #include "TWIlib.h"
19 #include "lighting.h"
20 #include "quantum.h"
21 #define BACKLIGHT_BREATHING
22
23 extern void backlight_set(uint8_t level);
24
25 #ifdef AUDIO_ENABLE
26     float tone_startup[][2] = SONG(STARTUP_SOUND);
27     float tone_goodbye[][2] = SONG(GOODBYE_SOUND);
28 #endif
29
30
31 void shutdown_user(void) {
32     #ifdef AUDIO_ENABLE
33         PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
34     _delay_ms(150);
35     stop_all_notes();
36     #endif
37 }
38
39
40 void matrix_init_kb(void)
41 {
42     debug_enable=true;
43     print("meira matrix_init_kb\n");
44 #ifdef AUDIO_ENABLE
45     _delay_ms(20); // gets rid of tick
46     PLAY_NOTE_ARRAY(tone_startup, false, 0);
47 #endif
48
49
50 #ifdef ISSI_ENABLE
51     issi_init();
52 #endif
53     backlight_set(5);
54 #ifdef WATCHDOG_ENABLE
55     // This is done after turning the layer LED red, if we're caught in a loop
56     // we should get a flashing red light
57     wdt_enable(WDTO_500MS);
58 #endif
59
60     // put your keyboard start-up code here
61     // runs once when the firmware starts up
62     matrix_init_user();
63 }
64
65 void matrix_scan_kb(void)
66 {
67 #ifdef WATCHDOG_ENABLE
68     wdt_reset();
69 #endif
70 #ifdef ISSI_ENABLE
71     // switch/underglow lighting update
72     static uint32_t issi_device = 0;
73     static uint32_t twi_last_ready = 0;
74     if(twi_last_ready > 1000){
75         // Its been way too long since the last ISSI update, reset the I2C bus and start again
76         xprintf("TWI failed to recover, TWI re-init\n");
77         twi_last_ready = 0;
78         TWIInit();
79         force_issi_refresh();
80     }
81     if(isTWIReady()){
82         twi_last_ready = 0;
83         // If the i2c bus is available, kick off the issi update, alternate between devices
84         update_issi(issi_device, issi_device);
85         if(issi_device){
86             issi_device = 0;
87         }else{
88             issi_device = 3;
89         }
90     }else{
91         twi_last_ready++;
92     }
93 #endif
94     matrix_scan_user();
95 }
96
97 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
98     // Test code that turns on the switch led for the key that is pressed
99     // set_backlight_by_keymap(record->event.key.col, record->event.key.row);
100     if (keycode == RESET) {
101         reset_keyboard_kb();
102     } else {
103     }
104         return process_record_user(keycode, record);
105 }
106
107 void led_set_kb(uint8_t usb_led) {
108         // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
109         led_set_user(usb_led);
110 }
111
112 //void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
113 //{
114 //#ifdef AUDIO_ENABLE
115 //    int8_t sign = 1;
116 //#endif
117 //    if(id == LFK_ESC_TILDE){
118 //        // Send ~ on shift-esc
119 //        void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
120 //        uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
121 //        method(shifted ? KC_GRAVE : KC_ESCAPE);
122 //        send_keyboard_report();
123 //    }else if(event->event.pressed){
124 //        switch(id){
125 //            case LFK_CLEAR:
126 //                // Go back to default layer
127 //                layer_clear();
128 //                break;
129 //#ifdef ISSI_ENABLE
130 //            case LFK_LED_TEST:
131 //                led_test();
132 //                break;
133 //#endif
134 //        }
135 //    }
136 //}
137
138 void reset_keyboard_kb(){
139 #ifdef WATCHDOG_ENABLE
140     MCUSR = 0;
141     wdt_disable();
142     wdt_reset();
143 #endif
144     xprintf("programming!\n");
145     reset_keyboard();
146 }