找一个编码器旋钮按如下连接
器件引脚 | MCU引脚 |
---|---|
A | PA1 |
B | GND |
C | PA2 |
D
和E
其中一个接GND,一个接PA0
我们在支持包里面找到这个例程,拷贝一份
#define USER_KEY_GPIO_PORT (GPIOA)
#define USER_KEY_GPIO_PIN (GPIO_Pin_0)
#define KEY_EC_A_PORT (GPIOA)
#define KEY_EC_A_PIN (GPIO_Pin_1)
#define KEY_EC_B_PORT (GPIOA)
#define KEY_EC_B_PIN (GPIO_Pin_2)
#define read_ec_a_level() (GPIO_ReadInputDataBit(KEY_EC_A_PORT, KEY_EC_A_PIN))
#define read_ec_b_level() (GPIO_ReadInputDataBit(KEY_EC_B_PORT, KEY_EC_B_PIN))
void key_init(void);
uint16_t read_array_button(void);
uint16_t KeyEc_ProcessEvent(uint8_t task_id, uint16_t events);
void key_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = USER_KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USER_KEY_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY_EC_A_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_EC_A_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEY_EC_B_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_EC_B_PORT, &GPIO_InitStructure);
// 注册任务,并启动
KeyEcTaskId = TMOS_ProcessEventRegister(KeyEc_ProcessEvent);
tmos_set_event(KeyEcTaskId, START_REPORT_EVT);
}
uint16_t KeyEc_ProcessEvent(uint8_t task_id, uint16_t events)
{
uint8_t ec_a_state = 0;
uint8_t ec_b_state = 0;
static uint8_t old_ec_a_state = 0;
static uint8_t old_ec_b_state = 0;
if (events & START_REPORT_EVT)
{
ec_a_state = read_ec_a_level() == 0;
ec_b_state = read_ec_b_level() == 0;
if ((old_ec_a_state) && (!ec_a_state))
{
if (ec_b_state)
{
PRINT("EC++\\r\\n");
// hidEmuSendConsumerReport(1, 0);
hidEmuSendConsumerReport(0xE9, 0);
hidEmuSendConsumerReport(0, 0);
}
else
{
PRINT("EC--\\r\\n");
// hidEmuSendConsumerReport(0, 1);
hidEmuSendConsumerReport(0xEA, 0);
hidEmuSendConsumerReport(0, 0);
}
}
old_ec_a_state = ec_a_state;
old_ec_b_state = ec_b_state;
tmos_start_task(KeyEcTaskId, START_REPORT_EVT, 1);
return (events ^ START_REPORT_EVT);
}
return 0;
}
HidEmu_ProcessEvent
安卓、iOS、MacOS都是一样的,就Windows不一样
修改此处报文
然后在此处添加播放&暂停
键的键值0xCD
即可
安卓的报文,需要再添加一个item,如图
然后上报就是3字节
hidEmuSendConsumerReport
函数也改成3字节的,第三个字节是播放/暂停键
更多回帖