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