修正输入口可能导致误触长按动作的问题,基本测试通过,还需要大量测试

This commit is contained in:
nnhy 2016-09-26 14:39:40 +00:00
parent 133f97041a
commit f47abcf1ea
2 changed files with 55 additions and 33 deletions

View File

@ -322,9 +322,9 @@ InputPort::InputPort(Pin pin, bool floating, PuPd pull) : Port()
_Value = 0; _Value = 0;
_PressStart = 0; _PressStart = 0;
_PressStart2 = 0; //_PressStart2 = 0;
PressTime = 0; PressTime = 0;
_PressLast = 0; //_PressLast = 0;
if(pin != P0) if(pin != P0)
{ {
@ -353,9 +353,14 @@ bool InputPort::Read() const
return Port::Read() ^ Invert; return Port::Read() ^ Invert;
} }
/*int pt[16];
int ps[16];
bool vs[16];
int vi = 0;*/
void InputPort::OnPress(bool down) void InputPort::OnPress(bool down)
{ {
//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 _Value=%d\r\n", _PIN_NAME(_Pin), down, Invert, _Value);
/* /*
110110 110110
@ -364,32 +369,32 @@ void InputPort::OnPress(bool down)
2使 2使
3100 3100
*/ */
// 状态机。上一次和这一次状态相同时,认为出错,抛弃
if(down && _Value == Rising) return;
if(!down && _Value != Rising) return;
_Value = down ? Rising : Falling;
UInt64 now = Sys.Ms(); UInt64 now = Sys.Ms();
// 预处理抖动。如果相邻两次间隔小于抖动时间,那么忽略上一次未处理的值(可能失败) /*if(vi < 16)
bool shake = false;
if(ShakeTime && ShakeTime > now - _PressLast)
{ {
_Value = 0; pt[vi] = _Pin;
shake = true; ps[vi] = now;
} vs[vi] = down;
vi++;
}*/
if(down) if(down)
{ {
_PressStart2 = _PressStart; _PressStart = now;
_PressStart = now;
} }
else else
{ {
// 如果这次是弹起倒退按下的时间。为了避免较大的误差限定10秒内 PressTime = now - _PressStart;
if(shake && _PressStart2 > 0 && _PressStart2 + 10000 >= _PressStart)
{
_PressStart = _PressStart2;
_PressStart2 = 0;
}
if (_PressStart > 0) PressTime = now - _PressStart; // 处理抖动
if(PressTime < ShakeTime) return;
} }
_PressLast = now;
if(down) if(down)
{ {
@ -408,21 +413,37 @@ void InputPort::OnPress(bool down)
{ {
// 允许两个值并存 // 允许两个值并存
//_Value |= down ? Rising : Falling; //_Value |= down ? Rising : Falling;
_Value = down ? Rising : Falling;
Sys.SetTask(_taskInput, true, ShakeTime); Sys.SetTask(_taskInput, true, ShakeTime);
} }
} }
//int s = 0;
void InputPort::InputTask(void* param) void InputPort::InputTask(void* param)
{ {
auto port = (InputPort*)param; auto port = (InputPort*)param;
byte v = port->_Value; byte v = port->_Value;
if(!v) return; if(!v) return;
if(port->Handler) if(port->Handler)
{ {
port->_Value = 0; /*if(v & Falling)
v &= port->Mode; {
debug_printf("\r\n");
for(int i=0; i<vi; i++)
{
debug_printf("port %02X %d %d", pt[i], ps[i], vs[i]);
if(vs[i])
s = ps[i];
else
debug_printf(" %d ms", ps[i] - s);
debug_printf("\r\n");
}
vi = 0;
}*/
//port->_Value = 0;
v &= port->Mode;
if(v & Rising) port->Handler(port, true, port->Param); if(v & Rising) port->Handler(port, true, port->Param);
if(v & Falling) port->Handler(port, false, port->Param); if(v & Falling) port->Handler(port, false, port->Param);
} }
@ -482,7 +503,7 @@ void InputPort::OnClose()
ClosePin(); ClosePin();
RemoveFromCenter(this); //RemoveFromCenter(this);
} }
// 注册回调 及中断使能 // 注册回调 及中断使能
@ -495,9 +516,10 @@ bool InputPort::Register(IOReadHandler handler, void* param)
if(!OnRegister()) return false; if(!OnRegister()) return false;
if(handler)AddToCenter(this); //if(handler) AddToCenter(this);
if (!_taskInput && !HardEvent)_taskInput = Sys.AddTask(InputTask, this, -1, -1, "输入中断"); if (!_taskInput && !HardEvent) _taskInput = Sys.AddTask(InputTask, this, -1, -1, "输入中断");
//_taskInput = Sys.AddTask(InputTask, this, 3000, 3000, "输入中断");
return true; return true;
} }
@ -521,9 +543,9 @@ void InputPort::CenterTask(void* param)
if (stat) if (stat)
{ {
port->_PressStart = 0; port->_PressStart = 0;
port->_PressStart2 = 0; //port->_PressStart2 = 0;
port->PressTime = 0; port->PressTime = 0;
port->_PressLast = now; //port->_PressLast = now;
port->_Value = 1; port->_Value = 1;
// debug_printf("\r\nclear %d",i); // debug_printf("\r\nclear %d",i);
} }

View File

@ -187,9 +187,9 @@ protected:
private: private:
byte _Value = 0; byte _Value = 0;
uint _taskInput = 0; // 输入任务 uint _taskInput = 0; // 输入任务
UInt64 _PressStart = 0; // 开始按下时间 UInt64 _PressStart = 0; // 开始按下时间
UInt64 _PressStart2 = 0; // 开始按下时间 //UInt64 _PressStart2 = 0; // 开始按下时间
UInt64 _PressLast = 0; // 最后一次按下时间 //UInt64 _PressLast = 0; // 最后一次按下时间
static void InputTask(void* param); static void InputTask(void* param);
IOReadHandler Handler = nullptr; IOReadHandler Handler = nullptr;
@ -200,7 +200,7 @@ private:
void ClosePin(); void ClosePin();
bool OnRegister(); bool OnRegister();
public: private:
static InputPort * InterruptPorts[16]; static InputPort * InterruptPorts[16];
static uint CenterTaskId; static uint CenterTaskId;
static void CenterTask(void* param); static void CenterTask(void* param);