Skip to content

Commit

Permalink
Migrate RN42 to UART driver and refactor (#15492)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Dec 22, 2021
1 parent 78648b3 commit 77d3e56
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 71 deletions.
3 changes: 2 additions & 1 deletion common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)

ifeq ($(strip $(BLUETOOTH_DRIVER)), RN42)
OPT_DEFS += -DMODULE_RN42
SRC += $(TMK_DIR)/protocol/serial_uart.c
SRC += $(DRIVER_PATH)/bluetooth/rn42.c
QUANTUM_LIB_SRC += uart.c
endif
endif
101 changes: 101 additions & 0 deletions drivers/bluetooth/rn42.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* Copyright 2021
*
* 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 "report.h"
#include "uart.h"

#ifndef RN42_BAUD_RATE
# define RN42_BAUD_RATE 115200
#endif

// https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734
static inline uint16_t rn42_consumer_usage_to_bitmap(uint16_t usage) {
switch (usage) {
case AC_HOME:
return 0x0001;
case AL_EMAIL:
return 0x0002;
case AC_SEARCH:
return 0x0004;
case AL_KEYBOARD_LAYOUT:
return 0x0008;
case AUDIO_VOL_UP:
return 0x0010;
case AUDIO_VOL_DOWN:
return 0x0020;
case AUDIO_MUTE:
return 0x0040;
case TRANSPORT_PLAY_PAUSE:
return 0x0080;
case TRANSPORT_NEXT_TRACK:
return 0x0100;
case TRANSPORT_PREV_TRACK:
return 0x0200;
case TRANSPORT_STOP:
return 0x0400;
case TRANSPORT_EJECT:
return 0x0800;
case TRANSPORT_FAST_FORWARD:
return 0x1000;
case TRANSPORT_REWIND:
return 0x2000;
case TRANSPORT_STOP_EJECT:
return 0x4000;
case AL_LOCAL_BROWSER:
return 0x8000;
default:
return 0;
}
}

void rn42_init(void) {
uart_init(RN42_BAUD_RATE);
}

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]);
}
}

void rn42_send_mouse(report_mouse_t *report) {
uart_write(0xFD);
uart_write(0x00);
uart_write(0x03);
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);
}

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);
uart_write(0xFD);
uart_write(0x03);
uart_write(0x03);
uart_write(bitmap & 0xFF);
uart_write((bitmap >> 8) & 0xFF);
}
25 changes: 25 additions & 0 deletions drivers/bluetooth/rn42.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright 2021
*
* 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 "report.h"

void rn42_init(void);

void rn42_send_keyboard(report_keyboard_t *report);

void rn42_send_mouse(report_mouse_t *report);

void rn42_send_consumer(uint16_t data);
77 changes: 7 additions & 70 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ extern keymap_config_t keymap_config;
# include "outputselect.h"
# ifdef MODULE_ADAFRUIT_BLE
# include "adafruit_ble.h"
# else
# include "../serial.h"
# elif MODULE_RN42
# include "rn42.h"
# endif
#endif

Expand All @@ -90,46 +90,6 @@ extern keymap_config_t keymap_config;
# include "joystick.h"
#endif

// https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734
static inline uint16_t CONSUMER2RN42(uint16_t usage) {
switch (usage) {
case AC_HOME:
return 0x0001;
case AL_EMAIL:
return 0x0002;
case AC_SEARCH:
return 0x0004;
case AL_KEYBOARD_LAYOUT:
return 0x0008;
case AUDIO_VOL_UP:
return 0x0010;
case AUDIO_VOL_DOWN:
return 0x0020;
case AUDIO_MUTE:
return 0x0040;
case TRANSPORT_PLAY_PAUSE:
return 0x0080;
case TRANSPORT_NEXT_TRACK:
return 0x0100;
case TRANSPORT_PREV_TRACK:
return 0x0200;
case TRANSPORT_STOP:
return 0x0400;
case TRANSPORT_EJECT:
return 0x0800;
case TRANSPORT_FAST_FORWARD:
return 0x1000;
case TRANSPORT_REWIND:
return 0x2000;
case TRANSPORT_STOP_EJECT:
return 0x4000;
case AL_LOCAL_BROWSER:
return 0x8000;
default:
return 0;
}
}

uint8_t keyboard_idle = 0;
/* 0: Boot Protocol, 1: Report Protocol(default) */
uint8_t keyboard_protocol = 1;
Expand Down Expand Up @@ -688,14 +648,7 @@ static void send_keyboard(report_keyboard_t *report) {
# ifdef MODULE_ADAFRUIT_BLE
adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
# elif MODULE_RN42
serial_send(0xFD);
serial_send(0x09);
serial_send(0x01);
serial_send(report->mods);
serial_send(report->reserved);
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
serial_send(report->keys[i]);
}
rn42_send_keyboard(report);
# endif
return;
}
Expand Down Expand Up @@ -741,16 +694,8 @@ 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
serial_send(0xFD);
serial_send(0x00);
serial_send(0x03);
serial_send(report->buttons);
serial_send(report->x);
serial_send(report->y);
serial_send(report->v); // should try sending the wheel v here
serial_send(report->h); // should try sending the wheel h here
serial_send(0x00);
# elif MODULE_RN42
rn42_send_mouse(report);
# endif
return;
}
Expand Down Expand Up @@ -821,15 +766,7 @@ static void send_consumer(uint16_t data) {
# ifdef MODULE_ADAFRUIT_BLE
adafruit_ble_send_consumer_key(data);
# elif MODULE_RN42
static uint16_t last_data = 0;
if (data == last_data) return;
last_data = data;
uint16_t bitmap = CONSUMER2RN42(data);
serial_send(0xFD);
serial_send(0x03);
serial_send(0x03);
serial_send(bitmap & 0xFF);
serial_send((bitmap >> 8) & 0xFF);
rn42_send_consumer(data);
# endif
return;
}
Expand Down Expand Up @@ -1077,7 +1014,7 @@ void protocol_pre_init(void) {
sei();

#if defined(MODULE_RN42)
serial_init();
rn42_init();
#endif

/* wait for USB startup & debug output */
Expand Down

0 comments on commit 77d3e56

Please sign in to comment.