qmk_firmware

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

annepro2_ble.c (5176B)


      1 /*
      2     Copyright (C) 2020 Yaotian Feng, Codetector<codetector@codetector.cn>
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8         http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15 */
     16 
     17 #include "annepro2_ble.h"
     18 #include "ch.h"
     19 #include "hal.h"
     20 #include "host.h"
     21 #include "host_driver.h"
     22 #include "report.h"
     23 
     24 /* -------------------- Static Function Prototypes -------------------------- */
     25 static uint8_t ap2_ble_leds(void);
     26 static void    ap2_ble_mouse(report_mouse_t *report);
     27 static void    ap2_ble_extra(report_extra_t *report);
     28 static void    ap2_ble_keyboard(report_keyboard_t *report);
     29 
     30 static void ap2_ble_swtich_ble_driver(void);
     31 
     32 /* -------------------- Static Local Variables ------------------------------ */
     33 static host_driver_t ap2_ble_driver = {
     34     ap2_ble_leds, ap2_ble_keyboard, NULL, ap2_ble_mouse, ap2_ble_extra
     35 };
     36 
     37 static uint8_t ble_mcu_wakeup[11] = {0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x01, 0x7d, 0x02, 0x01, 0x02};
     38 
     39 static uint8_t ble_mcu_start_broadcast[10] = {
     40     0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x01,  // Broadcast ID[0-3]
     41 };
     42 
     43 static uint8_t ble_mcu_connect[10] = {
     44     0x7b, 0x12, 0x53, 0x00, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x04,  // Connect ID [0-3]
     45 };
     46 
     47 static uint8_t ble_mcu_send_report[10] = {
     48     0x7b, 0x12, 0x53, 0x00, 0x0A, 0x00, 0x00, 0x7d, 0x10, 0x04,
     49 };
     50 
     51 static uint8_t ble_mcu_send_consumer_report[10] = {
     52     0x7b, 0x12, 0x53, 0x00, 0x06, 0x00, 0x00, 0x7d, 0x10, 0x08,
     53 };
     54 
     55 static uint8_t ble_mcu_unpair[10] = {
     56     0x7b, 0x12, 0x53, 0x00, 0x02, 0x00, 0x00, 0x7d, 0x40, 0x05,
     57 };
     58 
     59 static uint8_t ble_mcu_bootload[11] = {0x7b, 0x10, 0x51, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x01};
     60 
     61 static host_driver_t *last_host_driver = NULL;
     62 #ifdef NKRO_ENABLE
     63 static bool lastNkroStatus = false;
     64 #endif  // NKRO_ENABLE
     65 
     66 /* -------------------- Public Function Implementation ---------------------- */
     67 
     68 void annepro2_ble_bootload(void) { sdWrite(&SD1, ble_mcu_bootload, sizeof(ble_mcu_bootload)); }
     69 
     70 void annepro2_ble_startup(void) { sdWrite(&SD1, ble_mcu_wakeup, sizeof(ble_mcu_wakeup)); }
     71 
     72 void annepro2_ble_broadcast(uint8_t port) {
     73     if (port > 3) {
     74         port = 3;
     75     }
     76     // sdPut(&SD1, 0x00);
     77     sdWrite(&SD1, ble_mcu_start_broadcast, sizeof(ble_mcu_start_broadcast));
     78     sdPut(&SD1, port);
     79     sdPut(&SD1, 0x00);
     80     static int lastBroadcast = -1;
     81     if (lastBroadcast == port) {
     82         annepro2_ble_connect(port);
     83     }
     84     lastBroadcast = port;
     85 }
     86 
     87 void annepro2_ble_connect(uint8_t port) {
     88     if (port > 3) {
     89         port = 3;
     90     }
     91     sdWrite(&SD1, ble_mcu_connect, sizeof(ble_mcu_connect));
     92     sdPut(&SD1, port);
     93     sdPut(&SD1, 0x00);
     94     ap2_ble_swtich_ble_driver();
     95 }
     96 
     97 void annepro2_ble_disconnect(void) {
     98     /* Skip if the driver is already enabled */
     99     if (host_get_driver() != &ap2_ble_driver) {
    100         return;
    101     }
    102 
    103     clear_keyboard();
    104 #ifdef NKRO_ENABLE
    105     keymap_config.nkro = lastNkroStatus;
    106 #endif
    107     host_set_driver(last_host_driver);
    108 }
    109 
    110 void annepro2_ble_unpair(void) {
    111     // sdPut(&SD1, 0x0);
    112     sdWrite(&SD1, ble_mcu_unpair, sizeof(ble_mcu_unpair));
    113 }
    114 
    115 /* ------------------- Static Function Implementation ----------------------- */
    116 static void ap2_ble_swtich_ble_driver(void) {
    117     if (host_get_driver() == &ap2_ble_driver) {
    118         return;
    119     }
    120     clear_keyboard();
    121     last_host_driver = host_get_driver();
    122 #ifdef NKRO_ENABLE
    123     lastNkroStatus = keymap_config.nkro;
    124 #endif
    125     keymap_config.nkro = false;
    126     host_set_driver(&ap2_ble_driver);
    127 }
    128 
    129 static uint8_t ap2_ble_leds(void) {
    130     return 0;  // TODO: Figure out how to obtain LED status
    131 }
    132 
    133 static void ap2_ble_mouse(report_mouse_t *report) {}
    134 
    135 static inline uint16_t CONSUMER2AP2(uint16_t usage) {
    136     switch (usage) {
    137         case AUDIO_VOL_DOWN:
    138             return 0x04;
    139         case AUDIO_VOL_UP:
    140             return 0x02;
    141         case AUDIO_MUTE:
    142             return 0x01;
    143         case TRANSPORT_PLAY_PAUSE:
    144             return 0x08;
    145         case TRANSPORT_NEXT_TRACK:
    146             return 0x10;
    147         case TRANSPORT_PREV_TRACK:
    148             return 0x20;
    149         default:
    150             return 0x00;
    151     }
    152 }
    153 
    154 static void ap2_ble_extra(report_extra_t *report) {
    155     if (report->report_id == REPORT_ID_CONSUMER) {
    156         sdPut(&SD1, 0x0);
    157         sdWrite(&SD1, ble_mcu_send_consumer_report, sizeof(ble_mcu_send_consumer_report));
    158         sdPut(&SD1, CONSUMER2AP2(report->usage));
    159         static const uint8_t dummy[3] = {0};
    160         sdWrite(&SD1, dummy, sizeof(dummy));
    161     }
    162 }
    163 
    164 /*!
    165  * @brief  Send keyboard HID report for Bluetooth driver
    166  */
    167 static void ap2_ble_keyboard(report_keyboard_t *report) {
    168     sdPut(&SD1, 0x0);
    169     sdWrite(&SD1, ble_mcu_send_report, sizeof(ble_mcu_send_report));
    170     sdWrite(&SD1, (uint8_t *)report, KEYBOARD_REPORT_SIZE);
    171 }