qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

rn42.c (2835B)


      1 /* Copyright 2021
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 2 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 
     17 #include "rn42.h"
     18 
     19 #include "report.h"
     20 #include "uart.h"
     21 
     22 #ifndef RN42_BAUD_RATE
     23 #    define RN42_BAUD_RATE 115200
     24 #endif
     25 
     26 // https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734
     27 static inline uint16_t rn42_consumer_usage_to_bitmap(uint16_t usage) {
     28     switch (usage) {
     29         case AC_HOME:
     30             return 0x0001;
     31         case AL_EMAIL:
     32             return 0x0002;
     33         case AC_SEARCH:
     34             return 0x0004;
     35         case AL_KEYBOARD_LAYOUT:
     36             return 0x0008;
     37         case AUDIO_VOL_UP:
     38             return 0x0010;
     39         case AUDIO_VOL_DOWN:
     40             return 0x0020;
     41         case AUDIO_MUTE:
     42             return 0x0040;
     43         case TRANSPORT_PLAY_PAUSE:
     44             return 0x0080;
     45         case TRANSPORT_NEXT_TRACK:
     46             return 0x0100;
     47         case TRANSPORT_PREV_TRACK:
     48             return 0x0200;
     49         case TRANSPORT_STOP:
     50             return 0x0400;
     51         case TRANSPORT_EJECT:
     52             return 0x0800;
     53         case TRANSPORT_FAST_FORWARD:
     54             return 0x1000;
     55         case TRANSPORT_REWIND:
     56             return 0x2000;
     57         case TRANSPORT_STOP_EJECT:
     58             return 0x4000;
     59         case AL_LOCAL_BROWSER:
     60             return 0x8000;
     61         default:
     62             return 0;
     63     }
     64 }
     65 
     66 void rn42_init(void) {
     67     uart_init(RN42_BAUD_RATE);
     68 }
     69 
     70 void rn42_send_keyboard(report_keyboard_t *report) {
     71     uart_write(0xFD);
     72     uart_write(0x09);
     73     uart_write(0x01);
     74 
     75     uart_write(report->mods);
     76     uart_write(0x00);
     77     uart_write(report->keys[0]);
     78     uart_write(report->keys[1]);
     79     uart_write(report->keys[2]);
     80     uart_write(report->keys[3]);
     81     uart_write(report->keys[4]);
     82     uart_write(report->keys[5]);
     83 }
     84 
     85 void rn42_send_mouse(report_mouse_t *report) {
     86     uart_write(0xFD);
     87     uart_write(0x05);
     88     uart_write(0x02);
     89 
     90     uart_write(report->buttons);
     91     uart_write(report->x);
     92     uart_write(report->y);
     93     uart_write(report->v);
     94 }
     95 
     96 void rn42_send_consumer(uint16_t usage) {
     97     uint16_t bitmap = rn42_consumer_usage_to_bitmap(usage);
     98 
     99     uart_write(0xFD);
    100     uart_write(0x03);
    101     uart_write(0x03);
    102 
    103     uart_write(bitmap & 0xFF);
    104     uart_write(bitmap >> 8);
    105 }