diff --git a/Board/AP0801.cpp b/Board/AP0801.cpp index 59f9239b..1b651d96 100644 --- a/Board/AP0801.cpp +++ b/Board/AP0801.cpp @@ -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& 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 diff --git a/Board/AP0801.h b/Board/AP0801.h index 0684c077..cee3577a 100644 --- a/Board/AP0801.h +++ b/Board/AP0801.h @@ -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 LedPins; - List ButtonPins; - List Leds; - List Buttons; - List Outputs; List 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& 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 diff --git a/Board/AP0802.cpp b/Board/AP0802.cpp index daa60841..5650867d 100644 --- a/Board/AP0802.cpp +++ b/Board/AP0802.cpp @@ -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 diff --git a/Board/B8266.cpp b/Board/B8266.cpp new file mode 100644 index 00000000..36739083 --- /dev/null +++ b/Board/B8266.cpp @@ -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 +*/ diff --git a/Board/B8266.h b/Board/B8266.h new file mode 100644 index 00000000..fef85991 --- /dev/null +++ b/Board/B8266.h @@ -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 diff --git a/Board/BaseBoard.cpp b/Board/BaseBoard.cpp index f688d7dd..04f811b5 100644 --- a/Board/BaseBoard.cpp +++ b/Board/BaseBoard.cpp @@ -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& 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); + } +} diff --git a/Board/BaseBoard.h b/Board/BaseBoard.h index c6a8a5ab..bb80c31e 100644 --- a/Board/BaseBoard.h +++ b/Board/BaseBoard.h @@ -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 LedPins; // 指示灯 引脚 + List ButtonPins; // 按键 引脚 + List Leds; // 指示灯 + List 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& press); }; #endif diff --git a/Board/IOK027X.cpp b/Board/IOK027X.cpp index 400c32bd..66fe4736 100644 --- a/Board/IOK027X.cpp +++ b/Board/IOK027X.cpp @@ -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(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(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 diff --git a/Board/IOK027X.h b/Board/IOK027X.h index bb5bf01a..6c4ac49a 100644 --- a/Board/IOK027X.h +++ b/Board/IOK027X.h @@ -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 LedPins; - List 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 diff --git a/Board/IOK0612.cpp b/Board/IOK0612.cpp index 9abfbe17..cc3473f1 100644 --- a/Board/IOK0612.cpp +++ b/Board/IOK0612.cpp @@ -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; iInvert = 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& 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(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); - } } /* diff --git a/Board/IOK0612.h b/Board/IOK0612.h index 533a73db..29d4b6ce 100644 --- a/Board/IOK0612.h +++ b/Board/IOK0612.h @@ -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 LedPins; - List Leds; - List ButtonPins; - List 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& 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 diff --git a/Board/TokenBoard.cpp b/Board/TokenBoard.cpp new file mode 100644 index 00000000..9440ea86 --- /dev/null +++ b/Board/TokenBoard.cpp @@ -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); + } +} diff --git a/Board/TokenBoard.h b/Board/TokenBoard.h new file mode 100644 index 00000000..9a6717d4 --- /dev/null +++ b/Board/TokenBoard.h @@ -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 diff --git a/vs/SmartOS.vcxproj b/vs/SmartOS.vcxproj index b11756d4..e23ccfa6 100644 --- a/vs/SmartOS.vcxproj +++ b/vs/SmartOS.vcxproj @@ -33,6 +33,7 @@ + diff --git a/vs/SmartOS.vcxproj.filters b/vs/SmartOS.vcxproj.filters index b3847f07..8c78eb90 100644 --- a/vs/SmartOS.vcxproj.filters +++ b/vs/SmartOS.vcxproj.filters @@ -572,5 +572,8 @@ Drivers + + Board + \ No newline at end of file