修改长按逻辑为延时关闭,且不提供复位和清零配置

This commit is contained in:
WangQiang 2016-04-22 08:57:27 +00:00
parent 5de32c89e3
commit 6fc7930c40
2 changed files with 152 additions and 67 deletions

View File

@ -32,8 +32,6 @@ Button_GrayLevel::Button_GrayLevel() : ByteDataPort()
_tid = 0;
Next = 0xFF;
OnPress = nullptr;
}
void Button_GrayLevel::Set(Pin key, Pin relay, bool relayInvert)
@ -41,8 +39,7 @@ void Button_GrayLevel::Set(Pin key, Pin relay, bool relayInvert)
Key.Set(key);
// 中断过滤模式
if(!OnPress)
Key.Mode = InputPort::Rising;
Key.Mode = InputPort::Both;
Key.ShakeTime = 40;
Key.Register(OnKeyPress, this);
@ -71,6 +68,24 @@ void Button_GrayLevel::RenewGrayLevel()
}
}
void Button_GrayLevel::GrayLevelUp()
{
if (_Pwm)
{
_Pwm->Pulse[_Channel] = (0xFF - OnGrayLevel);
_Pwm->Config();
}
}
void Button_GrayLevel::GrayLevelDown()
{
if (_Pwm)
{
_Pwm->Pulse[_Channel] = (0xFF - OnGrayLevel);
_Pwm->Config();
}
}
void Button_GrayLevel::OnKeyPress(InputPort* port, bool down, void* param)
{
Button_GrayLevel* btn = (Button_GrayLevel*)param;
@ -80,15 +95,66 @@ void Button_GrayLevel::OnKeyPress(InputPort* port, bool down, void* param)
void Button_GrayLevel::OnKeyPress(InputPort* port, bool down)
{
// 每次按下弹起,都取反状态
if(down)
/*if(down)
{
SetValue(!_Value);
if(_Handler) _Handler(this, _Param);
}
else*/
if (!down) // 要处理长按而不动作 所以必须是弹起响应
{
switch (Stat)
{
case normal:
// 常规模式,长按进入设置模式,并且不动作。否则执行动作
if (_Value == true)
{
if (port->PressTime > 1500)
{
if (port->PressTime > 3000)
DelayClose2(10 * 60 * 1000);
else
DelayClose2(2 * 60 * 1000);
return;
}
}
SetValue(!_Value);
if (_Handler) _Handler(this, _Param);
break;
case set:
Stat = normal;
Next = 0xff;
SetValue(!_Value);
if (_Handler) _Handler(this, _Param);
break;
}
}
}
void Close2Task(void * param)
{
auto bt = (Button_GrayLevel *)param;
bt->GrayLevelUp();
if (bt->delaytime > 1000)
{
bt->delaytime = bt->delaytime - 1000;
Sys.SetTask(bt->_task2, true, 1000);
}
else
{
if(OnPress) OnPress(port, down, this);
bt->delaytime = 0;
Sys.SetTask(bt->_task2, false);
bt->SetValue(false);
}
Sys.Sleep(100);
bt->GrayLevelDown();
}
void Button_GrayLevel::DelayClose2(int ms)
{
if (_task2)Sys.AddTask(Close2Task, this, -1, 1000, "button close");
Sys.SetTask(_task2, true, 1000);
delaytime = ms;
}
void Button_GrayLevel::Register(EventHandler handler, void* param)

View File

@ -15,6 +15,14 @@ struct ButtonPin
byte PwmIndex;
};
enum ButtonStat :byte
{
normal = 0, // 正常
set = 1, // 设置
externSet = 2, // 外部设置
execution = 3, // 执行设置
};
// 面板按钮
// 这里必须使用_packed关键字生成对齐的代码否则_Value只占一个字节导致后面的成员进行内存操作时错乱
//__packed class Button
@ -29,9 +37,12 @@ public:
InputPort Key; // 输入按键
OutputPort Relay; // 继电器
enum ButtonStat Stat = normal; // 状态
// 长按事件。
InputPort::IOReadHandler OnPress;
// 外部设置模式调用
typedef bool(*IOHandler)(Button_GrayLevel* bt, bool down, void * param);
IOHandler ExterSet = nullptr;
void * ExterSetParam = nullptr;
public:
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
@ -45,6 +56,9 @@ public:
void RenewGrayLevel();
void Register(EventHandler handler, void* param = nullptr);
virtual int Write(byte* pcmd); // 重载 ByteDataPort 的函数 自定义 Delay Flush Open Close
//virtual int Read(byte* cmd);
virtual int OnWrite(byte data);
virtual byte OnRead();
@ -70,6 +84,11 @@ public:
static void InitZero(Pin zero, int us = 2300);
static bool UpdateLevel(byte* level, Button_GrayLevel* btns, byte count);
void GrayLevelDown();
void GrayLevelUp();
void DelayClose2(int ms); // 自定义延时关闭
int delaytime = 0;
int _task2 = 0;
// 过零检测
private:
static int ACZeroAdjTime; // 过零检测时间补偿 默认 2300us