键盘外置驱动编写

  

  在事件系统的基础上注册一个事件handler来处理各项事件。

  假设要编写一个custom_driver.c:

  

 1 // 引入事件系统头文件
 2 #include "../keyboard/keyboard_evt.h"
 3 
 4 // 编写事件处理函数
 5 static void custom_driver_event_handler(enum user_event event, void* arg)
 6 {
 7     // code...
 8 }
 9 
10 // 注册事件
11 EVENT_HANDLER(custom_driver_event_handler);

  然后在 driver.mk 内添加编译控制命令

 

  事件说明:

   详细见events.h。 默认的所有事件的arg都不是一个指针,而是一个枚举。需要使用的时候请将这个参数强制转换为枚举。若需要自定义事件,请自行在驱动模块内定义事件的event(ID)和参数类型。

 

  

  自定义FN处理过程

  

1 #include "../keyboard/keyboard_fn.h"
2 
3 void custom_fn_handler(keyrecord_t* record, uint8_t id, uint8_t opt) {
4     // TMK Fn正常处理流程
5 }
6 
7 FN_HANDLER(custom_fn_handler);

  添加额外的ADC测量通道

 1 #include "../keyboard/adc_convert.h"
 2 
 3 static struct adc_channel_config custom_adc_channel = {
 4     .adc_start = &adc_on_start, // 测量开始事件
 5     .adc_finish = &adc_result_handler, // 测量结果handler
 6     .period = 2000, // 测量周期,ms
 7     .config = &channel_config, // 通道配置
 8 };
 9 
10 ADC_CONVERT_CHANNEL(custom_adc_channel);

  在配置存储中注册一个条目

#include "../keyboard/data_storage.h"

// 注册一个名为item的,长度为4的存储区
CONFIG_SECTION(item, 4);

void write_item() {
    // 数据操作
    memcpy(item.data, src, item.len);
    // 写入存储区
    storage_write(1 << STORAGE_CONFIG);
}

  注册一个自定义的HID配置项目

#include "../protocol/hid_configuration.h"
#include "../keyboard/data_storage.h"

CONFIG_SECTION(item, 4); // 注册存储区
HID_CONFIG(0x20, item); // 注册配置项目,ID为0x20

  注册一个新的通讯方式

 1 #include "keyboard_host_driver.h"
 2 
 3 static struct host_driver custom_driver = {
 4     .keyboard_leds = &get_keyboard_led, // 获取LED值
 5     .queue_empty = &queue_empty, // 发送队列是否为空
 6     .send_packet = &custom_driver_send, // 发送数据包
 7     .driver_working = &custom_driver_working, // 当前驱动是否工作
 8 };
 9 
10 // 以优先级3注册此驱动。
11 // 数字越小则优先级越高,会优先判断优先级高的驱动是否在工作并使用其通讯。
12 KEYBOARD_HOST_DRIVER(3, custom_driver);

 

posted @ 2022-03-03 16:30  Orangeko  阅读(194)  评论(0编辑  收藏  举报