From 34d2e1092f55f02dcb7636e045ae6483ae3bb1cd Mon Sep 17 00:00:00 2001 From: Ferràn Quer i Guerrero Date: Fri, 25 Apr 2014 17:43:07 +0000 Subject: [PATCH] --- Project/applications/smartcities/Makefile-timer | 32 ++++++++++++++++++++++++++++++++ Project/applications/smartcities/timer-loop.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 0 deletions(-) create mode 100644 Project/applications/smartcities/Makefile-timer create mode 100644 Project/applications/smartcities/timer-loop.c diff --git a/Project/applications/smartcities/Makefile-timer b/Project/applications/smartcities/Makefile-timer new file mode 100644 index 0000000..aa12e2d --- /dev/null +++ b/Project/applications/smartcities/Makefile-timer @@ -0,0 +1,32 @@ + +# +# WiSmart Applications Makefile +# +# Copyright 2012 eConais S.A +# + + +# Set the sdk root folder +SDK_ROOT = ../../ +PROJECT_OUT = timer-loop + +USER_SRC = timer-loop.c +USER_INCDIR = include/ + +# if you need to add build Defines options add to USER_DEFS define +#USER_DEFS += -D + +# select SDK's version: +SDK_VERSION = SDK_REV_9_2 + +# select the MCU type e.g. STM32F1, STM32F4 +PLAT = STM32F1 + +# select the MCU flash/ram size e.g. MCU_386K_64K, MCU_768K_96K, MCU_1M_96K +MCU_SIZE = MCU_768K_96K + +USER_DEFS += -D$(SDK_VERSION) + +# this must be the last include +include $(SDK_ROOT)/applications/rules.mk + diff --git a/Project/applications/smartcities/timer-loop.c b/Project/applications/smartcities/timer-loop.c new file mode 100644 index 0000000..f884fa0 --- /dev/null +++ b/Project/applications/smartcities/timer-loop.c @@ -0,0 +1,130 @@ +#include "libwismart.h" +#include "libwismart_irqs.h" +#include "lwip/inet.h" + +#define HALF_HOUR 60*3 +#define FIVE_MIN 60*1 +#define LONG_PERIOD HALF_HOUR +#define SHORT_PERIOD FIVE_MIN + +void initLibwismart(void) +{ + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); + libwismart_Init(hwif); +} + +/************************************************** + * NTP el primer cop. + * Mostres síncrones amb els altres nodes. + * NTP i enviament de mostres + * cada mitja hora asíncrona amb els altres nodes. + **************************************************/ + +unsigned int getSystemTime() +{ + return libwismart_GetTime()/1000; +} +unsigned int getElapsedTime(unsigned int t) +{ + return libwismart_ElapsedTime(t*1000)/1000; +} +void sleep_thread(unsigned int seconds) +{ + chThdSleepMilliseconds(seconds*1000); + //sleep(seconds); +} + +/* Funcions que simulen comportament real */ +void testf(unsigned int delay, const char* premesg, const char* postmesg) +{ + printf("%s\r\n",premesg); + sleep_thread(delay); + printf("%s\r\n",postmesg); +} +unsigned int getNTPTime() +{ + testf(1,"Asking for NTP time...", "NTP time received"); + return getSystemTime(); +} +void collect_data() +{ + testf(5, "Collecting data...", "Data collected"); +} +void store_data() +{ + testf(1, "Storing data...", "Data stored"); +} +void wifi_connect() +{ + testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected"); +} + +void main(void) +{ + + // Init wismart + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + + // Code + + int i = 0; + printf(",----------------.\r\n"); + printf("| TIME LOOP TEST |\r\n"); + printf("'----------------'\r\n\r\n"); + + printf("--------\r\n"); + printf("Loop #-1\r\n\r\n"); + + unsigned int time = getNTPTime(); + unsigned int timestamp = 0; + unsigned int delay = getSystemTime(); + unsigned int delay2 = 0; + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); + + //int* a_rawData; + //char* a_cookedData; + + while(1) + { + printf("\r\n\r\n--------\r\n"); + printf("Loop #%i\r\n\r\n",i); + + time += getElapsedTime(delay); + printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); + + delay = getSystemTime(); + /* Collect data from sensors */ + collect_data(/*a_rawData*/); + time += getElapsedTime(delay); + printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); + + delay = getSystemTime(); + if (i == LONG_PERIOD/SHORT_PERIOD ) + { + /* Wi-Fi connect */ + wifi_connect(); + /* Send data to server, empty the buffer */ + time = getNTPTime(); + printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); + i = 0; + } + delay2 = getElapsedTime(delay); + + delay = getSystemTime(); + timestamp = time - delay2; + printf("timestamp (absolute):\t%d\r\ntimestamp mod LONG_PERIOD:\t%d\r\ntimestamp mod SHORT_PERIOD:\t%d\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD); + + /* Add data to the buffer with timestamp*/ + //a_cookedData = format_data(a_rawData, timestamp); + store_data(/* data */); + + time += getElapsedTime(delay); + printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); + + delay = getSystemTime(); + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); + i++; + } +} \ No newline at end of file -- libgit2 0.22.2