Parent directory

bluetooth_custom.c

4964 bytes
  1/*
  2Copyright 2019 Basic I/O Instruments(Scott Wei) <scot.wei@gmail.com>
  3This program is free software: you can redistribute it and/or modify
  4it under the terms of the GNU General Public License as published by
  5the Free Software Foundation, either version 2 of the License, or
  6(at your option) any later version.
  7This program is distributed in the hope that it will be useful,
  8but WITHOUT ANY WARRANTY; without even the implied warranty of
  9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 10GNU General Public License for more details.
 11You should have received a copy of the GNU General Public License
 12along with this program. If not, see <http://www.gnu.org/licenses/>.
 13*/
 14
 15#include "bluetooth.h"
 16#include "uart.h"
 17#include "progmem.h"
 18#include "wait.h"
 19#include "debug.h"
 20#include "usb_descriptor.h"
 21#include "report.h"
 22
 23void send_str(const char *str)
 24{
 25    uint8_t c;
 26    while ((c = pgm_read_byte(str++)))
 27        uart_write(c);
 28}
 29
 30void serial_send(uint8_t data)
 31{
 32    dprintf("Sending: %u\n", data);
 33}
 34
 35void send_bytes(uint8_t data)
 36{
 37    char hexStr[3];
 38    sprintf(hexStr, "%02X", data);
 39    for (int j = 0; j < sizeof(hexStr) - 1; j++)
 40    {
 41        uart_write(hexStr[j]);
 42    }
 43}
 44
 45#ifdef BLUEFRUIT_TRACE_SERIAL
 46static void bluefruit_trace_header(void)
 47{
 48    dprintf("+------------------------------------+\n");
 49    dprintf("| HID report to Bluefruit via serial |\n");
 50    dprintf("+------------------------------------+\n|");
 51}
 52
 53static void bluefruit_trace_footer(void)
 54{
 55    dprintf("|\n+------------------------------------+\n\n");
 56}
 57#endif
 58
 59static void bluefruit_serial_send(uint8_t data)
 60{
 61#ifdef BLUEFRUIT_TRACE_SERIAL
 62    dprintf(" %02X ", data);
 63#endif
 64    serial_send(data);
 65}
 66
 67void bluetooth_init(void) {
 68    uart_init(76800);
 69    wait_ms(250);
 70
 71    send_str(PSTR("\r\n"));
 72    send_str(PSTR("\r\n"));
 73    send_str(PSTR("\r\n"));
 74}
 75
 76void bluetooth_task(void) {}
 77
 78void bluetooth_send_keyboard(report_keyboard_t *report)
 79{
 80#ifdef BLUEFRUIT_TRACE_SERIAL
 81    bluefruit_trace_header();
 82#endif
 83    dprintf("Sending...\n");
 84
 85    send_str(PSTR("AT+BLEKEYBOARDCODE="));
 86
 87    send_bytes(report->mods);
 88    send_str(PSTR("-"));
 89    send_bytes(0);
 90    for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
 91        send_str(PSTR("-"));
 92        send_bytes(report->keys[i]);
 93    }
 94
 95    send_str(PSTR("\r\n"));
 96#ifdef BLUEFRUIT_TRACE_SERIAL
 97    bluefruit_trace_footer();
 98#endif
 99}
100
101void bluetooth_send_mouse(report_mouse_t *report)
102{
103#ifdef BLUEFRUIT_TRACE_SERIAL
104    bluefruit_trace_header();
105#endif
106    bluefruit_serial_send(0xFD);
107    bluefruit_serial_send(0x00);
108    bluefruit_serial_send(0x03);
109    bluefruit_serial_send(report->buttons);
110    bluefruit_serial_send(report->x);
111    bluefruit_serial_send(report->y);
112    bluefruit_serial_send(report->v); // should try sending the wheel v here
113    bluefruit_serial_send(report->h); // should try sending the wheel h here
114    bluefruit_serial_send(0x00);
115#ifdef BLUEFRUIT_TRACE_SERIAL
116    bluefruit_trace_footer();
117#endif
118}
119
120/*
121+-----------------+-------------------+-------+
122| Consumer Key    | Bit Map           | Hex   |
123+-----------------+-------------------+-------+
124| Home            | 00000001 00000000 | 01 00 |
125| KeyboardLayout  | 00000010 00000000 | 02 00 |
126| Search          | 00000100 00000000 | 04 00 |
127| Snapshot        | 00001000 00000000 | 08 00 |
128| VolumeUp        | 00010000 00000000 | 10 00 |
129| VolumeDown      | 00100000 00000000 | 20 00 |
130| Play/Pause      | 01000000 00000000 | 40 00 |
131| Fast Forward    | 10000000 00000000 | 80 00 |
132| Rewind          | 00000000 00000001 | 00 01 |
133| Scan Next Track | 00000000 00000010 | 00 02 |
134| Scan Prev Track | 00000000 00000100 | 00 04 |
135| Random Play     | 00000000 00001000 | 00 08 |
136| Stop            | 00000000 00010000 | 00 10 |
137+-------------------------------------+-------+
138*/
139#define CONSUMER2BLUEFRUIT(usage) \
140    (usage == AUDIO_MUTE ? 0x00e2 : (usage == AUDIO_VOL_UP ? 0x00e9 : (usage == AUDIO_VOL_DOWN ? 0x00ea : (usage == TRANSPORT_NEXT_TRACK ? 0x00b5 : (usage == TRANSPORT_PREV_TRACK ? 0x00b6 : (usage == TRANSPORT_STOP ? 0x00b7 : (usage == TRANSPORT_STOP_EJECT ? 0x00b8 : (usage == TRANSPORT_PLAY_PAUSE ? 0x00b1 : (usage == AL_CC_CONFIG ? 0x0183 : (usage == AL_EMAIL ? 0x018c : (usage == AL_CALCULATOR ? 0x0192 : (usage == AL_LOCAL_BROWSER ? 0x0196 : (usage == AC_SEARCH ? 0x021f : (usage == AC_HOME ? 0x0223 : (usage == AC_BACK ? 0x0224 : (usage == AC_FORWARD ? 0x0225 : (usage == AC_STOP ? 0x0226 : (usage == AC_REFRESH ? 0x0227 : (usage == AC_BOOKMARKS ? 0x022a : 0)))))))))))))))))))
141
142void bluetooth_send_consumer(uint16_t usage)
143{
144    uint16_t bitmap = CONSUMER2BLUEFRUIT(usage);
145
146#ifdef BLUEFRUIT_TRACE_SERIAL
147    dprintf("\nData: %04X; bitmap: %04X\n", data, bitmap);
148    bluefruit_trace_header();
149#endif
150    send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
151    send_bytes((bitmap >> 8) & 0xFF);
152    send_bytes(bitmap & 0xFF);
153    send_str(PSTR("\r\n"));
154#ifdef BLUEFRUIT_TRACE_SERIAL
155    bluefruit_trace_footer();
156#endif
157}