diff --git a/Project/applications/smartcities/include/timer-loop.h b/Project/applications/smartcities/include/timer-loop.h index 166ebdf..e4e6155 100644 --- a/Project/applications/smartcities/include/timer-loop.h +++ b/Project/applications/smartcities/include/timer-loop.h @@ -7,8 +7,8 @@ #define LONG_PERIOD HALF_HOUR //60*3 // for testing #define SHORT_PERIOD FIVE_MIN //60*1 // for testing -unsigned int getSystemTime(); +unsigned long getSystemTime(); -unsigned int getElapsedTime(unsigned int t); +unsigned long getElapsedTime(unsigned long t); -void sleep_thread(unsigned int seconds); \ No newline at end of file +void sleep_thread(unsigned long seconds); \ No newline at end of file diff --git a/Project/applications/smartcities/test_timer-loop.c b/Project/applications/smartcities/test_timer-loop.c new file mode 100644 index 0000000..d7ca6c7 --- /dev/null +++ b/Project/applications/smartcities/test_timer-loop.c @@ -0,0 +1,97 @@ +#include +#include "timer-loop.h" + +/* 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"); +} + +/* Main de testing */ +void main(void) +{ + // Init wismart + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + + // Code + + int i = 1; + printf(",----------------.\r\n"); + printf("| TIME LOOP TEST |\r\n"); + printf("'----------------'\r\n\r\n"); + + printf("--------\r\n"); + printf("Loop #0\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 diff --git a/Project/applications/smartcities/timer-loop.c b/Project/applications/smartcities/timer-loop.c index 3073389..950d1e8 100644 --- a/Project/applications/smartcities/timer-loop.c +++ b/Project/applications/smartcities/timer-loop.c @@ -8,118 +8,31 @@ void initLibwismart(void) 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. - **************************************************/ +/*************************************************** + ** CATALAN **************************************** + * Agafem el temps des de l'NTP el primer cop. * + * Mostres síncrones amb els altres nodes modsense * + * Cada mitja hora (long period) asíncrona amb els * + * altres nodes, NTP i enviament de mostres. * + ** ENGLISH **************************************** + * Get NTP time the first time. * + * Values sync'd with other modularsense nodes * + * Every half an hour (long period), async'd to * + * other nodes, get NTP and send all data. * + ***************************************************/ +/* + * unsigned int representa: + * - segons: 2^32 -1 = 4.294.967.295 segons // 136.1 average Gregorian years + * - milisegons:ans/1000 = 4.294.967,295 segons // 49 days 17 hours 2 minutes 47.3 seconds + */ +unsigned long getSystemTime() +{ return (long)libwismart_GetTime()/1000; } -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); -} +unsigned long getElapsedTime(unsigned long t) +{ return (long)libwismart_ElapsedTime(t*1000)/1000; } -/* 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() +void sleep_thread(unsigned long seconds) { - 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 = 1; - printf(",----------------.\r\n"); - printf("| TIME LOOP TEST |\r\n"); - printf("'----------------'\r\n\r\n"); - - printf("--------\r\n"); - printf("Loop #0\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++; - } + chThdSleepMilliseconds( (unsigned int)seconds*1000 ); + //sleep(seconds); //for testing in linux machine } \ No newline at end of file