summaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2023-12-11 23:06:18 +0000
committerGitHub <noreply@github.com>2023-12-11 23:06:18 +0000
commit4682226e20d17437c0a6f67f5d6160432074d948 (patch)
tree94a85cb8b8ae55c8f3db7ab09d8af13d66bf0bfa /quantum
parentd85f954d3065da06e0c7eb14068d61f18b23bd25 (diff)
Keymap introspection for Dip Switches (#22543)
Diffstat (limited to 'quantum')
-rw-r--r--quantum/dip_switch.c28
-rw-r--r--quantum/dip_switch.h7
-rw-r--r--quantum/keyboard.h13
-rw-r--r--quantum/keymap_common.c12
-rw-r--r--quantum/keymap_introspection.c18
-rw-r--r--quantum/keymap_introspection.h12
6 files changed, 89 insertions, 1 deletions
diff --git a/quantum/dip_switch.c b/quantum/dip_switch.c
index c4d64aa133..e901f3e0c4 100644
--- a/quantum/dip_switch.c
+++ b/quantum/dip_switch.c
@@ -61,6 +61,28 @@ __attribute__((weak)) bool dip_switch_update_mask_kb(uint32_t state) {
return dip_switch_update_mask_user(state);
}
+#ifdef DIP_SWITCH_MAP_ENABLE
+# include "keymap_introspection.h"
+# include "action.h"
+
+# ifndef DIP_SWITCH_MAP_KEY_DELAY
+# define DIP_SWITCH_MAP_KEY_DELAY TAP_CODE_DELAY
+# endif
+
+static void dip_switch_exec_mapping(uint8_t index, bool on) {
+ // The delays below cater for Windows and its wonderful requirements.
+ action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, true) : MAKE_DIPSWITCH_OFF_EVENT(index, true));
+# if DIP_SWITCH_MAP_KEY_DELAY > 0
+ wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
+# endif // DIP_SWITCH_MAP_KEY_DELAY > 0
+
+ action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, false) : MAKE_DIPSWITCH_OFF_EVENT(index, false));
+# if DIP_SWITCH_MAP_KEY_DELAY > 0
+ wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
+# endif // DIP_SWITCH_MAP_KEY_DELAY > 0
+}
+#endif // DIP_SWITCH_MAP_ENABLE
+
void dip_switch_init(void) {
#ifdef DIP_SWITCH_PINS
# if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
@@ -109,11 +131,17 @@ void dip_switch_read(bool forced) {
dip_switch_mask |= dip_switch_state[i] << i;
if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
has_dip_state_changed = true;
+#ifndef DIP_SWITCH_MAP_ENABLE
dip_switch_update_kb(i, dip_switch_state[i]);
+#else
+ dip_switch_exec_mapping(i, dip_switch_state[i]);
+#endif
}
}
if (has_dip_state_changed) {
+#ifndef DIP_SWITCH_MAP_ENABLE
dip_switch_update_mask_kb(dip_switch_mask);
+#endif
memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
}
}
diff --git a/quantum/dip_switch.h b/quantum/dip_switch.h
index ee5b550ada..7629859359 100644
--- a/quantum/dip_switch.h
+++ b/quantum/dip_switch.h
@@ -46,3 +46,10 @@ void dip_switch_read(bool forced);
void dip_switch_init(void);
void dip_switch_task(void);
+
+#ifdef DIP_SWITCH_MAP_ENABLE
+# define NUM_DIP_STATES 2
+# define DIP_SWITCH_OFF_ON(off, on) \
+ { (off), (on) }
+extern const uint16_t dip_switch_map[NUM_DIP_SWITCHES][NUM_DIP_STATES];
+#endif // DIP_SWITCH_MAP_ENABLE
diff --git a/quantum/keyboard.h b/quantum/keyboard.h
index 5ea57815a7..0f39fde682 100644
--- a/quantum/keyboard.h
+++ b/quantum/keyboard.h
@@ -32,7 +32,7 @@ typedef struct {
uint8_t row;
} keypos_t;
-typedef enum keyevent_type_t { TICK_EVENT = 0, KEY_EVENT = 1, ENCODER_CW_EVENT = 2, ENCODER_CCW_EVENT = 3, COMBO_EVENT = 4 } keyevent_type_t;
+typedef enum keyevent_type_t { TICK_EVENT = 0, KEY_EVENT = 1, ENCODER_CW_EVENT = 2, ENCODER_CCW_EVENT = 3, COMBO_EVENT = 4, DIP_SWITCH_ON_EVENT = 5, DIP_SWITCH_OFF_EVENT = 6 } keyevent_type_t;
/* key event */
typedef struct {
@@ -48,6 +48,8 @@ typedef struct {
/* special keypos_t entries */
#define KEYLOC_ENCODER_CW 253
#define KEYLOC_ENCODER_CCW 252
+#define KEYLOC_DIP_SWITCH_ON 251
+#define KEYLOC_DIP_SWITCH_OFF 250
static inline bool IS_NOEVENT(const keyevent_t event) {
return event.type == TICK_EVENT;
@@ -64,6 +66,9 @@ static inline bool IS_COMBOEVENT(const keyevent_t event) {
static inline bool IS_ENCODEREVENT(const keyevent_t event) {
return event.type == ENCODER_CW_EVENT || event.type == ENCODER_CCW_EVENT;
}
+static inline bool IS_DIPSWITCHEVENT(const keyevent_t event) {
+ return event.type == DIP_SWITCH_ON_EVENT || event.type == DIP_SWITCH_OFF_EVENT;
+}
/* Common keypos_t object factory */
#define MAKE_KEYPOS(row_num, col_num) ((keypos_t){.row = (row_num), .col = (col_num)})
@@ -92,6 +97,12 @@ static inline bool IS_ENCODEREVENT(const keyevent_t event) {
# define MAKE_ENCODER_CCW_EVENT(enc_id, press) MAKE_EVENT(KEYLOC_ENCODER_CCW, (enc_id), (press), ENCODER_CCW_EVENT)
#endif // ENCODER_MAP_ENABLE
+#ifdef DIP_SWITCH_MAP_ENABLE
+/* Dip Switch events */
+# define MAKE_DIPSWITCH_ON_EVENT(switch_id, press) MAKE_EVENT(KEYLOC_DIP_SWITCH_ON, (switch_id), (press), DIP_SWITCH_ON_EVENT)
+# define MAKE_DIPSWITCH_OFF_EVENT(switch_id, press) MAKE_EVENT(KEYLOC_DIP_SWITCH_OFF, (switch_id), (press), DIP_SWITCH_OFF_EVENT)
+#endif // DIP_SWITCH_MAP_ENABLE
+
/* it runs once at early stage of startup before keyboard_init. */
void keyboard_setup(void);
/* it runs once after initializing host side protocol, debug and MCU peripherals. */
diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c
index 91e47a72ee..abdcd5c7ba 100644
--- a/quantum/keymap_common.c
+++ b/quantum/keymap_common.c
@@ -29,6 +29,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "encoder.h"
#endif
+#ifdef DIP_SWITCH_MAP_ENABLE
+# include "dip_switch.h"
+#endif
+
#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
#endif
@@ -204,5 +208,13 @@ __attribute__((weak)) uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key
return keycode_at_encodermap_location(layer, key.col, false);
}
#endif // ENCODER_MAP_ENABLE
+#ifdef DIP_SWITCH_MAP_ENABLE
+ else if (key.row == KEYLOC_DIP_SWITCH_ON && key.col < NUM_DIP_SWITCHES) {
+ return keycode_at_dip_switch_map_location(key.col, true);
+ } else if (key.row == KEYLOC_DIP_SWITCH_OFF && key.col < NUM_DIP_SWITCHES) {
+ return keycode_at_dip_switch_map_location(key.col, false);
+ }
+#endif // DIP_SWITCH_MAP_ENABLE
+
return KC_NO;
}
diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c
index e4a01d2e9a..71e3b429ea 100644
--- a/quantum/keymap_introspection.c
+++ b/quantum/keymap_introspection.c
@@ -72,6 +72,24 @@ __attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num,
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Dip Switch mapping
+
+#if defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
+
+uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on) {
+ if (switch_idx < NUM_DIP_SWITCHES) {
+ return pgm_read_word(&dip_switch_map[switch_idx][!!on]);
+ }
+ return KC_TRNS;
+}
+
+uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
+ return keycode_at_dip_switch_map_location_raw(switch_idx, on);
+}
+
+#endif // defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Combos
#if defined(COMBO_ENABLE)
diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h
index 2012a2b8cc..f7516bf42a 100644
--- a/quantum/keymap_introspection.h
+++ b/quantum/keymap_introspection.h
@@ -36,6 +36,18 @@ uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx,
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Dip Switch mapping
+
+#if defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
+
+// Get the keycode for the dip_switch mapping location, stored in firmware rather than any other persistent storage
+uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on);
+// Get the keycode for the dip_switch mapping location, potentially stored dynamically
+uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on);
+
+#endif // defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Combos
#if defined(COMBO_ENABLE)