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

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Fri Jun 03 15:38:22 2011 +0000
Parent:
18:9286e5010c14
Commit message:

Changed in this revision

Stack.lib Show annotated file Show diff for this revision Revisions of this file
action.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
weather.h Show annotated file Show diff for this revision Revisions of this file
diff -r 9286e5010c14 -r 69b77f9e0446 Stack.lib
--- a/Stack.lib	Tue May 31 15:40:14 2011 +0000
+++ b/Stack.lib	Fri Jun 03 15:38:22 2011 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/okini3939/code/Stack/#d76eb9986be8
+http://mbed.org/users/okini3939/code/Stack/#6385db13c2d6
diff -r 9286e5010c14 -r 69b77f9e0446 action.cpp
--- a/action.cpp	Tue May 31 15:40:14 2011 +0000
+++ b/action.cpp	Fri Jun 03 15:38:22 2011 +0000
@@ -11,8 +11,9 @@
     char str[5];
 };
 
-#define CF_MNE_NUM 25
+#define CF_MNE_NUM 26
 const struct MNE_str mne_str[CF_MNE_NUM] = {
+    {MNE_DEF, "DEF"},
     {MNE_LD, "LD"}, {MNE_LDI, "LDI"}, {MNE_LDP, "LDP"}, {MNE_LDF, "LDF"},
     {MNE_ALD, "@LD"}, {MNE_ALDI, "@LDI"}, {MNE_ALDP, "@LDP"}, {MNE_ALDF, "@LDF"},
     {MNE_OR, "OR"}, {MNE_ORI, "ORI"}, {MNE_ORP, "ORP"}, {MNE_ORF, "ORF"},
@@ -42,6 +43,7 @@
 
     tim = localtime(&s->sec);
     keynum = conf.actions[i].keynum;
+    // right value
     check = conf.actions[i].value;
 
     // left value
@@ -112,18 +114,22 @@
     case 't': // Timer
         if (keynum >= TIMER_NUM) break;
         if (conf.actions[i].expression == EXP_NULL) {
-            value = s->timer_flg[keynum] && s->timer_cnt[keynum] == 0;
+            value = s->timer_flg[keynum] && s->timer_cnt[keynum] >= s->timer_set[keynum];
         } else {
             value = s->timer_cnt[keynum];
         }
         break;
     case 'c': // Counter
         if (keynum >= COUNTER_NUM) break;
-        value = s->count[keynum];
+        if (conf.actions[i].expression == EXP_NULL) {
+             value = s->count_cnt[keynum] >= s->count_set[keynum];
+        } else {
+             value = s->count_cnt[keynum];
+        }
         break;
     }
 
-    // expression, right value
+    // expression
     switch (conf.actions[i].expression) {
     case EXP_EQ:
         return value == check;
@@ -138,50 +144,43 @@
     case EXP_GT:
         return value > check;
     case EXP_MOD:
-        return (int)value % (int)check;
+        return ((int)value / 10) % (int)check;
     case EXP_NMOD:
-        return ! (int)value % (int)check;
+        return ! (((int)value / 10)  % (int)check);
     }
 
     return value != 0;
 }
 
 void exec_action (int i, int reg, eMNEMONIC sr) {
-    static int tw_old = 0;
     int keynum;
 
     keynum = conf.actions[i].keynum;
 
     switch (conf.actions[i].key) {
     case 'P': // Pachube
-        if (conf.ipaddr[0] && conf.pachube_apikey[0] && conf.pachube_feedid[0] && reg) {
+        if (sr == MNE_OUT && reg)
+          if (conf.ipaddr[0] && conf.pachube_apikey[0] && conf.pachube_feedid[0]) {
             pachube(csv);
         }
         break;
 
     case 'S': // Weather Stations
-        if (conf.ipaddr[0] && conf.stations_id[0] && conf.stations_pin[0] && reg) {
+        if (sr == MNE_OUT && reg)
+          if (conf.ipaddr[0] && conf.stations_id[0] && conf.stations_pin[0]) {
             weatherstations();
         }
         break;
 
-    case 'T': // Twitter
-        if (conf.ipaddr[0] && conf.twitter_user[0] && conf.twitter_pwd[0]) {
-            if (sr == MNE_OUT && reg) {
-                twitter(keynum);
-            } else
-            if (sr == MNE_SET && reg && tw_old == 0) {
-                twitter(keynum);
-                tw_old = 1;
-            } else
-            if (reg == 0) {
-                tw_old = 0;
-            }
+    case 'W': // Twitter
+        if (sr == MNE_OUT && reg)
+          if (conf.ipaddr[0] && conf.twitter_user[0] && conf.twitter_pwd[0]) {
+              twitter(keynum);
         }
         break;
 
     case 'X': // XBee
-        if (reg) {
+        if (sr == MNE_OUT && reg) {
             xbee.printf(csv);
         }
         break;
@@ -202,27 +201,35 @@
     case 't': // Timer
         if (keynum >= TIMER_NUM) break;
         if (sr == MNE_OUT) {
-            // set timer
-            if (sensor.timer_flg[keynum]) sensor.timer_cnt[keynum] = conf.actions[i].value * 10;
+            if (! sensor.timer_flg[keynum]) {
+                // set timer
+                sensor.timer_cnt[keynum] = 0;
+            }
             sensor.timer_flg[keynum] = reg;
         } else
-        if (sr == MNE_SET && reg) {
-            sensor.timer_cnt[keynum] = conf.actions[i].value * 10;
-        } else
         if (sr == MNE_RST && reg) {
-            sensor.timer_flg[keynum] = 0;
+            sensor.timer_cnt[keynum] = 0;
         }
         break;
 
     case 'c': // Counter
         if (keynum >= COUNTER_NUM) break;
         if (sr == MNE_OUT && reg) {
-            sensor.count[keynum] ++;
+            if (sensor.count_rev[keynum]) {
+                sensor.count_cnt[keynum] --;
+            } else {
+                sensor.count_cnt[keynum] ++;
+            }
         } else
         if (sr == MNE_RST && reg) {
-            sensor.count[keynum] = 0;
+            sensor.count_cnt[keynum] = 0;
         }
-
+    case 'r': // Counter (reverse)
+        if (keynum >= COUNTER_NUM) break;
+        if (sr == MNE_OUT) {
+            sensor.count_rev[keynum] = reg;
+        }
+        break;
     }
 
 #ifdef DEBUG
@@ -234,53 +241,57 @@
     int i;
     char j, reg, ena;
     Stack stack(40);
+    Sensor sensor_tmp;
 
+    // input
     sensor.sec = time(NULL) + (60 * 60 * 9);
     sensor.input[0] = conf.inputtype ? *aimoist > 0.5 : 0;
     sensor.input[1] = swin2;
+    sensor_tmp = sensor;
 
+    // mnemonic decode
     for(i = 0; i < conf.actionscount; i ++) {
         switch (conf.actions[i].mnemonic) {
         case MNE_LD:
             stack.push(reg);
             reg = check_exp(&sensor, i);
-            ena = enable;
+            ena = 1;
             break;
         case MNE_LDI:
             stack.push(reg);
             reg = ! check_exp(&sensor, i);
-            ena = enable;
+            ena = 1;
             break;
         case MNE_LDP:
             stack.push(reg);
             reg = check_exp(&sensor, i) && ! check_exp(&sensor_old, i);
-            ena = enable;
+            ena = 1;
             break;
         case MNE_LDF:
             stack.push(reg);
             reg = ! check_exp(&sensor, i) && check_exp(&sensor_old, i);
-            ena = enable;
+            ena = 1;
             break;
 
         case MNE_ALD:
             stack.push(reg);
             reg = check_exp(&sensor, i);
-            ena = 1;
+            ena = enable;
             break;
         case MNE_ALDI:
             stack.push(reg);
             reg = ! check_exp(&sensor, i);
-            ena = 1;
+            ena = enable;
             break;
         case MNE_ALDP:
             stack.push(reg);
             reg = check_exp(&sensor, i) && ! check_exp(&sensor_old, i);
-            ena = 1;
+            ena = enable;
             break;
         case MNE_ALDF:
             stack.push(reg);
             reg = ! check_exp(&sensor, i) && check_exp(&sensor_old, i);
-            ena = 1;
+            ena = enable;
             break;
 
         case MNE_AND:
@@ -340,13 +351,14 @@
         }
     }
 
+    // output
     led3 = swout1 = sensor.output[0];
     led4 = swout2 = sensor.output[1];
+    sensor_old = sensor_tmp;
+
 #ifdef DEBUG
-    printf("timer %d\r\n", sensor.timer_cnt[0]);
+    printf("timer0=%d(%d) timer1=%d(%d)\r\n", sensor.timer_cnt[0], sensor.timer_set[0], sensor.timer_cnt[1], sensor.timer_set[1]);
 #endif
-
-    sensor_old = sensor;
     return 0;
 }
 
@@ -360,6 +372,7 @@
     conf.actions[i].value = 0;
 
     if (tmp) {
+        // expression
         for (j = 0; j < CF_EXP_NUM; j ++) {
             len = strlen(exp_str[j].str);
             if (strncmp(tmp, exp_str[j].str, len) == 0 && tmp[len] >= '0' && tmp[len] <= '9') {
@@ -386,12 +399,20 @@
         if (feof(fp)) break;
 
         if (c != '\r' && c != '\n' && i < 40 - 1) {
+            // load
             buf[i] = c;
             i ++;
             continue;
         }
         buf[i] = 0;
+        
+        if (i == 0 || buf[0] == '#' || buf[0] == ';') {
+            // comment
+            i = 0;
+            continue;
+        }
 
+        // mnemonic
         for (j = 0; j < CF_MNE_NUM; j ++) {
             len = strlen(mne_str[j].str);
             if (strncmp(buf, mne_str[j].str, len) == 0 && buf[len] == ' ') {
@@ -406,6 +427,21 @@
         i = 0;
     }
 
+    // init
+    for (i = 0; i < count; i ++) {
+      if (conf.actions[i].mnemonic == MNE_DEF) {
+        switch (conf.actions[i].key) {
+        case 't': // Timer
+            sensor.timer_set[conf.actions[i].keynum] = conf.actions[i].value * 10;
+            break;
+
+        case 'c': // Counter
+            sensor.count_set[conf.actions[i].keynum] = conf.actions[i].value;
+            break;
+        }
+      }
+    }
+
     fclose(fp);    
     conf.actionscount = count;
 
diff -r 9286e5010c14 -r 69b77f9e0446 main.cpp
--- a/main.cpp	Tue May 31 15:40:14 2011 +0000
+++ b/main.cpp	Fri Jun 03 15:38:22 2011 +0000
@@ -146,7 +146,9 @@
 
     // timer
     for (i = 0; i < TIMER_NUM; i ++) {
-        if (sensor.timer_flg[i] && sensor.timer_cnt[i]) sensor.timer_cnt[i] --;
+        if (sensor.timer_flg[i] && sensor.timer_cnt[i] < sensor.timer_set[i]) {
+            sensor.timer_cnt[i] ++;
+        }
     }
     // uptime
     locUpTime = locUpTime + 10;
@@ -295,8 +297,8 @@
     ledflg = 0;
     strcpy(ledbuf, VERSION);
 
+    init();
     ticker.attach(&isr_timer, 0.1); // Interrupt 10Hz
-    init();
     pc.printf("%s\r\n\r\n", VERSION);
 
     if (conf.filetype) {
diff -r 9286e5010c14 -r 69b77f9e0446 weather.h
--- a/weather.h	Tue May 31 15:40:14 2011 +0000
+++ b/weather.h	Fri Jun 03 15:38:22 2011 +0000
@@ -19,14 +19,15 @@
 #define CF_TWITTER_NUM 5
 
 #define INPUT_NUM 2
-#define OUTPUT_NUM 4
-#define TIMER_NUM 4
-#define COUNTER_NUM 4
+#define OUTPUT_NUM 10
+#define TIMER_NUM 10
+#define COUNTER_NUM 10
 
 #define LED_FREQ 4 // x 0.1s
 
 enum eMNEMONIC {
     MNE_NULL,
+    MNE_DEF,
     MNE_LD, MNE_LDI, MNE_LDP, MNE_LDF,
     MNE_ALD, MNE_ALDI, MNE_ALDP, MNE_ALDF,
     MNE_OR, MNE_ORI, MNE_ORP, MNE_ORF,
@@ -64,9 +65,10 @@
 struct Sensor {
     float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2;
     time_t sec;
-    int input[INPUT_NUM], output[OUTPUT_NUM], timer_flg[TIMER_NUM];
-    unsigned long timer_cnt[TIMER_NUM];
-    unsigned long count[COUNTER_NUM];
+    int input[INPUT_NUM], output[OUTPUT_NUM];
+    int timer_flg[TIMER_NUM];
+    unsigned int timer_set[TIMER_NUM], timer_cnt[TIMER_NUM];
+    unsigned int count_set[COUNTER_NUM], count_cnt[COUNTER_NUM], count_rev[COUNTER_NUM];
 };
 
 struct Config {