From 83988597f4d916a37b2b0987f393ceaa007532eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Wed, 15 Sep 2021 17:40:22 +0200 Subject: Add Support for USB programmable buttons (#12950) --- quantum/action.c | 5 ++ quantum/keyboard.c | 7 +++ .../process_keycode/process_programmable_button.c | 31 ++++++++++ .../process_keycode/process_programmable_button.h | 23 +++++++ quantum/programmable_button.c | 37 ++++++++++++ quantum/programmable_button.h | 30 ++++++++++ quantum/quantum.c | 3 + quantum/quantum.h | 4 ++ quantum/quantum_keycodes.h | 70 ++++++++++++++++++++++ 9 files changed, 210 insertions(+) create mode 100644 quantum/process_keycode/process_programmable_button.c create mode 100644 quantum/process_keycode/process_programmable_button.h create mode 100644 quantum/programmable_button.c create mode 100644 quantum/programmable_button.h (limited to 'quantum') diff --git a/quantum/action.c b/quantum/action.c index be135f18f2..95f39d23d4 100644 --- a/quantum/action.c +++ b/quantum/action.c @@ -18,6 +18,7 @@ along with this program. If not, see . #include "keycode.h" #include "keyboard.h" #include "mousekey.h" +#include "programmable_button.h" #include "command.h" #include "led.h" #include "action_layer.h" @@ -988,6 +989,10 @@ void clear_keyboard_but_mods_and_keys() { mousekey_clear(); mousekey_send(); #endif +#ifdef PROGRAMMABLE_BUTTON_ENABLE + programmable_button_clear(); + programmable_button_send(); +#endif } /** \brief Utilities for actions. (FIXME: Needs better description) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 5846507b3f..6054faa03b 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -76,6 +76,9 @@ along with this program. If not, see . #ifdef JOYSTICK_ENABLE # include "process_joystick.h" #endif +#ifdef PROGRAMMABLE_BUTTON_ENABLE +# include "programmable_button.h" +#endif #ifdef HD44780_ENABLE # include "hd44780.h" #endif @@ -542,6 +545,10 @@ MATRIX_LOOP_END: digitizer_task(); #endif +#ifdef PROGRAMMABLE_BUTTON_ENABLE + programmable_button_send(); +#endif + // update LED if (led_status != host_keyboard_leds()) { led_status = host_keyboard_leds(); diff --git a/quantum/process_keycode/process_programmable_button.c b/quantum/process_keycode/process_programmable_button.c new file mode 100644 index 0000000000..c6e77faacc --- /dev/null +++ b/quantum/process_keycode/process_programmable_button.c @@ -0,0 +1,31 @@ +/* +Copyright 2021 Thomas Weißschuh + +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 . +*/ + +#include "process_programmable_button.h" +#include "programmable_button.h" + +bool process_programmable_button(uint16_t keycode, keyrecord_t *record) { + if (keycode >= PROGRAMMABLE_BUTTON_MIN && keycode <= PROGRAMMABLE_BUTTON_MAX) { + uint8_t button = keycode - PROGRAMMABLE_BUTTON_MIN + 1; + if (record->event.pressed) { + programmable_button_on(button); + } else { + programmable_button_off(button); + } + } + return true; +} diff --git a/quantum/process_keycode/process_programmable_button.h b/quantum/process_keycode/process_programmable_button.h new file mode 100644 index 0000000000..47c6ce5614 --- /dev/null +++ b/quantum/process_keycode/process_programmable_button.h @@ -0,0 +1,23 @@ +/* +Copyright 2021 Thomas Weißschuh + +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 . +*/ + +#pragma once + +#include +#include "quantum.h" + +bool process_programmable_button(uint16_t keycode, keyrecord_t *record); diff --git a/quantum/programmable_button.c b/quantum/programmable_button.c new file mode 100644 index 0000000000..be828fd17c --- /dev/null +++ b/quantum/programmable_button.c @@ -0,0 +1,37 @@ +/* +Copyright 2021 Thomas Weißschuh + +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 . +*/ + +#include "programmable_button.h" +#include "host.h" + +#define REPORT_BIT(index) (((uint32_t)1) << (index - 1)) + +static uint32_t programmable_button_report = 0; + +void programmable_button_clear(void) { programmable_button_report = 0; } + +void programmable_button_send(void) { host_programmable_button_send(programmable_button_report); } + +void programmable_button_on(uint8_t index) { programmable_button_report |= REPORT_BIT(index); } + +void programmable_button_off(uint8_t index) { programmable_button_report &= ~REPORT_BIT(index); } + +bool programmable_button_is_on(uint8_t index) { return !!(programmable_button_report & REPORT_BIT(index)); }; + +uint32_t programmable_button_get_report(void) { return programmable_button_report; }; + +void programmable_button_set_report(uint32_t report) { programmable_button_report = report; } diff --git a/quantum/programmable_button.h b/quantum/programmable_button.h new file mode 100644 index 0000000000..e89b8b9fd6 --- /dev/null +++ b/quantum/programmable_button.h @@ -0,0 +1,30 @@ +/* +Copyright 2021 Thomas Weißschuh + +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 . +*/ + +#pragma once + +#include +#include +#include "report.h" + +void programmable_button_clear(void); +void programmable_button_send(void); +void programmable_button_on(uint8_t index); +void programmable_button_off(uint8_t index); +bool programmable_button_is_on(uint8_t index); +uint32_t programmable_button_get_report(void); +void programmable_button_set_report(uint32_t report); diff --git a/quantum/quantum.c b/quantum/quantum.c index 9d77fa4383..326c8370b1 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -295,6 +295,9 @@ bool process_record_quantum(keyrecord_t *record) { #endif #ifdef JOYSTICK_ENABLE process_joystick(keycode, record) && +#endif +#ifdef PROGRAMMABLE_BUTTON_ENABLE + process_programmable_button(keycode, record) && #endif true)) { return false; diff --git a/quantum/quantum.h b/quantum/quantum.h index 86b717e445..5cbe84d0c9 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -147,6 +147,10 @@ extern layer_state_t layer_state; # include "process_joystick.h" #endif +#ifdef PROGRAMMABLE_BUTTON_ENABLE +# include "process_programmable_button.h" +#endif + #ifdef GRAVE_ESC_ENABLE # include "process_grave_esc.h" #endif diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h index ef4b0f457b..2ea81dd4c8 100644 --- a/quantum/quantum_keycodes.h +++ b/quantum/quantum_keycodes.h @@ -524,6 +524,40 @@ enum quantum_keycodes { // Additional magic key MAGIC_TOGGLE_GUI, + // Programmable Button + PROGRAMMABLE_BUTTON_1, + PROGRAMMABLE_BUTTON_2, + PROGRAMMABLE_BUTTON_3, + PROGRAMMABLE_BUTTON_4, + PROGRAMMABLE_BUTTON_5, + PROGRAMMABLE_BUTTON_6, + PROGRAMMABLE_BUTTON_7, + PROGRAMMABLE_BUTTON_8, + PROGRAMMABLE_BUTTON_9, + PROGRAMMABLE_BUTTON_10, + PROGRAMMABLE_BUTTON_11, + PROGRAMMABLE_BUTTON_12, + PROGRAMMABLE_BUTTON_13, + PROGRAMMABLE_BUTTON_14, + PROGRAMMABLE_BUTTON_15, + PROGRAMMABLE_BUTTON_16, + PROGRAMMABLE_BUTTON_17, + PROGRAMMABLE_BUTTON_18, + PROGRAMMABLE_BUTTON_19, + PROGRAMMABLE_BUTTON_20, + PROGRAMMABLE_BUTTON_21, + PROGRAMMABLE_BUTTON_22, + PROGRAMMABLE_BUTTON_23, + PROGRAMMABLE_BUTTON_24, + PROGRAMMABLE_BUTTON_25, + PROGRAMMABLE_BUTTON_26, + PROGRAMMABLE_BUTTON_27, + PROGRAMMABLE_BUTTON_28, + PROGRAMMABLE_BUTTON_29, + PROGRAMMABLE_BUTTON_30, + PROGRAMMABLE_BUTTON_31, + PROGRAMMABLE_BUTTON_32, + // Start of custom keycode range for keyboards and keymaps - always leave at the end SAFE_RANGE }; @@ -854,3 +888,39 @@ enum quantum_keycodes { #define OS_TOGG ONESHOT_TOGGLE #define OS_ON ONESHOT_ENABLE #define OS_OFF ONESHOT_DISABLE + +// Programmable Button aliases +#define PB_1 PROGRAMMABLE_BUTTON_1 +#define PB_2 PROGRAMMABLE_BUTTON_2 +#define PB_3 PROGRAMMABLE_BUTTON_3 +#define PB_4 PROGRAMMABLE_BUTTON_4 +#define PB_5 PROGRAMMABLE_BUTTON_5 +#define PB_6 PROGRAMMABLE_BUTTON_6 +#define PB_7 PROGRAMMABLE_BUTTON_7 +#define PB_8 PROGRAMMABLE_BUTTON_8 +#define PB_9 PROGRAMMABLE_BUTTON_9 +#define PB_10 PROGRAMMABLE_BUTTON_10 +#define PB_11 PROGRAMMABLE_BUTTON_11 +#define PB_12 PROGRAMMABLE_BUTTON_12 +#define PB_13 PROGRAMMABLE_BUTTON_13 +#define PB_14 PROGRAMMABLE_BUTTON_14 +#define PB_15 PROGRAMMABLE_BUTTON_15 +#define PB_16 PROGRAMMABLE_BUTTON_16 +#define PB_17 PROGRAMMABLE_BUTTON_17 +#define PB_18 PROGRAMMABLE_BUTTON_18 +#define PB_19 PROGRAMMABLE_BUTTON_19 +#define PB_20 PROGRAMMABLE_BUTTON_20 +#define PB_21 PROGRAMMABLE_BUTTON_21 +#define PB_22 PROGRAMMABLE_BUTTON_22 +#define PB_23 PROGRAMMABLE_BUTTON_23 +#define PB_24 PROGRAMMABLE_BUTTON_24 +#define PB_25 PROGRAMMABLE_BUTTON_25 +#define PB_26 PROGRAMMABLE_BUTTON_26 +#define PB_27 PROGRAMMABLE_BUTTON_27 +#define PB_28 PROGRAMMABLE_BUTTON_28 +#define PB_29 PROGRAMMABLE_BUTTON_29 +#define PB_30 PROGRAMMABLE_BUTTON_30 +#define PB_31 PROGRAMMABLE_BUTTON_31 +#define PB_32 PROGRAMMABLE_BUTTON_32 +#define PROGRAMMABLE_BUTTON_MIN PROGRAMMABLE_BUTTON_1 +#define PROGRAMMABLE_BUTTON_MAX PROGRAMMABLE_BUTTON_32 -- cgit v1.2.3