Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for BT121 Bluetooth module
  • Loading branch information
vomindoraan committed Jan 1, 2020
1 parent 3030584 commit 6d2b18c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 17 deletions.
1 change: 1 addition & 0 deletions keyboards/martian104/readme.md
Expand Up @@ -6,6 +6,7 @@ Keyboard Maintainer: [Konstantin Đorđević](https://github.com/vomindoraan)

Hardware Supported:
- AT90USB1286 _(microcontroller)_
- BT121 _(Bluetooth module)_
- IS31FL3737 _(LED matrix driver)_

Hardware Availability: See [martiantec.com](https://martiantec.com) for more information.
Expand Down
23 changes: 12 additions & 11 deletions keyboards/martian104/rules.mk
Expand Up @@ -7,17 +7,18 @@ BOOTLOADER = atmel-dfu
# OPT_DEFS += -DBOOTLOADER_SIZE=4096

# Build options
BACKLIGHT_ENABLE = yes
BLUETOOTH_ENABLE = no
BOOTMAGIC_ENABLE = no
COMMAND_ENABLE = yes
CONSOLE_ENABLE = yes
EXTRAKEY_ENABLE = yes
# LED_MATRIX_ENABLE = IS31FL3737
MOUSEKEY_ENABLE = yes
NKRO_ENABLE = yes
RGBLIGHT_ENABLE = no
UNICODE_ENABLE = no
BACKLIGHT_ENABLE = yes
BLUETOOTH = BT121
BOOTMAGIC_ENABLE = no
COMMAND_ENABLE = yes
CONSOLE_ENABLE = yes
EXTRAKEY_ENABLE = yes
KEYBOARD_SHARED_EP = yes
# LED_MATRIX_ENABLE = IS31FL3737
MOUSEKEY_ENABLE = yes
NKRO_ENABLE = no
RGBLIGHT_ENABLE = no
UNICODE_ENABLE = no

# Optimizations
EXTRAFLAGS += -flto
12 changes: 10 additions & 2 deletions quantum/config_common.h
Expand Up @@ -286,7 +286,11 @@
/* USART configuration */
#ifdef BLUETOOTH_ENABLE
# ifdef __AVR_ATmega32U4__
# define SERIAL_UART_BAUD 9600
# ifdef MODULE_BT121
# define SERIAL_UART_BAUD 250000
# else
# define SERIAL_UART_BAUD 9600
# endif
# define SERIAL_UART_DATA UDR1
# define SERIAL_UART_UBRR (F_CPU / (16UL * SERIAL_UART_BAUD) - 1)
# define SERIAL_UART_RXD_VECT USART1_RX_vect
Expand All @@ -304,7 +308,11 @@
sei(); \
} while (0)
# elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__))
# define SERIAL_UART_BAUD 115200
# ifdef MODULE_BT121
# define SERIAL_UART_BAUD 250000
# else
# define SERIAL_UART_BAUD 115200
# endif
# define SERIAL_UART_DATA UDR1
/* UBRR should result in ~16 and set UCSR1A = _BV(U2X1) as per rn42 documentation. HC05 needs baudrate configured accordingly */
# define SERIAL_UART_UBRR (F_CPU / (8UL * SERIAL_UART_BAUD) - 1)
Expand Down
6 changes: 6 additions & 0 deletions tmk_core/common.mk
Expand Up @@ -176,6 +176,12 @@ ifeq ($(strip $(BLUETOOTH)), RN42)
TMK_COMMON_DEFS += -DNO_USB_STARTUP_CHECK
endif

ifeq ($(strip $(BLUETOOTH)), BT121)
TMK_COMMON_DEFS += -DBLUETOOTH_ENABLE
TMK_COMMON_DEFS += -DMODULE_BT121
TMK_COMMON_DEFS += -DNO_USB_STARTUP_CHECK
endif

ifeq ($(strip $(ONEHAND_ENABLE)), yes)
SWAP_HANDS_ENABLE = yes # backwards compatibility
endif
Expand Down
5 changes: 5 additions & 0 deletions tmk_core/protocol/lufa.mk
Expand Up @@ -43,6 +43,11 @@ ifeq ($(strip $(BLUETOOTH)), RN42)
$(TMK_DIR)/protocol/serial_uart.c
endif

ifeq ($(strip $(BLUETOOTH)), BT121)
LUFA_SRC += $(LUFA_DIR)/bluetooth.c \
$(TMK_DIR)/protocol/serial_uart.c
endif

ifeq ($(strip $(VIRTSER_ENABLE)), yes)
LUFA_SRC += $(LUFA_ROOT_PATH)/Drivers/USB/Class/Device/CDCClassDevice.c
endif
Expand Down
45 changes: 41 additions & 4 deletions tmk_core/protocol/lufa/lufa.c
Expand Up @@ -573,7 +573,17 @@ static void send_keyboard(report_keyboard_t *report) {
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
bluefruit_serial_send(report->keys[i]);
}
# else
# elif MODULE_BT121
bluefruit_serial_send(0xFD); // Packet header
bluefruit_serial_send(REPORT_ID_KEYBOARD); // Payload size varies based on report ID
bluefruit_serial_send(report->mods);
bluefruit_serial_send(report->reserved);
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
bluefruit_serial_send(report->keys[i]);
}
bluefruit_serial_send(keyboard_leds());
bluefruit_serial_send(0xDF); // Packet trailer
# else // Bluefruit EZ-Key
bluefruit_serial_send(0xFD);
bluefruit_serial_send(report->mods);
bluefruit_serial_send(report->reserved);
Expand Down Expand Up @@ -629,7 +639,16 @@ static void send_mouse(report_mouse_t *report) {
# ifdef MODULE_ADAFRUIT_BLE
// FIXME: mouse buttons
adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
# else
# elif MODULE_BT121
bluefruit_serial_send(0xFD); // Packet header
bluefruit_serial_send(REPORT_ID_MOUSE); // Payload size varies based on report ID
bluefruit_serial_send(report->buttons);
bluefruit_serial_send(report->x);
bluefruit_serial_send(report->y);
bluefruit_serial_send(report->v);
bluefruit_serial_send(report->h);
bluefruit_serial_send(0xDF); // Packet trailer
# else // Bluefruit EZ-Key
bluefruit_serial_send(0xFD);
bluefruit_serial_send(0x00);
bluefruit_serial_send(0x03);
Expand Down Expand Up @@ -668,6 +687,18 @@ static void send_mouse(report_mouse_t *report) {
*/
static void send_system(uint16_t data) {
#ifdef EXTRAKEY_ENABLE
# ifdef BLUETOOTH_ENABLE
# ifdef MODULE_BT121
bluefruit_serial_send(0xFD); // Packet header
bluefruit_serial_send(REPORT_ID_SYSTEM); // Payload size varies based on report ID
// uint16_t usage = data - SYSTEM_POWER_DOWN + 1;
uint16_t usage = data;
bluefruit_serial_send(usage & 0xFF);
bluefruit_serial_send((usage >> 8) & 0xFF);
bluefruit_serial_send(0xDF); // Packet trailer
# endif
# endif

uint8_t timeout = 255;

if (USB_DeviceState != DEVICE_STATE_Configured) return;
Expand Down Expand Up @@ -707,7 +738,13 @@ static void send_consumer(uint16_t data) {
bluefruit_serial_send(0x03);
bluefruit_serial_send(bitmap & 0xFF);
bluefruit_serial_send((bitmap >> 8) & 0xFF);
# else
# elif MODULE_BT121
bluefruit_serial_send(0xFD); // Packet header
bluefruit_serial_send(REPORT_ID_CONSUMER); // Payload size varies based on report ID
bluefruit_serial_send(data & 0xFF);
bluefruit_serial_send((data >> 8) & 0xFF);
bluefruit_serial_send(0xDF); // Packet trailer
# else // Bluefruit EZ-Key
static uint16_t last_data = 0;
if (data == last_data) return;
last_data = data;
Expand Down Expand Up @@ -954,7 +991,7 @@ int main(void) {
setup_usb();
sei();

#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42) || defined(MODULE_BT121)
serial_init();
#endif

Expand Down

0 comments on commit 6d2b18c

Please sign in to comment.