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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /*
00002  * Weather Station - mbed Weather Platform
00003  * Copyright (c) 2011 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file
00008  * @brief Weather Station
00009  */
00010 
00011 #include "mbed.h"
00012 #include <new>
00013 #include "weather.h"
00014 #include "EthernetPowerControl.h"
00015 
00016 const char VERSION[] = "mbed Weather Platform 0.4.0 (C) 2011 Suga koubou";
00017 Serial pc(USBTX, USBRX);
00018 PwmOut led1(LED1);
00019 DigitalOut led2(LED2), led3(LED3), led4(LED4);
00020 #ifdef USE_3LED
00021 DigitalOut led_red(p28), led_yellow(p23);
00022 #endif
00023 volatile uint32_t locUpTime = 0;
00024 char csv[FORMAT_STR_SIZE];
00025 
00026 
00027 void no_memory () {
00028     printf("panic: can't allocate to memory!\r\n");
00029     exit(-1);
00030 }
00031 
00032 // Interrupt 10Hz
00033 void isr_timer () {
00034 
00035     // uptime
00036     locUpTime = locUpTime + 10;
00037 
00038     pool_ilip();
00039     pool_display();
00040 
00041     LED_NET_ACT_OFF;
00042 }
00043 
00044 void init () {
00045 
00046     init_conf();
00047 
00048     if (conf.baud) {
00049         pc.baud(conf.baud);
00050     }
00051 
00052     if (time(NULL) == -1) {
00053         time_t sec;
00054         if (! fstime(conf.dir, &sec)) {
00055             set_time(sec);
00056         }
00057     }
00058 
00059     init_sensor();
00060     init_ilip(conf.dir);
00061     init_log();
00062     init_display();
00063 
00064     if (init_net()) {
00065         pc.printf("Power down: ethernet\r\n");
00066         PHY_PowerDown();
00067     }
00068 
00069     pc.printf("Interval: %d sec.\r\n", conf.interval);
00070 }
00071 
00072 int main() {
00073     Timer timer;
00074     Ticker ticker;
00075     int ledlevel = 0, ledflg = 0;
00076 
00077     set_new_handler(no_memory); // new handler function
00078 
00079     LED_BUSY_ON;
00080     init();
00081     LED_BUSY_OFF;
00082 
00083     ticker.attach(&isr_timer, 0.1); // Interrupt 10Hz
00084     timer.start();
00085     while(1) {
00086         // main loop
00087         LED_BUSY_ON;
00088         pool_net();
00089 
00090         __disable_irq();
00091         update_sensor();
00092         __enable_irq();
00093 
00094         // create CSV
00095         format_str(conf.csv_mesg, csv, sizeof_1(csv));
00096         pc.printf(csv);
00097         pc.printf("\r\n");
00098         if (write_log(csv)) {
00099             pc.printf("error: can't open file.\r\n");
00100         }
00101 
00102         // I2C LCD or LED
00103         update_display();
00104 
00105         // in/out
00106         exec_ilip(1);
00107 
00108         LED_BUSY_OFF;
00109 
00110         while (locUpTime % 100 != 0) {
00111             pool_net();
00112             wait_ms(100);
00113         }
00114 
00115         // interval (wait)
00116         while (timer.read() < conf.interval) {
00117             ledlevel = ledlevel + (ledflg ? -5 : 5);
00118             if (ledlevel > 100) {
00119                 ledlevel = 100;
00120                 ledflg = 1;
00121             } else
00122             if (ledlevel < 0) {
00123                 ledlevel = 0;
00124                 ledflg = 0;
00125             }
00126             led1 = ledlevel / 100.0;
00127             
00128             pool_net();
00129             wait_ms(100);
00130 
00131             // in/out (timer) 1s
00132             if (locUpTime % 100 == 0) {
00133                 exec_ilip(0);
00134             }
00135 
00136             // for debug
00137             if (pc.readable()) {
00138                 int i;
00139                 i = pc.getc();
00140                 if (i == ' ') {
00141                     break;
00142                 } else {
00143                     pc.printf("( %d )\r\n", (int)timer.read());
00144                 }
00145             }
00146         }
00147         timer.reset();
00148     }
00149 }