Code cleanup

This commit is contained in:
Christoffer Martinsson 2023-06-05 11:56:20 +02:00
parent 9342f95aee
commit 9479278166

View File

@ -27,22 +27,22 @@ uint64_t indicator_timestamp = 0;
bool key_pressed = false; bool key_pressed = false;
bool win_latched = false; bool win_latched = false;
const byte KP_ROWS = 4; const byte KEYBOARD_MATRIX_ROWS = 4;
const byte KP_COLS = 12; const byte KEYBOARD_MATRIX_COLS = 12;
byte kp_rowPins[KP_ROWS] = {0, 1, 2, 3}; byte keyboard_martix_row_pins[KEYBOARD_MATRIX_ROWS] = {0, 1, 2, 3};
byte kp_colPins[KP_COLS] = {9, 8, 7, 6, 5, 4, 19, 18, 17, 16, 15, 14}; byte keyboard_matrix_col_pins[KEYBOARD_MATRIX_COLS] = {9, 8, 7, 6, 5, 4, 19, 18, 17, 16, 15, 14};
const uint8_t MAX_SIMULTANIOUS_KEYS = LIST_MAX; const uint8_t MAX_SIMULTANIOUS_KEYS = LIST_MAX;
const char kp_keys_id[KP_ROWS][KP_COLS] = { const char keyboard_matrix_id[KEYBOARD_MATRIX_ROWS][KEYBOARD_MATRIX_COLS] = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}, {25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36},
{0, 0, 0, 37, 38, 39, 40, 41, 42, 0, 0, 0}, {0, 0, 0, 37, 38, 39, 40, 41, 42, 0, 0, 0},
}; };
Keypad kp_keypad = Keypad(makeKeymap(kp_keys_id), kp_rowPins, kp_colPins, KP_ROWS, KP_COLS); Keypad keyboard_matrix = Keypad(makeKeymap(keyboard_matrix_id), keyboard_martix_row_pins, keyboard_matrix_col_pins, KEYBOARD_MATRIX_ROWS, KEYBOARD_MATRIX_COLS);
struct Button struct Button
{ {
@ -138,16 +138,15 @@ Button buttons[NBR_OF_BUTTONS] =
@param keycode code to apply action. @param keycode code to apply action.
@param kstate PRESSED or RELEASED. @param kstate PRESSED or RELEASED.
@return 1 if action applied. 0 if action aborted.
*/ */
uint8_t set_key(uint16_t keycode, uint8_t kstate) void set_key(uint16_t keycode, uint8_t kstate)
{ {
if (keycode == NO_KEY || keycode == KEY_FN) return 0; if (keycode == NO_KEY || keycode == KEY_FN) return;
if (keycode == KEY_GUI_LATCH) if (keycode == KEY_GUI_LATCH)
{ {
win_latched = true; win_latched = true;
return 1; return;
} }
if (kstate == RELEASED) if (kstate == RELEASED)
@ -175,18 +174,16 @@ uint8_t set_key(uint16_t keycode, uint8_t kstate)
Keyboard.press(keycode); Keyboard.press(keycode);
} }
} }
return 1;
} }
/** /**
* process_keypad * process_keypad
* *
* @return uint8_t 1 if key(s) processed, 0 if not.
*/ */
uint8_t process_keys() void process_keys()
{ {
/* Scan keypad, exit if not ready */ /* Scan keypad, exit if not ready */
if (kp_keypad.getKeys() == false) return 0; if (keyboard_matrix.getKeys() == false) return;
int fn_mode = 0; int fn_mode = 0;
int corner_pressed = 0; int corner_pressed = 0;
@ -194,23 +191,23 @@ uint8_t process_keys()
for (int i = 0; i < MAX_SIMULTANIOUS_KEYS; i++) for (int i = 0; i < MAX_SIMULTANIOUS_KEYS; i++)
{ {
/* Update button table if key pressed/releaseed */ /* Update button table if key pressed/releaseed */
if ((kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == RELEASED) && (kp_keypad.key[i].stateChanged == true)) if ((keyboard_matrix.key[i].kstate == PRESSED || keyboard_matrix.key[i].kstate == RELEASED) && (keyboard_matrix.key[i].stateChanged == true))
{ {
buttons[kp_keypad.key[i].kchar - 1].run_keycode = true; buttons[keyboard_matrix.key[i].kchar - 1].run_keycode = true;
buttons[kp_keypad.key[i].kchar - 1].kstate = kp_keypad.key[i].kstate; buttons[keyboard_matrix.key[i].kchar - 1].kstate = keyboard_matrix.key[i].kstate;
} }
/* Skip if key is not pressed */ /* Skip if key is not pressed */
if (kp_keypad.key[i].kstate == IDLE || kp_keypad.key[i].kstate == RELEASED) continue; if (keyboard_matrix.key[i].kstate == IDLE || keyboard_matrix.key[i].kstate == RELEASED) continue;
/* Count number of corner keys pressed */ /* Count number of corner keys pressed */
if (kp_keypad.key[i].kchar == 1) corner_pressed++; // Upper left if (keyboard_matrix.key[i].kchar == 1) corner_pressed++; // Upper left
if (kp_keypad.key[i].kchar == 25) corner_pressed++; // Upper right if (keyboard_matrix.key[i].kchar == 25) corner_pressed++; // Upper right
if (kp_keypad.key[i].kchar == 12) corner_pressed++; // Lower left if (keyboard_matrix.key[i].kchar == 12) corner_pressed++; // Lower left
if (kp_keypad.key[i].kchar == 36) corner_pressed++; // Lower right if (keyboard_matrix.key[i].kchar == 36) corner_pressed++; // Lower right
/* Count number of FN keys pressed */ /* Count number of FN keys pressed */
if (buttons[kp_keypad.key[i].kchar - 1].keycode == KEY_FN) fn_mode++; if (buttons[keyboard_matrix.key[i].kchar - 1].keycode == KEY_FN) fn_mode++;
} }
if (corner_pressed == 4) if (corner_pressed == 4)
@ -250,7 +247,6 @@ uint8_t process_keys()
buttons[i].last_keycode = buttons[i].fn2_keycode; buttons[i].last_keycode = buttons[i].fn2_keycode;
} }
} }
return 1;
} }
void setup() void setup()