Webserver only w/o any other functions, single thread. Running on STM32F013+W5500
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
Diff: main.cpp
- Revision:
- 39:083cf93121a9
- Parent:
- 38:f8735ae519aa
- Child:
- 40:c966abbe2d62
diff -r f8735ae519aa -r 083cf93121a9 main.cpp --- a/main.cpp Mon Jun 13 23:06:38 2016 +0000 +++ b/main.cpp Tue Jun 14 21:25:04 2016 +0000 @@ -169,6 +169,39 @@ // Watchdog Watchdog wdt; + +// Some variable types that can be modified through RPC. +//int wheelsOn; +//char lcdBannerMessage; +//float speed; +int current_time; +int do0OnTime, do0OffTime; +int do1OnTime, do1OffTime; + +//RPCVariable<int> rpcLights(&wheelsOn, "wheels"); +//RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner"); +//RPCVariable<float> rpcSpeed(&speed, "speed"); +RPCVariable<int> rpcCurrentTime(¤t_time, "Time"); +RPCVariable<int> rpcdo0OnTime(&do0OnTime, "do0OnTime"); +RPCVariable<int> rpcdo0OffTime(&do0OffTime, "do0OffTime"); +RPCVariable<int> rpcdo1OnTime(&do0OnTime, "do1OnTime"); +RPCVariable<int> rpcdo1OffTime(&do0OffTime, "do1OffTime"); + +// RPC function definitions +// Create a function of the required format +void set_current_time(Arguments* args, Reply* rep); +void set_current_time(Arguments* args, Reply* rep){ + time_t ct = (time_t)current_time; // convert + struct tm *st = localtime(&ct); + + set_time(ct); // set time + + DBG("Set current time to: %s", ctime(&ct)); + DBG("Time only: %d:%d:%d", st->tm_hour, st->tm_min, st->tm_sec); +} +// Attach it to an RPC object +RPCFunction rpcSetCurrentTime(&set_current_time, "SetTime"); + /* * NNIO Protocol */ @@ -268,26 +301,105 @@ wdt.Service(); } - -// These are examples of some variable types that can be modified through RPC. -int wheelsOn; -char lcdBannerMessage; -float speed; - -RPCVariable<int> rpcLights(&wheelsOn, "wheels"); -RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner"); -RPCVariable<float> rpcSpeed(&speed, "speed"); - -/** - * RPC function definitions - */ -// Create a function of the required format -void do_blink(Arguments* args, Reply* rep); -void do_blink(Arguments* args, Reply* rep){ - DBG("RPC function called"); +// Timer thread to check on/off time +void digital_outputs_timer_thread(void const* args) +{ + Thread::wait(700); + + while(true) { + // read current time + time_t seconds = time(NULL); + struct tm *st = localtime(&seconds); + int current_time_in_seconds = 3600*(st->tm_hour) + 60*(st->tm_min) + st->tm_sec; + DBG("Current time: %d:%d:%d", st->tm_hour, st->tm_min, st->tm_sec); + + // check do0 + if (do0OnTime < do0OffTime) { + if ((current_time_in_seconds >= do0OnTime) && (current_time_in_seconds < do0OffTime)){ + if (dout0 == 0) { + dout0 = 1; + DBG("do0 ON"); + } + else { + DBG("do0 ON'ed"); + } + } + else { + if (dout0 == 1) { + dout0 = 0; + DBG("do0 OFF'ed"); + } + else { + DBG("do0 OFF"); + } + } + } + else { + if ((current_time_in_seconds >= do0OffTime) && ((current_time_in_seconds < do0OnTime))) { + if (dout0 == 1) { + dout0 = 0; + DBG("do0 OFF"); + } + else { + DBG("do0 OFF'ed"); + } + } + else { + if (dout0 == 0) { + dout0 = 1; + DBG("do0 ON"); + } + else { + DBG("do0 ON'ed"); + } + } + } + + // check do1 + if (do1OnTime < do1OffTime) { + if ((current_time_in_seconds >= do1OnTime) && (current_time_in_seconds < do1OffTime)){ + if (dout1 == 0) { + dout1 = 1; + DBG("do1 ON"); + } + else { + DBG("do1 ON'ed"); + } + } + else { + if (dout1 == 1) { + dout1 = 0; + DBG("do1 OFF"); + } + else { + DBG("do1 OFF'ed"); + } + } + } + else { + if ((current_time_in_seconds >= do1OffTime) && ((current_time_in_seconds < do1OnTime))) { + if (dout1 == 1) { + dout1 = 0; + DBG("do1 OFF"); + } + else { + DBG("do1 OFF'ed"); + } + } + else { + if (dout1 == 0) { + dout1 = 1; + DBG("do1 ON"); + } + else { + DBG("do1 ON'ed"); + } + } + } + // wait 1s + Thread::wait(10000); // Thread::wait() in ms + } } -// Attach it to an RPC object -RPCFunction blink(&do_blink, "blink"); // Main code int main() @@ -318,6 +430,7 @@ */ Thread t2(auto_update_timer_thread); Thread t3(wdt_reset_thread); + Thread t4(digital_outputs_timer_thread); /* * Ethernet