触摸开关自动识别输入口的倒置,两位三位触摸芯片都是低电平有效,只有四位触摸芯片高电平有效。
增加触摸开关的长按事件InitButtonPress,支持长按3秒重启,5秒重置
This commit is contained in:
parent
4c63c376ba
commit
12a0856b39
|
@ -10,7 +10,7 @@
|
|||
#endif
|
||||
|
||||
InputPort* Button_GrayLevel::ACZero = NULL;
|
||||
byte Button_GrayLevel::OnGrayLevel = 0xff; // 开灯时 led 灰度
|
||||
byte Button_GrayLevel::OnGrayLevel = 0xFF; // 开灯时 led 灰度
|
||||
byte Button_GrayLevel::OffGrayLevel = 0x00; // 关灯时 led 灰度
|
||||
int Button_GrayLevel::ACZeroAdjTime=2300;
|
||||
|
||||
|
@ -22,40 +22,45 @@ Button_GrayLevel::Button_GrayLevel() : ByteDataPort()
|
|||
Index = 0;
|
||||
_Value = false;
|
||||
|
||||
_GrayLevelDrive = NULL;
|
||||
_PulseIndex = 0xff;
|
||||
_Pwm = NULL;
|
||||
_Channel = 0;
|
||||
|
||||
_Handler = NULL;
|
||||
_Param = NULL;
|
||||
|
||||
_tid = 0;
|
||||
Next = 0xFF;
|
||||
|
||||
OnPress = NULL;
|
||||
}
|
||||
|
||||
void Button_GrayLevel::Set(Pin key, Pin relay, bool relayInvert)
|
||||
{
|
||||
assert_param(key != P0);
|
||||
|
||||
// 中断过滤模式
|
||||
Key.Mode = InputPort::Rising;
|
||||
Key.ShakeTime = 10;
|
||||
Key.Set(key);
|
||||
Key.Register(OnPress, this);
|
||||
|
||||
// 中断过滤模式
|
||||
if(!OnPress)
|
||||
Key.Mode = InputPort::Rising;
|
||||
|
||||
// 自动识别倒置
|
||||
if(Key.Read())
|
||||
Key.Invert = true;
|
||||
|
||||
Key.ShakeTime = 10;
|
||||
Key.Register(OnKeyPress, this);
|
||||
Key.Open();
|
||||
|
||||
if(relay != P0)
|
||||
{
|
||||
Relay.Invert = relayInvert;
|
||||
Relay.Set(relay).Open();
|
||||
}
|
||||
if(relay != P0) Relay.Init(relay, relayInvert).Open();
|
||||
}
|
||||
|
||||
void Button_GrayLevel::Set(PWM* drive, byte pulseIndex)
|
||||
{
|
||||
if(drive && pulseIndex < 4)
|
||||
{
|
||||
_GrayLevelDrive = drive;
|
||||
_PulseIndex = pulseIndex;
|
||||
_Pwm = drive;
|
||||
_Channel = pulseIndex;
|
||||
// 刷新输出
|
||||
RenewGrayLevel();
|
||||
}
|
||||
|
@ -63,20 +68,20 @@ void Button_GrayLevel::Set(PWM* drive, byte pulseIndex)
|
|||
|
||||
void Button_GrayLevel::RenewGrayLevel()
|
||||
{
|
||||
if(_GrayLevelDrive)
|
||||
if(_Pwm)
|
||||
{
|
||||
_GrayLevelDrive->Pulse[_PulseIndex] = _Value? (0xff-OnGrayLevel) : (0xff-OffGrayLevel);
|
||||
_GrayLevelDrive->Config();
|
||||
_Pwm->Pulse[_Channel] = _Value? (0xFF - OnGrayLevel) : (0xFF - OffGrayLevel);
|
||||
_Pwm->Config();
|
||||
}
|
||||
}
|
||||
|
||||
void Button_GrayLevel::OnPress(InputPort* port, bool down, void* param)
|
||||
void Button_GrayLevel::OnKeyPress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
Button_GrayLevel* btn = (Button_GrayLevel*)param;
|
||||
if(btn) btn->OnPress(port->_Pin, down);
|
||||
if(btn) btn->OnKeyPress(port, down);
|
||||
}
|
||||
|
||||
void Button_GrayLevel::OnPress(Pin pin, bool down)
|
||||
void Button_GrayLevel::OnKeyPress(InputPort* port, bool down)
|
||||
{
|
||||
// 每次按下弹起,都取反状态
|
||||
if(down)
|
||||
|
@ -84,6 +89,10 @@ void Button_GrayLevel::OnPress(Pin pin, bool down)
|
|||
SetValue(!_Value);
|
||||
if(_Handler) _Handler(this, _Param);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(OnPress) OnPress(port, down, this);
|
||||
}
|
||||
}
|
||||
|
||||
void Button_GrayLevel::Register(EventHandler handler, void* param)
|
||||
|
@ -170,7 +179,7 @@ void Button_GrayLevel::Init(byte tim, byte count, Button_GrayLevel* btns, EventH
|
|||
static PWM LedPWM(tim);
|
||||
// 设置分频 尽量不要改 Prescaler * Period 就是 PWM 周期
|
||||
LedPWM.Prescaler = 0x04; // 随便改 只要肉眼看不到都没问题
|
||||
LedPWM.Period = 0xff; // 对应灰度调节范围
|
||||
LedPWM.Period = 0xFF; // 对应灰度调节范围
|
||||
LedPWM.Start();
|
||||
|
||||
// 配置 LED 引脚
|
||||
|
|
|
@ -21,20 +21,6 @@ struct ButtonPin
|
|||
// 干脆把_Value挪到最后解决问题
|
||||
class Button_GrayLevel : public ByteDataPort
|
||||
{
|
||||
private:
|
||||
static void OnPress(InputPort* port, bool down, void* param);
|
||||
void OnPress(Pin pin, bool down);
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
// 指示灯灰度驱动器 PWM;
|
||||
PWM* _GrayLevelDrive;
|
||||
|
||||
byte _PulseIndex;
|
||||
private:
|
||||
bool _Value; // 状态
|
||||
ushort Reserved; // 补足对齐问题
|
||||
|
||||
public:
|
||||
int Index; // 索引号,方便在众多按钮中标识按钮
|
||||
#if DEBUG
|
||||
|
@ -43,8 +29,9 @@ public:
|
|||
|
||||
InputPort Key; // 输入按键
|
||||
OutputPort Relay; // 继电器
|
||||
static byte OnGrayLevel; // 开灯时 led 灰度
|
||||
static byte OffGrayLevel; // 关灯时 led 灰度
|
||||
|
||||
// 长按事件。
|
||||
InputPort::IOReadHandler OnPress;
|
||||
|
||||
public:
|
||||
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
|
||||
|
@ -61,9 +48,28 @@ public:
|
|||
virtual int OnWrite(byte data);
|
||||
virtual byte OnRead();
|
||||
|
||||
private:
|
||||
// 指示灯灰度驱动器 PWM;
|
||||
PWM* _Pwm;
|
||||
byte _Channel;
|
||||
|
||||
bool _Value; // 状态
|
||||
ushort Reserved; // 补足对齐问题
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
|
||||
static void OnKeyPress(InputPort* port, bool down, void* param);
|
||||
void OnKeyPress(InputPort* port, bool down);
|
||||
|
||||
public:
|
||||
static byte OnGrayLevel; // 开灯时 led 灰度
|
||||
static byte OffGrayLevel; // 关灯时 led 灰度
|
||||
|
||||
static void Init(byte tim, byte count, Button_GrayLevel* btns, EventHandler onpress, const ButtonPin* pins, byte* level, const byte* state);
|
||||
static void InitZero(Pin zero, int us = 2300);
|
||||
static bool UpdateLevel(byte* level, Button_GrayLevel* btns, byte count);
|
||||
|
||||
// 过零检测
|
||||
private:
|
||||
static int ACZeroAdjTime; // 过零检测时间补偿 默认 2300us
|
||||
|
|
2
Port.cpp
2
Port.cpp
|
@ -514,7 +514,7 @@ void InputPort::OnPress(bool down)
|
|||
_PressStart = Sys.Ms();
|
||||
else
|
||||
if (_PressStart > 0) PressTime = Sys.Ms() - _PressStart;
|
||||
debug_printf("OnPress P%c%d down=%d Invert=%d 时间=%d\r\n", _PIN_NAME(_Pin), down, Invert, PressTime);
|
||||
//debug_printf("OnPress P%c%d down=%d Invert=%d 时间=%d\r\n", _PIN_NAME(_Pin), down, Invert, PressTime);
|
||||
|
||||
if(down)
|
||||
{
|
||||
|
|
|
@ -133,6 +133,28 @@ void ClearConfig()
|
|||
Sys.Reset();
|
||||
}
|
||||
|
||||
void CheckUserPress(InputPort* port, bool down, void* param)
|
||||
{
|
||||
if(down) return;
|
||||
|
||||
debug_printf("长按 %d 毫秒 \r\n", port->PressTime);
|
||||
|
||||
// 按下5秒,清空设置并重启
|
||||
if(port->PressTime >= 5000)
|
||||
ClearConfig();
|
||||
// 按下3秒,重启
|
||||
else if(port->PressTime >= 3000)
|
||||
Sys.Reset();
|
||||
}
|
||||
|
||||
void InitButtonPress(Button_GrayLevel* btns, byte count)
|
||||
{
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
btns[i].OnPress = CheckUserPress;
|
||||
}
|
||||
}
|
||||
|
||||
/*void NoUsed()
|
||||
{
|
||||
Setup(1234, "");
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#include "TinyNet\TinyClient.h"
|
||||
|
||||
#include "App\Button_GrayLevel.h"
|
||||
|
||||
void Setup(ushort code, const char* name, COM_Def message = COM1, int baudRate = 1024000);
|
||||
|
||||
void* InitConfig(void* data, uint size);
|
||||
|
@ -16,6 +18,9 @@ ITransport* CreateShunCom(COM_Def index, int baudRate, Pin rst, Pin power, Pin s
|
|||
|
||||
TinyClient* CreateTinyClient(ITransport* port);
|
||||
|
||||
void CheckUserPress(InputPort* port, bool down, void* param = NULL);
|
||||
void InitButtonPress(Button_GrayLevel* btns, byte count);
|
||||
|
||||
//void NoUsed();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue