CodeLibrary/MAShiroFan/lib/encoder/encoder.cpp

97 lines
1.9 KiB
C++
Raw Normal View History

/*
* File name: encoder.cpp
* Author: JRNitre
* Version: MASVersion.0.0.0b
* Date: 2024-05-10
*
* Description:
* .
*/
// 头文件
#include "encoder.h"
// 全局变量
long encoderLastToggled = 0;
ESP32Encoder encoder;
/*
* Function: encoder_init
* Description:
* .
*
* Input: void
* Output: void
* Return: BasicEncoder
*
* Other: nullptr
*/
void encoder_init(){
// 设置工作模式
ESP32Encoder::useInternalWeakPullResistors = puType::up;
// 设置编码器工作引脚
encoder.attachHalfQuad(ENCODER_DT, ENCODER_CLK);
// 设置计数器数值
encoder.setCount(0);
// 清除计数器
encoder.clearCount();
}
/*
* Function: encoder_getCount
* Description:
* .
*
* Input: void
* Output: void
* Return: long
*
* Other: nullptr
*/
long encoder_getCount(){
return encoder.getCount();
}
/*
* Function: encoder_getKeyNum
* Description:
* .
*
* Input: void
* Output: void
* Return: uint8_t
*
* Other:
*
* 1.
* 2.
* 3.
*
*/
uint8_t encoder_getKeyNum(){
uint8_t retKey = 0;
// 判断编码器是否有旋转
if (encoderLastToggled != encoder_getCount()){
if (encoderLastToggled > encoder_getCount()){
retKey = 1;
}
if (encoderLastToggled < encoder_getCount()){
retKey = 2;
}
} else {
retKey = 0;
}
// 同步最新的计数器数值
encoderLastToggled = encoder_getCount();
// 返回最终的键值
return retKey;
}