2019-12-12 20:46:29 +00:00
|
|
|
// -*- mode: C; tab-width: 2 ; indent-tabs-mode: nil -*-
|
|
|
|
//
|
2019-12-15 08:09:15 +00:00
|
|
|
// Unified Keyboard Project
|
2019-12-12 20:46:29 +00:00
|
|
|
// ASDF keyboard firmware
|
|
|
|
//
|
|
|
|
// asdf_keymaps.c
|
|
|
|
//
|
|
|
|
// Copyright 2019 David Fenyes
|
|
|
|
//
|
|
|
|
// 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 3 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2021-01-29 20:30:19 +00:00
|
|
|
#include <stddef.h>
|
2021-02-19 20:10:25 +00:00
|
|
|
#include "asdf_config.h"
|
2019-12-12 20:46:29 +00:00
|
|
|
#include "asdf.h"
|
|
|
|
#include "asdf_arch.h"
|
2020-03-12 05:45:59 +00:00
|
|
|
#include "asdf_physical.h"
|
2020-03-06 19:24:03 +00:00
|
|
|
#include "asdf_virtual.h"
|
2020-05-01 21:39:21 +00:00
|
|
|
#include "asdf_hook.h"
|
2021-11-28 21:28:03 +00:00
|
|
|
#include "asdf_keymap_setup.h"
|
2019-12-12 20:46:29 +00:00
|
|
|
#include "asdf_keymaps.h"
|
2021-01-29 20:30:19 +00:00
|
|
|
#include "asdf_modifiers.h"
|
2019-12-12 20:46:29 +00:00
|
|
|
|
2021-02-20 06:01:07 +00:00
|
|
|
// The keymap array contains the keymap information for each modifier state
|
|
|
|
// This structure is populated using the asdf_keymaps_add_map() function.
|
2021-02-19 20:10:25 +00:00
|
|
|
static asdf_keycode_map_t keymaps[ASDF_MOD_NUM_MODIFIERS] = {};
|
2020-05-01 21:39:21 +00:00
|
|
|
|
2021-02-19 20:10:25 +00:00
|
|
|
// The current keymap index. This is stored so bitwise operators on the keymap index can be performed.
|
|
|
|
static uint8_t current_keymap_index;
|
2021-01-29 20:30:19 +00:00
|
|
|
|
2021-02-17 17:27:58 +00:00
|
|
|
// PROCEDURE: asdf_keymaps_add_map
|
2021-02-20 06:01:07 +00:00
|
|
|
// INPUTS: (asdf_keycode_t *) - pointer to the keycode matrix to add in to map
|
2021-02-19 20:10:25 +00:00
|
|
|
// (uint8_t) modifier_index - the modifier value for the keycode matrix being added
|
|
|
|
// (uint8_t) rows - number of rows in the keymap
|
|
|
|
// (uint8_t) cols - number of columns in the keymap
|
2021-01-29 20:30:19 +00:00
|
|
|
//
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
2021-02-17 17:27:58 +00:00
|
|
|
// DESCRIPTION: Called by keymap building modules. This routine adds a keymap to the current
|
|
|
|
// setup function into the keymap setup array.
|
2021-01-29 20:30:19 +00:00
|
|
|
//
|
2021-02-17 17:27:58 +00:00
|
|
|
// SIDE EFFECTS:
|
2021-01-29 20:30:19 +00:00
|
|
|
//
|
2021-02-19 20:10:25 +00:00
|
|
|
// NOTES: If the keymap modifier index, num_rows, or num_cols are not valid then no
|
2021-02-17 17:27:58 +00:00
|
|
|
// action is performed.
|
2021-01-29 20:30:19 +00:00
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
2021-02-20 06:01:07 +00:00
|
|
|
void asdf_keymaps_add_map(const asdf_keycode_t *matrix,
|
2021-02-19 20:10:25 +00:00
|
|
|
modifier_index_t modifier_index,
|
|
|
|
uint8_t num_rows, uint8_t num_cols)
|
2021-01-29 20:30:19 +00:00
|
|
|
{
|
2021-02-19 20:10:25 +00:00
|
|
|
if ((modifier_index < ASDF_MOD_NUM_MODIFIERS)
|
|
|
|
&& (num_rows <= ASDF_MAX_ROWS)
|
|
|
|
&& (num_cols <= ASDF_MAX_COLS))
|
2021-02-17 17:27:58 +00:00
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
keymaps[modifier_index].matrix = (asdf_keycode_t *) matrix;
|
2021-02-17 17:27:58 +00:00
|
|
|
keymaps[modifier_index].rows = num_rows;
|
|
|
|
keymaps[modifier_index].cols = num_cols;
|
|
|
|
}
|
2021-01-29 20:30:19 +00:00
|
|
|
}
|
2019-12-12 20:46:29 +00:00
|
|
|
|
2021-02-19 20:10:25 +00:00
|
|
|
// PROCEDURE: asdf_keymaps_num_rows
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: uint8_t - returns number of rows in keymap for current modifier state
|
|
|
|
//
|
|
|
|
// DESCRIPTION: See OUTPUTS
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: none
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: publice
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
uint8_t asdf_keymaps_num_rows(void)
|
|
|
|
{
|
|
|
|
return keymaps[asdf_modifier_index()].rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_num_cols
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: uint8_t - returns number of columns in keymap for current modifier state
|
|
|
|
//
|
|
|
|
// DESCRIPTION: See OUTPUTS
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: none
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: publice
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
uint8_t asdf_keymaps_num_cols(void)
|
|
|
|
{
|
|
|
|
return keymaps[asdf_modifier_index()].cols;
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:02:37 +00:00
|
|
|
// PROCEDURE: asdf_keymaps_reset
|
2021-02-19 20:10:25 +00:00
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
2021-09-22 02:02:37 +00:00
|
|
|
// DESCRIPTION: Reset keymaps to initial state:
|
|
|
|
// - Clear all keycode mapping matrices.
|
|
|
|
// - Clear all virtual devices
|
|
|
|
// - Reset all hooks to default state.
|
2021-02-19 20:10:25 +00:00
|
|
|
//
|
|
|
|
// SIDE EFFECTS: see DESCRIPTION
|
|
|
|
//
|
|
|
|
// SCOPE: private
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 2
|
|
|
|
//
|
2021-09-22 02:02:37 +00:00
|
|
|
static void asdf_keymaps_reset(void)
|
2021-02-19 20:10:25 +00:00
|
|
|
{
|
2021-09-22 02:02:37 +00:00
|
|
|
// Clear all keycode mappings
|
2021-02-19 20:10:25 +00:00
|
|
|
for (uint8_t i = 0; i < ASDF_MOD_NUM_MODIFIERS; i++) {
|
2021-09-22 02:02:37 +00:00
|
|
|
asdf_keymaps_add_map((const asdf_keycode_t *) NULL, (modifier_index_t) i,
|
|
|
|
0, 0);
|
2021-02-19 20:10:25 +00:00
|
|
|
}
|
2021-09-22 02:02:37 +00:00
|
|
|
|
|
|
|
// Clear virtual devices
|
|
|
|
asdf_virtual_init();
|
|
|
|
|
|
|
|
// Reset hooks
|
|
|
|
asdf_hook_init();
|
2021-02-19 20:10:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 06:01:07 +00:00
|
|
|
// PROCEDURE: asdf_keymaps_select
|
2020-02-20 05:22:23 +00:00
|
|
|
// INPUTS: (uint8_t) index - index of the keymap number to select
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: accepts a index value. If the requested keymap index is valid,
|
2020-03-21 19:26:57 +00:00
|
|
|
// then:
|
2021-01-29 20:30:19 +00:00
|
|
|
// 1) assign the value to the global (to the module) current_keymap_index variable
|
2020-05-15 19:48:40 +00:00
|
|
|
// 2) execute the architecture-dependent init routine, to undo any settings
|
|
|
|
// from the previous keymap
|
2021-02-19 20:10:25 +00:00
|
|
|
// 3) execute the keymap-specific setup routine.
|
2020-02-20 05:22:23 +00:00
|
|
|
//
|
2021-02-19 20:10:25 +00:00
|
|
|
// SIDE EFFECTS: See DESCRIPTION
|
2020-02-20 05:22:23 +00:00
|
|
|
//
|
2021-01-29 20:30:19 +00:00
|
|
|
// NOTES: If the requested index is not valid, then no action is performed.
|
2020-02-20 05:22:23 +00:00
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 2
|
|
|
|
//
|
2021-02-20 06:01:07 +00:00
|
|
|
void asdf_keymaps_select(uint8_t index)
|
2020-02-20 05:22:23 +00:00
|
|
|
{
|
|
|
|
if (index < ASDF_NUM_KEYMAPS) {
|
2021-11-28 21:28:03 +00:00
|
|
|
|
|
|
|
|
2020-05-15 19:48:40 +00:00
|
|
|
asdf_arch_init();
|
2021-09-22 02:02:37 +00:00
|
|
|
asdf_keymaps_reset();
|
2021-02-19 20:10:25 +00:00
|
|
|
current_keymap_index = index;
|
2021-11-28 21:28:03 +00:00
|
|
|
|
|
|
|
asdf_print("\r\nIndex is %d.", index);
|
|
|
|
asdf_keymap_setup(index);
|
|
|
|
asdf_print("\r\nAfter setup function\n");
|
2020-02-20 05:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 20:46:29 +00:00
|
|
|
|
2021-02-19 20:10:25 +00:00
|
|
|
// PROCEDURE: ASDF_KEYMAPS_DUMMY_FUNCTION
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: null function to populate the keymap setup function table. Since
|
|
|
|
// this function does nothing, then selecting an unregistered keymap has no
|
|
|
|
// effect (i.e., the previous keymap persisits)
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: See Description
|
|
|
|
//
|
|
|
|
// SCOPE: private
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_dummy_function(void) {}
|
|
|
|
|
2019-12-12 20:46:29 +00:00
|
|
|
// PROCEDURE: asdf_keymaps_init
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
2021-05-19 18:32:23 +00:00
|
|
|
// DESCRIPTION: initialize the keymap list. Called at startup. This function
|
|
|
|
// calls the auto-generated keymap table init function, then selects keymap 0.
|
2019-12-12 20:46:29 +00:00
|
|
|
//
|
2021-02-17 17:27:58 +00:00
|
|
|
// SIDE EFFECTS: See DESCRIPTION
|
2019-12-12 20:46:29 +00:00
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_init(void)
|
|
|
|
{
|
2021-11-28 05:19:16 +00:00
|
|
|
asdf_keymaps_select(0);
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_0_clear
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 0 switch is open. Clears the 0 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_0_clear(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index & (~ASDF_KEYMAP_BIT_0));
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_0_set
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 0 switch is closed. Sets the 0 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_0_set(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index | ASDF_KEYMAP_BIT_0);
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_1_clear
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 1 switch is open. Clears the 1 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_1_clear(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index & (~ASDF_KEYMAP_BIT_1));
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_1_set
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 1 switch is closed. Sets the 1 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_1_set(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index | ASDF_KEYMAP_BIT_1);
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_2_clear
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 2 switch is open. Clears the 2 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_2_clear(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index & (~ASDF_KEYMAP_BIT_2));
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_2_set
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 2 switch is closed. Sets the 2 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_2_set(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index | ASDF_KEYMAP_BIT_2);
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_3_clear
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 3 switch is open. Clears the 3 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_3_clear(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index & (~ASDF_KEYMAP_BIT_3));
|
2020-02-20 05:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_map_select_3_set
|
|
|
|
// INPUTS: none
|
|
|
|
// OUTPUTS: none
|
|
|
|
//
|
|
|
|
// DESCRIPTION: called when map select 3 switch is closed. Sets the 3 bit in the
|
|
|
|
// keymap index.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: changes selected keymap
|
|
|
|
//
|
|
|
|
// NOTES:
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
void asdf_keymaps_map_select_3_set(void)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
asdf_keymaps_select(current_keymap_index | ASDF_KEYMAP_BIT_3);
|
2019-12-12 20:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROCEDURE: asdf_keymaps_get_code
|
|
|
|
// INPUTS: row, col: row and column of key that has been pressed
|
|
|
|
// modifiers_index: index into the keymap array, based on modifier state
|
|
|
|
// OUTPUTS: returns a key code.
|
|
|
|
//
|
|
|
|
// DESCRIPTION: Given a key row and column, and an index based on modifier
|
|
|
|
// state, return the appropriate keycode.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: none
|
|
|
|
//
|
|
|
|
// SCOPE: public
|
|
|
|
//
|
2020-02-20 05:28:14 +00:00
|
|
|
// NOTES: Since the first value in each row is the physical row number, we add 1
|
|
|
|
// to the column number to read the code for a given row and column number.
|
|
|
|
//
|
2019-12-12 20:46:29 +00:00
|
|
|
// COMPLEXITY: 1
|
|
|
|
//
|
|
|
|
asdf_keycode_t asdf_keymaps_get_code(const uint8_t row, const uint8_t col,
|
|
|
|
const uint8_t modifier_index)
|
|
|
|
{
|
2021-02-20 06:01:07 +00:00
|
|
|
uint8_t num_cols = keymaps[current_keymap_index].cols;
|
|
|
|
asdf_keycode_t keycode = 0;
|
|
|
|
|
|
|
|
if (keymaps[current_keymap_index].cols && keymaps[current_keymap_index].rows) {
|
|
|
|
asdf_keycode_t (*keycode_matrix)[num_cols] = (void *) (keymaps[modifier_index].matrix);
|
|
|
|
keycode = FLASH_READ_MATRIX_ELEMENT(keycode_matrix, row, col);
|
|
|
|
}
|
|
|
|
return keycode;
|
2019-12-12 20:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------|---------|---------+---------+---------+---------+---------+---------+
|
|
|
|
// Above line is 80 columns, and should display completely in the editor.
|