Shinichiro Nakamura
/
StarBoardOrangeExpansion2
main.cpp@0:e9a647f18c71, 2010-11-04 (annotated)
- Committer:
- shintamainjp
- Date:
- Thu Nov 04 14:33:43 2010 +0000
- Revision:
- 0:e9a647f18c71
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 0:e9a647f18c71 | 1 | /** |
shintamainjp | 0:e9a647f18c71 | 2 | * ============================================================================= |
shintamainjp | 0:e9a647f18c71 | 3 | * Expansion Board One - Example application No.2 (Version 0.0.1) |
shintamainjp | 0:e9a647f18c71 | 4 | * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/ |
shintamainjp | 0:e9a647f18c71 | 5 | * ============================================================================= |
shintamainjp | 0:e9a647f18c71 | 6 | * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 0:e9a647f18c71 | 7 | * |
shintamainjp | 0:e9a647f18c71 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
shintamainjp | 0:e9a647f18c71 | 9 | * of this software and associated documentation files (the "Software"), to deal |
shintamainjp | 0:e9a647f18c71 | 10 | * in the Software without restriction, including without limitation the rights |
shintamainjp | 0:e9a647f18c71 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
shintamainjp | 0:e9a647f18c71 | 12 | * copies of the Software, and to permit persons to whom the Software is |
shintamainjp | 0:e9a647f18c71 | 13 | * furnished to do so, subject to the following conditions: |
shintamainjp | 0:e9a647f18c71 | 14 | * |
shintamainjp | 0:e9a647f18c71 | 15 | * The above copyright notice and this permission notice shall be included in |
shintamainjp | 0:e9a647f18c71 | 16 | * all copies or substantial portions of the Software. |
shintamainjp | 0:e9a647f18c71 | 17 | * |
shintamainjp | 0:e9a647f18c71 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
shintamainjp | 0:e9a647f18c71 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
shintamainjp | 0:e9a647f18c71 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
shintamainjp | 0:e9a647f18c71 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
shintamainjp | 0:e9a647f18c71 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
shintamainjp | 0:e9a647f18c71 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
shintamainjp | 0:e9a647f18c71 | 24 | * THE SOFTWARE. |
shintamainjp | 0:e9a647f18c71 | 25 | * ============================================================================= |
shintamainjp | 0:e9a647f18c71 | 26 | */ |
shintamainjp | 0:e9a647f18c71 | 27 | |
shintamainjp | 0:e9a647f18c71 | 28 | #include "mbed.h" |
shintamainjp | 0:e9a647f18c71 | 29 | #include "PachubeV2CSV.h" |
shintamainjp | 0:e9a647f18c71 | 30 | #include "EthernetNetIf.h" |
shintamainjp | 0:e9a647f18c71 | 31 | #include "HTTPClient.h" |
shintamainjp | 0:e9a647f18c71 | 32 | #include "ThermistorMCP9701.h" |
shintamainjp | 0:e9a647f18c71 | 33 | #include "TextLCD.h" |
shintamainjp | 0:e9a647f18c71 | 34 | #include "appconf.h" |
shintamainjp | 0:e9a647f18c71 | 35 | |
shintamainjp | 0:e9a647f18c71 | 36 | /* |
shintamainjp | 0:e9a647f18c71 | 37 | * Definitions for a configuration file. |
shintamainjp | 0:e9a647f18c71 | 38 | */ |
shintamainjp | 0:e9a647f18c71 | 39 | #define CONFIG_FILENAME "/local/SBOE2.CFG" |
shintamainjp | 0:e9a647f18c71 | 40 | |
shintamainjp | 0:e9a647f18c71 | 41 | LocalFileSystem localfs("local"); |
shintamainjp | 0:e9a647f18c71 | 42 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
shintamainjp | 0:e9a647f18c71 | 43 | EthernetNetIf netif; |
shintamainjp | 0:e9a647f18c71 | 44 | BusOut led(LED1, LED2, LED3, LED4); |
shintamainjp | 0:e9a647f18c71 | 45 | ThermistorMCP9701 thermistor1(p16); |
shintamainjp | 0:e9a647f18c71 | 46 | ThermistorMCP9701 thermistor2(p17); |
shintamainjp | 0:e9a647f18c71 | 47 | ThermistorMCP9701 thermistor3(p18); |
shintamainjp | 0:e9a647f18c71 | 48 | ThermistorMCP9701 thermistor4(p19); |
shintamainjp | 0:e9a647f18c71 | 49 | ThermistorMCP9701 thermistor5(p20); |
shintamainjp | 0:e9a647f18c71 | 50 | static appconf_t appconf; |
shintamainjp | 0:e9a647f18c71 | 51 | |
shintamainjp | 0:e9a647f18c71 | 52 | /** |
shintamainjp | 0:e9a647f18c71 | 53 | * Display a splash screen. |
shintamainjp | 0:e9a647f18c71 | 54 | */ |
shintamainjp | 0:e9a647f18c71 | 55 | void splash(void) { |
shintamainjp | 0:e9a647f18c71 | 56 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 57 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 58 | lcd.printf("StarBoard Orange"); |
shintamainjp | 0:e9a647f18c71 | 59 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 60 | lcd.printf("Expansion Board "); |
shintamainjp | 0:e9a647f18c71 | 61 | wait(2); |
shintamainjp | 0:e9a647f18c71 | 62 | |
shintamainjp | 0:e9a647f18c71 | 63 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 64 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 65 | lcd.printf("Example app No.2"); |
shintamainjp | 0:e9a647f18c71 | 66 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 67 | lcd.printf(" "); |
shintamainjp | 0:e9a647f18c71 | 68 | wait(2); |
shintamainjp | 0:e9a647f18c71 | 69 | } |
shintamainjp | 0:e9a647f18c71 | 70 | |
shintamainjp | 0:e9a647f18c71 | 71 | /** |
shintamainjp | 0:e9a647f18c71 | 72 | * Convert double to char. |
shintamainjp | 0:e9a647f18c71 | 73 | * |
shintamainjp | 0:e9a647f18c71 | 74 | * @param val Value. |
shintamainjp | 0:e9a647f18c71 | 75 | * @param buf A pointer to a buffer. |
shintamainjp | 0:e9a647f18c71 | 76 | * @param bufsiz The buffer size. |
shintamainjp | 0:e9a647f18c71 | 77 | */ |
shintamainjp | 0:e9a647f18c71 | 78 | void convertDoubleToChar(double val, char *buf, size_t bufsiz) { |
shintamainjp | 0:e9a647f18c71 | 79 | snprintf(buf, bufsiz, "%f", val); |
shintamainjp | 0:e9a647f18c71 | 80 | } |
shintamainjp | 0:e9a647f18c71 | 81 | |
shintamainjp | 0:e9a647f18c71 | 82 | /** |
shintamainjp | 0:e9a647f18c71 | 83 | * Entry point. |
shintamainjp | 0:e9a647f18c71 | 84 | */ |
shintamainjp | 0:e9a647f18c71 | 85 | int main() { |
shintamainjp | 0:e9a647f18c71 | 86 | |
shintamainjp | 0:e9a647f18c71 | 87 | /* |
shintamainjp | 0:e9a647f18c71 | 88 | * Splash. |
shintamainjp | 0:e9a647f18c71 | 89 | */ |
shintamainjp | 0:e9a647f18c71 | 90 | splash(); |
shintamainjp | 0:e9a647f18c71 | 91 | |
shintamainjp | 0:e9a647f18c71 | 92 | /* |
shintamainjp | 0:e9a647f18c71 | 93 | * Initialize ethernet interface. |
shintamainjp | 0:e9a647f18c71 | 94 | */ |
shintamainjp | 0:e9a647f18c71 | 95 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 96 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 97 | lcd.printf("Initializing. "); |
shintamainjp | 0:e9a647f18c71 | 98 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 99 | lcd.printf("Ethernet: "); |
shintamainjp | 0:e9a647f18c71 | 100 | EthernetErr ethErr = netif.setup(); |
shintamainjp | 0:e9a647f18c71 | 101 | if (ethErr) { |
shintamainjp | 0:e9a647f18c71 | 102 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 103 | lcd.printf("Ethernet:NG "); |
shintamainjp | 0:e9a647f18c71 | 104 | error("Network setup failed.\n"); |
shintamainjp | 0:e9a647f18c71 | 105 | } else { |
shintamainjp | 0:e9a647f18c71 | 106 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 107 | lcd.printf("Ethernet:OK "); |
shintamainjp | 0:e9a647f18c71 | 108 | } |
shintamainjp | 0:e9a647f18c71 | 109 | wait(2); |
shintamainjp | 0:e9a647f18c71 | 110 | |
shintamainjp | 0:e9a647f18c71 | 111 | /* |
shintamainjp | 0:e9a647f18c71 | 112 | * Read configuration variables from a file. |
shintamainjp | 0:e9a647f18c71 | 113 | */ |
shintamainjp | 0:e9a647f18c71 | 114 | appconf_init(&appconf); |
shintamainjp | 0:e9a647f18c71 | 115 | if (appconf_read(CONFIG_FILENAME, &appconf) != 0) { |
shintamainjp | 0:e9a647f18c71 | 116 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 117 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 118 | lcd.printf("ConfigFile"); |
shintamainjp | 0:e9a647f18c71 | 119 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 120 | lcd.printf("Read error."); |
shintamainjp | 0:e9a647f18c71 | 121 | error("Failure to read a configuration file.\n"); |
shintamainjp | 0:e9a647f18c71 | 122 | } |
shintamainjp | 0:e9a647f18c71 | 123 | |
shintamainjp | 0:e9a647f18c71 | 124 | /* |
shintamainjp | 0:e9a647f18c71 | 125 | * Check the interval time. |
shintamainjp | 0:e9a647f18c71 | 126 | */ |
shintamainjp | 0:e9a647f18c71 | 127 | if (appconf.interval < 60) { |
shintamainjp | 0:e9a647f18c71 | 128 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 129 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 130 | lcd.printf("Invalid interval"); |
shintamainjp | 0:e9a647f18c71 | 131 | error("Inavlid interval time found.\n"); |
shintamainjp | 0:e9a647f18c71 | 132 | } |
shintamainjp | 0:e9a647f18c71 | 133 | |
shintamainjp | 0:e9a647f18c71 | 134 | /* |
shintamainjp | 0:e9a647f18c71 | 135 | * Initialize objects. |
shintamainjp | 0:e9a647f18c71 | 136 | */ |
shintamainjp | 0:e9a647f18c71 | 137 | PachubeV2CSV web(appconf.apikey); |
shintamainjp | 0:e9a647f18c71 | 138 | |
shintamainjp | 0:e9a647f18c71 | 139 | int cnt = 0; |
shintamainjp | 0:e9a647f18c71 | 140 | do { |
shintamainjp | 0:e9a647f18c71 | 141 | /* |
shintamainjp | 0:e9a647f18c71 | 142 | * Sense. |
shintamainjp | 0:e9a647f18c71 | 143 | */ |
shintamainjp | 0:e9a647f18c71 | 144 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 145 | int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0; |
shintamainjp | 0:e9a647f18c71 | 146 | for (int i = 0; i < appconf.interval; i++) { |
shintamainjp | 0:e9a647f18c71 | 147 | led = 1 << (i % 4); |
shintamainjp | 0:e9a647f18c71 | 148 | printf("%d/%d\n", i + 1, appconf.interval); |
shintamainjp | 0:e9a647f18c71 | 149 | v1 += thermistor1.read(); |
shintamainjp | 0:e9a647f18c71 | 150 | v2 += thermistor2.read(); |
shintamainjp | 0:e9a647f18c71 | 151 | v3 += thermistor3.read(); |
shintamainjp | 0:e9a647f18c71 | 152 | v4 += thermistor4.read(); |
shintamainjp | 0:e9a647f18c71 | 153 | v5 += thermistor5.read(); |
shintamainjp | 0:e9a647f18c71 | 154 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 155 | lcd.printf("| 0| 1| 2| 3| 4|"); |
shintamainjp | 0:e9a647f18c71 | 156 | lcd.locate(0, 1); |
shintamainjp | 0:e9a647f18c71 | 157 | lcd.printf("|%-2.0f|%-2.0f|%-2.0f|%-2.0f|%-2.0f|", thermistor1.read(), thermistor2.read(), thermistor3.read(), thermistor4.read(), thermistor5.read()); |
shintamainjp | 0:e9a647f18c71 | 158 | wait(1); |
shintamainjp | 0:e9a647f18c71 | 159 | } |
shintamainjp | 0:e9a647f18c71 | 160 | v1 /= appconf.interval; |
shintamainjp | 0:e9a647f18c71 | 161 | v2 /= appconf.interval; |
shintamainjp | 0:e9a647f18c71 | 162 | v3 /= appconf.interval; |
shintamainjp | 0:e9a647f18c71 | 163 | v4 /= appconf.interval; |
shintamainjp | 0:e9a647f18c71 | 164 | v5 /= appconf.interval; |
shintamainjp | 0:e9a647f18c71 | 165 | |
shintamainjp | 0:e9a647f18c71 | 166 | /* |
shintamainjp | 0:e9a647f18c71 | 167 | * Post. |
shintamainjp | 0:e9a647f18c71 | 168 | */ |
shintamainjp | 0:e9a647f18c71 | 169 | lcd.cls(); |
shintamainjp | 0:e9a647f18c71 | 170 | lcd.locate(0, 0); |
shintamainjp | 0:e9a647f18c71 | 171 | lcd.printf("Posting No.%d", ++cnt); |
shintamainjp | 0:e9a647f18c71 | 172 | char val1[16]; |
shintamainjp | 0:e9a647f18c71 | 173 | char val2[16]; |
shintamainjp | 0:e9a647f18c71 | 174 | char val3[16]; |
shintamainjp | 0:e9a647f18c71 | 175 | char val4[16]; |
shintamainjp | 0:e9a647f18c71 | 176 | char val5[16]; |
shintamainjp | 0:e9a647f18c71 | 177 | convertDoubleToChar(v1, val1, sizeof(val1)); |
shintamainjp | 0:e9a647f18c71 | 178 | convertDoubleToChar(v2, val2, sizeof(val2)); |
shintamainjp | 0:e9a647f18c71 | 179 | convertDoubleToChar(v3, val3, sizeof(val3)); |
shintamainjp | 0:e9a647f18c71 | 180 | convertDoubleToChar(v4, val4, sizeof(val4)); |
shintamainjp | 0:e9a647f18c71 | 181 | convertDoubleToChar(v5, val5, sizeof(val5)); |
shintamainjp | 0:e9a647f18c71 | 182 | printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "0", std::string(val1))); |
shintamainjp | 0:e9a647f18c71 | 183 | printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "1", std::string(val2))); |
shintamainjp | 0:e9a647f18c71 | 184 | printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "2", std::string(val3))); |
shintamainjp | 0:e9a647f18c71 | 185 | printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "3", std::string(val4))); |
shintamainjp | 0:e9a647f18c71 | 186 | printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "4", std::string(val5))); |
shintamainjp | 0:e9a647f18c71 | 187 | } while (1); |
shintamainjp | 0:e9a647f18c71 | 188 | } |