23#include "px_kbd_matrix.h"
51 uint8_t
byte = key / 8;
52 uint8_t bit = key % 8;
56 key_state->bit_mask[byte] |= (1 << bit);
60 key_state->bit_mask[byte] &= ~(1 << bit);
67 uint8_t
byte = key / 8;
68 uint8_t bit = key % 8;
70 uint8_t state = (key_state->bit_mask[byte] >> bit) & 1;
83 key_result->bit_mask[i] = key_x->bit_mask[i] & key_y->bit_mask[i];
95 key_result->bit_mask[i] = key_x->bit_mask[i] | key_y->bit_mask[i];
107 key_result->bit_mask[i] = key_x->bit_mask[i] ^ key_y->bit_mask[i];
111static bool px_kbd_matrix_phantom_key(
px_link_list_t * key_press_list)
115 bool same_row_flag, same_col_flag;
121 while(key_press !=
NULL)
124 same_row_flag =
false;
125 same_col_flag =
false;
129 while(key_press_compare !=
NULL)
132 if(key_press_compare != key_press)
135 if(key_press->row == key_press_compare->row)
137 same_row_flag =
true;
140 if(key_press->col == key_press_compare->col)
142 same_col_flag =
true;
146 if( (same_row_flag ==
true) && (same_col_flag ==
true) )
189 key_press->row = row;
190 key_press->col = col;
200 if(px_kbd_matrix_phantom_key(key_press_list))
218 while(key_press_item !=
NULL)
224 if( (key_press->row == row) && (key_press->col == col) )
264 kbd_key_state_copy(&pressed, &(matrix->
key_history[0]));
267 kbd_key_state_and(&pressed, &(matrix->
key_history[i]), &pressed);
276 kbd_key_state_copy(&released, &(matrix->
key_history[0]));
279 kbd_key_state_or(&released, &(matrix->
key_history[i]), &released);
283 kbd_key_state_or(&(matrix->
key_state), &pressed, &
new);
284 kbd_key_state_and(&
new, &released, &
new);
286 kbd_key_state_xor(&(matrix->
key_state), &
new, &changed);
289 for (i = 0; i < nr_of_keys; i++)
291 if (kbd_key_state_get(&changed, i))
295 if(kbd_key_state_get(&
new, i))
304 px_kbd_matrix_on_key_event(matrix,
311 kbd_key_state_copy(&(matrix->
key_state), &
new);
326 bool key_pressed_flag =
false;
329 for (row = 0; row < matrix->
nr_of_rows; row++)
342 key_pressed_flag =
true;
347 for (row = 0; row < matrix->
nr_of_rows; row++)
352 matrix->
delay(
false);
354 return key_pressed_flag;
364 uint8_t row, col, key;
370 kbd_key_state_init(key_state);
373 if(px_kbd_matrix_key_pressed(matrix))
377 for (row = 0; row < matrix->
nr_of_rows; row++)
391 kbd_key_state_set(key_state, key, 1);
400 matrix->
delay(
false);
446 uint8_t nr_of_columns,
459 matrix->
delay = delay;
464 kbd_key_state_init(&(matrix->
key_state));
486 px_kbd_matrix_sample (matrix);
487 px_kbd_matrix_debounce(matrix);
488 px_kbd_matrix_repeat (matrix);
#define NULL
NULL pointer.
px_kbd_matrix_set_row_t set_row
Function handler to write a row output.
px_kbd_matrix_get_col_t get_col
Function handler to read a column input.
uint8_t nr_of_columns
Number of columns in matrix.
px_kbd_matrix_on_key_event_t on_key_event_handler
Function that will be called if a key is pressed or released.
uint8_t key_history_index
Wrapping index (used as a ring buffer)
px_kbd_matrix_key_t key_press_array[PX_KBD_MATRIX_MAX_NR_KEYS_PRESSED]
Storage for linked list to remember which keys are pressed.
px_kbd_matrix_key_state_t key_history[PX_KBD_MATRIX_NR_SAMPLES]
Array of bit masks that stores the key state history.
px_link_list_t key_press_list
Linked list of pressed keys.
px_kbd_matrix_key_state_t key_state
Bit mask that stores the current state of the keys.
px_kbd_matrix_delay_t delay
Function handler to read a column input.
uint16_t key_repeat_counter
Counter to determine if a pressed key must repeated (typematic)
uint8_t nr_of_rows
Number of rows in matrix.
void(* px_kbd_matrix_on_key_event_t)(uint8_t row, uint8_t col, px_kbd_matrix_event_t event)
Function that will be called when a key event has taken place.
#define PX_KBD_MATRIX_MAX_NR_KEYS_PRESSED
Maximum number of keys that may be pressed simultaneously.
void(* px_kbd_matrix_set_row_t)(uint8_t row, bool active)
Function that will be called to set a row output.
bool(* px_kbd_matrix_get_col_t)(uint8_t col)
Function that will be called to read a column input.
#define KBD_KEY_REPEAT_DELAY
Delay between pressed key repeats.
px_kbd_matrix_event_t
Key event types.
void(* px_kbd_matrix_delay_t)(bool active)
Function that will be called for row column scanning delay.
#define KBD_DELAY_BEFORE_REPEATING_KEY
Delay before a pressed key will be repeated.
#define PX_KBD_MATRIX_NR_SAMPLES
Number of samples that are necessary to debounce a keypress.
@ PX_KBD_MATRIX_EVENT_PRESSED
Key has been pressed.
@ PX_KBD_MATRIX_EVENT_RELEASED
Key has been released.
Key state bit mask (1 = key pressed; 0 = key released)
Linked list item that describes which key has been pressed.
Keyboard matrix definition and state.
bool px_link_list_is_empty(px_link_list_t *list)
See if the list is empty.
px_link_list_item_t * px_link_list_get_item_next(px_link_list_t *list, px_link_list_item_t *item)
Get a pointer to the next item in the list (after the specified item).
size_t px_link_list_get_item_count(px_link_list_t *list)
Get the number of items in the list.
px_link_list_item_t * px_link_list_remove_item_last(px_link_list_t *list)
Remove last item from the list.
bool px_link_list_insert_item_end(px_link_list_t *list, px_link_list_item_t *item)
Add item to the end of the list.
px_link_list_item_t * px_link_list_get_item_last(px_link_list_t *list)
Get a pointer to the last item in the list.
px_link_list_item_t * px_link_list_get_item_first(px_link_list_t *list)
Get a pointer to the first item in the list.
bool px_link_list_has_item(px_link_list_t *list, px_link_list_item_t *item)
See if item is in the list.
void px_link_list_init(px_link_list_t *list, size_t max_nr_of_items)
Initialises a linked list structure.
void px_link_list_remove_item(px_link_list_t *list, px_link_list_item_t *item)
Remove item from the list.
bool px_link_list_is_full(px_link_list_t *list)
See if the list is full.
void px_link_list_item_init(px_link_list_t *list, px_link_list_item_t *item)
Initialises a list item.
Link structure that must be at the head of each item in the list.