-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
efm32: driver: implement matrix key driver
Signed-off-by: perigoso <[email protected]>
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2021 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#include "platform/efm32gg/driver/key_matrix.h" | ||
#include "platform/efm32gg/systick.h" | ||
|
||
struct key_matrix_t key_matrix_init( | ||
struct gpio_pin_t *col_pin_list, | ||
u8 col_pin_list_size, | ||
struct gpio_pin_t *row_pin_list, | ||
u8 row_pin_list_size, | ||
struct key_t *key_data, | ||
u8 col_to_row) | ||
{ | ||
struct key_matrix_t matrix = {}; | ||
|
||
matrix.col_pin_list = col_pin_list; | ||
matrix.col_pin_list_size = col_pin_list_size; | ||
matrix.row_pin_list = row_pin_list; | ||
matrix.row_pin_list_size = row_pin_list_size; | ||
matrix.key_data = key_data; | ||
matrix.col_to_row = col_to_row; | ||
matrix.update_timestamp = systick_get_ticks(); | ||
|
||
for (size_t col = 0; col < matrix.col_pin_list_size; col++) gpio_set(col_pin_list[col], !matrix.col_to_row); | ||
|
||
return matrix; | ||
} | ||
|
||
void key_matrix_scan(struct key_matrix_t *matrix) | ||
{ | ||
u16 t_delta = systick_get_ticks() - matrix->update_timestamp; | ||
matrix->update_timestamp = systick_get_ticks(); | ||
|
||
for (size_t col = 0; col < matrix->col_pin_list_size; col++) { | ||
/* set col output */ | ||
gpio_set(matrix->col_pin_list[col], matrix->col_to_row); | ||
|
||
for (size_t row = 0; row < matrix->row_pin_list_size; row++) { | ||
u8 index = (col * matrix->row_pin_list_size) + row; | ||
enum key_status_t last_status = matrix->key_data[index].key_status; | ||
|
||
/* get row input */ | ||
matrix->key_data[index].key_status = | ||
(gpio_get(matrix->row_pin_list[row]) ^ matrix->col_to_row) ? key_released : key_pressed; | ||
|
||
if (matrix->key_data[index].key_status != last_status) | ||
matrix->key_data[index].key_t_delta = 0; | ||
else | ||
matrix->key_data[index].key_t_delta += t_delta; | ||
} | ||
|
||
/* clear col output */ | ||
gpio_set(matrix->col_pin_list[col], !matrix->col_to_row); | ||
|
||
/* settle time */ | ||
delay_us(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2021 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "util/types.h" | ||
|
||
#include "platform/efm32gg/gpio.h" | ||
|
||
#include "key_actions/key_actions.h" | ||
|
||
struct key_matrix_t { | ||
struct gpio_pin_t *col_pin_list; | ||
u8 col_pin_list_size; | ||
struct gpio_pin_t *row_pin_list; | ||
u8 row_pin_list_size; | ||
struct key_t *key_data; | ||
u32 update_timestamp; | ||
u8 col_to_row : 1; | ||
}; | ||
|
||
struct key_matrix_t key_matrix_init( | ||
struct gpio_pin_t *col_pin_list, | ||
u8 col_pin_list_size, | ||
struct gpio_pin_t *row_pin_list, | ||
u8 row_pin_list_size, | ||
struct key_t *key_data, | ||
u8 col_to_row); | ||
void key_matrix_scan(struct key_matrix_t *matrix); |