Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 支持自定义的模拟多路复用扫描 #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions application/main/src/keyboard/keyboard_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ static uint8_t debouncing = DEBOUNCE_RELOAD;
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];

static matrix_row_t read_cols(void);
static void select_row(uint8_t row);
static void unselect_rows(uint8_t row);

#ifdef ROW_IN
#define READ_COL(pin) (!nrf_gpio_pin_read(pin))
#else
Expand All @@ -63,7 +59,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 +70,7 @@ void matrix_init(void)
}
}
/** read all rows */
static matrix_row_t read_cols(void)
__attribute__((weak)) matrix_row_t read_cols(void)
{
matrix_row_t result = 0;

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

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

static void unselect_rows(uint8_t row)
__attribute__((weak)) void unselect_rows(uint8_t row)
{
if ((uint32_t)row_pin_array[row] > 31 )
return;
Expand All @@ -133,7 +129,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 +161,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 +241,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 +255,7 @@ void matrix_deinit(void)
* @brief 阵列准备睡眠
*
*/
void matrix_wakeup_prepare(void)
__attribute__((weak)) void matrix_wakeup_prepare(void)
{
// 这里监听所有按键作为唤醒按键,所以真正的唤醒判断应该在main的初始化过程中
#ifdef ROW_IN
Expand Down
7 changes: 7 additions & 0 deletions application/main/src/keyboard/keyboard_matrix.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#pragma once
#include <stdint.h>
#include "matrix.h"

void matrix_init(void);
matrix_row_t read_cols(void);
void select_row(uint8_t row);
void unselect_rows(uint8_t row);
uint8_t matrix_scan(void);
bool matrix_is_modified(void);
void matrix_deinit(void);
void matrix_wakeup_prepare(void);

Expand Down
29 changes: 29 additions & 0 deletions keyboard/magnet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 此工程的根目录
ROOT_DIR := ../..

# 特殊目录控制
SDK_ROOT := $(ROOT_DIR)/SDK
TEMPLATE_PATH := $(ROOT_DIR)/template
APP_MAIN_DIR := $(ROOT_DIR)/application/main
TMK_CORE_DIR := $(ROOT_DIR)/tmk/tmk_core
USB_SOURCE_DIR := $(ROOT_DIR)/usb
APP_PROJ_DIR := $(APP_MAIN_DIR)/project
APP_SRC_DIR := $(APP_MAIN_DIR)/src
INC_FOLDERS += .

# 配置文件文件名
CONFIG_H = config.h
CONFIG_H_DIR = .
SRC_FILES += matrix_analog.c keymap_plain.c

include ./rules.mk

purge:
-rm -rf $(OUTPUT_DIR)

go: purge ch554 package merge_bootloader_all

all: default ch554

include $(APP_PROJ_DIR)/kbd.mk
include $(USB_SOURCE_DIR)/usb.mk
1 change: 1 addition & 0 deletions keyboard/magnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# magnet
70 changes: 70 additions & 0 deletions keyboard/magnet/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <stdint.h>

/* Device */
#define VENDOR_ID 0xDE29
#define PRODUCT_ID 0xD707
#define CONF_VENDOR_ID 0x9A29
#define CONF_PRODUCT_ID 0x9707
#define DEVICE_VER 0x0001
#define MANUFACTURER "Leo Deng"
#define PRODUCT "Magnet"
#define DEVICE_BLE_APPEARANCE BLE_APPEARANCE_HID_KEYBOARD
#define MACADDR_SEPRATOR '_'

/* Key Matrix */
// MUX: RS2522XS16; 1 enable pin, 2 channel pins, 2 analog output pins, 8 analog input pins
#define MUX_COUNT 1
static const uint8_t enable_pin_array[MUX_COUNT] = { 6 }; //mux enable, digital output
#define MUX_CHANNELS 2
static const uint8_t channel_pin_array[MUX_CHANNELS] = { 7, 8 }; // mux channel, digital output
#define MATRIX_ROWS 4 // Used mux channels, max = 2 ^ MUX_CHANNELS
#define MATRIX_COLS 2 // Total mux sig pins, MUX_COUNT * MUX_OUTPUT
static const uint8_t row_pin_array[MATRIX_ROWS] = { 0 }; // fake
static const uint8_t column_pin_array[MATRIX_COLS] = { 4, 5 }; // mux output, analog input
#define DEBOUNCE 5
#define MATRIX_SCAN_DELAY_CYCLE 36
#define WAKE_UP_PIN 12

/* Magnet Switch Value Range */
#define MAGNET_THRESHOLD_TOP 486 // stay still
#define MAGNET_THRESHOLD_LIGHT 430
#define MAGNET_THRESHOLD_DEFAULT 380
#define MAGNET_THRESHOLD_HEAVY 330
#define MAGNET_THRESHOLD_BOTTOM 270 // fully push down

/* Command Key */
#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LEFT) | MOD_BIT(KC_RGHT)))

/* Power Saving */
#define SLEEP_SLOW_TIMEOUT 15 // 键盘闲置多久后转入慢速扫描模式 (s)
#define SLEEP_OFF_TIMEOUT 600 // 键盘闲置多久后转入自动关机 (s)
#define KEYBOARD_SCAN_INTERVAL 1 // 键盘最小时间单位TICK (ms)
#define KEYBOARD_FAST_SCAN_INTERVAL 10 // 通常模式下,多久扫描一次键盘 (ms)
#define KEYBOARD_SLOW_SCAN_INTERVAL 100 // 慢速模式下,多久扫描一次键盘 (ms)
#define LED_AUTOOFF_TIME 60 // LED自动熄灭时长(s),设为0则不自动熄灭
#define DYNAMIC_TX_POWER /* 启用自动发射功率调整 */
// #define HIGH_TX_POWER /* 更改发射功率到+4dBm */

/* Extra Features */
#define ENABLE_WATCHDOG /* 启用看门狗 */
#define KEYMAP_STORAGE /* 启用keymap存储 */
#define MACRO_STORAGE /* 启用宏存储功能 */
#define CONFIG_STORAGE /* 启用配置存储功能 */
#define BUTTONLESS_DFU /* 启用免按钮DFU */

/* USB HID */
#define KEYBOARD_EPSIZE 8 /* 键盘上传端点大小,请不要修改 */
#define NKRO_EPSIZE 28 /* 键盘NKRO端点大小,请不要修改 */

/* USB UART */
#define HAS_USB // 启用与CH554的通信支持
#define UART_RXD 26 // UART_RX IO
#define UART_TXD 27 // UART_TX IO
#define UART_BAUDRATE NRF_UART_BAUDRATE_115200 // 通信波特率,请不要修改

/* Battery */
#define BATTERY_ADC_PIN NRF_SAADC_INPUT_AIN0 // 电量检测引脚
#define PIN_CHARGING !UCC1 // CH554的充电检测。当UCC1拉低时表示正在充电
#define PIN_STANDBY !UCC2 // CH554的充电检测。当UCC2拉低时表示充电完成。若不配置则只使用PIN_CHARGING作为是否充电的检测标志
20 changes: 20 additions & 0 deletions keyboard/magnet/keymap_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <stdint.h>
#include "keymap.h"
#include "config.h"

extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
extern const action_t fn_actions[];

#define KEYMAP( \
K00, K01, \
K10, K11, \
K20, K21, \
K30, K31 \
) { \
{ K00, K01 }, \
{ K10, K11 }, \
{ K20, K21 }, \
{ K30, K31 } \
}
21 changes: 21 additions & 0 deletions keyboard/magnet/keymap_plain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "keymap_common.h"
#include "keyboard_fn.h"

const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
KEYMAP(
KC_1, KC_2,
KC_Q, KC_W,
KC_A, KC_S,
KC_Z, KC_FN0
),
KEYMAP(
KC_5, KC_6,
KC_T, KC_Y,
KC_G, KC_H,
KC_B, KC_TRNS
),
};

const action_t fn_actions[] = {
ACTION_LAYER_MOMENTARY(1),
};
Loading
Loading