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