简化脉冲端口,内部抖动可以过滤掉无效脉冲,这里仅需要记录两次脉冲之间的间隔
This commit is contained in:
parent
0b15e76a48
commit
d6f9967a6c
|
@ -1,128 +1,67 @@
|
|||
#include "Kernel\Sys.h"
|
||||
#include "Kernel\TTime.h"
|
||||
#include "Device\Port.h"
|
||||
|
||||
#include "PulsePort.h"
|
||||
#include "Platform\Pin.h"
|
||||
//#include "Platform\Pin.h"
|
||||
|
||||
PulsePort::PulsePort() { Int(); }
|
||||
|
||||
PulsePort::PulsePort(Pin pin)
|
||||
PulsePort::PulsePort()
|
||||
{
|
||||
if (pin == P0)return;
|
||||
_Port = new InputPort(pin);
|
||||
needFree = true;
|
||||
|
||||
Int();
|
||||
}
|
||||
void PulsePort::Int()
|
||||
{
|
||||
LastTriTime = 0;
|
||||
TriTime = 0; //触发时间
|
||||
TriCount = 0; //触发次数
|
||||
}
|
||||
bool PulsePort::Set(InputPort * port, uint minIntervals, uint maxIntervals)
|
||||
{
|
||||
if (port == nullptr)return false;
|
||||
|
||||
_Port = port;
|
||||
needFree = false;
|
||||
MinIntervals = minIntervals;
|
||||
MaxIntervals = maxIntervals;
|
||||
Int();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PulsePort::Set(Pin pin, uint minIntervals, uint maxIntervals)
|
||||
{
|
||||
if (pin == P0)return false;
|
||||
|
||||
_Port = new InputPort(pin);
|
||||
needFree = true;
|
||||
MinIntervals = minIntervals;
|
||||
MaxIntervals = maxIntervals;
|
||||
Int();
|
||||
return false;
|
||||
Port = nullptr;
|
||||
Min = 0;
|
||||
Max = 0;
|
||||
Last = 0;
|
||||
Time = 0; //触发时间
|
||||
Times = 0; //触发次数
|
||||
}
|
||||
|
||||
void PulsePort::Open()
|
||||
{
|
||||
if (_Port == nullptr)return;
|
||||
if (Handler == nullptr)return;
|
||||
if (Opened) return;
|
||||
|
||||
if (Opened)return;
|
||||
_Port->HardEvent = false;
|
||||
if (!_Port->Register([](InputPort* port, bool down, void* param) {((PulsePort*)param)->OnHandler(port, down); }, this))
|
||||
if (!Port) return;
|
||||
|
||||
Port->HardEvent = true;
|
||||
if (!Port->Register([](InputPort* port, bool down, void* param) {((PulsePort*)param)->OnPress(down); }, this))
|
||||
{
|
||||
debug_printf("PulsePort 注册失败/r/n");
|
||||
// 注册失败就返回 不要再往下了 没意义
|
||||
return;
|
||||
}
|
||||
_Port->Open();
|
||||
|
||||
/*_task = Sys.AddTask(
|
||||
[](void* param)
|
||||
{
|
||||
auto port = (PulsePort*)param;
|
||||
Sys.SetTask(port->_task, false);
|
||||
port->Handler(port, port->Param);
|
||||
},
|
||||
this, 100, -1, "PulsePort事件");*/
|
||||
Port->Open();
|
||||
|
||||
Opened = true;
|
||||
}
|
||||
|
||||
void PulsePort::Close()
|
||||
{
|
||||
_Port->Close();
|
||||
if (!Opened) return;
|
||||
|
||||
Port->Close();
|
||||
|
||||
Opened = false;
|
||||
if (needFree)
|
||||
delete _Port;
|
||||
}
|
||||
|
||||
void PulsePort::InputTask()
|
||||
void PulsePort::OnPress(bool down)
|
||||
{
|
||||
if (Handler)
|
||||
{
|
||||
Handler(this, this->Param);
|
||||
}
|
||||
}
|
||||
void PulsePort::Register(PulsePortHandler handler, void* param)
|
||||
{
|
||||
if (handler)
|
||||
{
|
||||
Handler = handler;
|
||||
Param = param;
|
||||
}
|
||||
if (!_task)_task = Sys.AddTask(&PulsePort::InputTask, this, -1, -1, "PulsePort中断");
|
||||
}
|
||||
// 只有弹起来才计算
|
||||
if (down) return;
|
||||
|
||||
void PulsePort::OnHandler(InputPort* port, bool down)
|
||||
{
|
||||
if (!down) return;
|
||||
auto preTime = port->PressTime;
|
||||
UInt64 now = Sys.Ms();
|
||||
auto time = (uint)(now - Last);
|
||||
// 无论如何都更新最后一次时间,避免连续超长
|
||||
Last = now;
|
||||
|
||||
// 默认最大最小为0时候,不做判断
|
||||
auto go1 = MinIntervals < preTime&&preTime < MaxIntervals&&MinIntervals != 0 && MaxIntervals != 0;
|
||||
auto go2 = preTime < MaxIntervals&&MinIntervals == 0 && MaxIntervals != 0;
|
||||
auto go3 = MinIntervals < preTime&&MinIntervals == 0 && MaxIntervals == 0;
|
||||
auto go4 = MinIntervals == 0 && MaxIntervals == 0;
|
||||
// 两次脉冲的间隔必须在一个范围内才算作有效
|
||||
if(Min > 0 && time < Min || Max > 0 && time > Max) return;
|
||||
|
||||
if (!(go1 || go2 || go3 || go4))return;
|
||||
// 取UTC时间的MS值
|
||||
UInt64 now = Sys.Ms();
|
||||
//上次触发时间
|
||||
LastTriTime = TriTime;
|
||||
//这次触发时间
|
||||
TriTime = now;
|
||||
//debug_printf("PulsePort preTime %ld ", preTime);
|
||||
auto time = ushort(TriTime - LastTriTime);
|
||||
Time = time;
|
||||
if (time > 100)
|
||||
{
|
||||
debug_printf(" time %d, Port %ld \r\n", time, _Port->_Pin);
|
||||
debug_printf(" time %d, Port %02X \r\n", time, Port->_Pin);
|
||||
}
|
||||
//debug_printf("VisTime %ld \r\n", time);
|
||||
|
||||
if (_task)Sys.SetTask(_task, true, -1);
|
||||
/*if (Handler)
|
||||
(Handler(this, this->Param));*/
|
||||
Press(*this);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -2,49 +2,36 @@
|
|||
#ifndef __Pulse_Port_H__
|
||||
#define __Pulse_Port_H__
|
||||
|
||||
#include "..\Kernel\Sys.h"
|
||||
#include "..\Kernel\TTime.h"
|
||||
#include "Port.h"
|
||||
#include "Kernel\Sys.h"
|
||||
//#include "..\Kernel\TTime.h"
|
||||
//#include "Port.h"
|
||||
|
||||
class InputPort;
|
||||
|
||||
// 输入类型为脉冲的IO驱动
|
||||
class PulsePort
|
||||
{
|
||||
public:
|
||||
// PulsePort 事件函数形式
|
||||
typedef void(*PulsePortHandler)(PulsePort* port, void* param);
|
||||
private:
|
||||
InputPort* Port; // 输入引脚
|
||||
|
||||
void Int();
|
||||
uint _task = 0; // 任务
|
||||
InputPort * _Port = nullptr; // 输入引脚
|
||||
bool needFree = false; // 是否需要释放对象
|
||||
PulsePortHandler Handler = nullptr;
|
||||
void* Param = nullptr;
|
||||
void InputTask();
|
||||
public:
|
||||
uint Min; // 最小时间间隔 单位 ms
|
||||
uint Max; // 最大时间间隔 单位 ms
|
||||
|
||||
UInt64 Last; // 上一个信号触发时间
|
||||
uint Time; // 触发时间
|
||||
uint Times; // 触发次数
|
||||
bool Opened; // 是否打开
|
||||
|
||||
Delegate<PulsePort&> Press;
|
||||
|
||||
PulsePort();
|
||||
PulsePort(Pin pin); // 默认浮空输入
|
||||
bool Set(InputPort * port, uint minIntervals = 0, uint maxIntervals = 0);
|
||||
bool Set(Pin pin, uint minIntervals = 0, uint maxIntervals = 0);
|
||||
|
||||
|
||||
uint MinIntervals = 0; // 最小时间间隔 单位 ms
|
||||
uint MaxIntervals = 0; // 最大时间间隔 单位 ms
|
||||
|
||||
|
||||
UInt64 LastTriTime; // 上一个信号触发时间
|
||||
UInt64 TriTime; //触发时间
|
||||
UInt64 TriCount; //触发次数
|
||||
|
||||
void Open();
|
||||
void Close();
|
||||
// 内部中断函数
|
||||
void OnHandler(InputPort* port, bool down);
|
||||
// 注册回调函数
|
||||
void Register(PulsePortHandler handler = NULL, void* param = NULL);
|
||||
|
||||
bool Opened = false; // 是否是打开的
|
||||
private:
|
||||
// 内部中断函数
|
||||
void OnPress(bool down);
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -8,7 +8,7 @@ void TestPulsePort()
|
|||
InitPort();
|
||||
}
|
||||
|
||||
void PulseHandler(PulsePort* port, void* param)
|
||||
static void PulseHandler(PulsePort& port)
|
||||
{
|
||||
#if defined(DEBUG)
|
||||
// down true 无遮挡 down false 有遮挡
|
||||
|
@ -23,8 +23,11 @@ void InitPort()
|
|||
{
|
||||
static InputPort io(PA6);
|
||||
static PulsePort Port;
|
||||
Port.Set(&io,35);
|
||||
Port.Register(PulseHandler,nullptr);
|
||||
//Port.Set(&io,35);
|
||||
//Port.Register(PulseHandler, nullptr);
|
||||
Port.Port = &io;
|
||||
Port.Min = 35;
|
||||
Port.Press = PulseHandler;
|
||||
Port.Open();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue