SmartOS/App/Button_GrayLevel.h

109 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __BUTTON_GRAY_LEVEL_H__
#define __BUTTON_GRAY_LEVEL_H__
#include "Sys.h"
#include "Port.h"
#include "Timer.h"
#include "Message\DataStore.h"
struct ButtonPin
{
Pin Led;
Pin Key;
Pin Relay;
bool Invert;
byte PwmIndex;
};
enum ButtonStat :byte
{
normal = 0, // 正常
set = 1, // 设置
externSet = 2, // 外部设置
execution = 3, // 执行设置
};
class Button_GrayLevel;
using TAction = Delegate<Button_GrayLevel&>::Action;
// 面板按钮
// 这里必须使用_packed关键字生成对齐的代码否则_Value只占一个字节导致后面的成员进行内存操作时错乱
//__packed class Button
// 干脆把_Value挪到最后解决问题
class Button_GrayLevel : public ByteDataPort
{
public:
int Index; // 索引号,方便在众多按钮中标识按钮
#if DEBUG
cstring Name; // 按钮名称
#endif
InputPort Key; // 输入按键
OutputPort Relay; // 继电器
enum ButtonStat Stat = normal; // 状态
// 外部设置模式调用
typedef bool(*IOHandler)(Button_GrayLevel* bt, bool down, void * param);
IOHandler ExterSet = nullptr;
void * ExterSetParam = nullptr;
public:
Delegate<Button_GrayLevel&> Press;
// 构造函数。指示灯和继电器一般开漏输出,需要倒置
Button_GrayLevel();
void Set(Pin key, Pin relay = P0, bool relayInvert = true);
// led 驱动器设置
void Set(PWM* drive, byte pulseIndex);
bool GetValue();
void SetValue(bool value);
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();
private:
// 指示灯灰度驱动器 PWM;
PWM* _Pwm;
byte _Channel;
bool _Value; // 状态
ushort Reserved; // 补足对齐问题
//EventHandler _Handler;
//void* _Param;
static void OnKeyPress(InputPort* port, bool down, void* param);
void OnKeyPress(InputPort* port, bool down);
public:
static byte OnGrayLevel; // 开灯时 led 灰度
static byte OffGrayLevel; // 关灯时 led 灰度
static void Init(TIMER tim, byte count, Button_GrayLevel* btns, TAction 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);
void GrayLevelDown();
void GrayLevelUp();
void DelayClose2(int ms); // 自定义延时关闭
int delaytime = 0;
int _task2 = 0;
// 过零检测
private:
static int ACZeroAdjTime; // 过零检测时间补偿 默认 2300us
public:
static InputPort* ACZero; // 交流过零检测引脚
static bool SetACZeroPin(Pin aczero); // 设置过零检测引脚
static void SetACZeroAdjTime(int us) { ACZeroAdjTime = us; }; // 设置 过零检测补偿时间
};
#endif