You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.0 KiB
131 lines
3.0 KiB
4 years ago
|
#include <logging/log.h>
|
||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||
5 years ago
|
|
||
4 years ago
|
#include <zmk/hid.h>
|
||
5 years ago
|
|
||
4 years ago
|
static struct zmk_hid_keypad_report kp_report = {
|
||
|
.report_id = 1,
|
||
|
.body = {
|
||
|
.modifiers = 0,
|
||
|
.keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
|
||
|
|
||
|
static struct zmk_hid_consumer_report consumer_report = {
|
||
|
.report_id = 2,
|
||
|
.body = {
|
||
|
.keys = 0x00}};
|
||
|
|
||
|
#define _TOGGLE_MOD(mod, state) \
|
||
|
if (modifier > MOD_RGUI) \
|
||
|
{ \
|
||
|
return -EINVAL; \
|
||
|
} \
|
||
|
WRITE_BIT(kp_report.body.modifiers, mod, state); \
|
||
5 years ago
|
return 0;
|
||
5 years ago
|
|
||
|
int zmk_hid_register_mod(zmk_mod modifier)
|
||
|
{
|
||
|
_TOGGLE_MOD(modifier, true);
|
||
|
}
|
||
|
int zmk_hid_unregister_mod(zmk_mod modifier)
|
||
|
{
|
||
|
_TOGGLE_MOD(modifier, false);
|
||
|
}
|
||
|
|
||
5 years ago
|
int zmk_hid_register_mods(zmk_mod_flags modifiers)
|
||
|
{
|
||
4 years ago
|
kp_report.body.modifiers |= modifiers;
|
||
5 years ago
|
return 0;
|
||
5 years ago
|
}
|
||
|
|
||
|
int zmk_hid_unregister_mods(zmk_mod_flags modifiers)
|
||
|
{
|
||
4 years ago
|
kp_report.body.modifiers &= ~modifiers;
|
||
5 years ago
|
return 0;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
#define KEY_OFFSET 0x02
|
||
|
#define MAX_KEYS 6
|
||
|
|
||
|
/*
|
||
|
#define TOGGLE_BOOT_KEY(match, val) \
|
||
|
for (int idx = 0; idx < MAX_KEYS; idx++) \
|
||
|
{ \
|
||
4 years ago
|
if (kp_report.boot.keys[idx + KEY_OFFSET] != match) \
|
||
5 years ago
|
{ \
|
||
|
continue; \
|
||
|
} \
|
||
4 years ago
|
kp_report.boot.keys[idx + KEY_OFFSET] = val; \
|
||
5 years ago
|
break; \
|
||
|
}
|
||
|
*/
|
||
|
|
||
4 years ago
|
#define TOGGLE_KEY(code, val) WRITE_BIT(kp_report.body.keys[code / 8], code % 8, val)
|
||
|
|
||
|
#define TOGGLE_CONSUMER(key, state) \
|
||
|
WRITE_BIT(consumer_report.body.keys, (key - 0x100), state);
|
||
5 years ago
|
|
||
4 years ago
|
enum zmk_hid_report_changes zmk_hid_press_key(zmk_key code)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (code >= KC_LCTL && code <= KC_RGUI)
|
||
|
{
|
||
|
return zmk_hid_register_mod(code - KC_LCTL);
|
||
|
}
|
||
|
|
||
4 years ago
|
if (ZK_IS_CONSUMER(code))
|
||
5 years ago
|
{
|
||
4 years ago
|
LOG_DBG("Toggling a consumer key!");
|
||
|
TOGGLE_CONSUMER(code, true);
|
||
|
return Consumer;
|
||
5 years ago
|
}
|
||
4 years ago
|
else
|
||
|
{
|
||
|
if (code > ZMK_HID_MAX_KEYCODE)
|
||
|
{
|
||
|
return -EINVAL;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
// TOGGLE_BOOT_KEY(0U, code);
|
||
5 years ago
|
|
||
4 years ago
|
TOGGLE_KEY(code, true);
|
||
5 years ago
|
|
||
4 years ago
|
return Keypad;
|
||
|
}
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
enum zmk_hid_report_changes zmk_hid_release_key(zmk_key code)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (code >= KC_LCTL && code <= KC_RGUI)
|
||
|
{
|
||
|
return zmk_hid_unregister_mod(code - KC_LCTL);
|
||
|
}
|
||
|
|
||
4 years ago
|
if (ZK_IS_CONSUMER(code))
|
||
5 years ago
|
{
|
||
4 years ago
|
TOGGLE_CONSUMER(code, false);
|
||
|
return Consumer;
|
||
5 years ago
|
}
|
||
4 years ago
|
else
|
||
|
{
|
||
|
if (code > ZMK_HID_MAX_KEYCODE)
|
||
|
{
|
||
|
return -EINVAL;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
// TOGGLE_BOOT_KEY(0U, code);
|
||
5 years ago
|
|
||
4 years ago
|
TOGGLE_KEY(code, false);
|
||
5 years ago
|
|
||
4 years ago
|
return Keypad;
|
||
|
}
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
struct zmk_hid_keypad_report *zmk_hid_get_keypad_report()
|
||
|
{
|
||
|
return &kp_report;
|
||
|
}
|
||
|
|
||
|
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report()
|
||
5 years ago
|
{
|
||
4 years ago
|
return &consumer_report;
|
||
5 years ago
|
}
|