Commit 672f41a6d8008288a8b81cde68a54d2b4be331e6

Authored by Ferràn Quer i Guerrero
1 parent c23a8c45

--no commit message

Project/applications/smartcities/include/timer-loop.h
... ... @@ -7,8 +7,8 @@
7 7 #define LONG_PERIOD HALF_HOUR //60*3 // for testing
8 8 #define SHORT_PERIOD FIVE_MIN //60*1 // for testing
9 9  
10   -unsigned int getSystemTime();
  10 +unsigned long getSystemTime();
11 11  
12   -unsigned int getElapsedTime(unsigned int t);
  12 +unsigned long getElapsedTime(unsigned long t);
13 13  
14   -void sleep_thread(unsigned int seconds);
15 14 \ No newline at end of file
  15 +void sleep_thread(unsigned long seconds);
16 16 \ No newline at end of file
... ...
Project/applications/smartcities/test_timer-loop.c 0 → 100644
  1 +#include <stdio.h>
  2 +#include "timer-loop.h"
  3 +
  4 +/* Funcions que simulen comportament real */
  5 +void testf(unsigned int delay, const char* premesg, const char* postmesg)
  6 +{
  7 + printf("%s\r\n",premesg);
  8 + sleep_thread(delay);
  9 + printf("%s\r\n",postmesg);
  10 +}
  11 +unsigned int getNTPTime()
  12 +{
  13 + testf(1,"Asking for NTP time...", "NTP time received");
  14 + return getSystemTime();
  15 +}
  16 +void collect_data()
  17 +{
  18 + testf(5, "Collecting data...", "Data collected");
  19 +}
  20 +void store_data()
  21 +{
  22 + testf(1, "Storing data...", "Data stored");
  23 +}
  24 +void wifi_connect()
  25 +{
  26 + testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected");
  27 +}
  28 +
  29 +/* Main de testing */
  30 +void main(void)
  31 +{
  32 + // Init wismart
  33 + initLibwismart();
  34 + libwismart_PowerSave_Enable();
  35 + libwismart_PowerSave_HigherProfile(TRUE);
  36 +
  37 + // Code
  38 +
  39 + int i = 1;
  40 + printf(",----------------.\r\n");
  41 + printf("| TIME LOOP TEST |\r\n");
  42 + printf("'----------------'\r\n\r\n");
  43 +
  44 + printf("--------\r\n");
  45 + printf("Loop #0\r\n\r\n");
  46 +
  47 + unsigned int time = getNTPTime();
  48 + unsigned int timestamp = 0;
  49 + unsigned int delay = getSystemTime();
  50 + unsigned int delay2 = 0;
  51 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  52 +
  53 + //int* a_rawData;
  54 + //char* a_cookedData;
  55 +
  56 + while(1)
  57 + {
  58 + printf("\r\n\r\n--------\r\n");
  59 + printf("Loop #%i\r\n\r\n",i);
  60 +
  61 + time += getElapsedTime(delay);
  62 + 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);
  63 +
  64 + delay = getSystemTime();
  65 + /* Collect data from sensors */
  66 + collect_data(/*a_rawData*/);
  67 + time += getElapsedTime(delay);
  68 + 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);
  69 +
  70 + delay = getSystemTime();
  71 + if (i == LONG_PERIOD/SHORT_PERIOD )
  72 + {
  73 + /* Wi-Fi connect */
  74 + wifi_connect();
  75 + /* Send data to server, empty the buffer */
  76 + time = getNTPTime();
  77 + 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);
  78 + i = 0;
  79 + }
  80 + delay2 = getElapsedTime(delay);
  81 +
  82 + delay = getSystemTime();
  83 + timestamp = time - delay2;
  84 + 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);
  85 +
  86 + /* Add data to the buffer with timestamp*/
  87 + //a_cookedData = format_data(a_rawData, timestamp);
  88 + store_data(/* data */);
  89 +
  90 + time += getElapsedTime(delay);
  91 + 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);
  92 +
  93 + delay = getSystemTime();
  94 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  95 + i++;
  96 + }
  97 +}
0 98 \ No newline at end of file
... ...
Project/applications/smartcities/timer-loop.c
... ... @@ -8,118 +8,31 @@ void initLibwismart(void)
8 8 libwismart_Init(hwif);
9 9 }
10 10  
11   -/**************************************************
12   - * NTP el primer cop.
13   - * Mostres síncrones amb els altres nodes.
14   - * NTP i enviament de mostres
15   - * cada mitja hora asíncrona amb els altres nodes.
16   - **************************************************/
  11 +/***************************************************
  12 + ** CATALAN ****************************************
  13 + * Agafem el temps des de l'NTP el primer cop. *
  14 + * Mostres síncrones amb els altres nodes modsense *
  15 + * Cada mitja hora (long period) asíncrona amb els *
  16 + * altres nodes, NTP i enviament de mostres. *
  17 + ** ENGLISH ****************************************
  18 + * Get NTP time the first time. *
  19 + * Values sync'd with other modularsense nodes *
  20 + * Every half an hour (long period), async'd to *
  21 + * other nodes, get NTP and send all data. *
  22 + ***************************************************/
  23 +/*
  24 + * unsigned int representa:
  25 + * - segons: 2^32 -1 = 4.294.967.295 segons // 136.1 average Gregorian years
  26 + * - milisegons:ans/1000 = 4.294.967,295 segons // 49 days 17 hours 2 minutes 47.3 seconds
  27 + */
  28 +unsigned long getSystemTime()
  29 +{ return (long)libwismart_GetTime()/1000; }
17 30  
18   -unsigned int getSystemTime()
19   -{
20   - return libwismart_GetTime()/1000;
21   -}
22   -unsigned int getElapsedTime(unsigned int t)
23   -{
24   - return libwismart_ElapsedTime(t*1000)/1000;
25   -}
26   -void sleep_thread(unsigned int seconds)
27   -{
28   - chThdSleepMilliseconds(seconds*1000);
29   - //sleep(seconds);
30   -}
  31 +unsigned long getElapsedTime(unsigned long t)
  32 +{ return (long)libwismart_ElapsedTime(t*1000)/1000; }
31 33  
32   -/* Funcions que simulen comportament real */
33   -void testf(unsigned int delay, const char* premesg, const char* postmesg)
34   -{
35   - printf("%s\r\n",premesg);
36   - sleep_thread(delay);
37   - printf("%s\r\n",postmesg);
38   -}
39   -unsigned int getNTPTime()
40   -{
41   - testf(1,"Asking for NTP time...", "NTP time received");
42   - return getSystemTime();
43   -}
44   -void collect_data()
  34 +void sleep_thread(unsigned long seconds)
45 35 {
46   - testf(5, "Collecting data...", "Data collected");
47   -}
48   -void store_data()
49   -{
50   - testf(1, "Storing data...", "Data stored");
51   -}
52   -void wifi_connect()
53   -{
54   - testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected");
55   -}
56   -
57   -void main(void)
58   -{
59   -
60   - // Init wismart
61   - initLibwismart();
62   - libwismart_PowerSave_Enable();
63   - libwismart_PowerSave_HigherProfile(TRUE);
64   -
65   - // Code
66   -
67   - int i = 1;
68   - printf(",----------------.\r\n");
69   - printf("| TIME LOOP TEST |\r\n");
70   - printf("'----------------'\r\n\r\n");
71   -
72   - printf("--------\r\n");
73   - printf("Loop #0\r\n\r\n");
74   -
75   - unsigned int time = getNTPTime();
76   - unsigned int timestamp = 0;
77   - unsigned int delay = getSystemTime();
78   - unsigned int delay2 = 0;
79   - sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
80   -
81   - //int* a_rawData;
82   - //char* a_cookedData;
83   -
84   - while(1)
85   - {
86   - printf("\r\n\r\n--------\r\n");
87   - printf("Loop #%i\r\n\r\n",i);
88   -
89   - time += getElapsedTime(delay);
90   - 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);
91   -
92   - delay = getSystemTime();
93   - /* Collect data from sensors */
94   - collect_data(/*a_rawData*/);
95   - time += getElapsedTime(delay);
96   - 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);
97   -
98   - delay = getSystemTime();
99   - if (i == LONG_PERIOD/SHORT_PERIOD )
100   - {
101   - /* Wi-Fi connect */
102   - wifi_connect();
103   - /* Send data to server, empty the buffer */
104   - time = getNTPTime();
105   - 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);
106   - i = 0;
107   - }
108   - delay2 = getElapsedTime(delay);
109   -
110   - delay = getSystemTime();
111   - timestamp = time - delay2;
112   - 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);
113   -
114   - /* Add data to the buffer with timestamp*/
115   - //a_cookedData = format_data(a_rawData, timestamp);
116   - store_data(/* data */);
117   -
118   - time += getElapsedTime(delay);
119   - 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);
120   -
121   - delay = getSystemTime();
122   - sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
123   - i++;
124   - }
  36 + chThdSleepMilliseconds( (unsigned int)seconds*1000 );
  37 + //sleep(seconds); //for testing in linux machine
125 38 }
126 39 \ No newline at end of file
... ...