From 9313633c6b93b4e45502e520d7a96c2037edea64 Mon Sep 17 00:00:00 2001 From: nnhy Date: Wed, 28 Oct 2015 02:02:58 +0000 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E9=92=88=E5=AF=B9=E5=BE=AE?= =?UTF-8?q?=E7=BD=91=E7=9A=84=E5=BF=AB=E9=80=9F=E8=BE=85=E5=8A=A9=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E4=B8=89=E4=BD=8D=E8=A7=A6=E6=91=B8=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinyNet/Tiny.cpp | 131 +++++++++++++++++++++++++++++++++++++++++ TinyNet/Tiny.h | 20 +++++++ TinyNet/TinyClient.cpp | 2 + TinyNet/TinyClient.h | 2 + TinyNet/TinyConfig.cpp | 2 + TinyNet/TinyConfig.h | 4 +- 6 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 TinyNet/Tiny.cpp create mode 100644 TinyNet/Tiny.h diff --git a/TinyNet/Tiny.cpp b/TinyNet/Tiny.cpp new file mode 100644 index 00000000..8dbca450 --- /dev/null +++ b/TinyNet/Tiny.cpp @@ -0,0 +1,131 @@ +#include "Tiny.h" + +#include "SerialPort.h" +#include "WatchDog.h" +#include "Config.h" + +#include "Drivers\NRF24L01.h" +#include "Drivers\ShunCom.h" +#include "Net\Zigbee.h" +#include "TinyNet\TinyClient.h" + +uint OnSerial(ITransport* transport, ByteArray& bs, void* param, void* param2) +{ + debug_printf("OnSerial len=%d \t", bs.Length()); + bs.Show(true); + + TinyClient* client = TinyClient::Current; + if(client) client->Store.Write(1, bs); + + return 0; +} + +void Setup(ushort code, const char* name, COM_Def message, int baudRate) +{ + Sys.Code = code; + Sys.Name = (char*)name; + + // 初始化系统 + //Sys.Clock = 48000000; + Sys.MessagePort = message; // 指定printf输出的串口 + Sys.Init(); + Sys.ShowInfo(); + +#if DEBUG + // 打开串口输入便于调试数据操作,但是会影响性能 + SerialPort* sp = SerialPort::GetMessagePort(); + if(baudRate != 1024000) + { + sp->Close(); + sp->SetBaudRate(baudRate); + sp->Register(OnSerial); + sp->Open(); + } + + WatchDog::Start(20000); +#else + WatchDog::Start(); +#endif +} + +ITransport* Create2401(SPI_TypeDef* spi_, Pin ce, Pin irq, Pin power, bool powerInvert) +{ + static Spi spi(spi_, 10000000, true); + static NRF24L01 nrf; + nrf.Init(&spi, ce, irq, power); + nrf.Power.Invert = powerInvert; + + nrf.AutoAnswer = false; + nrf.PayloadWidth= 32; + nrf.Channel = TinyConfig::Current->Channel; + nrf.Speed = TinyConfig::Current->Speed; + + //if(!nrf.Check()) debug_printf("请检查NRF24L01线路\r\n"); + + return &nrf; +} + +ITransport* CreateShunCom(COM_Def index, int baudRate, Pin rst, Pin power, Pin slp, Pin cfg) +{ + static SerialPort sp(index, baudRate); + static ShunCom zb; + zb.Power.Init(power, TinyConfig::Current->HardVer < 0x08); + zb.Sleep.Init(slp, true); + zb.Config.Init(cfg, true); + zb.Init(&sp, rst); + + return &zb; +} + +TinyClient* CreateTinyClient(ITransport* port) +{ + static TinyController ctrl; + ctrl.Port = port; + static TinyClient tc(&ctrl); + tc.Cfg = TinyConfig::Current; + + TinyClient::Current = &tc; + + //ctrl.Mode = 3; + //ctrl.Open(); + + return &tc; +} + +void* InitConfig(void* data, uint size) +{ + // Flash最后一块作为配置区 + Config::Current = &Config::CreateFlash(); + + // 启动信息 + HotConfig* hot = &HotConfig::Current(); + hot->Times++; + + data = hot->Next(); + if(hot->Times == 1) + { + memset(data, 0x00, size); + ((byte*)data)[0] = size; + } + + // 默认出厂设置 + static TinyConfig tc; + TinyConfig::Current = &tc; + tc.LoadDefault(); + tc.Channel = 120; + tc.Speed = 250; + tc.HardVer = 0x08; + + // 尝试加载配置区设置 + tc.Load(); + + return data; +} + +/*void NoUsed() +{ + Setup(1234, ""); + Create2401(SPI1, P0, P0); + CreateShunCom(COM2, 38400, P0, P0, P0, P0); + CreateTinyClient(NULL); +}*/ diff --git a/TinyNet/Tiny.h b/TinyNet/Tiny.h new file mode 100644 index 00000000..071b0088 --- /dev/null +++ b/TinyNet/Tiny.h @@ -0,0 +1,20 @@ +#ifndef _Tiny_H_ +#define _Tiny_H_ + +#include "Sys.h" +#include "Net\ITransport.h" + +#include "TinyNet\TinyClient.h" + +void Setup(ushort code, const char* name, COM_Def message = COM1, int baudRate = 1024000); + +void* InitConfig(void* data, uint size); + +ITransport* Create2401(SPI_TypeDef* spi_, Pin ce, Pin irq, Pin power = P0, bool powerInvert = false); +ITransport* CreateShunCom(COM_Def index, int baudRate, Pin rst, Pin power, Pin slp, Pin cfg); + +TinyClient* CreateTinyClient(ITransport* port); + +//void NoUsed(); + +#endif diff --git a/TinyNet/TinyClient.cpp b/TinyNet/TinyClient.cpp index 836c48b9..7be376ac 100644 --- a/TinyNet/TinyClient.cpp +++ b/TinyNet/TinyClient.cpp @@ -5,6 +5,8 @@ #include "JoinMessage.h" +TinyClient* TinyClient::Current = NULL; + static bool OnClientReceived(Message& msg, void* param); static void TinyClientTask(void* param); diff --git a/TinyNet/TinyClient.h b/TinyNet/TinyClient.h index 08b0bcf9..d1389b10 100644 --- a/TinyNet/TinyClient.h +++ b/TinyNet/TinyClient.h @@ -39,6 +39,8 @@ public: MessageHandler Received; void* Param; + static TinyClient* Current; + // 数据区 public: DataStore Store; // 数据存储区 diff --git a/TinyNet/TinyConfig.cpp b/TinyNet/TinyConfig.cpp index 7b850e34..d59d5410 100644 --- a/TinyNet/TinyConfig.cpp +++ b/TinyNet/TinyConfig.cpp @@ -1,6 +1,8 @@ #include "TinyConfig.h" #include "Config.h" +TinyConfig* TinyConfig::Current = NULL; + // 初始化 /*TinyConfig::TinyConfig() { diff --git a/TinyNet/TinyConfig.h b/TinyNet/TinyConfig.h index c63c0630..d02691e1 100644 --- a/TinyNet/TinyConfig.h +++ b/TinyNet/TinyConfig.h @@ -34,13 +34,15 @@ struct TinyConfig //TinyConfig(); //const TinyConfig& Default(); void LoadDefault(); - + void Load(); void Save(); // 序列化到消息数据流 void Write(Stream& ms) const; void Read(Stream& ms); + + static TinyConfig* Current; }; #pragma pack(pop) // 恢复对齐状态