]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
Convert split_common to use generic GPIO api
[qmk_firmware.git] / quantum / split_common / split_util.c
1 #include "split_util.h"
2 #include "matrix.h"
3 #include "keyboard.h"
4 #include "config.h"
5 #include "timer.h"
6 #include "split_flags.h"
7 #include "quantum.h"
8
9 #ifdef BACKLIGHT_ENABLE
10 #   include "backlight.h"
11 #endif
12
13 #if defined(USE_I2C) || defined(EH)
14 #  include "i2c.h"
15 #endif
16
17 volatile bool isLeftHand = true;
18
19 volatile uint8_t setTries = 0;
20
21 static void setup_handedness(void) {
22   #ifdef SPLIT_HAND_PIN
23     // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
24     setPinInput(SPLIT_HAND_PIN);
25     isLeftHand = readPin(SPLIT_HAND_PIN);
26   #else
27     #ifdef EE_HANDS
28       isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
29     #else
30       #ifdef MASTER_RIGHT
31         isLeftHand = !has_usb();
32       #else
33         isLeftHand = has_usb();
34       #endif
35     #endif
36   #endif
37 }
38
39 static void keyboard_master_setup(void) {
40 #if defined(USE_I2C) || defined(EH)
41   i2c_master_init();
42   #ifdef SSD1306OLED
43     matrix_master_OLED_init ();
44   #endif
45 #else
46   serial_master_init();
47 #endif
48
49     // For master the Backlight info needs to be sent on startup
50     // Otherwise the salve won't start with the proper info until an update
51     BACKLIT_DIRTY = true;
52 }
53
54 static void keyboard_slave_setup(void) {
55   timer_init();
56 #if defined(USE_I2C) || defined(EH)
57     i2c_slave_init(SLAVE_I2C_ADDRESS);
58 #else
59     serial_slave_init();
60 #endif
61 }
62
63 bool has_usb(void) {
64    USBCON |= (1 << OTGPADE); //enables VBUS pad
65    _delay_us(5);
66    return (USBSTA & (1<<VBUS));  //checks state of VBUS
67 }
68
69 void split_keyboard_setup(void) {
70    setup_handedness();
71
72    if (has_usb()) {
73       keyboard_master_setup();
74    } else {
75       keyboard_slave_setup();
76    }
77    sei();
78 }
79
80 void keyboard_slave_loop(void) {
81    matrix_init();
82
83    //Init RGB
84    #ifdef RGBLIGHT_ENABLE
85       rgblight_init();
86    #endif
87
88    while (1) {
89     // Matrix Slave Scan
90     matrix_slave_scan();
91
92     // Read Backlight Info
93     #ifdef BACKLIGHT_ENABLE
94         #ifdef USE_I2C
95             if (BACKLIT_DIRTY) {
96                 backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
97                 BACKLIT_DIRTY = false;
98             }
99         #else // USE_SERIAL
100             backlight_set(serial_m2s_buffer.backlight_level);
101         #endif
102     #endif
103     // Read RGB Info
104     #ifdef RGBLIGHT_ENABLE
105         #ifdef USE_I2C
106             if (RGB_DIRTY) {
107                 // Disable interupts (RGB data is big)
108                 cli();
109                 // Create new DWORD for RGB data
110                 uint32_t dword;
111
112                 // Fill the new DWORD with the data that was sent over
113                 uint8_t *dword_dat = (uint8_t *)(&dword);
114                 for (int i = 0; i < 4; i++) {
115                     dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
116                 }
117
118                 // Update the RGB now with the new data and set RGB_DIRTY to false
119                 rgblight_update_dword(dword);
120                 RGB_DIRTY = false;
121                 // Re-enable interupts now that RGB is set
122                 sei();
123             }
124         #else // USE_SERIAL
125           #ifdef RGBLIGHT_SPLIT
126             // Add serial implementation for RGB here
127           #endif
128         #endif
129     #endif
130    }
131 }
132
133 // this code runs before the usb and keyboard is initialized
134 void matrix_setup(void) {
135     split_keyboard_setup();
136
137     if (!has_usb()) {
138         //rgblight_init();
139         keyboard_slave_loop();
140     }
141 }