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:
19:69b77f9e0446
Parent:
18:9286e5010c14
--- 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;