summaryrefslogtreecommitdiff
path: root/drivers/bluetooth/rn42.c
blob: 5d497cda2044119a0c79efb986bb26ca4d6f8687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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);
}