新增设置默认连接WiFi方法,可替换默认WSWL

使用方法:bsp.InitWiFi("18YUAN","12345678");
This commit is contained in:
arlenxiao 2017-03-16 16:02:54 +08:00
parent 56958f8582
commit 839861d38a
6 changed files with 389 additions and 350 deletions

View File

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

View File

@ -23,11 +23,15 @@ public:
Alarm* AlarmObj; Alarm* AlarmObj;
uint LedsTaskId; uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK026X(); IOK026X();
void Init(ushort code, cstring name, COM message = COM1); void Init(ushort code, cstring name, COM message = COM1);
void* InitData(void* data, int size); void* InitData(void* data, int size);
void InitWiFi(cstring ssid, cstring pass);
void Register(int index, IDataPort& dp); void Register(int index, IDataPort& dp);
void InitLeds(); void InitLeds();

View File

@ -25,6 +25,9 @@ IOK027X::IOK027X()
Data = nullptr; Data = nullptr;
Size = 0; Size = 0;
Current = this; Current = this;
SSID = "WSWL";
Pass = "12345678";
} }
void IOK027X::Init(ushort code, cstring name, COM message) void IOK027X::Init(ushort code, cstring name, COM message)
@ -81,6 +84,12 @@ void* IOK027X::InitData(void* data, int size)
return data; return data;
} }
void IOK027X::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
void IOK027X::InitLeds() void IOK027X::InitLeds()
{ {
for (int i = 0; i < LedPins.Count(); i++) for (int i = 0; i < LedPins.Count(); i++)
@ -105,8 +114,8 @@ NetworkInterface* IOK027X::Create8266(Pin power)
if (!join) if (!join)
{ {
*esp->SSID = "WSWL"; *esp->SSID = SSID;
*esp->Pass = "12345678"; *esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP; esp->Mode = NetworkType::STA_AP;
} }

View File

@ -24,11 +24,15 @@ public:
Alarm* AlarmObj; Alarm* AlarmObj;
uint LedsTaskId; uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK027X(); IOK027X();
void Init(ushort code, cstring name, COM message = COM1); void Init(ushort code, cstring name, COM message = COM1);
void* InitData(void* data, int size); void* InitData(void* data, int size);
void InitWiFi(cstring ssid, cstring pass);
void Register(int index, IDataPort& dp); void Register(int index, IDataPort& dp);
void SetRestore(Pin pin = PB4); //设置重置引脚 void SetRestore(Pin pin = PB4); //设置重置引脚
void InitLeds(); void InitLeds();

View File

@ -27,6 +27,9 @@ IOK0612::IOK0612()
Data = nullptr; Data = nullptr;
Size = 0; Size = 0;
Current = this; Current = this;
SSID = "WSWL";
Pass = "12345678";
} }
void IOK0612::Init(ushort code, cstring name, COM message) void IOK0612::Init(ushort code, cstring name, COM message)
@ -83,6 +86,12 @@ void* IOK0612::InitData(void* data, int size)
return data; return data;
} }
void IOK0612::InitWiFi(cstring ssid,cstring pass)
{
SSID = ssid;
Pass = pass;
}
void IOK0612::InitLeds() void IOK0612::InitLeds()
{ {
for(int i=0; i<LedPins.Count(); i++) for(int i=0; i<LedPins.Count(); i++)
@ -128,8 +137,8 @@ NetworkInterface* IOK0612::Create8266()
if (!join) if (!join)
{ {
*esp->SSID = "WSWL"; *esp->SSID = SSID;
*esp->Pass = "12345678"; *esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP; esp->Mode = NetworkType::STA_AP;
} }

View File

@ -25,11 +25,15 @@ public:
Alarm* AlarmObj; Alarm* AlarmObj;
uint LedsTaskId; uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK0612(); IOK0612();
void Init(ushort code, cstring name, COM message = COM1); void Init(ushort code, cstring name, COM message = COM1);
void* InitData(void* data, int size); void* InitData(void* data, int size);
void InitWiFi(cstring ssid,cstring pass);
void Register(int index, IDataPort& dp); void Register(int index, IDataPort& dp);
void InitLeds(); void InitLeds();