Commit 34d2e1092f55f02dcb7636e045ae6483ae3bb1cd

Authored by Ferràn Quer i Guerrero
1 parent 34b0e052

--no commit message

Project/applications/smartcities/Makefile-timer 0 → 100644
  1 +
  2 +#
  3 +# WiSmart Applications Makefile
  4 +#
  5 +# Copyright 2012 eConais S.A
  6 +#
  7 +
  8 +
  9 +# Set the sdk root folder
  10 +SDK_ROOT = ../../
  11 +PROJECT_OUT = timer-loop
  12 +
  13 +USER_SRC = timer-loop.c
  14 +USER_INCDIR = include/
  15 +
  16 +# if you need to add build Defines options add to USER_DEFS define
  17 +#USER_DEFS += -D
  18 +
  19 +# select SDK's version:
  20 +SDK_VERSION = SDK_REV_9_2
  21 +
  22 +# select the MCU type e.g. STM32F1, STM32F4
  23 +PLAT = STM32F1
  24 +
  25 +# select the MCU flash/ram size e.g. MCU_386K_64K, MCU_768K_96K, MCU_1M_96K
  26 +MCU_SIZE = MCU_768K_96K
  27 +
  28 +USER_DEFS += -D$(SDK_VERSION)
  29 +
  30 +# this must be the last include
  31 +include $(SDK_ROOT)/applications/rules.mk
  32 +
Project/applications/smartcities/timer-loop.c 0 → 100644
  1 +#include "libwismart.h"
  2 +#include "libwismart_irqs.h"
  3 +#include "lwip/inet.h"
  4 +
  5 +#define HALF_HOUR 60*3
  6 +#define FIVE_MIN 60*1
  7 +#define LONG_PERIOD HALF_HOUR
  8 +#define SHORT_PERIOD FIVE_MIN
  9 +
  10 +void initLibwismart(void)
  11 +{
  12 + wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
  13 + libwismart_Init(hwif);
  14 +}
  15 +
  16 +/**************************************************
  17 + * NTP el primer cop.
  18 + * Mostres síncrones amb els altres nodes.
  19 + * NTP i enviament de mostres
  20 + * cada mitja hora asíncrona amb els altres nodes.
  21 + **************************************************/
  22 +
  23 +unsigned int getSystemTime()
  24 +{
  25 + return libwismart_GetTime()/1000;
  26 +}
  27 +unsigned int getElapsedTime(unsigned int t)
  28 +{
  29 + return libwismart_ElapsedTime(t*1000)/1000;
  30 +}
  31 +void sleep_thread(unsigned int seconds)
  32 +{
  33 + chThdSleepMilliseconds(seconds*1000);
  34 + //sleep(seconds);
  35 +}
  36 +
  37 +/* Funcions que simulen comportament real */
  38 +void testf(unsigned int delay, const char* premesg, const char* postmesg)
  39 +{
  40 + printf("%s\r\n",premesg);
  41 + sleep_thread(delay);
  42 + printf("%s\r\n",postmesg);
  43 +}
  44 +unsigned int getNTPTime()
  45 +{
  46 + testf(1,"Asking for NTP time...", "NTP time received");
  47 + return getSystemTime();
  48 +}
  49 +void collect_data()
  50 +{
  51 + testf(5, "Collecting data...", "Data collected");
  52 +}
  53 +void store_data()
  54 +{
  55 + testf(1, "Storing data...", "Data stored");
  56 +}
  57 +void wifi_connect()
  58 +{
  59 + testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected");
  60 +}
  61 +
  62 +void main(void)
  63 +{
  64 +
  65 + // Init wismart
  66 + initLibwismart();
  67 + libwismart_PowerSave_Enable();
  68 + libwismart_PowerSave_HigherProfile(TRUE);
  69 +
  70 + // Code
  71 +
  72 + int i = 0;
  73 + printf(",----------------.\r\n");
  74 + printf("| TIME LOOP TEST |\r\n");
  75 + printf("'----------------'\r\n\r\n");
  76 +
  77 + printf("--------\r\n");
  78 + printf("Loop #-1\r\n\r\n");
  79 +
  80 + unsigned int time = getNTPTime();
  81 + unsigned int timestamp = 0;
  82 + unsigned int delay = getSystemTime();
  83 + unsigned int delay2 = 0;
  84 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  85 +
  86 + //int* a_rawData;
  87 + //char* a_cookedData;
  88 +
  89 + while(1)
  90 + {
  91 + printf("\r\n\r\n--------\r\n");
  92 + printf("Loop #%i\r\n\r\n",i);
  93 +
  94 + time += getElapsedTime(delay);
  95 + 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);
  96 +
  97 + delay = getSystemTime();
  98 + /* Collect data from sensors */
  99 + collect_data(/*a_rawData*/);
  100 + time += getElapsedTime(delay);
  101 + 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);
  102 +
  103 + delay = getSystemTime();
  104 + if (i == LONG_PERIOD/SHORT_PERIOD )
  105 + {
  106 + /* Wi-Fi connect */
  107 + wifi_connect();
  108 + /* Send data to server, empty the buffer */
  109 + time = getNTPTime();
  110 + 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);
  111 + i = 0;
  112 + }
  113 + delay2 = getElapsedTime(delay);
  114 +
  115 + delay = getSystemTime();
  116 + timestamp = time - delay2;
  117 + 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);
  118 +
  119 + /* Add data to the buffer with timestamp*/
  120 + //a_cookedData = format_data(a_rawData, timestamp);
  121 + store_data(/* data */);
  122 +
  123 + time += getElapsedTime(delay);
  124 + 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);
  125 +
  126 + delay = getSystemTime();
  127 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  128 + i++;
  129 + }
  130 +}
0 \ No newline at end of file 131 \ No newline at end of file