diff options
Diffstat (limited to 'drivers')
50 files changed, 579 insertions, 179 deletions
diff --git a/drivers/bluetooth/bluefruit_le.cpp b/drivers/bluetooth/bluefruit_le.cpp index 19310767cf..39c14ddd13 100644 --- a/drivers/bluetooth/bluefruit_le.cpp +++ b/drivers/bluetooth/bluefruit_le.cpp @@ -5,7 +5,7 @@ #include <alloca.h> #include "debug.h" #include "timer.h" -#include "action_util.h" +#include "gpio.h" #include "ringbuffer.hpp" #include <string.h> #include "spi_master.h" @@ -79,9 +79,7 @@ struct sdep_msg { enum queue_type { QTKeyReport, // 1-byte modifier + 6-byte key report QTConsumer, // 16-bit key code -#ifdef MOUSE_ENABLE QTMouseMove, // 4-byte mouse report -#endif }; struct queue_item { @@ -290,7 +288,7 @@ static void resp_buf_wait(const char *cmd) { } } -static bool ble_init(void) { +void bluefruit_le_init(void) { state.initialized = false; state.configured = false; state.is_connected = false; @@ -309,7 +307,6 @@ static bool ble_init(void) { wait_ms(1000); // Give it a second to initialize state.initialized = true; - return state.initialized; } static inline uint8_t min(uint8_t a, uint8_t b) { @@ -433,7 +430,7 @@ bool bluefruit_le_is_connected(void) { bool bluefruit_le_enable_keyboard(void) { char resbuf[128]; - if (!state.initialized && !ble_init()) { + if (!state.initialized) { return false; } @@ -442,7 +439,7 @@ bool bluefruit_le_enable_keyboard(void) { // Disable command echo static const char kEcho[] PROGMEM = "ATE=0"; // Make the advertised name match the keyboard - static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); + static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" PRODUCT; // Turn on keyboard support static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; @@ -581,10 +578,12 @@ static bool process_queue_item(struct queue_item *item, uint16_t timeout) { snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, item->key.keys[0], item->key.keys[1], item->key.keys[2], item->key.keys[3], item->key.keys[4], item->key.keys[5]); return at_command(cmdbuf, NULL, 0, true, timeout); +#ifdef EXTRAKEY_ENABLE case QTConsumer: strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x")); snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer); return at_command(cmdbuf, NULL, 0, true, timeout); +#endif #ifdef MOUSE_ENABLE case QTMouseMove: @@ -613,41 +612,24 @@ static bool process_queue_item(struct queue_item *item, uint16_t timeout) { } } -void bluefruit_le_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { +void bluefruit_le_send_keyboard(report_keyboard_t *report) { struct queue_item item; - bool didWait = false; item.queue_type = QTKeyReport; - item.key.modifier = hid_modifier_mask; - item.added = timer_read(); - - while (nkeys >= 0) { - item.key.keys[0] = keys[0]; - item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; - item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; - item.key.keys[3] = nkeys >= 3 ? keys[3] : 0; - item.key.keys[4] = nkeys >= 4 ? keys[4] : 0; - item.key.keys[5] = nkeys >= 5 ? keys[5] : 0; - - if (!send_buf.enqueue(item)) { - if (!didWait) { - dprint("wait for buf space\n"); - didWait = true; - } - send_buf_send_one(); - continue; - } + item.key.modifier = report->mods; + item.key.keys[0] = report->keys[0]; + item.key.keys[1] = report->keys[1]; + item.key.keys[2] = report->keys[2]; + item.key.keys[3] = report->keys[3]; + item.key.keys[4] = report->keys[4]; + item.key.keys[5] = report->keys[5]; - if (nkeys <= 6) { - return; - } - - nkeys -= 6; - keys += 6; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); } } -void bluefruit_le_send_consumer_key(uint16_t usage) { +void bluefruit_le_send_consumer(uint16_t usage) { struct queue_item item; item.queue_type = QTConsumer; @@ -658,22 +640,20 @@ void bluefruit_le_send_consumer_key(uint16_t usage) { } } -#ifdef MOUSE_ENABLE -void bluefruit_le_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { +void bluefruit_le_send_mouse(report_mouse_t *report) { struct queue_item item; item.queue_type = QTMouseMove; - item.mousemove.x = x; - item.mousemove.y = y; - item.mousemove.scroll = scroll; - item.mousemove.pan = pan; - item.mousemove.buttons = buttons; + item.mousemove.x = report->x; + item.mousemove.y = report->y; + item.mousemove.scroll = report->v; + item.mousemove.pan = report->h; + item.mousemove.buttons = report->buttons; while (!send_buf.enqueue(item)) { send_buf_send_one(); } } -#endif uint32_t bluefruit_le_read_battery_voltage(void) { return state.vbat; diff --git a/drivers/bluetooth/bluefruit_le.h b/drivers/bluetooth/bluefruit_le.h index de301c6167..a3de03c35c 100644 --- a/drivers/bluetooth/bluefruit_le.h +++ b/drivers/bluetooth/bluefruit_le.h @@ -7,9 +7,7 @@ #include <stdbool.h> #include <stdint.h> -#include <string.h> - -#include "config_common.h" +#include "report.h" #ifdef __cplusplus extern "C" { @@ -26,6 +24,8 @@ extern bool bluefruit_le_query_is_connected(void); * calling ble_task() periodically. */ extern bool bluefruit_le_is_connected(void); +extern void bluefruit_le_init(void); + /* Call this periodically to process BLE-originated things */ extern void bluefruit_le_task(void); @@ -34,18 +34,16 @@ extern void bluefruit_le_task(void); * this set of keys. * Also sends a key release indicator, so that the keys do not remain * held down. */ -extern void bluefruit_le_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); +extern void bluefruit_le_send_keyboard(report_keyboard_t *report); /* Send a consumer usage. * (milliseconds) */ -extern void bluefruit_le_send_consumer_key(uint16_t usage); +extern void bluefruit_le_send_consumer(uint16_t usage); -#ifdef MOUSE_ENABLE /* Send a mouse/wheel movement report. * The parameters are signed and indicate positive or negative direction * change. */ -extern void bluefruit_le_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); -#endif +extern void bluefruit_le_send_mouse(report_mouse_t *report); /* Compute battery voltage by reading an analog pin. * Returns the integer number of millivolts */ diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c new file mode 100644 index 0000000000..d5382401e7 --- /dev/null +++ b/drivers/bluetooth/bluetooth.c @@ -0,0 +1,62 @@ +/* + * Copyright 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "bluetooth.h" + +#if defined(BLUETOOTH_BLUEFRUIT_LE) +# include "bluefruit_le.h" +#elif defined(BLUETOOTH_RN42) +# include "rn42.h" +#endif + +void bluetooth_init(void) { +#if defined(BLUETOOTH_BLUEFRUIT_LE) + bluefruit_le_init(); +#elif defined(BLUETOOTH_RN42) + rn42_init(); +#endif +} + +void bluetooth_task(void) { +#if defined(BLUETOOTH_BLUEFRUIT_LE) + bluefruit_le_task(); +#endif +} + +void bluetooth_send_keyboard(report_keyboard_t *report) { +#if defined(BLUETOOTH_BLUEFRUIT_LE) + bluefruit_le_send_keyboard(report); +#elif defined(BLUETOOTH_RN42) + rn42_send_keyboard(report); +#endif +} + +void bluetooth_send_mouse(report_mouse_t *report) { +#if defined(BLUETOOTH_BLUEFRUIT_LE) + bluefruit_le_send_mouse(report); +#elif defined(BLUETOOTH_RN42) + rn42_send_mouse(report); +#endif +} + +void bluetooth_send_consumer(uint16_t usage) { +#if defined(BLUETOOTH_BLUEFRUIT_LE) + bluefruit_le_send_consumer(usage); +#elif defined(BLUETOOTH_RN42) + rn42_send_consumer(usage); +#endif +} diff --git a/drivers/bluetooth/bluetooth.h b/drivers/bluetooth/bluetooth.h new file mode 100644 index 0000000000..2e4d0df538 --- /dev/null +++ b/drivers/bluetooth/bluetooth.h @@ -0,0 +1,52 @@ +/* + * Copyright 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include <stdint.h> +#include "report.h" + +/** + * \brief Initialize the Bluetooth system. + */ +void bluetooth_init(void); + +/** + * \brief Perform housekeeping tasks. + */ +void bluetooth_task(void); + +/** + * \brief Send a keyboard report. + * + * \param report The keyboard report to send. + */ +void bluetooth_send_keyboard(report_keyboard_t *report); + +/** + * \brief Send a mouse report. + * + * \param report The mouse report to send. + */ +void bluetooth_send_mouse(report_mouse_t *report); + +/** + * \brief Send a consumer usage. + * + * \param usage The consumer usage to send. + */ +void bluetooth_send_consumer(uint16_t usage); diff --git a/drivers/bluetooth/rn42.c b/drivers/bluetooth/rn42.c index 5d497cda20..0eb1733723 100644 --- a/drivers/bluetooth/rn42.c +++ b/drivers/bluetooth/rn42.c @@ -14,6 +14,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "rn42.h" + #include "report.h" #include "uart.h" @@ -69,33 +71,35 @@ void rn42_send_keyboard(report_keyboard_t *report) { uart_write(0xFD); uart_write(0x09); uart_write(0x01); + uart_write(report->mods); uart_write(0x00); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - uart_write(report->keys[i]); - } + uart_write(report->keys[0]); + uart_write(report->keys[1]); + uart_write(report->keys[2]); + uart_write(report->keys[3]); + uart_write(report->keys[4]); + uart_write(report->keys[5]); } void rn42_send_mouse(report_mouse_t *report) { uart_write(0xFD); - uart_write(0x00); - uart_write(0x03); + uart_write(0x05); + uart_write(0x02); + uart_write(report->buttons); uart_write(report->x); uart_write(report->y); - uart_write(report->v); // should try sending the wheel v here - uart_write(report->h); // should try sending the wheel h here - uart_write(0x00); + uart_write(report->v); } -void rn42_send_consumer(uint16_t data) { - static uint16_t last_data = 0; - if (data == last_data) return; - last_data = data; - uint16_t bitmap = rn42_consumer_usage_to_bitmap(data); +void rn42_send_consumer(uint16_t usage) { + uint16_t bitmap = rn42_consumer_usage_to_bitmap(usage); + uart_write(0xFD); uart_write(0x03); uart_write(0x03); + uart_write(bitmap & 0xFF); - uart_write((bitmap >> 8) & 0xFF); + uart_write(bitmap >> 8); } diff --git a/drivers/bluetooth/rn42.h b/drivers/bluetooth/rn42.h index 4747759111..89b716bfcd 100644 --- a/drivers/bluetooth/rn42.h +++ b/drivers/bluetooth/rn42.h @@ -14,6 +14,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <stdint.h> + #include "report.h" void rn42_init(void); @@ -22,4 +24,4 @@ void rn42_send_keyboard(report_keyboard_t *report); void rn42_send_mouse(report_mouse_t *report); -void rn42_send_consumer(uint16_t data); +void rn42_send_consumer(uint16_t usage); diff --git a/drivers/gpio/mcp23018.c b/drivers/gpio/mcp23018.c index 41cbfe087e..b4fbf296f8 100644 --- a/drivers/gpio/mcp23018.c +++ b/drivers/gpio/mcp23018.c @@ -18,14 +18,20 @@ enum { CMD_GPIOB = 0x13, }; -void mcp23018_init(uint8_t addr) { +bool mcp23018_init(uint8_t slave_addr) { static uint8_t s_init = 0; - if (!s_init) { + uint8_t addr = SLAVE_TO_ADDR(slave_addr); + if (0 == s_init) { i2c_init(); - wait_ms(1000); + wait_ms(100); - s_init = 1; + // probe that the expander is actually connected by reading from it + uint8_t data = 0; + if (I2C_STATUS_SUCCESS == i2c_readReg(addr, 0, &data, sizeof(data), 150)) { + s_init = 1; + } } + return (s_init > 0); } bool mcp23018_set_config(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf) { diff --git a/drivers/gpio/mcp23018.h b/drivers/gpio/mcp23018.h index e7c2730dd1..081fdb24e3 100644 --- a/drivers/gpio/mcp23018.h +++ b/drivers/gpio/mcp23018.h @@ -33,7 +33,7 @@ enum { /** * Init expander and any other dependent drivers */ -void mcp23018_init(uint8_t slave_addr); +bool mcp23018_init(uint8_t slave_addr); /** * Configure input/output to a given port diff --git a/drivers/haptic/solenoid.c b/drivers/haptic/solenoid.c index 637a77da3d..4e43903255 100644 --- a/drivers/haptic/solenoid.c +++ b/drivers/haptic/solenoid.c @@ -20,11 +20,12 @@ #include "haptic.h" #include "gpio.h" #include "usb_device_state.h" +#include "util.h" #include <stdlib.h> uint8_t solenoid_dwell = SOLENOID_DEFAULT_DWELL; static pin_t solenoid_pads[] = SOLENOID_PINS; -#define NUMBER_OF_SOLENOIDS (sizeof(solenoid_pads) / sizeof(pin_t)) +#define NUMBER_OF_SOLENOIDS ARRAY_SIZE(solenoid_pads) bool solenoid_on[NUMBER_OF_SOLENOIDS] = {false}; bool solenoid_buzzing[NUMBER_OF_SOLENOIDS] = {false}; uint16_t solenoid_start[NUMBER_OF_SOLENOIDS] = {0}; @@ -147,7 +148,7 @@ void solenoid_check(void) { void solenoid_setup(void) { #ifdef SOLENOID_PINS_ACTIVE_STATE bool state_temp[] = SOLENOID_PINS_ACTIVE_STATE; - uint8_t bound_check = (sizeof(state_temp) / sizeof(bool)); + uint8_t bound_check = ARRAY_SIZE(state_temp); #endif for (uint8_t i = 0; i < NUMBER_OF_SOLENOIDS; i++) { diff --git a/drivers/haptic/solenoid.h b/drivers/haptic/solenoid.h index 952f86e922..17f5345bc6 100644 --- a/drivers/haptic/solenoid.h +++ b/drivers/haptic/solenoid.h @@ -54,7 +54,7 @@ # endif #endif -void solenoidbuzz_on(void); +void solenoid_buzz_on(void); void solenoid_buzz_off(void); void solenoid_set_buzz(uint8_t buzz); diff --git a/drivers/led/aw20216.c b/drivers/led/aw20216.c index 55083936ef..cbb0b60774 100644 --- a/drivers/led/aw20216.c +++ b/drivers/led/aw20216.c @@ -133,7 +133,7 @@ void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { } void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { - for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { + for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; i++) { AW20216_set_color(i, red, green, blue); } } diff --git a/drivers/led/aw20216.h b/drivers/led/aw20216.h index 0a17050fed..c6e71b4b4e 100644 --- a/drivers/led/aw20216.h +++ b/drivers/led/aw20216.h @@ -28,7 +28,7 @@ typedef struct aw_led { uint8_t b; } aw_led; -extern const aw_led PROGMEM g_aw_leds[DRIVER_LED_TOTAL]; +extern const aw_led PROGMEM g_aw_leds[RGB_MATRIX_LED_COUNT]; void AW20216_init(pin_t cs_pin, pin_t en_pin); void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/ckled2001-simple.c b/drivers/led/ckled2001-simple.c index da4bf20b99..6c4ffd398e 100644 --- a/drivers/led/ckled2001-simple.c +++ b/drivers/led/ckled2001-simple.c @@ -148,7 +148,7 @@ void CKLED2001_init(uint8_t addr) { void CKLED2001_set_value(int index, uint8_t value) { ckled2001_led led; - if (index >= 0 && index < DRIVER_LED_TOTAL) { + if (index >= 0 && index < LED_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); g_pwm_buffer[led.driver][led.v] = value; @@ -157,7 +157,7 @@ void CKLED2001_set_value(int index, uint8_t value) { } void CKLED2001_set_value_all(uint8_t value) { - for (int i = 0; i < DRIVER_LED_TOTAL; i++) { + for (int i = 0; i < LED_MATRIX_LED_COUNT; i++) { CKLED2001_set_value(i, value); } } diff --git a/drivers/led/ckled2001-simple.h b/drivers/led/ckled2001-simple.h index 731da2e1cd..a56cad3461 100644 --- a/drivers/led/ckled2001-simple.h +++ b/drivers/led/ckled2001-simple.h @@ -25,7 +25,7 @@ typedef struct ckled2001_led { uint8_t v; } __attribute__((packed)) ckled2001_led; -extern const ckled2001_led PROGMEM g_ckled2001_leds[DRIVER_LED_TOTAL]; +extern const ckled2001_led PROGMEM g_ckled2001_leds[LED_MATRIX_LED_COUNT]; void CKLED2001_init(uint8_t addr); bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); diff --git a/drivers/led/ckled2001.c b/drivers/led/ckled2001.c index 16a096780b..a99b479d1c 100644 --- a/drivers/led/ckled2001.c +++ b/drivers/led/ckled2001.c @@ -147,7 +147,7 @@ void CKLED2001_init(uint8_t addr) { void CKLED2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { ckled2001_led led; - if (index >= 0 && index < DRIVER_LED_TOTAL) { + if (index >= 0 && index < RGB_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); g_pwm_buffer[led.driver][led.r] = red; @@ -158,7 +158,7 @@ void CKLED2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { } void CKLED2001_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { - for (int i = 0; i < DRIVER_LED_TOTAL; i++) { + for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) { CKLED2001_set_color(i, red, green, blue); } } diff --git a/drivers/led/ckled2001.h b/drivers/led/ckled2001.h index 7d5ad34f95..aa70a0623f 100644 --- a/drivers/led/ckled2001.h +++ b/drivers/led/ckled2001.h @@ -27,7 +27,7 @@ typedef struct ckled2001_led { uint8_t b; } __attribute__((packed)) ckled2001_led; -extern const ckled2001_led PROGMEM g_ckled2001_leds[DRIVER_LED_TOTAL]; +extern const ckled2001_led PROGMEM g_ckled2001_leds[RGB_MATRIX_LED_COUNT]; void CKLED2001_init(uint8_t addr); bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); diff --git a/drivers/led/issi/is31fl3731-simple.c b/drivers/led/issi/is31fl3731-simple.c index 3abe9ea337..84060f9426 100644 --- a/drivers/led/issi/is31fl3731-simple.c +++ b/drivers/led/issi/is31fl3731-simple.c @@ -194,7 +194,7 @@ void IS31FL3731_init(uint8_t addr) { void IS31FL3731_set_value(int index, uint8_t value) { is31_led led; - if (index >= 0 && index < DRIVER_LED_TOTAL) { + if (index >= 0 && index < LED_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_is31_leds[index]), sizeof(led)); // Subtract 0x24 to get the second index of g_pwm_buffer @@ -204,7 +204,7 @@ void IS31FL3731_set_value(int index, uint8_t value) { } void IS31FL3731_set_value_all(uint8_t value) { - for (int i = 0; i < DRIVER_LED_TOTAL; i++) { + for (int i = 0; i < LED_MATRIX_LED_COUNT; i++) { IS31FL3731_set_value(i, value); } } diff --git a/drivers/led/issi/is31fl3731-simple.h b/drivers/led/issi/is31fl3731-simple.h index ded94b0470..1ddadd5209 100644 --- a/drivers/led/issi/is31fl3731-simple.h +++ b/drivers/led/issi/is31fl3731-simple.h @@ -27,7 +27,7 @@ typedef struct is31_led { uint8_t v; } __attribute__((packed)) is31_led; -extern const is31_led PROGMEM g_is31_leds[DRIVER_LED_TOTAL]; +extern const is31_led PROGMEM g_is31_leds[LED_MATRIX_LED_COUNT]; void IS31FL3731_init(uint8_t addr); void IS31FL3731_write_register(uint8_t addr, uint8_t reg, uint8_t data); |