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
main.cpp@7:f7c98d836f81, 2012-03-15 (annotated)
- Committer:
- okini3939
- Date:
- Thu Mar 15 02:42:39 2012 +0000
- Revision:
- 7:f7c98d836f81
- Parent:
- 6:898c306f7990
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 | 1:6c7141895545 | 7 | /** @file |
okini3939 | 1:6c7141895545 | 8 | * @brief Weather Station |
okini3939 | 1:6c7141895545 | 9 | */ |
okini3939 | 1:6c7141895545 | 10 | |
okini3939 | 0:bdb53686c194 | 11 | #include "mbed.h" |
okini3939 | 2:a3e5edf84f74 | 12 | #include <new> |
okini3939 | 0:bdb53686c194 | 13 | #include "weather.h" |
okini3939 | 2:a3e5edf84f74 | 14 | #include "EthernetPowerControl.h" |
okini3939 | 0:bdb53686c194 | 15 | |
okini3939 | 0:bdb53686c194 | 16 | const char VERSION[] = "mbed Weather Platform 0.4.0 (C) 2011 Suga koubou"; |
okini3939 | 0:bdb53686c194 | 17 | Serial pc(USBTX, USBRX); |
okini3939 | 2:a3e5edf84f74 | 18 | PwmOut led1(LED1); |
okini3939 | 2:a3e5edf84f74 | 19 | DigitalOut led2(LED2), led3(LED3), led4(LED4); |
okini3939 | 1:6c7141895545 | 20 | #ifdef USE_3LED |
okini3939 | 1:6c7141895545 | 21 | DigitalOut led_red(p28), led_yellow(p23); |
okini3939 | 1:6c7141895545 | 22 | #endif |
okini3939 | 2:a3e5edf84f74 | 23 | volatile uint32_t locUpTime = 0; |
okini3939 | 2:a3e5edf84f74 | 24 | char csv[FORMAT_STR_SIZE]; |
okini3939 | 2:a3e5edf84f74 | 25 | |
okini3939 | 0:bdb53686c194 | 26 | |
okini3939 | 2:a3e5edf84f74 | 27 | void no_memory () { |
okini3939 | 2:a3e5edf84f74 | 28 | printf("panic: can't allocate to memory!\r\n"); |
okini3939 | 2:a3e5edf84f74 | 29 | exit(-1); |
okini3939 | 2:a3e5edf84f74 | 30 | } |
okini3939 | 0:bdb53686c194 | 31 | |
okini3939 | 0:bdb53686c194 | 32 | // Interrupt 10Hz |
okini3939 | 0:bdb53686c194 | 33 | void isr_timer () { |
okini3939 | 0:bdb53686c194 | 34 | |
okini3939 | 0:bdb53686c194 | 35 | // uptime |
okini3939 | 0:bdb53686c194 | 36 | locUpTime = locUpTime + 10; |
okini3939 | 0:bdb53686c194 | 37 | |
okini3939 | 0:bdb53686c194 | 38 | pool_ilip(); |
okini3939 | 0:bdb53686c194 | 39 | pool_display(); |
okini3939 | 0:bdb53686c194 | 40 | |
okini3939 | 1:6c7141895545 | 41 | LED_NET_ACT_OFF; |
okini3939 | 0:bdb53686c194 | 42 | } |
okini3939 | 0:bdb53686c194 | 43 | |
okini3939 | 0:bdb53686c194 | 44 | void init () { |
okini3939 | 0:bdb53686c194 | 45 | |
okini3939 | 0:bdb53686c194 | 46 | init_conf(); |
okini3939 | 0:bdb53686c194 | 47 | |
okini3939 | 2:a3e5edf84f74 | 48 | if (conf.baud) { |
okini3939 | 2:a3e5edf84f74 | 49 | pc.baud(conf.baud); |
okini3939 | 0:bdb53686c194 | 50 | } |
okini3939 | 0:bdb53686c194 | 51 | |
okini3939 | 7:f7c98d836f81 | 52 | if (time(NULL) == -1) { |
okini3939 | 7:f7c98d836f81 | 53 | time_t sec; |
okini3939 | 7:f7c98d836f81 | 54 | if (! fstime(conf.dir, &sec)) { |
okini3939 | 7:f7c98d836f81 | 55 | set_time(sec); |
okini3939 | 7:f7c98d836f81 | 56 | } |
okini3939 | 7:f7c98d836f81 | 57 | } |
okini3939 | 7:f7c98d836f81 | 58 | |
okini3939 | 6:898c306f7990 | 59 | init_sensor(); |
okini3939 | 2:a3e5edf84f74 | 60 | init_ilip(conf.dir); |
okini3939 | 7:f7c98d836f81 | 61 | init_log(); |
okini3939 | 2:a3e5edf84f74 | 62 | init_display(); |
okini3939 | 2:a3e5edf84f74 | 63 | |
okini3939 | 2:a3e5edf84f74 | 64 | if (init_net()) { |
okini3939 | 2:a3e5edf84f74 | 65 | pc.printf("Power down: ethernet\r\n"); |
okini3939 | 2:a3e5edf84f74 | 66 | PHY_PowerDown(); |
okini3939 | 0:bdb53686c194 | 67 | } |
okini3939 | 0:bdb53686c194 | 68 | |
okini3939 | 2:a3e5edf84f74 | 69 | pc.printf("Interval: %d sec.\r\n", conf.interval); |
okini3939 | 0:bdb53686c194 | 70 | } |
okini3939 | 0:bdb53686c194 | 71 | |
okini3939 | 0:bdb53686c194 | 72 | int main() { |
okini3939 | 0:bdb53686c194 | 73 | Timer timer; |
okini3939 | 0:bdb53686c194 | 74 | Ticker ticker; |
okini3939 | 2:a3e5edf84f74 | 75 | int ledlevel = 0, ledflg = 0; |
okini3939 | 2:a3e5edf84f74 | 76 | |
okini3939 | 2:a3e5edf84f74 | 77 | set_new_handler(no_memory); // new handler function |
okini3939 | 0:bdb53686c194 | 78 | |
okini3939 | 0:bdb53686c194 | 79 | LED_BUSY_ON; |
okini3939 | 0:bdb53686c194 | 80 | init(); |
okini3939 | 0:bdb53686c194 | 81 | LED_BUSY_OFF; |
okini3939 | 0:bdb53686c194 | 82 | |
okini3939 | 0:bdb53686c194 | 83 | ticker.attach(&isr_timer, 0.1); // Interrupt 10Hz |
okini3939 | 0:bdb53686c194 | 84 | timer.start(); |
okini3939 | 0:bdb53686c194 | 85 | while(1) { |
okini3939 | 0:bdb53686c194 | 86 | // main loop |
okini3939 | 0:bdb53686c194 | 87 | LED_BUSY_ON; |
okini3939 | 0:bdb53686c194 | 88 | pool_net(); |
okini3939 | 0:bdb53686c194 | 89 | |
okini3939 | 0:bdb53686c194 | 90 | __disable_irq(); |
okini3939 | 0:bdb53686c194 | 91 | update_sensor(); |
okini3939 | 0:bdb53686c194 | 92 | __enable_irq(); |
okini3939 | 0:bdb53686c194 | 93 | |
okini3939 | 0:bdb53686c194 | 94 | // create CSV |
okini3939 | 2:a3e5edf84f74 | 95 | format_str(conf.csv_mesg, csv, sizeof_1(csv)); |
okini3939 | 0:bdb53686c194 | 96 | pc.printf(csv); |
okini3939 | 1:6c7141895545 | 97 | pc.printf("\r\n"); |
okini3939 | 5:90f840432195 | 98 | if (write_log(csv)) { |
okini3939 | 5:90f840432195 | 99 | pc.printf("error: can't open file.\r\n"); |
okini3939 | 5:90f840432195 | 100 | } |
okini3939 | 5:90f840432195 | 101 | |
okini3939 | 5:90f840432195 | 102 | // I2C LCD or LED |
okini3939 | 1:6c7141895545 | 103 | update_display(); |
okini3939 | 0:bdb53686c194 | 104 | |
okini3939 | 0:bdb53686c194 | 105 | // in/out |
okini3939 | 0:bdb53686c194 | 106 | exec_ilip(1); |
okini3939 | 0:bdb53686c194 | 107 | |
okini3939 | 0:bdb53686c194 | 108 | LED_BUSY_OFF; |
okini3939 | 0:bdb53686c194 | 109 | |
okini3939 | 0:bdb53686c194 | 110 | while (locUpTime % 100 != 0) { |
okini3939 | 0:bdb53686c194 | 111 | pool_net(); |
okini3939 | 0:bdb53686c194 | 112 | wait_ms(100); |
okini3939 | 0:bdb53686c194 | 113 | } |
okini3939 | 0:bdb53686c194 | 114 | |
okini3939 | 0:bdb53686c194 | 115 | // interval (wait) |
okini3939 | 2:a3e5edf84f74 | 116 | while (timer.read() < conf.interval) { |
okini3939 | 2:a3e5edf84f74 | 117 | ledlevel = ledlevel + (ledflg ? -5 : 5); |
okini3939 | 2:a3e5edf84f74 | 118 | if (ledlevel > 100) { |
okini3939 | 2:a3e5edf84f74 | 119 | ledlevel = 100; |
okini3939 | 2:a3e5edf84f74 | 120 | ledflg = 1; |
okini3939 | 2:a3e5edf84f74 | 121 | } else |
okini3939 | 2:a3e5edf84f74 | 122 | if (ledlevel < 0) { |
okini3939 | 2:a3e5edf84f74 | 123 | ledlevel = 0; |
okini3939 | 2:a3e5edf84f74 | 124 | ledflg = 0; |
okini3939 | 2:a3e5edf84f74 | 125 | } |
okini3939 | 2:a3e5edf84f74 | 126 | led1 = ledlevel / 100.0; |
okini3939 | 2:a3e5edf84f74 | 127 | |
okini3939 | 0:bdb53686c194 | 128 | pool_net(); |
okini3939 | 0:bdb53686c194 | 129 | wait_ms(100); |
okini3939 | 0:bdb53686c194 | 130 | |
okini3939 | 0:bdb53686c194 | 131 | // in/out (timer) 1s |
okini3939 | 0:bdb53686c194 | 132 | if (locUpTime % 100 == 0) { |
okini3939 | 0:bdb53686c194 | 133 | exec_ilip(0); |
okini3939 | 0:bdb53686c194 | 134 | } |
okini3939 | 0:bdb53686c194 | 135 | |
okini3939 | 0:bdb53686c194 | 136 | // for debug |
okini3939 | 0:bdb53686c194 | 137 | if (pc.readable()) { |
okini3939 | 0:bdb53686c194 | 138 | int i; |
okini3939 | 0:bdb53686c194 | 139 | i = pc.getc(); |
okini3939 | 0:bdb53686c194 | 140 | if (i == ' ') { |
okini3939 | 0:bdb53686c194 | 141 | break; |
okini3939 | 0:bdb53686c194 | 142 | } else { |
okini3939 | 0:bdb53686c194 | 143 | pc.printf("( %d )\r\n", (int)timer.read()); |
okini3939 | 0:bdb53686c194 | 144 | } |
okini3939 | 0:bdb53686c194 | 145 | } |
okini3939 | 0:bdb53686c194 | 146 | } |
okini3939 | 0:bdb53686c194 | 147 | timer.reset(); |
okini3939 | 0:bdb53686c194 | 148 | } |
okini3939 | 0:bdb53686c194 | 149 | } |