]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
Update split keyboard docs (#4735)
[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       #ifdef(MASTER_RIGHT)
40         isLeftHand = !has_usb();
41       #else
42         isLeftHand = has_usb();
43       #endif
44     #endif
45   #endif
46 }
47
48 static void keyboard_master_setup(void) {
49 #if defined(USE_I2C) || defined(EH)
50   i2c_master_init();
51   #ifdef SSD1306OLED
52     matrix_master_OLED_init ();
53   #endif
54 #else
55   serial_master_init();
56 #endif
57
58     // For master the Backlight info needs to be sent on startup
59     // Otherwise the salve won't start with the proper info until an update
60     BACKLIT_DIRTY = true;
61 }
62
63 static void keyboard_slave_setup(void) {
64   timer_init();
65 #if defined(USE_I2C) || defined(EH)
66     i2c_slave_init(SLAVE_I2C_ADDRESS);
67 #else
68     serial_slave_init();
69 #endif
70 }
71
72 bool has_usb(void) {
73    USBCON |= (1 << OTGPADE); //enables VBUS pad
74    _delay_us(5);
75    return (USBSTA & (1<<VBUS));  //checks state of VBUS
76 }
77
78 void split_keyboard_setup(void) {
79    setup_handedness();
80
81    if (has_usb()) {
82       keyboard_master_setup();
83    } else {
84       keyboard_slave_setup();
85    }
86    sei();
87 }
88
89 void keyboard_slave_loop(void) {
90    matrix_init();
91
92    //Init RGB
93    #ifdef RGBLIGHT_ENABLE
94       rgblight_init();
95    #endif
96
97    while (1) {
98     // Matrix Slave Scan
99     matrix_slave_scan();
100
101     // Read Backlight Info
102     #ifdef BACKLIGHT_ENABLE
103         #ifdef USE_I2C
104             if (BACKLIT_DIRTY) {
105                 backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
106                 BACKLIT_DIRTY = false;
107             }
108         #else // USE_SERIAL
109             backlight_set(serial_m2s_buffer.backlight_level);
110         #endif
111     #endif
112     // Read RGB Info
113     #ifdef RGBLIGHT_ENABLE
114         #ifdef USE_I2C
115             if (RGB_DIRTY) {
116                 // Disable interupts (RGB data is big)
117                 cli();
118                 // Create new DWORD for RGB data
119                 uint32_t dword;
120
121                 // Fill the new DWORD with the data that was sent over
122                 uint8_t *dword_dat = (uint8_t *)(&dword);
123                 for (int i = 0; i < 4; i++) {
124                     dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
125                 }
126
127                 // Update the RGB now with the new data and set RGB_DIRTY to false
128                 rgblight_update_dword(dword);
129                 RGB_DIRTY = false;
130                 // Re-enable interupts now that RGB is set
131                 sei();
132             }
133         #else // USE_SERIAL
134           #ifdef RGBLIGHT_SPLIT
135             // Add serial implementation for RGB here
136           #endif
137         #endif
138     #endif
139    }
140 }
141
142 // this code runs before the usb and keyboard is initialized
143 void matrix_setup(void) {
144     split_keyboard_setup();
145
146     if (!has_usb()) {
147         //rgblight_init();
148         keyboard_slave_loop();
149     }
150 }