summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-12-22 16:51:39 +1100
committerGitHub <noreply@github.com>2021-12-22 16:51:39 +1100
commit77d3e564f723c12f2ffb501553b1e78bbe0ea84a (patch)
tree2ab02b8bfb5cf9a07fec715b927ac9b8ec172200 /drivers
parent78648b37e97d0574a79632fe09ef7d8a0a4cbc27 (diff)
Migrate RN42 to UART driver and refactor (#15492)
Diffstat (limited to 'drivers')
-rw-r--r--drivers/bluetooth/rn42.c101
-rw-r--r--drivers/bluetooth/rn42.h25
2 files changed, 126 insertions, 0 deletions
diff --git a/drivers/bluetooth/rn42.c b/drivers/bluetooth/rn42.c
new file mode 100644
index 0000000000..ee4c8c330e
--- /dev/null
+++ b/drivers/bluetooth/rn42.c
@@ -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);
+}
diff --git a/drivers/bluetooth/rn42.h b/drivers/bluetooth/rn42.h
new file mode 100644
index 0000000000..4747759111
--- /dev/null
+++ b/drivers/bluetooth/rn42.h
@@ -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);