增强委托Delegate,把一切委托调用封装为函数指针和目标对象指针,极大方便使用,虽然执行的时候可能错误传递参数
This commit is contained in:
parent
4e064b34e4
commit
d5fe4ad544
|
@ -10,8 +10,8 @@ Button::Button()
|
|||
Index = 0;
|
||||
_Value = false;
|
||||
|
||||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
//_Handler = nullptr;
|
||||
//_Param = nullptr;
|
||||
}
|
||||
|
||||
Button::~Button()
|
||||
|
@ -54,11 +54,12 @@ void Button::OnPress(Pin pin, bool down)
|
|||
{
|
||||
SetValue(!_Value);
|
||||
|
||||
if(_Handler) _Handler(this, _Param);
|
||||
//if(_Handler) _Handler(this, _Param);
|
||||
Press(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Button::Register(EventHandler handler, void* param)
|
||||
/*void Button::Register(EventHandler handler, void* param)
|
||||
{
|
||||
if(handler)
|
||||
{
|
||||
|
@ -70,7 +71,7 @@ void Button::Register(EventHandler handler, void* param)
|
|||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
bool Button::GetValue() { return _Value; }
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@ private:
|
|||
static void OnPress(InputPort* port, bool down, void* param);
|
||||
void OnPress(Pin pin, bool down);
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
//EventHandler _Handler;
|
||||
//void* _Param;
|
||||
public:
|
||||
cstring Name; // 按钮名称
|
||||
int Index; // 索引号,方便在众多按钮中标识按钮
|
||||
|
@ -25,6 +25,8 @@ public:
|
|||
OutputPort Led; // 指示灯
|
||||
OutputPort Relay; // 继电器
|
||||
|
||||
Delegate Press; // 按下事件
|
||||
|
||||
public:
|
||||
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
|
||||
Button();
|
||||
|
@ -35,7 +37,7 @@ public:
|
|||
bool GetValue();
|
||||
void SetValue(bool value);
|
||||
|
||||
void Register(EventHandler handler, void* param = nullptr);
|
||||
//void Register(EventHandler handler, void* param = nullptr);
|
||||
|
||||
virtual int OnWrite(byte data);
|
||||
virtual byte OnRead();
|
||||
|
|
|
@ -27,8 +27,8 @@ Button_GrayLevel::Button_GrayLevel() : ByteDataPort()
|
|||
_Pwm = nullptr;
|
||||
_Channel = 0;
|
||||
|
||||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
//_Handler = nullptr;
|
||||
//_Param = nullptr;
|
||||
|
||||
_tid = 0;
|
||||
Next = 0xFF;
|
||||
|
@ -119,13 +119,15 @@ void Button_GrayLevel::OnKeyPress(InputPort* port, bool down)
|
|||
}
|
||||
}
|
||||
SetValue(!_Value);
|
||||
if (_Handler) _Handler(this, _Param);
|
||||
//if (_Handler) _Handler(this, _Param);
|
||||
Press(this);
|
||||
break;
|
||||
case set:
|
||||
Stat = normal;
|
||||
Next = 0xff;
|
||||
SetValue(!_Value);
|
||||
if (_Handler) _Handler(this, _Param);
|
||||
//if (_Handler) _Handler(this, _Param);
|
||||
Press(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +159,7 @@ void Button_GrayLevel::DelayClose2(int ms)
|
|||
delaytime = ms;
|
||||
}
|
||||
|
||||
void Button_GrayLevel::Register(EventHandler handler, void* param)
|
||||
/*void Button_GrayLevel::Register(EventHandler handler, void* param)
|
||||
{
|
||||
if (handler)
|
||||
{
|
||||
|
@ -169,7 +171,7 @@ void Button_GrayLevel::Register(EventHandler handler, void* param)
|
|||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
int Button_GrayLevel::OnWrite(byte data)
|
||||
{
|
||||
|
@ -229,7 +231,7 @@ bool Button_GrayLevel::SetACZeroPin(Pin aczero)
|
|||
return false;
|
||||
}
|
||||
|
||||
void Button_GrayLevel::Init(TIMER tim, byte count, Button_GrayLevel* btns, EventHandler onpress
|
||||
void Button_GrayLevel::Init(TIMER tim, byte count, Button_GrayLevel* btns, Action onpress
|
||||
, const ButtonPin* pins, byte* level, const byte* state)
|
||||
{
|
||||
debug_printf("\r\n初始化开关按钮 \r\n");
|
||||
|
@ -278,7 +280,8 @@ void Button_GrayLevel::Init(TIMER tim, byte count, Button_GrayLevel* btns, Event
|
|||
#if DEBUG
|
||||
btns[i].Name = names[i];
|
||||
#endif
|
||||
btns[i].Register(onpress);
|
||||
//btns[i].Register(onpress);
|
||||
btns[i].Press = onpress;
|
||||
|
||||
// 灰度 LED 绑定到 Button
|
||||
btns[i].Set(&LedPWM, pins[i].PwmIndex);
|
||||
|
|
|
@ -45,6 +45,8 @@ public:
|
|||
void * ExterSetParam = nullptr;
|
||||
|
||||
public:
|
||||
Delegate Press;
|
||||
|
||||
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
|
||||
Button_GrayLevel();
|
||||
|
||||
|
@ -54,7 +56,7 @@ public:
|
|||
bool GetValue();
|
||||
void SetValue(bool value);
|
||||
void RenewGrayLevel();
|
||||
void Register(EventHandler handler, void* param = nullptr);
|
||||
//void Register(EventHandler handler, void* param = nullptr);
|
||||
|
||||
virtual int Write(byte* pcmd); // 重载 ByteDataPort 的函数 自定义 Delay Flush Open Close
|
||||
//virtual int Read(byte* cmd);
|
||||
|
@ -70,8 +72,8 @@ private:
|
|||
bool _Value; // 状态
|
||||
ushort Reserved; // 补足对齐问题
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
//EventHandler _Handler;
|
||||
//void* _Param;
|
||||
|
||||
static void OnKeyPress(InputPort* port, bool down, void* param);
|
||||
void OnKeyPress(InputPort* port, bool down);
|
||||
|
@ -80,7 +82,7 @@ public:
|
|||
static byte OnGrayLevel; // 开灯时 led 灰度
|
||||
static byte OffGrayLevel; // 关灯时 led 灰度
|
||||
|
||||
static void Init(TIMER tim, byte count, Button_GrayLevel* btns, EventHandler onpress, const ButtonPin* pins, byte* level, const byte* state);
|
||||
static void Init(TIMER tim, byte count, Button_GrayLevel* btns, Action 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);
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ void Button_magnetic::Init()
|
|||
|
||||
Name = nullptr;
|
||||
_Value = false;
|
||||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
//_Handler = nullptr;
|
||||
//_Param = nullptr;
|
||||
}
|
||||
|
||||
Button_magnetic::Button_magnetic(Pin key, Pin led, Pin relay_pin1, Pin relay_pin2)
|
||||
|
@ -72,11 +72,12 @@ void Button_magnetic::OnPress(Pin pin, bool down)
|
|||
{
|
||||
SetValue(!_Value);
|
||||
|
||||
if(_Handler) _Handler(this, _Param);
|
||||
//if(_Handler) _Handler(this, _Param);
|
||||
Press();
|
||||
}
|
||||
}
|
||||
|
||||
void Button_magnetic::Register(EventHandler handler, void* param)
|
||||
/*void Button_magnetic::Register(EventHandler handler, void* param)
|
||||
{
|
||||
if(handler)
|
||||
{
|
||||
|
@ -88,7 +89,7 @@ void Button_magnetic::Register(EventHandler handler, void* param)
|
|||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
bool Button_magnetic::GetValue() { return _Value; }
|
||||
|
||||
|
|
|
@ -20,10 +20,12 @@ private:
|
|||
static void OnPress(InputPort* port, bool down, void* param);
|
||||
void OnPress(Pin pin, bool down);
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
//EventHandler _Handler;
|
||||
//void* _Param;
|
||||
public:
|
||||
cstring Name;
|
||||
|
||||
Delegate Press;
|
||||
|
||||
InputPort* Key; // 输入按键
|
||||
OutputPort* Led; // 指示灯
|
||||
|
@ -40,7 +42,7 @@ public:
|
|||
bool GetValue();
|
||||
void SetValue(bool value);
|
||||
|
||||
void Register(EventHandler handler, void* param = nullptr);
|
||||
//void Register(EventHandler handler, void* param = nullptr);
|
||||
|
||||
private:
|
||||
bool _Value; // 状态
|
||||
|
|
|
@ -6,13 +6,13 @@ void Sensor::Init()
|
|||
Led = nullptr;
|
||||
Buzzer = nullptr;
|
||||
Pir=nullptr; //门磁
|
||||
Pir=nullptr; //人体感应
|
||||
//Pir=nullptr; //人体感应
|
||||
|
||||
Name = nullptr;
|
||||
Index = 0;
|
||||
_Value = false;
|
||||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
//_Handler = nullptr;
|
||||
//_Param = nullptr;
|
||||
}
|
||||
|
||||
Sensor::Sensor(Pin key, Pin led, Pin buzzer)
|
||||
|
@ -64,11 +64,12 @@ void Sensor::OnPress(Pin pin, bool down)
|
|||
{
|
||||
SetValue(!_Value);
|
||||
|
||||
if(_Handler) _Handler(this, _Param);
|
||||
//if(_Handler) _Handler(this, _Param);
|
||||
Press();
|
||||
}
|
||||
}
|
||||
|
||||
void Sensor::Register(EventHandler handler, void* param)
|
||||
/*void Sensor::Register(EventHandler handler, void* param)
|
||||
{
|
||||
if(handler)
|
||||
{
|
||||
|
@ -80,7 +81,7 @@ void Sensor::Register(EventHandler handler, void* param)
|
|||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
bool Sensor::GetValue() { return _Value; }
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
|
||||
//I2C* Ifrared //红外转发
|
||||
|
||||
|
||||
Delegate Press;
|
||||
|
||||
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
|
||||
Sensor() { Init(); }
|
||||
|
@ -32,7 +32,7 @@ public:
|
|||
bool GetValue();
|
||||
void SetValue(bool value);
|
||||
|
||||
void Register(EventHandler handler, void* param = nullptr);
|
||||
//void Register(EventHandler handler, void* param = nullptr);
|
||||
|
||||
|
||||
private:
|
||||
|
@ -41,8 +41,8 @@ private:
|
|||
static void OnPress(InputPort* port, bool down, void* param);
|
||||
void OnPress(Pin pin, bool down);
|
||||
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
//EventHandler _Handler;
|
||||
//void* _Param;
|
||||
|
||||
private:
|
||||
bool _Value; // 状态
|
||||
|
|
|
@ -48,7 +48,8 @@ void Music::Sound()
|
|||
if(_timer!= nullptr && _phonatePin != nullptr && _tuneSet != nullptr && _tuneNum != 0)
|
||||
{
|
||||
_timer->SetFrequency(100000);
|
||||
_timer->Register(TimerHander, this);
|
||||
//_timer->Register(TimerHander, this);
|
||||
_timer->OnTick = Delegate(&Music::TimerHander, this);
|
||||
_timer->Open();
|
||||
Sounding = true;
|
||||
}
|
||||
|
@ -69,7 +70,7 @@ void Music::Unsound()
|
|||
void Music::SetTuneSet(const Tune* tune, int num)
|
||||
{
|
||||
if(Sounding) Unsound();
|
||||
|
||||
|
||||
if(tune != nullptr && num != 0)
|
||||
{
|
||||
_tuneSet = tune;
|
||||
|
@ -82,11 +83,13 @@ bool Music::getStat()
|
|||
return Sounding;
|
||||
}
|
||||
|
||||
void Music::TimerHander(void* sender,void* param)
|
||||
void Music::TimerHander(Timer* timer)
|
||||
{
|
||||
if(param == nullptr)return;
|
||||
/*if(param == nullptr)return;
|
||||
Music * music = (Music * )param;
|
||||
music->phonate();
|
||||
music->phonate();*/
|
||||
|
||||
phonate();
|
||||
}
|
||||
|
||||
void Music::phonate()
|
||||
|
|
|
@ -37,7 +37,7 @@ private:
|
|||
volatile int sound_cnt;
|
||||
volatile int music_freq;
|
||||
volatile int music_beat;
|
||||
void static TimerHander(void* sender, void* param);
|
||||
void TimerHander(Timer* timer);
|
||||
void phonate();
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
#include "Type.h"
|
||||
#include "DateTime.h"
|
||||
#include "SString.h"
|
||||
|
||||
#include "Delegate.h"
|
||||
|
||||
/************************************************ Delegate ************************************************/
|
||||
Delegate::Delegate()
|
||||
{
|
||||
Method = nullptr;
|
||||
Target = nullptr;
|
||||
}
|
||||
|
||||
Delegate::Delegate(void* func)
|
||||
{
|
||||
Method = func;
|
||||
Target = nullptr;
|
||||
}
|
||||
|
||||
Delegate::Delegate(void* func, void* target)
|
||||
{
|
||||
Method = func;
|
||||
Target = target;
|
||||
}
|
||||
|
||||
Delegate::Delegate(Func func) { Method = (void*)func; }
|
||||
Delegate::Delegate(Action func) { Method = (void*)func; }
|
||||
Delegate::Delegate(Action2 func){ Method = (void*)func; }
|
||||
Delegate::Delegate(Action3 func){ Method = (void*)func; }
|
||||
|
||||
/*void Delegate::Add(Func func)
|
||||
{
|
||||
Method = (void*)func;
|
||||
}*/
|
||||
|
||||
void Delegate::operator()()
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
if(Target)
|
||||
((Action)Method)(Target);
|
||||
else
|
||||
((Func)Method)();
|
||||
}
|
||||
|
||||
void Delegate::operator()(void* arg)
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
if(Target)
|
||||
((Action2)Method)(Target, arg);
|
||||
else
|
||||
((Action)Method)(arg);
|
||||
}
|
||||
|
||||
void Delegate::operator()(void* arg, void* arg2)
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
if(Target)
|
||||
((Action3)Method)(Target, arg, arg2);
|
||||
else
|
||||
((Action2)Method)(arg, arg2);
|
||||
}
|
228
Core/Delegate.h
228
Core/Delegate.h
|
@ -1,6 +1,80 @@
|
|||
#ifndef _Delegate_H_
|
||||
#define _Delegate_H_
|
||||
|
||||
// 没有参数和返回值的委托
|
||||
typedef void (*Func)(void);
|
||||
// 一个参数没有返回值的委托,一般param参数用作目标对象,调用者用静态函数包装成员函数
|
||||
typedef void (*Action)(void* param);
|
||||
typedef void(*Action2)(void*, void*);
|
||||
typedef void(*Action3)(void*, void*, void*);
|
||||
// 事件处理委托,一般sender表示事件发出者,param用作目标对象,调用者用静态函数包装成员函数
|
||||
typedef void (*EventHandler)(void* sender, void* param);
|
||||
// 传入数据缓冲区地址和长度,如有反馈,仍使用该缓冲区,返回数据长度
|
||||
typedef uint (*DataHandler)(void* sender, byte* buf, uint size, void* param);
|
||||
|
||||
// 事件处理器
|
||||
class Delegate
|
||||
{
|
||||
public:
|
||||
void* Method; // 函数指针
|
||||
void* Target; // 参数
|
||||
|
||||
Delegate();
|
||||
Delegate(void* func);
|
||||
Delegate(void* func, void* target);
|
||||
Delegate(Func func);
|
||||
Delegate(Action func);
|
||||
Delegate(Action2 func);
|
||||
Delegate(Action3 func);
|
||||
|
||||
template<typename T>
|
||||
Delegate(void(T::*func)(), T* target) { Method = (void*)&func; Target = target; }
|
||||
template<typename T, typename TArg>
|
||||
Delegate(void(T::*func)(TArg), T* target) { Method = (void*)&func; Target = target; }
|
||||
template<typename T, typename TArg, typename TArg2>
|
||||
Delegate(void(T::*func)(TArg, TArg2), T* target) { Method = (void*)&func; Target = target; }
|
||||
|
||||
void operator()();
|
||||
void operator()(void* arg);
|
||||
void operator()(void* arg, void* arg2);
|
||||
template<typename T>
|
||||
void operator()()
|
||||
{
|
||||
if(Method)
|
||||
{
|
||||
auto obj = (T*)Target;
|
||||
typedef void(T::*TAction)();
|
||||
auto act = *(TAction*)Method;
|
||||
|
||||
(obj->*act)();
|
||||
}
|
||||
}
|
||||
template<typename T, typename TArg>
|
||||
void operator()(TArg arg)
|
||||
{
|
||||
if(Method)
|
||||
{
|
||||
auto obj = (T*)Target;
|
||||
typedef void(T::*TAction)(TArg);
|
||||
auto act = *(TAction*)Method;
|
||||
|
||||
(obj->*act)(arg);
|
||||
}
|
||||
}
|
||||
template<typename T, typename TArg, typename TArg2>
|
||||
void operator()(TArg arg, TArg arg2)
|
||||
{
|
||||
if(Method)
|
||||
{
|
||||
auto obj = (T*)Target;
|
||||
typedef void(T::*TAction)(TArg, TArg2);
|
||||
auto act = *(TAction*)Method;
|
||||
|
||||
(obj->*act)(arg, arg2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
成员函数指针的操作
|
||||
class A{
|
||||
|
@ -21,158 +95,4 @@ A* pa=&a;
|
|||
要调用一个成员函数,仅仅有成员函数指针是不够的,还需要一个对象指针,所以要用一个类将两者绑到一起。
|
||||
*/
|
||||
|
||||
// 没有参数和返回值的委托
|
||||
typedef void (*Func)(void);
|
||||
// 一个参数没有返回值的委托,一般param参数用作目标对象,调用者用静态函数包装成员函数
|
||||
typedef void (*Action)(void* param);
|
||||
// 事件处理委托,一般sender表示事件发出者,param用作目标对象,调用者用静态函数包装成员函数
|
||||
typedef void (*EventHandler)(void* sender, void* param);
|
||||
// 数据接收委托,一般sender表示事件发出者,param用作目标对象,调用者用静态函数包装成员函数
|
||||
// 传入数据缓冲区地址和长度,如有反馈,仍使用该缓冲区,返回数据长度
|
||||
typedef uint (*DataHandler)(void* sender, byte* buf, uint size, void* param);
|
||||
|
||||
class Delegate
|
||||
{
|
||||
public:
|
||||
void* Target;
|
||||
void* Method;
|
||||
|
||||
// 构造函数后面的冒号起分割作用,是类给成员变量赋值的方法
|
||||
// 初始化列表,更适用于成员变量的常量const型
|
||||
template<typename T, typename TArg>
|
||||
Delegate(T* target, void(T::*func)(TArg)) : Target(target), Method(&func) {}
|
||||
|
||||
template<typename T, typename TArg>
|
||||
void Invoke(TArg value)
|
||||
{
|
||||
auto obj = (T*)Target;
|
||||
typedef void(T::*TAction)(TArg);
|
||||
auto act = *(TAction*)Method;
|
||||
|
||||
(obj->*act)(value);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename TArg, typename TArg2>
|
||||
class Delegate2
|
||||
{
|
||||
public:
|
||||
Delegate2(T* target, void(T::*func)(TArg, TArg2)) : _Target(target), _Func(func) {}
|
||||
|
||||
void Invoke(TArg value, TArg2 value2)
|
||||
{
|
||||
(_Target->*_Func)(value, value2);
|
||||
}
|
||||
|
||||
private:
|
||||
T* _Target;
|
||||
void (T::*_Func)(TArg, TArg2);
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
// 函数模版接口
|
||||
template <typename TParameter>
|
||||
class ifunction
|
||||
{
|
||||
public:
|
||||
// 函数参数的类型
|
||||
typedef TParameter parameter_type;
|
||||
|
||||
// 将被重载的函数操作
|
||||
virtual void operator ()(TParameter) const = 0;
|
||||
};
|
||||
|
||||
// 无参函数模版接口
|
||||
template <>
|
||||
class ifunction<void>
|
||||
{
|
||||
public:
|
||||
typedef void parameter_type;
|
||||
|
||||
virtual void operator ()() const = 0;
|
||||
};
|
||||
|
||||
// 对象函数模版
|
||||
template <typename TObject, typename TParameter>
|
||||
class function : public ifunction<TParameter>
|
||||
{
|
||||
public:
|
||||
typedef TObject object_type; // 对象类型
|
||||
typedef TParameter parameter_type; // 函数参数的类型
|
||||
|
||||
function(TObject& object, void(TObject::* p_function)(TParameter))
|
||||
: p_object(&object),
|
||||
p_function(p_function)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator ()(TParameter data) const
|
||||
{
|
||||
// 调用对象的成员函数
|
||||
(p_object->*p_function)(data);
|
||||
}
|
||||
|
||||
private:
|
||||
TObject* p_object; // 对象指针
|
||||
void (TObject::* p_function)(TParameter); // 成员函数指针
|
||||
};
|
||||
|
||||
// 对象无参函数模版
|
||||
template <typename TObject>
|
||||
class function<TObject, void> : public ifunction<void>
|
||||
{
|
||||
public:
|
||||
function(TObject& object, void(TObject::* p_function)(void))
|
||||
: p_object(&object),
|
||||
p_function(p_function)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator ()() const
|
||||
{
|
||||
(p_object->*p_function)();
|
||||
}
|
||||
|
||||
private:
|
||||
TObject* p_object;
|
||||
void (TObject::* p_function)();
|
||||
};
|
||||
|
||||
// 全局函数模版
|
||||
template <typename TParameter>
|
||||
class function<void, TParameter> : public ifunction<TParameter>
|
||||
{
|
||||
public:
|
||||
function(void(*p_function)(TParameter))
|
||||
: p_function(p_function)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator ()(TParameter data) const
|
||||
{
|
||||
(*p_function)(data);
|
||||
}
|
||||
|
||||
private:
|
||||
void (*p_function)(TParameter);
|
||||
};
|
||||
|
||||
template <>
|
||||
class function<void, void> : public ifunction<void>
|
||||
{
|
||||
public:
|
||||
function(void(*p_function)(void))
|
||||
: p_function(p_function)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator ()() const
|
||||
{
|
||||
(*p_function)();
|
||||
}
|
||||
|
||||
private:
|
||||
void (*p_function)();
|
||||
};
|
||||
|
||||
#endif //_Delegate_H_
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
#include "Type.h"
|
||||
#include "DateTime.h"
|
||||
#include "SString.h"
|
||||
|
||||
#include "EventHandler.h"
|
||||
|
||||
/************************************************ EventHandler ************************************************/
|
||||
EventHandler::EventHandler()
|
||||
{
|
||||
Method = nullptr;
|
||||
Target = nullptr;
|
||||
}
|
||||
|
||||
EventHandler& EventHandler::operator=(const Func& func)
|
||||
{
|
||||
Method = (void*)func;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void EventHandler::Add(Func func)
|
||||
{
|
||||
Method = (void*)func;
|
||||
}
|
||||
|
||||
void EventHandler::operator()()
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
if(Target)
|
||||
((Action)Method)(Target);
|
||||
else
|
||||
((Func)Method)();
|
||||
}
|
||||
|
||||
/*void EventHandler::Invoke(void* arg)
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
auto func = (Func)Method;
|
||||
|
||||
if(Target)
|
||||
func(Target, arg);
|
||||
else
|
||||
((Action)Method)(arg);
|
||||
}
|
||||
|
||||
void EventHandler::Invoke(void* arg1, void* arg2)
|
||||
{
|
||||
if(!Method) return;
|
||||
|
||||
auto func = (Func)Method;
|
||||
|
||||
if(Target)
|
||||
func(Target, arg1, arg2);
|
||||
else
|
||||
func(arg1, arg2);
|
||||
}*/
|
|
@ -24,15 +24,16 @@ Timer::Timer(TIMER index)
|
|||
|
||||
Opened = false;
|
||||
|
||||
_Handler = nullptr;
|
||||
_Param = nullptr;
|
||||
//_Handler = nullptr;
|
||||
//_Param = nullptr;
|
||||
}
|
||||
|
||||
Timer::~Timer()
|
||||
{
|
||||
Close();
|
||||
|
||||
if(_Handler) Register(nullptr);
|
||||
//if(_Handler) Register(nullptr);
|
||||
if(OnTick.Method) SetHandler(false);
|
||||
|
||||
Timers[_index] = nullptr;
|
||||
}
|
||||
|
@ -91,17 +92,25 @@ void Timer::Close()
|
|||
Opened = false;
|
||||
}
|
||||
|
||||
void Timer::Register(EventHandler handler, void* param)
|
||||
/*void Timer::Register(EventHandler handler, void* param)
|
||||
{
|
||||
_Handler = handler;
|
||||
_Param = param;
|
||||
|
||||
SetHandler(handler != nullptr);
|
||||
}*/
|
||||
|
||||
void Timer::Register(const Delegate& dlg)
|
||||
{
|
||||
OnTick = dlg;
|
||||
|
||||
SetHandler(dlg.Method);
|
||||
}
|
||||
|
||||
void Timer::OnInterrupt()
|
||||
{
|
||||
if(_Handler) _Handler(this, _Param);
|
||||
//if(_Handler) _Handler(this, _Param);
|
||||
OnTick(this);
|
||||
}
|
||||
|
||||
/*================ PWM ================*/
|
||||
|
|
|
@ -16,12 +16,14 @@ public:
|
|||
void* _Timer;
|
||||
bool Opened; // 可能在中断里关闭自己
|
||||
|
||||
Timer(TIMER index);
|
||||
virtual ~Timer();
|
||||
|
||||
ushort Prescaler; // 预分频。实际值,此时无需减一。
|
||||
uint Period; // 周期。实际值,此时无需减一。
|
||||
|
||||
Delegate OnTick;
|
||||
|
||||
Timer(TIMER index);
|
||||
virtual ~Timer();
|
||||
|
||||
virtual void Open(); // 开始定时器
|
||||
virtual void Close(); // 停止定时器
|
||||
virtual void Config();
|
||||
|
@ -31,14 +33,15 @@ public:
|
|||
uint GetCounter();
|
||||
void SetCounter(uint cnt); // 设置计数器值
|
||||
|
||||
void Register(EventHandler handler, void* param = nullptr);
|
||||
//void Register(EventHandler handler, void* param = nullptr);
|
||||
void Register(const Delegate& dlg);
|
||||
|
||||
static void ClockCmd(int idx, bool state);
|
||||
|
||||
private:
|
||||
static void OnHandler(ushort num, void* param);
|
||||
EventHandler _Handler;
|
||||
void* _Param;
|
||||
//EventHandler _Handler;
|
||||
//void* _Param;
|
||||
|
||||
protected:
|
||||
virtual void OnInterrupt();
|
||||
|
@ -47,12 +50,12 @@ public:
|
|||
static const byte TimerCount; // 定时器个数
|
||||
|
||||
static Timer* Create(byte index = 0xFF); // 创建指定索引的定时器,如果已有则直接返回,默认0xFF表示随机分配
|
||||
|
||||
|
||||
private:
|
||||
void OnInit();
|
||||
void OnOpen();
|
||||
void OnClose();
|
||||
|
||||
|
||||
static const void* GetTimer(byte idx);
|
||||
};
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ Dhcp::Dhcp(ISocketHost& host) : Host(host)
|
|||
MaxTimes = 6;
|
||||
ExpiredTime = 500 * 10;
|
||||
|
||||
OnStop = nullptr;
|
||||
//OnStop = nullptr;
|
||||
taskID = 0;
|
||||
|
||||
auto port = dynamic_cast<ITransport*>(Socket);
|
||||
|
@ -200,7 +200,8 @@ void Dhcp::Stop()
|
|||
}
|
||||
}
|
||||
|
||||
if(OnStop) OnStop(this, nullptr);
|
||||
//if(OnStop) OnStop(this, nullptr);
|
||||
OnStop(this);
|
||||
}
|
||||
|
||||
void Dhcp::Loop(void* param)
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
void Start(); // 开始
|
||||
void Stop(); // 停止
|
||||
|
||||
EventHandler OnStop;
|
||||
Delegate OnStop;
|
||||
|
||||
private:
|
||||
static uint OnReceive(ITransport* port, Buffer& bs, void* param, void* param2);
|
||||
|
|
|
@ -2,18 +2,17 @@
|
|||
#include "Port.h"
|
||||
#include "Timer.h"
|
||||
|
||||
Timer* timer;
|
||||
//Timer* timer;
|
||||
|
||||
void TimerTask(void* sender, void* param)
|
||||
void TimerTask(OutputPort* leds, Timer* timer)
|
||||
{
|
||||
OutputPort* leds = (OutputPort*)param;
|
||||
*leds = !*leds;
|
||||
}
|
||||
|
||||
uint frequency = 0;
|
||||
int step = 1;
|
||||
|
||||
void TimerTask2(void* sender, void* param)
|
||||
void TimerTask2(Timer* timer)
|
||||
{
|
||||
frequency += step;
|
||||
|
||||
|
@ -36,14 +35,16 @@ void TestTimer(OutputPort& leds)
|
|||
debug_printf("\r\n");
|
||||
debug_printf("TestTimer Start......\r\n");
|
||||
|
||||
timer = new Timer(Timer2);
|
||||
auto timer = new Timer(Timer2);
|
||||
timer->SetFrequency(50);
|
||||
timer->Register(TimerTask, &leds);
|
||||
//timer->Register(TimerTask, &leds);
|
||||
timer->OnTick = Delegate((void*)&TimerTask, &leds);
|
||||
timer->Open();
|
||||
|
||||
Timer* timer2 = Timer::Create();
|
||||
auto timer2 = Timer::Create();
|
||||
timer2->SetFrequency(10);
|
||||
timer2->Register(TimerTask2, nullptr);
|
||||
//timer2->Register(TimerTask2, nullptr);
|
||||
timer2->OnTick = (void*)&TimerTask2;
|
||||
timer2->Open();
|
||||
|
||||
debug_printf("\r\n TestTimer Finish!\r\n");
|
||||
|
|
|
@ -234,22 +234,18 @@ bool CheckUserPress(InputPort* port, bool down, void* param)
|
|||
return false;
|
||||
}
|
||||
|
||||
void CheckUserPress2(InputPort* port, bool down, void* param)
|
||||
{
|
||||
CheckUserPress(port, down, param);
|
||||
}
|
||||
|
||||
void CheckUserPress3(void* sender, void* param)
|
||||
void CheckUserPress3(void* sender)
|
||||
{
|
||||
auto but = (Button_GrayLevel *)sender;
|
||||
CheckUserPress(&but->Key, but->Key.Read(), param);
|
||||
CheckUserPress(&but->Key, but->Key.Read(), nullptr);
|
||||
}
|
||||
|
||||
void InitButtonPress(Button_GrayLevel* btns, byte count)
|
||||
{
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
btns[i].Register(CheckUserPress3, nullptr);// = CheckUserPress2;
|
||||
//btns[i].Register(CheckUserPress3, nullptr);// = CheckUserPress2;
|
||||
btns[i].Press = CheckUserPress3;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue