Skip to content

Commit

Permalink
可覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
myst729 committed Nov 29, 2023
1 parent d63185e commit c755f6c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 96 deletions.
16 changes: 8 additions & 8 deletions application/main/src/keyboard/keyboard_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void unselect_rows(uint8_t row);
* @brief 初始化键盘阵列
*
*/
void matrix_init(void)
__attribute__((weak)) void matrix_init(void)
{
for (uint_fast8_t i = MATRIX_COLS; i--;) {
#ifdef ROW_IN
Expand All @@ -74,7 +74,7 @@ void matrix_init(void)
}
}
/** read all rows */
static matrix_row_t read_cols(void)
__attribute__((weak)) static matrix_row_t read_cols(void)
{
matrix_row_t result = 0;

Expand All @@ -86,7 +86,7 @@ static matrix_row_t read_cols(void)
return result;
}

static void select_row(uint8_t row)
__attribute__((weak)) static void select_row(uint8_t row)
{
if ((uint32_t)row_pin_array[row] > 31 )
return;
Expand All @@ -108,7 +108,7 @@ nrf_gpio_cfg(
#endif
}

static void unselect_rows(uint8_t row)
__attribute__((weak)) static void unselect_rows(uint8_t row)
{
if ((uint32_t)row_pin_array[row] > 31 )
return;
Expand All @@ -133,7 +133,7 @@ static inline void delay_us(void)
}
}

uint8_t matrix_scan(void)
__attribute__((weak)) uint8_t matrix_scan(void)
{
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
Expand Down Expand Up @@ -165,7 +165,7 @@ uint8_t matrix_scan(void)
return 1;
}

bool matrix_is_modified(void)
__attribute__((weak)) bool matrix_is_modified(void)
{
if (debouncing)
return false;
Expand Down Expand Up @@ -245,7 +245,7 @@ uint8_t matrix_key_count(void)
* @brief 禁用所有阵列针脚
*
*/
void matrix_deinit(void)
__attribute__((weak)) void matrix_deinit(void)
{
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
nrf_gpio_cfg_default(column_pin_array[i]);
Expand All @@ -259,7 +259,7 @@ void matrix_deinit(void)
* @brief 阵列准备睡眠
*
*/
void matrix_wakeup_prepare(void)
__attribute__((weak)) void matrix_wakeup_prepare(void)
{
// 这里监听所有按键作为唤醒按键,所以真正的唤醒判断应该在main的初始化过程中
#ifdef ROW_IN
Expand Down
2 changes: 1 addition & 1 deletion keyboard/magnet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INC_FOLDERS += .
# 配置文件文件名
CONFIG_H = config.h
CONFIG_H_DIR = .
SRC_FILES += keymap_plain.c
SRC_FILES += matrix_analog.c keymap_plain.c

include ./rules.mk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "nrf_delay.h"
#include "nrf_gpio.h"

#include "../config/keyboard_config.h"
#include "keyboard_config.h"
#include "adc_convert.h"
#include "ble_keyboard.h"
#include "debug.h"
Expand All @@ -15,15 +15,6 @@
#include "util.h"
#include "wait.h"

#ifndef DEBOUNCE
#define DEBOUNCE 1
#endif

/* matrix state (1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t read_cols_on_row(void);
static void select_mux_channel(uint8_t channel);

// 行程触发阈值
// TODO 先这样,动态调节的功能后面再加
static uint16_t magnet_threshold = MAGNET_THRESHOLD_DEFAULT;
Expand Down Expand Up @@ -74,8 +65,8 @@ void matrix_init(void) {
nrf_gpio_cfg_default(WAKE_UP_PIN);
}

/* Read all cols */
static matrix_row_t read_cols_on_row(void) {
/* Read all mux sig pins */
static matrix_row_t read_cols(void) {
matrix_row_t result = 0;

for (uint_fast8_t c = 0; c < MATRIX_COLS; c++) {
Expand All @@ -94,7 +85,7 @@ static matrix_row_t read_cols_on_row(void) {
}

/* Select mux channel */
static void select_mux_channel(uint8_t channel) {
static void select_row(uint8_t channel) {
for (uint_fast8_t i = 0; i < MUX_CHANNELS; i++) {
uint8_t state = (channel >> i) & 1;
if (state) {
Expand All @@ -109,24 +100,11 @@ static void select_mux_channel(uint8_t channel) {
}
}

static inline void delay_us(void) {
#ifdef __GNUC__
#define __nop() __asm("NOP")
#endif

#ifndef MATRIX_SCAN_DELAY_CYCLE
#define MATRIX_SCAN_DELAY_CYCLE 36
#endif
for (int i = 0; i < MATRIX_SCAN_DELAY_CYCLE; i++) {
__nop(); // 64MHz, 64 cycles = 1us
}
}

uint8_t matrix_scan(void) {
for (uint_fast8_t i = 0; i < MATRIX_ROWS; i++) {
select_mux_channel(i);
select_row(i);
delay_us(); // wait stable
matrix_row_t cols = read_cols_on_row();
matrix_row_t cols = read_cols();
matrix[i] = cols;
}

Expand All @@ -137,64 +115,6 @@ bool matrix_is_modified(void) {
return true;
}

/* 外部按键 */
#ifdef MATRIX_FORIGN_KEY
static bool matrix_oneshot_send[MATRIX_ROWS];
static matrix_row_t matrix_forign_oneshot[MATRIX_ROWS];
static matrix_row_t matrix_forign[MATRIX_ROWS];

/* 添加一个按键按下事件(会自动清空) */
void matrix_forign_add_oneshot(uint8_t row, uint8_t col) {
if (row >= MATRIX_ROWS) {
return;
}
if (col >= sizeof(matrix_row_t) * 8) {
return;
}

matrix_forign_oneshot[row] |= (1 << col);
}

/* 添加一个按键事件 */
void matrix_forign_set(uint8_t row, uint8_t col, bool press) {
if (row >= MATRIX_ROWS) {
return;
}
if (col >= sizeof(matrix_row_t) * 8) {
return;
}
if (press) {
matrix_forign[row] |= (1 << col);
} else {
matrix_forign[row] &= ~(1 << col);
}
}

inline matrix_row_t matrix_get_row(uint8_t row) {
matrix_row_t val = matrix[row] | matrix_forign[row];

// 发送一次后在下次发送空包,防止多次连击
if (matrix_oneshot_send[row]) {
val |= matrix_forign_oneshot[row];
matrix_forign_oneshot[row] = 0; // 清空单个按键
}
matrix_oneshot_send[row] = !matrix_oneshot_send[row];
return val;
}
#else
inline matrix_row_t matrix_get_row(uint8_t row) {
return matrix[row];
}
#endif

uint8_t matrix_key_count(void) {
uint8_t count = 0;
for (uint_fast8_t i = 0; i < MATRIX_ROWS; i++) {
count += bitpop16(matrix[i]);
}
return count;
}

/* 禁用所有阵列针脚 */
void matrix_deinit(void) {
for (uint_fast8_t i = 0; i < MUX_COUNT; i++) {
Expand Down Expand Up @@ -225,6 +145,6 @@ void matrix_wakeup_prepare(void) {
}

// 只保留一个唤醒引脚,上拉即唤醒。
// TODO 考虑用振动开关实现
// TODO 考虑用振动开关,或者TTP223电容触摸
nrf_gpio_cfg_sense_input(WAKE_UP_PIN, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
}

0 comments on commit c755f6c

Please sign in to comment.