Last active
June 22, 2023 05:46
-
-
Save dexter93/63549b82c731ccc0b378033e6a952ec5 to your computer and use it in GitHub Desktop.
Shaping it up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2021 Dimitris Mantzouranis <[email protected]> | |
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 <stdint.h> | |
#include <stdbool.h> | |
#include "quantum.h" | |
#include "hal.h" | |
uint8_t ble_led_status = 0; | |
static uint8_t iton_ble_leds(void); | |
static void iton_ble_keyboard(report_keyboard_t *report); | |
static void iton_ble_mouse(report_mouse_t *report); | |
static void iton_ble_system(uint16_t data); | |
static void iton_ble_consumer(uint16_t data); | |
static host_driver_t *lastHostDriver = NULL; | |
host_driver_t iton_ble_driver = { | |
iton_ble_leds, | |
iton_ble_keyboard, | |
iton_ble_mouse, | |
iton_ble_system, | |
iton_ble_consumer, | |
}; | |
enum page { | |
std_hid = 0xA1, //Standard HID Report | |
nkro_hid = 0xA2, //NKRO Report | |
media_hid = 0xA3, //Media keys Report | |
sys_rep = 0xA4, //System control Report | |
fn_hid = 0xA5, //Fn keys Report | |
bt_ctrl = 0xA6, //Bluetooth control Report | |
bt_name = 0xA7, //Bluetooth name Report | |
pwr_val = 0xA8, //Power values set Report | |
led_state = 0xB1, //LED Indicators Report | |
pwr_state = 0xB6, //Power status Report | |
}; | |
enum bt_control_addr{ | |
bt_status = 0x51, //Bluetooth status | |
batt_low = 0x52, //Battery low voltage | |
batt_dead = 0x53, //Battery dead/shutdown voltage | |
usb_status = 0x58, //USB status | |
batt_status = 0x5A, //Battery status | |
sys_poweroff = 0xA8, //System Power down | |
sys_sleep = 0xA9, //System sleep | |
sys_wakeup = 0xAA, //System wake up | |
}; | |
enum bt_control_data{ | |
usb_mode_sw = 0x01, //USB mode | |
low_batt_not = 0x06, //Low battery voltage notification | |
low_batt_off = 0x07, //Low battery shutdown notification | |
low_batt_ok = 0x0A, //Low battery disable notification | |
bt_mode_sw = 0x62, //Bluetooth mode | |
bt_pair_rst = 0x70, //Reset Bluetooth pairing info | |
mac_os = 0x74, //MAC OS mode | |
win_os = 0x75, //Windows mode | |
bt_pair_ok = 0x76, //Bluetooth pairing successful | |
bt_pair_init = 0x77, //Bluetooth pairing init | |
bt_pair_disc = 0x78, //Bluetooth disconnected - enter sleep | |
bt_pair_conn = 0x79, //Bluetooth reconnecting | |
bt_pair = 0x89, //Enter pairing mode | |
bt_pair_one = 0x81, //Pair the 1st host | |
bt_pair_two = 0x82, //Pair the 2nd host | |
bt_pair_three = 0x83, //Pair the 3rd host | |
bt_pair_four = 0x84, //Pair the 4th host | |
bt_pair_five = 0x85, //Pair the 5th host | |
bt_pair_six = 0x86, //Pair the 6th host | |
mode_is_bt = 0xA0, //Mode query | |
bt_is_name = 0xA1, //Bluetooth name query | |
}; | |
struct bt_ctrl_msg { | |
uint8_t type; | |
uint8_t cmd; | |
uint8_t param; | |
}; | |
struct iton_name { | |
uint8_t type=bt_name; | |
uint16_t checksum; | |
uint8_t len; | |
(uint8_t *)name; | |
}; | |
static uint8_t iton_ble_leds(void) { | |
return ble_led_status; | |
} | |
static void iton_ble_keyboard(report_keyboard_t *report) { | |
#ifdef NKRO_ENABLE | |
uint8_t buffer[17] = {nkro_hid, report->nkro.mods}; | |
#else | |
uint8_t buffer[KEYBOARD_REPORT_SIZE+3] = 0; | |
buffer[0] = std_hid; | |
buffer[1] = report->report.mods; | |
for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) { | |
buffer[i+3] = report->raw[i]; | |
} | |
#endif | |
spiStartSend(&SPID1, sizeof(buffer), buffer); | |
} | |
static void iton_ble_mouse(report_mouse_t *report) {} // Not Supported | |
static void iton_ble_system(uint16_t data){ | |
uint8_t buffer[]; | |
buffer[0] = sys_rep; | |
buffer[1] = (data + 0x27); | |
spiStartSend(&SPID1, sizeof(buffer), buffer); | |
} | |
static void iton_ble_consumer(uint16_t data){ | |
const struct bt_ctrl_msg *msg; | |
msg->type = media_hid; | |
msg->cmd = (uint8_t)(data >> 8); | |
msg->param = (uint8_t)(data & 0x00FF); | |
spiStartSend(&SPID1, sizeof(msg), &msg); | |
} | |
static void iton_control(const struct bt_ctrl_msg *msg, uint8_t cmd, uint8_t param) { | |
msg->type = bt_ctrl; | |
msg->cmd = cmd; | |
msg->param = param; | |
spiStartSend(&SPID1, sizeof(msg), &msg); | |
} | |
void iton_switch_bt() { | |
if (host_get_driver() == &iton_ble_driver) { | |
return; | |
} | |
clear_keyboard(); | |
lastHostDriver = host_get_driver(); | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_mode_sw); | |
host_set_driver(&iton_ble_driver); | |
} | |
void iton_switch_usb() { | |
if (host_get_driver() != &iton_ble_driver) { | |
return; | |
} | |
clear_keyboard(); | |
iton_control(const struct bt_ctrl_msg *msg, usb_status, usb_mode_sw); | |
host_set_driver(lastHostDriver); | |
} | |
void iton_clear_pairing() { | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_rst); | |
} | |
void iton_enter_pairing() { | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair); | |
} | |
void iton_select_host(uint8_t host) { | |
switch(host) { | |
case 1: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_one); | |
break; | |
case 2: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_two); | |
break; | |
case 3: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_three); | |
break; | |
case 4: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_four); | |
break; | |
case 5: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_five); | |
break; | |
case 6: | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, bt_pair_six); | |
break; | |
default: | |
; | |
} | |
} | |
void iton_macos_mode() { | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, mac_os); | |
} | |
void iton_windows_mode() { | |
iton_control(const struct bt_ctrl_msg *msg, bt_status, win_os); | |
} | |
void iton_set_device_name(char *name) { | |
const struct iton_name *msg; | |
msg->len = sizeof(name); | |
if (msg->len < 32){ | |
msg->checksum = 0; | |
for (uint8_t i = 0; i < msg->len; i++) { | |
msg->checksum += name[i]; | |
} | |
spiStartSend(&SPID1, sizeof(msg), &msg); | |
} else { | |
return; | |
} | |
} | |
static void iton_receive_notif(const struct bt_ctrl_msg *msg) { | |
spiStartReceive(&SPID1, sizeof(msg), &msg); | |
if(msg->type == led_state) { | |
ble_led_status=msg->cmd; | |
} | |
else if (msg->type == batt_status) { | |
switch(msg->param) { | |
case low_batt_not: | |
break; | |
case low_batt_off: | |
break; | |
case low_batt_ok: | |
break; | |
case mode_is_bt: | |
break; | |
case bt_is_name: | |
break; | |
default: | |
; | |
} | |
} | |
else if (msg->type == bt_status) { | |
switch(msg->param) { | |
case bt_pair_ok: | |
break; | |
case bt_pair_init: | |
break; | |
case bt_pair_disc: | |
break; | |
case bt_pair_conn: | |
break; | |
default: | |
; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment