mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/

Dependencies:   EthernetNetIf SDHCFileSystem I2CLEDDisp Agentbed NTPClient_NetServices mbed BMP085 HTTPClient ConfigFile I2CLCD

Revision:
18:9286e5010c14
Parent:
17:7c9d111a5d45
Child:
19:69b77f9e0446
--- a/main.cpp	Fri Apr 08 15:59:29 2011 +0000
+++ b/main.cpp	Tue May 31 15:40:14 2011 +0000
@@ -8,7 +8,7 @@
  * @brief mbed Weather Platform
  */
 
-#define VERSION "mbed Weather Platform 0.2.1 (C) 2011 Suga koubou Co.,Ltd."
+#define VERSION "mbed Weather Platform 0.3.0 (C) 2011 Suga koubou Co.,Ltd."
 
 //#define NONBLOCKING // ethernet function non-bloking
  
@@ -22,18 +22,20 @@
 #include "NTPClient.h"
 #include "HTTPClient.h"
 #include "Agentbed.h"
-#include "conf.h"
+#include "weather.h"
 #include "I2CLEDDisp.h"
 #include "I2CLCD.h"
 
 Serial pc(USBTX, USBRX), xbee(p13, p14);
-int seq = 0;
+volatile int seq = 0;
 char filename[20];
+char csv[100];
 struct Config conf;
 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
-LocalFileSystem local("local");
-SDFileSystem sd(p5, p6, p7, p8, "sd"); 
+LocalFileSystem *local;
+SDFileSystem *sd; 
 MSCFileSystem *usb;
+DigitalIn sd_cd(p21);
 
 // Sensors
 struct Sensor sensor, offset, sensor_old;
@@ -44,13 +46,14 @@
 AnalogIn ailight(p16), aiuv(p17);
 AnalogIn *aimoist;
 InterruptIn *intcounter;
-int count_counter;
-unsigned long lastCountTime;
+volatile int count_counter;
+volatile unsigned long lastCountTime;
 
+DigitalIn swin2(p19);
 DigitalOut swout1(p29), swout2(p30);
 I2CLEDDisp *leddisp;
 I2CLCD *lcd;
-int ledpos, ledflg;
+volatile int ledpos, ledflg;
 char ledbuf[200];
 Ticker ticker;
 
@@ -61,17 +64,6 @@
 DigitalOut led_g2(p24),led_g(p25), led_y(p26);
 DigitalIn eth_link(P1_25), eth_speed(P1_26);
 
-extern AgentbedClass Agentbed;
-extern uint32_t locUpTime;
-
-void writefile (char *);
-void pachube (char *);
-char *fmtstr (char *, char *, int);
-void twitter (int);
-void weatherstations ();
-void ntpdate ();
-int check_action (char);
-void pduReceived ();
 
 float get_photo (AnalogIn &ain) {
     float f;
@@ -106,8 +98,10 @@
     float t;
 
     if (flg) {
+        // count
         t = (float)count_counter;
     } else {
+        // count/min
         if (locUpTime > lastCountTime) {
             t = (float)(locUpTime - lastCountTime) / 6000.0;
         } else {
@@ -120,10 +114,15 @@
     return t;
 }
 
+// Interrupt 10Hz
+void isr_timer () {
+    int i;
+    static int ledfreq = 0;
 
-void ledscroll () {
-
+    // LED Display scroll
     if (conf.leddisp_mesg[0]) {
+      ledfreq ++;
+      if (ledfreq > LED_FREQ) {
         int i, addr, len;
         
         len = strlen(ledbuf);
@@ -140,53 +139,33 @@
         if (ledpos >= len + 4) {
             ledpos = -4;
         }
+
+        ledfreq = 0;
+      }
     }
 
-    locUpTime = locUpTime + 40; // 0.4s
-    led_y = 1;
-}
-
-void action (char *buf) {
-    int i;
-    
-    i = check_action('A');
-    if (i) {
-        swout1 = (i == ' ' ? 1 : i - '0');
-        led3 = swout1;
-    }
-    i = check_action('B');
-    if (i) {
-        swout2 = (i == ' ' ? 1 : i - '0');
-        led4 = swout2;
+    // timer
+    for (i = 0; i < TIMER_NUM; i ++) {
+        if (sensor.timer_flg[i] && sensor.timer_cnt[i]) sensor.timer_cnt[i] --;
     }
-    if (check_action('P')) {
-        if (conf.ipaddr[0] && conf.pachube_apikey[0] && conf.pachube_feedid[0]) {
-            pachube(buf);
-        }
-    }
-    i = check_action('T');
-    if (i) {
-        if (conf.ipaddr[0] && conf.twitter_user[0] && conf.twitter_pwd[0]) {
-            twitter(i == ' ' ? 0 : i - '0');
-        }
-    }
-    if (check_action('S')) {
-        if (conf.ipaddr[0] && conf.stations_id[0] && conf.stations_pin[0]) {
-            weatherstations();
-        }
-    }
-    if (check_action('X')) {
-        xbee.printf(buf);
-    }
-    sensor_old = sensor;
+    // uptime
+    locUpTime = locUpTime + 10;
+    led_y = 1;
 }
 
 void init () {
     FILE *fp;
 
-    if (config("/sd/" CONFIG_FILE) == -1) {
+    // load config
+    sd = new SDFileSystem(p5, p6, p7, p8, "sd");
+    if (config("/sd/" CONFIG_FILE) == 0) {
+        load_action("/sd/" ACTION_FILE);
+    } else {
+        delete sd;
+        local = new LocalFileSystem("local");
         pc.printf("local strage\r\n", conf.interval);
         config("/local/" CONFIG_FILE);
+        load_action("/local/" ACTION_FILE);
     }
 
     pc.printf("\r\nInterval: %d sec\r\n", conf.interval);
@@ -253,7 +232,7 @@
 
     if (conf.filetype) {
         // seq num
-        
+
         if (conf.filetype == 1) {
             strcpy(filename, "/sd");
         } else
@@ -262,7 +241,6 @@
             strcpy(filename, "/usb");
         }
         strcat(filename, "/weather.seq");
-
         // load
         fp = fopen(filename, "r");
         if (fp) {
@@ -270,7 +248,8 @@
             fclose(fp);
         }
         seq ++;
-        // save
+
+        // save CSV
         fp = fopen(filename, "w");
         if (fp) {
             fprintf(fp, "%d", seq);
@@ -289,6 +268,7 @@
 
     pc.printf("Actions: %d\r\n", conf.actionscount);
 
+    // moist sensor or counter
     if (conf.inputtype & INPUT_FALL) {
         intcounter = new InterruptIn(p18);
         intcounter->fall(&int_counter);
@@ -307,7 +287,6 @@
 int main () {
     Timer timer;
     time_t sec;
-    char buf[100];
     
     swout1 = 0;
     swout2 = 0;
@@ -315,15 +294,18 @@
     ledpos = -4;
     ledflg = 0;
     strcpy(ledbuf, VERSION);
-    ticker.attach(&ledscroll, 0.4);
+
+    ticker.attach(&isr_timer, 0.1); // Interrupt 10Hz
     init();
     pc.printf("%s\r\n\r\n", VERSION);
 
     if (conf.filetype) {
-        strcpy(buf, "date,pres(hPa),temp(`C),humi(%%),anemo(m/s),vane(`),rain(mm),light(lx),uv(mW/cm2),moist(kohm),\r\n");
-        writefile(buf);
+        // CSV header
+        strcpy(csv, "date,pres(hPa),temp(`C),humi(%%),anemo(m/s),vane(`),rain(mm),light(lx),uv(mW/cm2),moist(kohm),\r\n");
+        writefile(csv);
     }
-    
+
+    // I2C LED Display (print version)    
     if (conf.leddisp_mesg[0]) {
         ledpos = -4;
         sec = time(NULL);
@@ -353,7 +335,7 @@
         led1 = 0;
 
         sec = time(NULL);
-        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&sec));
+        strftime(csv, sizeof(csv), "%Y-%m-%d %H:%M:%S", localtime(&sec));
 
         // sensors
         __disable_irq();
@@ -371,23 +353,27 @@
 
         sensor.light = get_photo(ailight);
         if (conf.inputtype == INPUT_MOIST) {
+            // moist sensor
             sensor.moist = get_moist(*aimoist);
         } else {
+            // counter
             sensor.moist = get_counter(conf.inputtype & INPUT_CPM ? 0 : 1);
         }
         sensor.uv = get_uv(aiuv);
 
-        sprintf(&buf[strlen(buf)], ",%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\r\n",
+        sprintf(&csv[strlen(csv)], ",%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\r\n",
           sensor.pres, sensor.temp, sensor.humi, sensor.anemo, sensor.vane, sensor.rain, sensor.light, sensor.uv, sensor.moist, sensor.temp2);
         if (conf.filetype) {
-            // csv
-            writefile(buf);
+            // CSV
+            writefile(csv);
         }
         __enable_irq();
-        pc.printf(buf);
+        pc.printf(csv);
 
-        action(&buf[20]);
+        // in/out
+        action(1);
 
+        // I2C LED Display
         if (conf.leddisp_mesg[0]) {
             if (ledflg) {
                 ledpos = -4;
@@ -395,7 +381,9 @@
             }
             ledflg = 1;
         }
+        // I2C LCD
         if (conf.lcd_mesg[0]) {
+            char buf[80];
             fmtstr(conf.lcd_mesg, buf, sizeof(buf));
             lcd->cls();
             lcd->puts(buf);
@@ -403,10 +391,15 @@
 
         led1 = 1;
 
+        // interval (wait)
         while (timer.read() < conf.interval) {
-//            wait(1);
-//            pc.putc('.');
-            wait_ms(1);
+            wait_ms(100);
+
+            // in/out (timer)
+            if (locUpTime % 100 == 0)
+                action(0);
+
+            // ethernet LED
             if (conf.ipaddr[0]) {
                 led_g = eth_link ? 1 : 0;
                 led_g2 = 1;
@@ -415,7 +408,8 @@
                 led_g2 = 0;
             }
             Net::poll();
-            
+
+            // for debug                        
             if (pc.readable()) {
                 int i;
                 i = pc.getc();