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