mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: ChaNFSSD EthernetNetIf I2CLEDDisp Agentbed ChaNFSUSB ILinterpreter mbed BMP085 WeatherMeters ConfigFile ChaNFS I2CLCD
ilip.cpp@2:a3e5edf84f74, 2011-08-24 (annotated)
- Committer:
- okini3939
- Date:
- Wed Aug 24 13:22:32 2011 +0000
- Revision:
- 2:a3e5edf84f74
- Parent:
- 1:6c7141895545
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 1:6c7141895545 | 1 | /* |
okini3939 | 1:6c7141895545 | 2 | * Weather Station - mbed Weather Platform |
okini3939 | 1:6c7141895545 | 3 | * Copyright (c) 2011 Hiroshi Suga |
okini3939 | 1:6c7141895545 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 1:6c7141895545 | 5 | */ |
okini3939 | 1:6c7141895545 | 6 | |
okini3939 | 0:bdb53686c194 | 7 | /** @file |
okini3939 | 1:6c7141895545 | 8 | * @brief Weather Station |
okini3939 | 0:bdb53686c194 | 9 | */ |
okini3939 | 1:6c7141895545 | 10 | |
okini3939 | 0:bdb53686c194 | 11 | #include "mbed.h" |
okini3939 | 0:bdb53686c194 | 12 | #include "weather.h" |
okini3939 | 0:bdb53686c194 | 13 | #ifdef USE_IL |
okini3939 | 0:bdb53686c194 | 14 | #include "ILinterpreter.h" |
okini3939 | 0:bdb53686c194 | 15 | |
okini3939 | 0:bdb53686c194 | 16 | static ILinterpreter ilip; |
okini3939 | 0:bdb53686c194 | 17 | #endif |
okini3939 | 0:bdb53686c194 | 18 | static Serial xbee(p13, p14); |
okini3939 | 1:6c7141895545 | 19 | static volatile int measure_flg = 0, exinout_flg = 0; |
okini3939 | 1:6c7141895545 | 20 | #ifdef USE_EXINOUT |
okini3939 | 1:6c7141895545 | 21 | static DigitalIn *exin1, *exin2; |
okini3939 | 1:6c7141895545 | 22 | static DigitalOut *exout1, *exout2; |
okini3939 | 1:6c7141895545 | 23 | #endif |
okini3939 | 0:bdb53686c194 | 24 | |
okini3939 | 0:bdb53686c194 | 25 | #ifdef USE_IL |
okini3939 | 0:bdb53686c194 | 26 | // input relay |
okini3939 | 0:bdb53686c194 | 27 | float cb_input(char key, int keynum, eEXPRESSION exp, int old) { |
okini3939 | 0:bdb53686c194 | 28 | float value = 0; |
okini3939 | 0:bdb53686c194 | 29 | Sensor *s; |
okini3939 | 0:bdb53686c194 | 30 | |
okini3939 | 0:bdb53686c194 | 31 | if (old) { |
okini3939 | 0:bdb53686c194 | 32 | s = &sensor_old; |
okini3939 | 0:bdb53686c194 | 33 | } else { |
okini3939 | 0:bdb53686c194 | 34 | s = &sensor; |
okini3939 | 0:bdb53686c194 | 35 | } |
okini3939 | 0:bdb53686c194 | 36 | |
okini3939 | 0:bdb53686c194 | 37 | switch (key) { |
okini3939 | 0:bdb53686c194 | 38 | case 'A': // sensor |
okini3939 | 0:bdb53686c194 | 39 | switch (keynum) { |
okini3939 | 0:bdb53686c194 | 40 | case 0: |
okini3939 | 0:bdb53686c194 | 41 | value = s->pres; |
okini3939 | 0:bdb53686c194 | 42 | break; |
okini3939 | 0:bdb53686c194 | 43 | case 1: |
okini3939 | 0:bdb53686c194 | 44 | value = s->temp; |
okini3939 | 0:bdb53686c194 | 45 | break; |
okini3939 | 0:bdb53686c194 | 46 | case 2: |
okini3939 | 0:bdb53686c194 | 47 | value = s->humi; |
okini3939 | 0:bdb53686c194 | 48 | break; |
okini3939 | 0:bdb53686c194 | 49 | case 3: |
okini3939 | 0:bdb53686c194 | 50 | value = s->anemo; |
okini3939 | 0:bdb53686c194 | 51 | break; |
okini3939 | 0:bdb53686c194 | 52 | case 4: |
okini3939 | 0:bdb53686c194 | 53 | value = s->vane; |
okini3939 | 0:bdb53686c194 | 54 | break; |
okini3939 | 0:bdb53686c194 | 55 | case 5: |
okini3939 | 0:bdb53686c194 | 56 | value = s->rain; |
okini3939 | 0:bdb53686c194 | 57 | break; |
okini3939 | 0:bdb53686c194 | 58 | case 6: |
okini3939 | 0:bdb53686c194 | 59 | value = s->light; |
okini3939 | 0:bdb53686c194 | 60 | break; |
okini3939 | 0:bdb53686c194 | 61 | case 7: |
okini3939 | 0:bdb53686c194 | 62 | value = s->uv; |
okini3939 | 0:bdb53686c194 | 63 | break; |
okini3939 | 0:bdb53686c194 | 64 | case 8: |
okini3939 | 0:bdb53686c194 | 65 | value = s->moist; |
okini3939 | 0:bdb53686c194 | 66 | break; |
okini3939 | 0:bdb53686c194 | 67 | case 99: |
okini3939 | 0:bdb53686c194 | 68 | value = measure_flg; |
okini3939 | 0:bdb53686c194 | 69 | break; |
okini3939 | 0:bdb53686c194 | 70 | } |
okini3939 | 0:bdb53686c194 | 71 | break; |
okini3939 | 0:bdb53686c194 | 72 | |
okini3939 | 0:bdb53686c194 | 73 | case 'I': // input |
okini3939 | 0:bdb53686c194 | 74 | if (keynum >= INPUT_NUM) break; |
okini3939 | 0:bdb53686c194 | 75 | value = s->input[keynum]; |
okini3939 | 0:bdb53686c194 | 76 | break; |
okini3939 | 0:bdb53686c194 | 77 | |
okini3939 | 0:bdb53686c194 | 78 | case 'Q': // output |
okini3939 | 0:bdb53686c194 | 79 | if (keynum >= OUTPUT_NUM) break; |
okini3939 | 0:bdb53686c194 | 80 | value = s->output[keynum]; |
okini3939 | 0:bdb53686c194 | 81 | break; |
okini3939 | 0:bdb53686c194 | 82 | } |
okini3939 | 0:bdb53686c194 | 83 | |
okini3939 | 0:bdb53686c194 | 84 | return value; |
okini3939 | 0:bdb53686c194 | 85 | } |
okini3939 | 0:bdb53686c194 | 86 | |
okini3939 | 0:bdb53686c194 | 87 | // output relay |
okini3939 | 0:bdb53686c194 | 88 | void cb_output(char key, int keynum, int reg, eMNEMONIC mne) { |
okini3939 | 0:bdb53686c194 | 89 | |
okini3939 | 0:bdb53686c194 | 90 | #ifdef DEBUG |
okini3939 | 0:bdb53686c194 | 91 | pc.printf("OUT [%c %d] %d %d\r\n", key, keynum, reg, mne); |
okini3939 | 0:bdb53686c194 | 92 | #endif |
okini3939 | 0:bdb53686c194 | 93 | pool_net(); |
okini3939 | 0:bdb53686c194 | 94 | |
okini3939 | 0:bdb53686c194 | 95 | switch (key) { |
okini3939 | 0:bdb53686c194 | 96 | case 'Q': // output |
okini3939 | 0:bdb53686c194 | 97 | if (keynum >= OUTPUT_NUM) break; |
okini3939 | 0:bdb53686c194 | 98 | if (mne == MNE_OUT) { |
okini3939 | 0:bdb53686c194 | 99 | sensor.output[keynum] = reg; |
okini3939 | 0:bdb53686c194 | 100 | } else |
okini3939 | 0:bdb53686c194 | 101 | if (mne == MNE_SET && reg) { |
okini3939 | 0:bdb53686c194 | 102 | sensor.output[keynum] = 1; |
okini3939 | 0:bdb53686c194 | 103 | } else |
okini3939 | 0:bdb53686c194 | 104 | if (mne == MNE_RST && reg) { |
okini3939 | 0:bdb53686c194 | 105 | sensor.output[keynum] = 0; |
okini3939 | 0:bdb53686c194 | 106 | } |
okini3939 | 0:bdb53686c194 | 107 | break; |
okini3939 | 0:bdb53686c194 | 108 | |
okini3939 | 0:bdb53686c194 | 109 | case 'P': // Pachube |
okini3939 | 0:bdb53686c194 | 110 | if (mne == MNE_OUT && reg) { |
okini3939 | 2:a3e5edf84f74 | 111 | pachube(); |
okini3939 | 0:bdb53686c194 | 112 | } |
okini3939 | 0:bdb53686c194 | 113 | break; |
okini3939 | 0:bdb53686c194 | 114 | |
okini3939 | 0:bdb53686c194 | 115 | case 'S': // Weather Stations |
okini3939 | 0:bdb53686c194 | 116 | if (mne == MNE_OUT && reg) { |
okini3939 | 0:bdb53686c194 | 117 | weatherstations(); |
okini3939 | 0:bdb53686c194 | 118 | } |
okini3939 | 0:bdb53686c194 | 119 | break; |
okini3939 | 0:bdb53686c194 | 120 | |
okini3939 | 0:bdb53686c194 | 121 | case 'W': // Twitter |
okini3939 | 0:bdb53686c194 | 122 | if (mne == MNE_OUT && reg) { |
okini3939 | 0:bdb53686c194 | 123 | twitter(keynum); |
okini3939 | 0:bdb53686c194 | 124 | } |
okini3939 | 0:bdb53686c194 | 125 | break; |
okini3939 | 0:bdb53686c194 | 126 | |
okini3939 | 1:6c7141895545 | 127 | case 'B': // XBee |
okini3939 | 0:bdb53686c194 | 128 | if (mne == MNE_OUT && reg) { |
okini3939 | 0:bdb53686c194 | 129 | xbee.printf(csv); |
okini3939 | 1:6c7141895545 | 130 | xbee.printf("\r\n"); |
okini3939 | 0:bdb53686c194 | 131 | } |
okini3939 | 0:bdb53686c194 | 132 | break; |
okini3939 | 0:bdb53686c194 | 133 | |
okini3939 | 0:bdb53686c194 | 134 | case 'E': // E-mail |
okini3939 | 0:bdb53686c194 | 135 | if (mne == MNE_OUT && reg) { |
okini3939 | 0:bdb53686c194 | 136 | email(keynum); |
okini3939 | 0:bdb53686c194 | 137 | } |
okini3939 | 0:bdb53686c194 | 138 | break; |
okini3939 | 2:a3e5edf84f74 | 139 | |
okini3939 | 2:a3e5edf84f74 | 140 | case 'x': // NTP |
okini3939 | 2:a3e5edf84f74 | 141 | if (mne == MNE_OUT && reg && conf.ntpserver[0]) { |
okini3939 | 2:a3e5edf84f74 | 142 | ntp(conf.ntpserver); |
okini3939 | 2:a3e5edf84f74 | 143 | } |
okini3939 | 2:a3e5edf84f74 | 144 | break; |
okini3939 | 2:a3e5edf84f74 | 145 | |
okini3939 | 0:bdb53686c194 | 146 | } |
okini3939 | 0:bdb53686c194 | 147 | |
okini3939 | 0:bdb53686c194 | 148 | pool_net(); |
okini3939 | 0:bdb53686c194 | 149 | } |
okini3939 | 0:bdb53686c194 | 150 | #endif |
okini3939 | 0:bdb53686c194 | 151 | |
okini3939 | 0:bdb53686c194 | 152 | void pool_ilip () { |
okini3939 | 0:bdb53686c194 | 153 | #ifdef USE_IL |
okini3939 | 0:bdb53686c194 | 154 | ilip.pool(); |
okini3939 | 0:bdb53686c194 | 155 | #endif |
okini3939 | 0:bdb53686c194 | 156 | } |
okini3939 | 0:bdb53686c194 | 157 | |
okini3939 | 0:bdb53686c194 | 158 | // execute IL |
okini3939 | 0:bdb53686c194 | 159 | void exec_ilip (int enable) { |
okini3939 | 0:bdb53686c194 | 160 | Sensor sensor_tmp; |
okini3939 | 0:bdb53686c194 | 161 | |
okini3939 | 0:bdb53686c194 | 162 | measure_flg = enable; |
okini3939 | 0:bdb53686c194 | 163 | |
okini3939 | 1:6c7141895545 | 164 | #ifdef USE_EXINOUT |
okini3939 | 0:bdb53686c194 | 165 | // set input |
okini3939 | 1:6c7141895545 | 166 | if (exinout_flg) { |
okini3939 | 1:6c7141895545 | 167 | sensor.input[0] = *exin1 ? 0 : 1; |
okini3939 | 1:6c7141895545 | 168 | sensor.input[1] = *exin2 ? 0 : 1; |
okini3939 | 1:6c7141895545 | 169 | } |
okini3939 | 1:6c7141895545 | 170 | #endif |
okini3939 | 0:bdb53686c194 | 171 | |
okini3939 | 0:bdb53686c194 | 172 | sensor_tmp = sensor; |
okini3939 | 0:bdb53686c194 | 173 | |
okini3939 | 0:bdb53686c194 | 174 | #ifdef USE_IL |
okini3939 | 0:bdb53686c194 | 175 | // every time |
okini3939 | 0:bdb53686c194 | 176 | if (ilip.exec()) { |
okini3939 | 0:bdb53686c194 | 177 | printf("IL: out of stack\r\n"); |
okini3939 | 0:bdb53686c194 | 178 | } |
okini3939 | 0:bdb53686c194 | 179 | #else |
okini3939 | 0:bdb53686c194 | 180 | if (enable) { |
okini3939 | 0:bdb53686c194 | 181 | weatherstations(); |
okini3939 | 0:bdb53686c194 | 182 | pachube(csv); |
okini3939 | 0:bdb53686c194 | 183 | email(0); |
okini3939 | 0:bdb53686c194 | 184 | } |
okini3939 | 0:bdb53686c194 | 185 | #endif |
okini3939 | 0:bdb53686c194 | 186 | |
okini3939 | 1:6c7141895545 | 187 | #ifdef USE_EXINOUT |
okini3939 | 0:bdb53686c194 | 188 | // set output |
okini3939 | 1:6c7141895545 | 189 | if (exinout_flg) { |
okini3939 | 2:a3e5edf84f74 | 190 | led3 = *exout1 = sensor.output[0]; |
okini3939 | 2:a3e5edf84f74 | 191 | led4 = *exout2 = sensor.output[1]; |
okini3939 | 1:6c7141895545 | 192 | } |
okini3939 | 1:6c7141895545 | 193 | #endif |
okini3939 | 0:bdb53686c194 | 194 | |
okini3939 | 0:bdb53686c194 | 195 | sensor_old = sensor_tmp; |
okini3939 | 0:bdb53686c194 | 196 | } |
okini3939 | 0:bdb53686c194 | 197 | |
okini3939 | 0:bdb53686c194 | 198 | // init IL |
okini3939 | 0:bdb53686c194 | 199 | int init_ilip (char *dir) { |
okini3939 | 1:6c7141895545 | 200 | int r; |
okini3939 | 0:bdb53686c194 | 201 | char buf[40]; |
okini3939 | 0:bdb53686c194 | 202 | |
okini3939 | 2:a3e5edf84f74 | 203 | if (conf.xbeebaud) { |
okini3939 | 2:a3e5edf84f74 | 204 | xbee.baud(conf.xbeebaud); |
okini3939 | 0:bdb53686c194 | 205 | } |
okini3939 | 0:bdb53686c194 | 206 | |
okini3939 | 1:6c7141895545 | 207 | #ifdef USE_EXINOUT |
okini3939 | 2:a3e5edf84f74 | 208 | if (conf.inputtype == INPUT_EXINOUT) { |
okini3939 | 2:a3e5edf84f74 | 209 | exout1 = new DigitalOut(p30); |
okini3939 | 2:a3e5edf84f74 | 210 | exout2 = new DigitalOut(p29); |
okini3939 | 2:a3e5edf84f74 | 211 | exin1 = new DigitalIn(p18); |
okini3939 | 2:a3e5edf84f74 | 212 | exin1->mode(PullUp); |
okini3939 | 2:a3e5edf84f74 | 213 | exin2 = new DigitalIn(p19); |
okini3939 | 2:a3e5edf84f74 | 214 | exin2->mode(PullUp); |
okini3939 | 2:a3e5edf84f74 | 215 | exinout_flg = 1; |
okini3939 | 1:6c7141895545 | 216 | } |
okini3939 | 1:6c7141895545 | 217 | #endif |
okini3939 | 1:6c7141895545 | 218 | |
okini3939 | 0:bdb53686c194 | 219 | #ifdef USE_IL |
okini3939 | 0:bdb53686c194 | 220 | strcpy(buf, dir); |
okini3939 | 0:bdb53686c194 | 221 | strcat(buf, IL_FILE); |
okini3939 | 1:6c7141895545 | 222 | r = ilip.load(buf); |
okini3939 | 1:6c7141895545 | 223 | if (r > 0) { |
okini3939 | 1:6c7141895545 | 224 | pc.printf("IL load: %d\r\n", r); |
okini3939 | 0:bdb53686c194 | 225 | ilip.attach(cb_input, cb_output); |
okini3939 | 0:bdb53686c194 | 226 | } |
okini3939 | 0:bdb53686c194 | 227 | #endif |
okini3939 | 0:bdb53686c194 | 228 | |
okini3939 | 0:bdb53686c194 | 229 | return 0; |
okini3939 | 0:bdb53686c194 | 230 | } |