Code cleanup (Never Nesting)
This commit is contained in:
parent
12b5526e47
commit
61d5f8afe6
@ -36,23 +36,13 @@ const uint16_t KEY_WIN_LATCH = 2 + KEY_OFFSET; // Function layer 2 button
|
||||
|
||||
const uint8_t NBR_OF_BUTTONS = 42; // Number of buttons used (42 in this case)
|
||||
|
||||
struct Button
|
||||
{
|
||||
uint16_t keycode = NO_KEY;
|
||||
uint16_t fn1_keycode = NO_KEY;
|
||||
uint16_t fn2_keycode = NO_KEY;
|
||||
uint8_t kstate = IDLE;
|
||||
uint16_t last_keycode = NO_KEY;
|
||||
bool run_keycode = false;
|
||||
};
|
||||
|
||||
const byte KP_ROWS = 4;
|
||||
const byte KP_COLS = 12;
|
||||
|
||||
byte kp_rowPins[KP_ROWS] = {0, 1, 2, 3};
|
||||
byte kp_colPins[KP_COLS] = {9, 8, 7, 6, 5, 4, 19, 18, 17, 16, 15, 14};
|
||||
|
||||
const uint8_t MAX_PRESSED_KEYS = LIST_MAX;
|
||||
const uint8_t MAX_SIMULTANIOUS_KEYS = LIST_MAX;
|
||||
|
||||
const char kp_keys_id[KP_ROWS][KP_COLS] = {
|
||||
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
||||
@ -63,15 +53,24 @@ const char kp_keys_id[KP_ROWS][KP_COLS] = {
|
||||
|
||||
Keypad kp_keypad = Keypad(makeKeymap(kp_keys_id), kp_rowPins, kp_colPins, KP_ROWS, KP_COLS);
|
||||
|
||||
/*
|
||||
struct Button
|
||||
{
|
||||
uint16_t keycode = NO_KEY;
|
||||
uint16_t fn1_keycode = NO_KEY;
|
||||
uint16_t fn2_keycode = NO_KEY;
|
||||
uint8_t kstate = IDLE;
|
||||
uint16_t last_keycode = NO_KEY;
|
||||
bool run_keycode = false;
|
||||
};
|
||||
|
||||
/*
|
||||
* "Button ID" corresponding with the physical design of the actual keyboard. DO NOT CHANGE BTN ID!
|
||||
------------------------------------- -------------------------------------
|
||||
| 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 1O | 11 | 12 |
|
||||
| 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | 26 | 27 | 28 | 29 | 30 | | 31 | 32 | 33 | 34 | 35 | 36 |
|
||||
------------------| 37 | 38 | 39 | | 40 | 41 | 42 |------------------
|
||||
------------------- -------------------
|
||||
------------------------------------- -------------------------------------
|
||||
| 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 1O | 11 | 12 |
|
||||
| 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | 26 | 27 | 28 | 29 | 30 | | 31 | 32 | 33 | 34 | 35 | 36 |
|
||||
------------------| 37 | 38 | 39 | | 40 | 41 | 42 |------------------
|
||||
------------------- -------------------
|
||||
* "Fn0 key" is the layer 0 key to use.
|
||||
* "Fn1 key" is the layer 1 key to use. Don NOT add KEY_FN1 or KEY_FN2 to this layer.
|
||||
* "Fn2 key" is the layer 2 key to use. Don NOT add KEY_FN1 or KEY_FN2 to this layer.
|
||||
@ -90,7 +89,6 @@ Keypad kp_keypad = Keypad(makeKeymap(kp_keys_id), kp_rowPins, kp_colPins, KP_ROW
|
||||
* KEY_MINUS = +
|
||||
* KEY_LEFT_ALT = Alt
|
||||
* KEY_RIGHT_ALT = AltGr
|
||||
|
||||
*/
|
||||
|
||||
/* Keymap config ----------------------------------------------------------------------------------------------------------------------------------- */
|
||||
@ -198,128 +196,65 @@ uint8_t set_key(uint16_t keycode, uint8_t kstate)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* find_and_set_button
|
||||
*
|
||||
* @param key_id
|
||||
* @param state_changed true if state changed
|
||||
* @param key_state PRESSED or RELEASED
|
||||
* @return 1 if button found and set, 0 if not
|
||||
*/
|
||||
uint8_t update_button_status(uint8_t key_id, bool state_changed, uint8_t key_state)
|
||||
{
|
||||
if (key_id == 0 || key_id > NBR_OF_BUTTONS || state_changed == false) return 0;
|
||||
|
||||
buttons[key_id - 1].run_keycode = true;
|
||||
buttons[key_id - 1].kstate = key_state;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* find_fn_button
|
||||
*
|
||||
* @param key_id key character
|
||||
* @return 1 if FN button found, 0 if not
|
||||
*/
|
||||
uint8_t check_if_button_is_fn(char key_id)
|
||||
{
|
||||
if (key_id == 0 || key_id > NBR_OF_BUTTONS) return 0;
|
||||
|
||||
if (buttons[key_id - 1].keycode == KEY_FN) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* check_bootloader_mode
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
void check_if_to_enter_bootloader()
|
||||
{
|
||||
/* Enter bootloader if all four corner-buttons is pressed together */
|
||||
int reboot = 0;
|
||||
for (int i = 0; i < MAX_PRESSED_KEYS; i++)
|
||||
{
|
||||
if ((kp_keypad.key[i].kchar == 1) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD))
|
||||
{
|
||||
reboot += 1;
|
||||
}
|
||||
if ((kp_keypad.key[i].kchar == 25) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD))
|
||||
{
|
||||
reboot += 1;
|
||||
}
|
||||
if ((kp_keypad.key[i].kchar == 12) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD))
|
||||
{
|
||||
reboot += 1;
|
||||
}
|
||||
if ((kp_keypad.key[i].kchar == 36) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD))
|
||||
{
|
||||
reboot += 1;
|
||||
}
|
||||
}
|
||||
if (reboot == 4)
|
||||
{
|
||||
_reboot_Teensyduino_(); // reboot to bootloader
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get_fn_mode
|
||||
*
|
||||
* @return 0, 1 or 2 depending on how many FN buttons are pressed
|
||||
*/
|
||||
uint8_t get_fn_mode()
|
||||
{
|
||||
/* Check for Fn1 mode */
|
||||
int fn_mode = 0;
|
||||
for (int i = 0; i < MAX_PRESSED_KEYS; i++)
|
||||
{
|
||||
if (kp_keypad.key[i].kstate != PRESSED && kp_keypad.key[i].kstate != HOLD) continue;
|
||||
fn_mode += check_if_button_is_fn(kp_keypad.key[i].kchar);
|
||||
}
|
||||
return fn_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* process_keypad
|
||||
*
|
||||
* @return void
|
||||
* @return uint8_t 1 if key(s) processed, 0 if not.
|
||||
*/
|
||||
void process_keypad(uint8_t fn_mode)
|
||||
uint8_t process_keys()
|
||||
{
|
||||
/* Process key press/release */
|
||||
for (int i = 0; i < MAX_PRESSED_KEYS; i++)
|
||||
/* Scan keypad, exit if not ready */
|
||||
if (kp_keypad.getKeys() == false) return 0;
|
||||
|
||||
int fn_mode = 0;
|
||||
int corner_pressed = 0;
|
||||
|
||||
for (int i = 0; i < MAX_SIMULTANIOUS_KEYS; i++)
|
||||
{
|
||||
if (kp_keypad.key[i].kstate == PRESSED)
|
||||
/* Process key press/release */
|
||||
if ((kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == RELEASED) && (kp_keypad.key[i].stateChanged == true))
|
||||
{
|
||||
update_button_status(kp_keypad.key[i].kchar, kp_keypad.key[i].stateChanged, PRESSED);
|
||||
}
|
||||
else if (kp_keypad.key[i].kstate == RELEASED)
|
||||
{
|
||||
update_button_status(kp_keypad.key[i].kchar, kp_keypad.key[i].stateChanged, RELEASED);
|
||||
buttons[kp_keypad.key[i].kchar - 1].run_keycode = true;
|
||||
buttons[kp_keypad.key[i].kchar - 1].kstate = kp_keypad.key[i].kstate;
|
||||
}
|
||||
|
||||
if (kp_keypad.key[i].kstate == IDLE || kp_keypad.key[i].kstate == RELEASED) continue;
|
||||
|
||||
/* Count number of corner keys pressed */
|
||||
if (kp_keypad.key[i].kchar == 1) corner_pressed++; // Upper left
|
||||
if (kp_keypad.key[i].kchar == 25) corner_pressed++; // Upper right
|
||||
if (kp_keypad.key[i].kchar == 12) corner_pressed++; // Lower left
|
||||
if (kp_keypad.key[i].kchar == 36) corner_pressed++; // Lower right
|
||||
|
||||
/* Count number of FN keys pressed */
|
||||
if (buttons[kp_keypad.key[i].kchar - 1].keycode == KEY_FN) fn_mode++;
|
||||
}
|
||||
|
||||
/* Enter bootloader if all four corner-buttons is pressed together */
|
||||
if (corner_pressed == 4)
|
||||
{
|
||||
/* Release all keys and reboot to bootloader */
|
||||
Keyboard.releaseAll();
|
||||
delay(200); // Wait for usb to settle before rebooting
|
||||
_reboot_Teensyduino_();
|
||||
}
|
||||
|
||||
/* Execute key commands */
|
||||
for (int i = 0; i < NBR_OF_BUTTONS; i++)
|
||||
{
|
||||
/* Check if key should be processed */
|
||||
if (buttons[i].run_keycode == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (buttons[i].run_keycode == false) continue;
|
||||
|
||||
/* Reset run_keycode flag */
|
||||
buttons[i].run_keycode = false;
|
||||
|
||||
/* Check if key pressed or released */
|
||||
if (buttons[i].kstate == RELEASED)
|
||||
{
|
||||
/* Sending release command for last keycode related to this button */
|
||||
set_key(buttons[i].last_keycode, RELEASED);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Check if key pressed or released */
|
||||
if (fn_mode == 0)
|
||||
{
|
||||
set_key(buttons[i].keycode, PRESSED);
|
||||
@ -336,6 +271,7 @@ void process_keypad(uint8_t fn_mode)
|
||||
buttons[i].last_keycode = buttons[i].fn2_keycode;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void setup()
|
||||
@ -353,14 +289,14 @@ void loop()
|
||||
if (current_timestamp >= button_timestamp)
|
||||
{
|
||||
button_timestamp = current_timestamp + 1;
|
||||
process_keys();
|
||||
}
|
||||
|
||||
/* Scan keypad */
|
||||
if (kp_keypad.getKeys())
|
||||
{
|
||||
check_if_to_enter_bootloader();
|
||||
process_keypad(get_fn_mode());
|
||||
}
|
||||
|
||||
/* Update indicator 200ms */
|
||||
if (current_timestamp >= indicator_timestamp)
|
||||
{
|
||||
indicator_timestamp = current_timestamp + 200;
|
||||
|
||||
/* Set status indication */
|
||||
if (keyboard_leds & (1 << USB_LED_CAPS_LOCK))
|
||||
{
|
||||
@ -374,13 +310,8 @@ void loop()
|
||||
{
|
||||
status_led.off();
|
||||
}
|
||||
}
|
||||
|
||||
/* Update indicator 200ms */
|
||||
if (current_timestamp >= indicator_timestamp)
|
||||
{
|
||||
|
||||
status_led.update();
|
||||
indicator_timestamp = current_timestamp + 200;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user