aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/split_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/split_common/split_util.c')
-rw-r--r--quantum/split_common/split_util.c152
1 files changed, 47 insertions, 105 deletions
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c
index e41b6f638..5095cb8fd 100644
--- a/quantum/split_common/split_util.c
+++ b/quantum/split_common/split_util.c
@@ -4,142 +4,84 @@
4#include "config.h" 4#include "config.h"
5#include "timer.h" 5#include "timer.h"
6#include "split_flags.h" 6#include "split_flags.h"
7#include "transport.h"
7#include "quantum.h" 8#include "quantum.h"
8 9
9#ifdef EE_HANDS 10#ifdef EE_HANDS
10# include "tmk_core/common/eeprom.h" 11# include "tmk_core/common/eeprom.h"
11#endif 12# include "eeconfig.h"
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 13#endif
20 14
21volatile bool isLeftHand = true; 15volatile bool isLeftHand = true;
22 16
23volatile uint8_t setTries = 0; 17__attribute__((weak))
24 18bool is_keyboard_left(void) {
25static void setup_handedness(void) {
26 #ifdef SPLIT_HAND_PIN 19 #ifdef SPLIT_HAND_PIN
27 // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand 20 // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
28 setPinInput(SPLIT_HAND_PIN); 21 setPinInput(SPLIT_HAND_PIN);
29 isLeftHand = readPin(SPLIT_HAND_PIN); 22 return readPin(SPLIT_HAND_PIN);
30 #else 23 #else
31 #ifdef EE_HANDS 24 #ifdef EE_HANDS
32 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); 25 return eeprom_read_byte(EECONFIG_HANDEDNESS);
33 #else 26 #else
34 #ifdef MASTER_RIGHT 27 #ifdef MASTER_RIGHT
35 isLeftHand = !has_usb(); 28 return !is_keyboard_master();
36 #else 29 #else
37 isLeftHand = has_usb(); 30 return is_keyboard_master();
38 #endif 31 #endif
39 #endif 32 #endif
40 #endif 33 #endif
41} 34}
42 35
43static void keyboard_master_setup(void) { 36bool is_keyboard_master(void)
44#if defined(USE_I2C) || defined(EH) 37{
45 i2c_master_init(); 38#ifdef __AVR__
46 #ifdef SSD1306OLED 39 static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
47 matrix_master_OLED_init ();
48 #endif
49#else
50 serial_master_init();
51#endif
52 40
53 // For master the Backlight info needs to be sent on startup 41 // only check once, as this is called often
54 // Otherwise the salve won't start with the proper info until an update 42 if (usbstate == UNKNOWN)
55 BACKLIT_DIRTY = true; 43 {
56} 44 USBCON |= (1 << OTGPADE); // enables VBUS pad
45 wait_us(5);
57 46
58static void keyboard_slave_setup(void) { 47 usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
59 timer_init(); 48 }
60#if defined(USE_I2C) || defined(EH) 49
61 i2c_slave_init(SLAVE_I2C_ADDRESS); 50 return (usbstate == MASTER);
62#else 51#else
63 serial_slave_init(); 52 return true;
64#endif 53#endif
65} 54}
66 55
67bool has_usb(void) { 56static void keyboard_master_setup(void) {
68 USBCON |= (1 << OTGPADE); //enables VBUS pad 57#if defined(USE_I2C) || defined(EH)
69 _delay_us(5); 58 #ifdef SSD1306OLED
70 return (USBSTA & (1<<VBUS)); //checks state of VBUS 59 matrix_master_OLED_init ();
71} 60 #endif
72 61#endif
73void split_keyboard_setup(void) { 62 transport_master_init();
74 setup_handedness();
75 63
76 if (has_usb()) { 64 // For master the Backlight info needs to be sent on startup
77 keyboard_master_setup(); 65 // Otherwise the salve won't start with the proper info until an update
78 } else { 66 BACKLIT_DIRTY = true;
79 keyboard_slave_setup();
80 }
81 sei();
82} 67}
83 68
84void keyboard_slave_loop(void) { 69static void keyboard_slave_setup(void)
85 matrix_init(); 70{
86 71 transport_slave_init();
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} 72}
136 73
137// this code runs before the usb and keyboard is initialized 74// this code runs before the usb and keyboard is initialized
138void matrix_setup(void) { 75void matrix_setup(void)
139 split_keyboard_setup(); 76{
140 77 isLeftHand = is_keyboard_left();
141 if (!has_usb()) { 78
142 //rgblight_init(); 79 if (is_keyboard_master())
143 keyboard_slave_loop(); 80 {
144 } 81 keyboard_master_setup();
82 }
83 else
84 {
85 keyboard_slave_setup();
86 }
145} 87}