重构板级包,标准化操作接口,基础板、令牌板、Esp8266板,多个层次

This commit is contained in:
大石头 2017-04-09 18:05:23 +08:00
parent 39752ee2ff
commit 2e9b376b4b
15 changed files with 449 additions and 987 deletions

View File

@ -1,25 +1,12 @@
#include "AP0801.h"
#include "Kernel\Task.h"
#include "Device\WatchDog.h"
#include "Config.h"
#include "Drivers\NRF24L01.h"
#include "Drivers\W5500.h"
#include "Drivers\Esp8266\Esp8266.h"
#include "TokenNet\TokenController.h"
#include "TokenNet\TokenConfig.h"
#include "TokenNet\TokenClient.h"
#include "Device\RTC.h"
#include "Message\ProxyFactory.h"
AP0801* AP0801::Current = nullptr;
static TokenClient* Client = nullptr; // 令牌客户端
static ProxyFactory* ProxyFac = nullptr; // 透传管理器
AP0801::AP0801()
@ -29,15 +16,11 @@ AP0801::AP0801()
ButtonPins.Add(PE13);
ButtonPins.Add(PE14);
Client = nullptr;
LedInvert = false;
ProxyFac = nullptr;
AlarmObj = nullptr;
Data = nullptr;
Size = 0;
HardVer = 0;
Net.Spi = Spi2;
Net.Irq = PE1;
Net.Reset = PD13;
@ -51,105 +34,6 @@ AP0801::AP0801()
Current = this;
}
void AP0801::Init(ushort code, cstring name, COM message)
{
auto& sys = (TSys&)Sys;
sys.Code = code;
sys.Name = (char*)name;
// RTC 提取时间
HardRTC::Start(false, false);
// 初始化系统
sys.Init();
auto hot = &HotConfig::Current();
// 热启动次数
Sys.HotStart = hot->Times + 1;
#if DEBUG
sys.MessagePort = message; // 指定printf输出的串口
Sys.ShowInfo();
WatchDog::Start(20000, 10000);
#else
WatchDog::Start();
// 系统休眠时自动进入低功耗
// Power::AttachTimeSleep();
#endif
// Flash最后一块作为配置区
Config::Current = &Config::CreateFlash();
}
void* AP0801::InitData(void* data, int size)
{
// 启动信息
auto hot = &HotConfig::Current();
hot->Times++;
data = hot->Next();
if (hot->Times == 1)
{
Buffer ds(data, size);
ds.Clear();
ds[0] = size;
}
Data = data;
Size = size;
#if DEBUG
debug_printf("数据区%d", hot->Times);
Buffer(Data, Size).Show(true);
#endif
return data;
}
// 写入数据区并上报
void AP0801::Write(uint offset, byte data)
{
auto client = Client;
if (!client) return;
client->Store.Write(offset, data);
client->ReportAsync(offset, 1);
}
void AP0801::InitLeds()
{
for (int i = 0; i < LedPins.Count(); i++)
{
auto port = new OutputPort(LedPins[i], false);
port->Open();
Leds.Add(port);
}
}
/*static void ButtonOnpress(InputPort* port, bool down, void* param)
{
if (port->PressTime > 1000)
AP0801::Current->OnLongPress(port, down);
}*/
void AP0801::InitButtons(const Delegate2<InputPort&, bool>& press)
{
for (int i = 0; i < ButtonPins.Count(); i++)
{
auto port = new InputPort();
port->Index = i;
port->Set(ButtonPins[i]);
//port->ShakeTime = 40;
port->Press = press;
port->UsePress();
port->Open();
Buttons.Add(port);
}
}
NetworkInterface* AP0801::Create5500()
{
debug_printf("\r\nW5500::Create \r\n");
@ -200,40 +84,6 @@ NetworkInterface* AP0801::Create8266()
return esp;
}
/******************************** Token ********************************/
void AP0801::InitClient()
{
if (Client) return;
// 初始化令牌网
auto tk = TokenConfig::Create("smart.wslink.cn", NetType::Tcp, 33333, 3377);
// 创建客户端
auto tc = TokenClient::CreateFast(Buffer(Data, Size));
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
tc->UseLocal();
Client = tc;
}
void AP0801::Register(uint offset, IDataPort& dp)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(offset, dp);
}
void AP0801::Register(uint offset, uint size, Handler hook)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(offset, size, hook);
}
static void OnInitNet(void* param)
{
auto& bsp = *(AP0801*)param;
@ -241,7 +91,7 @@ static void OnInitNet(void* param)
bsp.Create5500();
bsp.Create8266();
Client->Open();
bsp.Client->Open();
}
void AP0801::InitNet()
@ -249,14 +99,6 @@ void AP0801::InitNet()
Sys.AddTask(OnInitNet, this, 0, -1, "InitNet");
}
void AP0801::Invoke(const String& ation, const Buffer& bs)
{
if (!Client) return;
Client->Invoke(ation, bs);
}
void AP0801::InitProxy()
{
if (ProxyFac)return;
@ -282,7 +124,7 @@ static void OnAlarm(AlarmItem& item)
if (bs[1] == 0x06)
{
auto client = Client;
auto client = AP0801::Current->Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
@ -352,39 +194,6 @@ ITransport* AP0801::Create2401(SPI spi_, Pin ce, Pin irq, Pin power, bool powerI
return &nrf;
}*/
void AP0801::Restore()
{
if (!Client) return;
if (Client) Client->Reset("按键重置");
}
int AP0801::GetStatus()
{
if (!Client) return 0;
return Client->Status;
}
void AP0801::OnLongPress(InputPort* port, bool down)
{
if (down) return;
debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime);
ushort time = port->PressTime;
auto client = Client;
if (time >= 5000 && time < 10000)
{
if (client) client->Reset("按键重置");
}
else if (time >= 3000)
{
if (client) client->Reboot("按键重启");
Sys.Reboot(1000);
}
}
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)
NSS | NSS | PD6 NSS

View File

@ -1,54 +1,29 @@
#ifndef _AP0801_H_
#define _AP0801_H_
#include "Kernel\Sys.h"
#include "Net\ITransport.h"
#include "TokenBoard.h"
#include "Net\Socket.h"
#include "Device\Spi.h"
#include "Device\SerialPort.h"
#include "App\Alarm.h"
#include "Device\Spi.h"
#include "Drivers\W5500.h"
#include "Drivers\Esp8266\Esp8266.h"
// 阿波罗0801
class AP0801
class AP0801 : public TokenBoard
{
public:
List<Pin> LedPins;
List<Pin> ButtonPins;
List<OutputPort*> Leds;
List<InputPort*> Buttons;
List<OutputPort*> Outputs;
List<InputPort*> Inputs;
Alarm* AlarmObj;
W5500Config Net;
Esp8266Config Esp;
uint HardVer;
SpiConfig Net;
SerialConfig Esp;
AP0801();
// 设置系统参数
void Init(ushort code, cstring name, COM message = COM1);
// 设置数据区
void* InitData(void* data, int size);
// 写入数据区并上报
void Write(uint offset, byte data);
//获取客户端的状态0未握手1已握手2已经登陆
int GetStatus();
typedef bool(*Handler)(uint offset, uint size, bool write);
void Register(uint offset, uint size, Handler hook);
void Register(uint offset, IDataPort& dp);
void InitLeds();
void InitButtons(const Delegate2<InputPort&, bool>& press);
// 打开以太网W5500
NetworkInterface* Create5500();
// 打开Esp8266作为主控或者纯AP
@ -56,20 +31,11 @@ public:
// ITransport* Create2401();
void InitClient();
void InitNet();
void InitProxy();
void InitAlarm();
void Restore();
// invoke指令
void Invoke(const String& ation, const Buffer& bs);
void OnLongPress(InputPort* port, bool down);
static AP0801* Current;
private:
void* Data;
int Size;
};
#endif

View File

@ -9,7 +9,7 @@ AP0802::AP0802(int hardver) : AP0801()
LedPins.Add(PE4);
LedPins.Add(PD0);
HardVer = hardver;
Sys.HardVer = hardver;
if (hardver >= 160712)
ButtonPins.Add(PE9);
else

149
Board/B8266.cpp Normal file
View File

@ -0,0 +1,149 @@
#include "B8266.h"
#include "Drivers\Esp8266\Esp8266.h"
B8266* B8266::Current = nullptr;
B8266::B8266()
{
LedPins.Add(PA4);
LedPins.Add(PA5);
Esp.Com = COM2;
Esp.Baudrate = 115200;
Esp.Power = PB2;
Esp.Reset = PA1;
Esp.LowPower = P0;
SSID = "WSWL";
Pass = "12345678";
Current = this;
}
void B8266::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
NetworkInterface* B8266::Create8266()
{
auto esp = new Esp8266();
esp->Init(Esp.Com, Esp.Baudrate);
esp->Set(Esp.Power, Esp.Reset, Esp.LowPower);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
//esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
Client->Register("GetAPs", &Esp8266::GetAPs, esp);
return esp;
}
void B8266::InitNet()
{
Create8266();
Client->Open();
}
static void OnAlarm(AlarmItem& item)
{
// 1长度n + 1类型 + 1偏移 + (n-2)数据
auto bs = item.GetData();
debug_printf("OnAlarm ");
bs.Show(true);
if (bs[1] == 0x06)
{
auto client = B8266::Current->Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
client->ReportAsync(bs[2], bs[0] - 2);
}
}
void B8266::InitAlarm()
{
if (!Client)return;
if (!AlarmObj) AlarmObj = new Alarm();
Client->Register("Policy/AlarmSet", &Alarm::Set, AlarmObj);
Client->Register("Policy/AlarmGet", &Alarm::Get, AlarmObj);
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
static void RestPress(InputPort& port, bool down)
{
if (down) return;
auto client = B8266::Current;
client->Restore();
}
void B8266::SetRestore(Pin pin)
{
if (pin == P0) return;
auto port = new InputPort(pin);
port->Open();
port->UsePress();
port->Press = RestPress;
RestPort = port;
}
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)
NSS | NSS | PD6 NSS
CLK | SCK | SCK
MISO | MISO | MISO
MOSI | MOSI | MOSI
PE3 IRQ | PE1 INT(IRQ) | PD11 INT(IRQ)
PD12 CE | PD13 NET_NRST | NET_NRST
PE6 POWER | POWER | POWER
ESP8266 (COM4)
TX
RX
PD3 RST
PE2 POWER
TFT
FSMC_D 0..15 TFT_D 0..15
NOE RD
NWE RW
NE1 RS
PE4 CE
PC7 LIGHT
PE5 RST
PE13 KEY1
PE14 KEY2
PE15 LED2
PD8 LED1
USB
PA11 PA12
*/

40
Board/B8266.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef _B8266_H_
#define _B8266_H_
#include "TokenBoard.h"
#include "Net\Socket.h"
#include "Device\Spi.h"
#include "Device\SerialPort.h"
#include "App\Alarm.h"
// Esp8266核心板
class B8266 : public TokenBoard
{
public:
Alarm* AlarmObj;
SerialConfig Esp;
cstring SSID;
cstring Pass;
B8266();
void InitWiFi(cstring ssid, cstring pass);
void SetRestore(Pin pin = PB4); // 设置重置引脚
NetworkInterface* Create8266();
void InitNet();
void InitAlarm();
static B8266* Current;
private:
InputPort* RestPort;
};
#endif

View File

@ -1,16 +1,17 @@
#include "BaseBoard.h"
#include "Device\Power.h"
#include "Device\WatchDog.h"
#include "Config.h"
#include "Kernel\Task.h"
#include "Device\RTC.h"
#include "Device\Power.h"
#include "Device\WatchDog.h"
BaseBoard::BaseBoard()
{
LedInvert = 2;
}
void BaseBoard::Init(ushort code, cstring name, COM message)
void BaseBoard::Init(ushort code, cstring name)
{
auto& sys = (TSys&)Sys;
sys.Code = code;
@ -25,8 +26,12 @@ void BaseBoard::Init(ushort code, cstring name, COM message)
// 初始化系统
sys.Init();
auto hot = &HotConfig::Current();
// 热启动次数
Sys.HotStart = hot->Times + 1;
#if DEBUG
sys.MessagePort = message; // 指定printf输出的串口
Sys.ShowInfo();
WatchDog::Start(20000, 10000);
@ -36,56 +41,38 @@ void BaseBoard::Init(ushort code, cstring name, COM message)
// 系统休眠时自动进入低功耗
Power::AttachTimeSleep();
#endif
}
// 初始化配置区
void BaseBoard::InitConfig()
{
// Flash最后一块作为配置区
Config::Current = &Config::CreateFlash();
}
void BaseBoard::InitReboot()
{
}
void BaseBoard::InitRestore()
{
}
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)
NSS | NSS | PD6 NSS
CLK | SCK | SCK
MISO | MISO | MISO
MOSI | MOSI | MOSI
PE3 IRQ | PE1 INT(IRQ) | PD11 INT(IRQ)
PD12 CE | PD13 NET_NRST | NET_NRST
PE6 POWER | POWER | POWER
ESP8266 (COM4)
TX
RX
PD3 RST
PE2 POWER
TFT
FSMC_D 0..15 TFT_D 0..15
NOE RD
NWE RW
NE1 RS
PE4 CE
PC7 LIGHT
PE5 RST
PE13 KEY1
PE14 KEY2
PE15 LED2
PD8 LED1
USB
PA11 PA12
*/
void BaseBoard::InitLeds()
{
for (int i = 0; i < LedPins.Count(); i++)
{
auto port = new OutputPort(LedPins[i], LedInvert);
//auto port = new OutputPort(LedPins[i], false);
port->Open();
Leds.Add(port);
}
}
void BaseBoard::InitButtons(const Delegate2<InputPort&, bool>& press)
{
for (int i = 0; i < ButtonPins.Count(); i++)
{
auto port = new InputPort();
port->Index = i;
port->Set(ButtonPins[i]);
//port->ShakeTime = 40;
port->Press = press;
port->UsePress();
port->Open();
Buttons.Add(port);
}
}

View File

@ -2,30 +2,28 @@
#define _BaseBoard_H_
#include "Kernel\Sys.h"
#include "Device\RTC.h"
/*struct
{
byte ClksCount;
byte OldIdx; // 初始值无效
UInt64 LastClkTim;
void Init()
{
ClksCount = 0;
OldIdx = 0xff;
LastClkTim = 0;
};
}ClickStr;*/
#include "Device\Port.h"
// 板级包基类
class BaseBoard
{
public:
List<Pin> LedPins; // 指示灯 引脚
List<Pin> ButtonPins; // 按键 引脚
List<OutputPort*> Leds; // 指示灯
List<InputPort*> Buttons;// 按键
byte LedInvert; // 指示灯倒置。默认自动检测
BaseBoard();
void Init(ushort code, cstring name, COM message = COM1);
//初始化按键重启
void InitReboot();
//初始化断电重置
void InitRestore();
// 设置系统参数
void Init(ushort code, cstring name);
// 初始化配置区
void InitConfig();
void InitLeds();
void InitButtons(const Delegate2<InputPort&, bool>& press);
};
#endif

View File

@ -1,203 +1,24 @@
#include "IOK027X.h"
#include "Kernel\Task.h"
#include "Device\Power.h"
#include "Device\WatchDog.h"
#include "Config.h"
#include "Drivers\Esp8266\Esp8266.h"
#include "TokenNet\TokenController.h"
#include "Kernel\Task.h"
IOK027X* IOK027X::Current = nullptr;
IOK027X::IOK027X()
{
LedPins.Clear();
ButtonPins.Clear();
LedPins.Add(PA4);
LedPins.Add(PA5);
LedsShow = 2;
LedsTaskId = 0;
Esp.Com = COM2;
Esp.Baudrate = 115200;
Esp.Power = PB2;
Esp.Reset = PA1;
Esp.LowPower = P0;
Host = nullptr; // 网络主机
Client = nullptr;
Data = nullptr;
Size = 0;
Current = this;
SSID = "WSWL";
Pass = "12345678";
}
void IOK027X::Init(ushort code, cstring name, COM message)
{
auto& sys = (TSys&)Sys;
sys.Code = code;
sys.Name = (char*)name;
// RTC 提取时间
auto Rtc = HardRTC::Instance();
Rtc->LowPower = false;
Rtc->External = false;
Rtc->Init();
Rtc->Start(false, false);
// 初始化系统
sys.Init();
#if DEBUG
sys.MessagePort = message; // 指定printf输出的串口
Sys.ShowInfo();
WatchDog::Start(20000, 10000);
#else
WatchDog::Start();
// 系统休眠时自动进入低功耗
//Power::AttachTimeSleep();
#endif
// Flash最后一块作为配置区
Config::Current = &Config::CreateFlash();
}
void* IOK027X::InitData(void* data, int size)
{
// 启动信息
auto hot = &HotConfig::Current();
hot->Times++;
data = hot->Next();
if (hot->Times == 1)
{
Buffer ds(data, size);
ds.Clear();
ds[0] = size;
}
Data = data;
Size = size;
#if DEBUG
debug_printf("数据区%d", hot->Times);
Buffer(Data, Size).Show(true);
#endif
return data;
}
void IOK027X::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
void IOK027X::InitLeds()
{
for (int i = 0; i < LedPins.Count(); i++)
{
auto port = new OutputPort(LedPins[i]);
port->Invert = true;
port->Open();
port->Write(false);
Leds.Add(port);
}
}
NetworkInterface* IOK027X::Create8266(Pin power)
{
auto esp = new Esp8266();
esp->Init(COM2);
esp->Set(power, PA1);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
//esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
Client->Register("GetAPs", &Esp8266::GetAPs, esp);
return esp;
}
/******************************** Token ********************************/
void IOK027X::InitClient()
{
if (Client) return;
// 初始化令牌网
auto tk = TokenConfig::Create("smart.wslink.cn", NetType::Udp, 33333, 3377);
// 创建客户端
auto tc = TokenClient::CreateFast(Buffer(Data, Size));
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
tc->UseLocal();
Client = tc;
}
void IOK027X::Register(int index, IDataPort& dp)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(index, dp);
}
void IOK027X::InitNet(Pin power)
{
Host = Create8266(power);
Client->Open();
}
static void OnAlarm(AlarmItem& item)
{
// 1长度n + 1类型 + 1偏移 + (n-2)数据
auto bs = item.GetData();
debug_printf("OnAlarm ");
bs.Show(true);
if (bs[1] == 0x06)
{
auto client = IOK027X::Current->Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
client->ReportAsync(bs[2], bs[0] - 2);
}
}
void IOK027X::InitAlarm()
{
if (!Client)return;
if (!AlarmObj) AlarmObj = new Alarm();
Client->Register("Policy/AlarmSet", &Alarm::Set, AlarmObj);
Client->Register("Policy/AlarmGet", &Alarm::Get, AlarmObj);
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
//双联开关被触发
// 联动触发
static void UnionPress(InputPort& port, bool down)
{
if (IOK027X::Current == nullptr) return;
@ -229,121 +50,6 @@ void IOK027X::Union(Pin pin1, Pin pin2)
}
}
static void RestPress(InputPort& port, bool down)
{
if (down) return;
auto client = IOK027X::Current;
client->Restore();
}
void IOK027X::SetRestore(Pin pin)
{
if (pin == P0) return;
auto port = new InputPort(pin);
port->Open();
port->UsePress();
port->Press = RestPress;
RestPort = port;
}
static bool ledstat2 = false;
void IOK027X::Restore()
{
for (int i = 0; i < 10; i++)
{
Leds[1]->Write(ledstat2);
ledstat2 = !ledstat2;
Sys.Sleep(300);
}
if (Client) Client->Reset("按键重置");
}
void IOK027X::FlushLed()
{
if (LedsShow == 0) // 启动时候20秒
{
auto esp = dynamic_cast<Esp8266*>(Host);
if (esp && esp->Linked) // 8266 初始化完成 且 连接完成
{
Sys.SetTaskPeriod(LedsTaskId, 500); // 慢闪
}
Leds[0]->Write(ledstat2);
ledstat2 = !ledstat2;
if (Sys.Ms() > 20000)
{
Leds[0]->Write(false);
LedsShow = 2; // 置为无效状态
}
}
bool stat = false;
// 3分钟内 Client还活着则表示 联网OK
if (Client && Client->LastActive + 180000 > Sys.Ms() && LedsShow == 1)stat = true;
Leds[1]->Write(stat);
if (LedsShow == 2)Sys.SetTask(LedsTaskId, false);
}
byte IOK027X::LedStat(byte showmode)
{
auto esp = dynamic_cast<Esp8266*>(Host);
if (esp)
{
if (showmode == 1)
{
esp->RemoveLed();
esp->SetLed(*Leds[0]);
}
else
{
esp->RemoveLed();
// 维护状态为false
Leds[0]->Write(false);
}
}
if (showmode != 2)
{
if (!LedsTaskId)
{
LedsTaskId = Sys.AddTask(&IOK027X::FlushLed, this, 500, 100, "CltLedStat");
debug_printf("AddTask(IOK027X:FlushLed)\r\n");
}
else
{
Sys.SetTask(LedsTaskId, true);
if (showmode == 1)Sys.SetTaskPeriod(LedsTaskId, 500);
}
LedsShow = showmode;
}
if (showmode == 2)
{
// 由任务自己结束顺带维护输出状态为false
// if (LedsTaskId)Sys.SetTask(LedsTaskId, false);
LedsShow = 2;
}
return LedsShow;
}
void IOK027X::OnLongPress(InputPort* port, bool down)
{
if (down) return;
debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime);
ushort time = port->PressTime;
auto client = IOK027X::Current->Client;
//if (time >= 10000 && time < 15000)
// Current->Restore();
//else
if (time >= 7000)
{
if (client) client->Reboot("按键重启");
Sys.Reboot(1000);
}
}
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)
NSS | NSS | PD6 NSS

View File

@ -1,61 +1,20 @@
#ifndef _IOK027X_H_
#define _IOK027X_H_
#include "Kernel\Sys.h"
#include "Net\ITransport.h"
#include "Net\Socket.h"
#include "B8266.h"
#include "TokenNet\TokenClient.h"
#include "App\Alarm.h"
#include "APP\Button_GrayLevel.h"
#include "Device\RTC.h"
// WIFI触摸开关 123位
class IOK027X
// WIFI触摸开关
class IOK027X : public B8266
{
public:
List<Pin> LedPins;
List<OutputPort*> Leds;
byte LedsShow; // LED 显示状态开关 0 刚启动时候的20秒 1 使能 2 失能
NetworkInterface* Host; // 网络主机
TokenClient* Client; //
Alarm* AlarmObj;
uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK027X();
void Init(ushort code, cstring name, COM message = COM1);
void* InitData(void* data, int size);
void InitWiFi(cstring ssid, cstring pass);
void Register(int index, IDataPort& dp);
void SetRestore(Pin pin = PB4); //设置重置引脚
void InitLeds();
void FlushLed(); // 刷新led状态输出
byte LedStat(byte showmode);
NetworkInterface* Create8266(Pin power = PB2);
void InitClient();
void InitNet(Pin power = PB2);
void InitAlarm();
//双联开关
// 联动开关
void Union(Pin pin1, Pin pin2);
void Restore();
void OnLongPress(InputPort* port, bool down);
static IOK027X* Current;
private:
void* Data;
int Size;
InputPort* RestPort;
};
#endif

View File

@ -1,294 +1,21 @@
#include "IOK0612.h"
#include "Kernel\Task.h"
#include "Device\Power.h"
#include "Device\WatchDog.h"
#include "Config.h"
#include "Drivers\Esp8266\Esp8266.h"
#include "TokenNet\TokenController.h"
#include "Kernel\Task.h"
IOK0612* IOK0612::Current = nullptr;
IOK0612::IOK0612()
{
//LedPins.Add(PA4);
ButtonPins.Add(PB6);
LedPins.Clear();
ButtonPins.Clear();
LedPins.Add(PB7);
ButtonPins.Add(PB6);
LedsShow = false;
LedsTaskId = 0;
Esp.Com = COM2;
Esp.Baudrate = 115200;
Esp.Power = PB12;
Esp.Reset = PA1;
Esp.LowPower = P0;
Host = nullptr; // 网络主机
Client = nullptr;
Data = nullptr;
Size = 0;
Current = this;
SSID = "WSWL";
Pass = "12345678";
}
void IOK0612::Init(ushort code, cstring name, COM message)
{
auto& sys = (TSys&)Sys;
sys.Code = code;
sys.Name = (char*)name;
// RTC 提取时间
auto Rtc = HardRTC::Instance();
Rtc->LowPower = false;
Rtc->External = false;
Rtc->Init();
Rtc->Start(false, false);
// 初始化系统
sys.Init();
#if DEBUG
sys.MessagePort = message; // 指定printf输出的串口
Sys.ShowInfo();
WatchDog::Start(20000, 10000);
#else
WatchDog::Start();
// 系统休眠时自动进入低功耗
//Power::AttachTimeSleep();
#endif
// Flash最后一块作为配置区
Config::Current = &Config::CreateFlash();
}
void* IOK0612::InitData(void* data, int size)
{
// 启动信息
auto hot = &HotConfig::Current();
hot->Times++;
data = hot->Next();
if (hot->Times == 1)
{
Buffer ds(data, size);
ds.Clear();
ds[0] = size;
}
Data = data;
Size = size;
#if DEBUG
debug_printf("数据区%d", hot->Times);
Buffer(Data, Size).Show(true);
#endif
return data;
}
void IOK0612::InitWiFi(cstring ssid,cstring pass)
{
SSID = ssid;
Pass = pass;
}
void IOK0612::InitLeds()
{
for(int i=0; i<LedPins.Count(); i++)
{
auto port = new OutputPort(LedPins[i]);
port->Invert = true;
port->Open();
port->Write(false);
Leds.Add(port);
}
}
/*static void ButtonOnpress(InputPort* port, bool down, void* param)
{
if (port->PressTime > 1000)
IOK0612::OnLongPress(port, down);
}*/
void IOK0612::InitButtons(const Delegate2<InputPort&, bool>& press)
{
for (int i = 0; i < ButtonPins.Count(); i++)
{
auto port = new InputPort(ButtonPins[i]);
port->Index = i;
port->Press = press;
port->UsePress();
port->Open();
Buttons.Add(port);
}
}
NetworkInterface* IOK0612::Create8266()
{
// auto esp = new Esp8266(COM2, PB2, PA1); // 触摸开关的
//auto esp = new Esp8266(COM2, PB12, PA1);
auto esp = new Esp8266();
esp->Init(COM2);
esp->Set(PB12, PA1);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
}
esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
esp->Open();
return esp;
}
/******************************** Token ********************************/
void IOK0612::InitClient()
{
if (Client) return;
// 初始化令牌网
auto tk = TokenConfig::Create("smart.wslink.cn", NetType::Udp, 33333, 3377);
// 创建客户端
auto tc = TokenClient::CreateFast(Buffer(Data, Size));
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
tc->UseLocal();
Client = tc;
}
void IOK0612::Register(int index, IDataPort& dp)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(index, dp);
}
void IOK0612::InitNet()
{
Host = Create8266();
Client->Open();
}
static void OnAlarm(AlarmItem& item)
{
// 1长度n + 1类型 + 1偏移 + (n-2)数据
auto bs = item.GetData();
debug_printf("OnAlarm ");
bs.Show(true);
if(bs[1] == 0x06)
{
auto client = IOK0612::Current->Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
client->ReportAsync(bs[2], bs[0] - 2);
}
}
void IOK0612::InitAlarm()
{
if (!Client) return;
if (!AlarmObj) AlarmObj = new Alarm();
Client->Register("Policy/AlarmSet", &Alarm::Set, AlarmObj);
Client->Register("Policy/AlarmGet", &Alarm::Get, AlarmObj);
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
void IOK0612::Invoke(const String& ation, const Buffer& bs)
{
if (!Client) return;
Client->Invoke(ation, bs);
}
void IOK0612::Restore()
{
if(Client) Client->Reset("按键重置");
}
void IOK0612::FlushLed()
{
bool stat = false;
// 3分钟内 Client还活着则表示 联网OK
if (Client && Client->LastActive + 180000 > Sys.Ms()&& LedsShow)stat = true;
Leds[1]->Write(stat);
if (!LedsShow)Sys.SetTask(LedsTaskId, false);
// if (!LedsShow)Sys.SetTask(Task::Scheduler()->Current->ID, false);
}
bool IOK0612::LedStat(bool enable)
{
auto esp = dynamic_cast<Esp8266*>(Host);
if (esp)
{
if (enable)
{
esp->RemoveLed();
esp->SetLed(*Leds[0]);
}
else
{
esp->RemoveLed();
// 维护状态为false
Leds[0]->Write(false);
}
}
if (enable)
{
if (!LedsTaskId)
LedsTaskId = Sys.AddTask(&IOK0612::FlushLed, this, 500, 500, "CltLedStat");
else
Sys.SetTask(LedsTaskId, true);
LedsShow = true;
}
else
{
// 由任务自己结束顺带维护输出状态为false
// if (LedsTaskId)Sys.SetTask(LedsTaskId, false);
LedsShow = false;
}
return LedsShow;
}
void IOK0612::OnLongPress(InputPort* port, bool down)
{
if (down) return;
debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime);
ushort time = port->PressTime;
auto client = IOK0612::Current->Client;
if (time >= 5000 && time < 10000)
{
if(client) client->Reset("按键重置");
}
else if (time >= 3000)
{
if(client) client->Reboot("按键重启");
Sys.Reboot(1000);
}
}
/*

View File

@ -1,61 +1,15 @@
#ifndef _IOK0612_H_
#define _IOK0612_H_
#include "Kernel\Sys.h"
#include "Net\ITransport.h"
#include "Net\Socket.h"
#include "TokenNet\TokenClient.h"
#include "App\Alarm.h"
#include "Device\RTC.h"
#include "B8266.h"
// WIFI WRGB调色
class IOK0612
class IOK0612 : public B8266
{
public:
List<Pin> LedPins;
List<OutputPort*> Leds;
List<Pin> ButtonPins;
List<InputPort*> Buttons;
bool LedsShow; // LED 显示状态开关
NetworkInterface* Host; // 网络主机
TokenClient* Client; //
Alarm* AlarmObj;
uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK0612();
void Init(ushort code, cstring name, COM message = COM1);
void* InitData(void* data, int size);
void InitWiFi(cstring ssid,cstring pass);
void Register(int index, IDataPort& dp);
void InitLeds();
void FlushLed(); // 刷新led状态输出
void InitButtons(const Delegate2<InputPort&, bool>& press);
bool LedStat(bool enable);
NetworkInterface* Create8266();
void InitClient();
void InitNet();
void InitAlarm();
void Invoke(const String& ation, const Buffer& bs);
void Restore();
static void OnLongPress(InputPort* port, bool down);
static IOK0612* Current;
private:
void* Data;
int Size;
};
#endif

124
Board/TokenBoard.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "TokenBoard.h"
TokenBoard::TokenBoard()
{
Client = nullptr;
Data = nullptr;
Size = 0;
}
void* TokenBoard::InitData(void* data, int size)
{
// 启动信息
auto hot = &HotConfig::Current();
hot->Times++;
data = hot->Next();
if (hot->Times == 1)
{
Buffer ds(data, size);
ds.Clear();
ds[0] = size;
}
Data = data;
Size = size;
#if DEBUG
debug_printf("数据区%d", hot->Times);
Buffer(Data, Size).Show(true);
#endif
return data;
}
// 写入数据区并上报
void TokenBoard::Write(uint offset, byte data)
{
auto client = Client;
if (!client) return;
client->Store.Write(offset, data);
client->ReportAsync(offset, 1);
}
/******************************** Token ********************************/
void TokenBoard::InitClient()
{
if (Client) return;
// 初始化配置区
InitConfig();
// 初始化令牌网
auto tk = TokenConfig::Create("smart.wslink.cn", NetType::Tcp, 33333, 3377);
// 创建客户端
auto tc = TokenClient::CreateFast(Buffer(Data, Size));
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
tc->UseLocal();
Client = tc;
}
void TokenBoard::Register(uint offset, IDataPort& dp)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(offset, dp);
}
void TokenBoard::Register(uint offset, uint size, Handler hook)
{
if (!Client) return;
auto& ds = Client->Store;
ds.Register(offset, size, hook);
}
void TokenBoard::Invoke(const String& ation, const Buffer& bs)
{
if (!Client) return;
Client->Invoke(ation, bs);
}
void TokenBoard::Restore()
{
if (!Client) return;
if (Client) Client->Reset("按键重置");
}
/*int TokenBoard::GetStatus()
{
if (!Client) return 0;
return Client->Status;
}*/
void TokenBoard::OnLongPress(InputPort* port, bool down)
{
if (down) return;
debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime);
auto client = Client;
if (!client) return;
ushort time = port->PressTime;
if (time >= 5000 && time < 10000)
{
client->Reset("按键重置");
}
else if (time >= 3000)
{
client->Reboot("按键重启");
Sys.Reboot(1000);
}
}

39
Board/TokenBoard.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef _TokenBoard_H_
#define _TokenBoard_H_
#include "BaseBoard.h"
#include "TokenNet\TokenClient.h"
// 令牌协议板级包基类
class TokenBoard : public BaseBoard
{
public:
TokenClient* Client; // 令牌客户端
TokenBoard();
// 设置数据区
void* InitData(void* data, int size);
// 写入数据区并上报
void Write(uint offset, byte data);
// 获取客户端的状态0未握手1已握手2已经登陆
//int GetStatus();
typedef bool(*Handler)(uint offset, uint size, bool write);
void Register(uint offset, uint size, Handler hook);
void Register(uint offset, IDataPort& dp);
// invoke指令
void Invoke(const String& ation, const Buffer& bs);
void OnLongPress(InputPort* port, bool down);
void Restore();
void InitClient();
private:
void* Data;
int Size;
};
#endif

View File

@ -33,6 +33,7 @@
<ClCompile Include="..\Board\AP0801.cpp" />
<ClCompile Include="..\Board\AP0802.cpp" />
<ClCompile Include="..\Board\AP0803.cpp" />
<ClCompile Include="..\Board\B8266.cpp" />
<ClCompile Include="..\Board\BaseBoard.cpp" />
<ClCompile Include="..\Board\IOK026X.cpp" />
<ClCompile Include="..\Board\IOK027X.cpp" />

View File

@ -572,5 +572,8 @@
<ClCompile Include="..\Drivers\A67.cpp">
<Filter>Drivers</Filter>
</ClCompile>
<ClCompile Include="..\Board\B8266.cpp">
<Filter>Board</Filter>
</ClCompile>
</ItemGroup>
</Project>