Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * =============================================================================
00003  * Expansion Board One - Example application No.2 (Version 0.0.1)
00004  * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/
00005  * =============================================================================
00006  * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  * 
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  * =============================================================================
00026  */
00027 
00028 #include "mbed.h"
00029 #include "PachubeV2CSV.h"
00030 #include "EthernetNetIf.h"
00031 #include "HTTPClient.h"
00032 #include "ThermistorMCP9701.h"
00033 #include "TextLCD.h"
00034 #include "appconf.h"
00035 
00036 /*
00037  * Definitions for a configuration file.
00038  */
00039 #define CONFIG_FILENAME         "/local/SBOE2.CFG"
00040 
00041 LocalFileSystem localfs("local");
00042 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00043 EthernetNetIf netif;
00044 BusOut led(LED1, LED2, LED3, LED4);
00045 ThermistorMCP9701 thermistor1(p16);
00046 ThermistorMCP9701 thermistor2(p17);
00047 ThermistorMCP9701 thermistor3(p18);
00048 ThermistorMCP9701 thermistor4(p19);
00049 ThermistorMCP9701 thermistor5(p20);
00050 static appconf_t appconf;
00051 
00052 /**
00053  * Display a splash screen.
00054  */
00055 void splash(void) {
00056     lcd.cls();
00057     lcd.locate(0, 0);
00058     lcd.printf("StarBoard Orange");
00059     lcd.locate(0, 1);
00060     lcd.printf("Expansion Board ");
00061     wait(2);
00062 
00063     lcd.cls();
00064     lcd.locate(0, 0);
00065     lcd.printf("Example app No.2");
00066     lcd.locate(0, 1);
00067     lcd.printf("                ");
00068     wait(2);
00069 }
00070 
00071 /**
00072  * Convert double to char.
00073  *
00074  * @param val Value.
00075  * @param buf A pointer to a buffer.
00076  * @param bufsiz The buffer size.
00077  */
00078 void convertDoubleToChar(double val, char *buf, size_t bufsiz) {
00079     snprintf(buf, bufsiz, "%f", val);
00080 }
00081 
00082 /**
00083  * Entry point.
00084  */
00085 int main() {
00086 
00087     /*
00088      * Splash.
00089      */
00090     splash();
00091 
00092     /*
00093      * Initialize ethernet interface.
00094      */
00095     lcd.cls();
00096     lcd.locate(0, 0);
00097     lcd.printf("Initializing.   ");
00098     lcd.locate(0, 1);
00099     lcd.printf("Ethernet:       ");
00100     EthernetErr ethErr = netif.setup();
00101     if (ethErr) {
00102         lcd.locate(0, 1);
00103         lcd.printf("Ethernet:NG     ");
00104         error("Network setup failed.\n");
00105     } else {
00106         lcd.locate(0, 1);
00107         lcd.printf("Ethernet:OK     ");
00108     }
00109     wait(2);
00110 
00111     /*
00112      * Read configuration variables from a file.
00113      */
00114     appconf_init(&appconf);
00115     if (appconf_read(CONFIG_FILENAME, &appconf) != 0) {
00116         lcd.cls();
00117         lcd.locate(0, 0);
00118         lcd.printf("ConfigFile");
00119         lcd.locate(0, 1);
00120         lcd.printf("Read error.");
00121         error("Failure to read a configuration file.\n");
00122     }
00123 
00124     /*
00125      * Check the interval time.
00126      */
00127     if (appconf.interval < 60) {
00128         lcd.cls();
00129         lcd.locate(0, 0);
00130         lcd.printf("Invalid interval");
00131         error("Inavlid interval time found.\n");
00132     }
00133 
00134     /*
00135      * Initialize objects.
00136      */
00137     PachubeV2CSV web(appconf.apikey);
00138 
00139     int cnt = 0;
00140     do {
00141         /*
00142          * Sense.
00143          */
00144         lcd.cls();
00145         int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0;
00146         for (int i = 0; i < appconf.interval; i++) {
00147             led = 1 << (i % 4);
00148             printf("%d/%d\n", i + 1, appconf.interval);
00149             v1 += thermistor1.read();
00150             v2 += thermistor2.read();
00151             v3 += thermistor3.read();
00152             v4 += thermistor4.read();
00153             v5 += thermistor5.read();
00154             lcd.locate(0, 0);
00155             lcd.printf("| 0| 1| 2| 3| 4|");
00156             lcd.locate(0, 1);
00157             lcd.printf("|%-2.0f|%-2.0f|%-2.0f|%-2.0f|%-2.0f|", thermistor1.read(), thermistor2.read(), thermistor3.read(), thermistor4.read(), thermistor5.read());
00158             wait(1);
00159         }
00160         v1 /= appconf.interval;
00161         v2 /= appconf.interval;
00162         v3 /= appconf.interval;
00163         v4 /= appconf.interval;
00164         v5 /= appconf.interval;
00165 
00166         /*
00167          * Post.
00168          */
00169         lcd.cls();
00170         lcd.locate(0, 0);
00171         lcd.printf("Posting No.%d", ++cnt);
00172         char val1[16];
00173         char val2[16];
00174         char val3[16];
00175         char val4[16];
00176         char val5[16];
00177         convertDoubleToChar(v1, val1, sizeof(val1));
00178         convertDoubleToChar(v2, val2, sizeof(val2));
00179         convertDoubleToChar(v3, val3, sizeof(val3));
00180         convertDoubleToChar(v4, val4, sizeof(val4));
00181         convertDoubleToChar(v5, val5, sizeof(val5));
00182         printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "0", std::string(val1)));
00183         printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "1", std::string(val2)));
00184         printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "2", std::string(val3)));
00185         printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "3", std::string(val4)));
00186         printf("updateDataStream(%d)\n", web.updateDataStream(atoi(appconf.feedid), "4", std::string(val5)));
00187     } while (1);
00188 }