Files
qmk/quantum/split_common/split_util.c
T

82 lines
2.0 KiB
C
Raw Normal View History

2018-07-16 19:25:02 -07:00
#include "split_util.h"
#include "matrix.h"
#include "keyboard.h"
#include "config.h"
#include "timer.h"
#include "transport.h"
#include "quantum.h"
2018-07-16 19:25:02 -07:00
2019-01-03 14:30:23 +11:00
#ifdef EE_HANDS
2019-08-30 11:19:03 -07:00
# include "tmk_core/common/eeprom.h"
# include "eeconfig.h"
2018-07-16 19:25:02 -07:00
#endif
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
2019-08-30 11:19:03 -07:00
# include "rgblight.h"
#endif
2018-07-16 19:25:02 -07:00
volatile bool isLeftHand = true;
2019-08-30 11:19:03 -07:00
__attribute__((weak)) bool is_keyboard_left(void) {
#if defined(SPLIT_HAND_PIN)
2018-07-16 19:25:02 -07:00
// Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
setPinInput(SPLIT_HAND_PIN);
return readPin(SPLIT_HAND_PIN);
2019-08-30 11:19:03 -07:00
#elif defined(EE_HANDS)
return eeprom_read_byte(EECONFIG_HANDEDNESS);
2019-08-30 11:19:03 -07:00
#elif defined(MASTER_RIGHT)
return !is_keyboard_master();
2019-08-30 11:19:03 -07:00
#endif
2019-08-30 11:19:03 -07:00
return is_keyboard_master();
2018-07-16 19:25:02 -07:00
}
2019-08-30 11:19:03 -07:00
__attribute__((weak)) bool is_keyboard_master(void) {
#ifdef __AVR__
2019-08-30 11:19:03 -07:00
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
2019-08-30 11:19:03 -07:00
// only check once, as this is called often
if (usbstate == UNKNOWN) {
USBCON |= (1 << OTGPADE); // enables VBUS pad
wait_us(5);
2019-08-30 11:19:03 -07:00
usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
}
2019-08-30 11:19:03 -07:00
return (usbstate == MASTER);
#else
2019-08-30 11:19:03 -07:00
return true;
#endif
}
2018-07-16 19:25:02 -07:00
static void keyboard_master_setup(void) {
#if defined(USE_I2C) || defined(EH)
2019-08-30 11:19:03 -07:00
# ifdef SSD1306OLED
matrix_master_OLED_init();
# endif
2018-07-16 19:25:02 -07:00
#endif
2019-08-30 11:19:03 -07:00
transport_master_init();
2018-07-16 19:25:02 -07:00
}
2019-08-30 11:19:03 -07:00
static void keyboard_slave_setup(void) { transport_slave_init(); }
2018-07-16 19:25:02 -07:00
// this code runs before the usb and keyboard is initialized
2019-08-30 11:19:03 -07:00
void matrix_setup(void) {
isLeftHand = is_keyboard_left();
2018-07-16 19:25:02 -07:00
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
2019-08-30 11:19:03 -07:00
uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
if (isLeftHand) {
rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
} else {
rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
}
#endif
2019-08-30 11:19:03 -07:00
if (is_keyboard_master()) {
keyboard_master_setup();
} else {
keyboard_slave_setup();
}
2018-07-16 19:25:02 -07:00
}