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@6:898c306f7990, 2012-01-15 (annotated)
- Committer:
- okini3939
- Date:
- Sun Jan 15 13:28:12 2012 +0000
- Revision:
- 6:898c306f7990
- Parent:
- 5:90f840432195
- Child:
- 7:f7c98d836f81
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 | 6:898c306f7990 | 52 | init_sensor(); |
okini3939 | 2:a3e5edf84f74 | 53 | init_ilip(conf.dir); |
okini3939 | 2:a3e5edf84f74 | 54 | init_file(); |
okini3939 | 2:a3e5edf84f74 | 55 | init_display(); |
okini3939 | 2:a3e5edf84f74 | 56 | |
okini3939 | 2:a3e5edf84f74 | 57 | if (init_net()) { |
okini3939 | 2:a3e5edf84f74 | 58 | pc.printf("Power down: ethernet\r\n"); |
okini3939 | 2:a3e5edf84f74 | 59 | PHY_PowerDown(); |
okini3939 | 0:bdb53686c194 | 60 | } |
okini3939 | 0:bdb53686c194 | 61 | |
okini3939 | 2:a3e5edf84f74 | 62 | pc.printf("Interval: %d sec.\r\n", conf.interval); |
okini3939 | 0:bdb53686c194 | 63 | } |
okini3939 | 0:bdb53686c194 | 64 | |
okini3939 | 0:bdb53686c194 | 65 | int main() { |
okini3939 | 0:bdb53686c194 | 66 | Timer timer; |
okini3939 | 0:bdb53686c194 | 67 | Ticker ticker; |
okini3939 | 2:a3e5edf84f74 | 68 | int ledlevel = 0, ledflg = 0; |
okini3939 | 2:a3e5edf84f74 | 69 | |
okini3939 | 2:a3e5edf84f74 | 70 | set_new_handler(no_memory); // new handler function |
okini3939 | 0:bdb53686c194 | 71 | |
okini3939 | 0:bdb53686c194 | 72 | LED_BUSY_ON; |
okini3939 | 0:bdb53686c194 | 73 | init(); |
okini3939 | 0:bdb53686c194 | 74 | LED_BUSY_OFF; |
okini3939 | 0:bdb53686c194 | 75 | |
okini3939 | 0:bdb53686c194 | 76 | ticker.attach(&isr_timer, 0.1); // Interrupt 10Hz |
okini3939 | 0:bdb53686c194 | 77 | timer.start(); |
okini3939 | 0:bdb53686c194 | 78 | while(1) { |
okini3939 | 0:bdb53686c194 | 79 | // main loop |
okini3939 | 0:bdb53686c194 | 80 | LED_BUSY_ON; |
okini3939 | 0:bdb53686c194 | 81 | pool_net(); |
okini3939 | 0:bdb53686c194 | 82 | |
okini3939 | 0:bdb53686c194 | 83 | __disable_irq(); |
okini3939 | 0:bdb53686c194 | 84 | update_sensor(); |
okini3939 | 0:bdb53686c194 | 85 | __enable_irq(); |
okini3939 | 0:bdb53686c194 | 86 | |
okini3939 | 0:bdb53686c194 | 87 | // create CSV |
okini3939 | 2:a3e5edf84f74 | 88 | format_str(conf.csv_mesg, csv, sizeof_1(csv)); |
okini3939 | 0:bdb53686c194 | 89 | pc.printf(csv); |
okini3939 | 1:6c7141895545 | 90 | pc.printf("\r\n"); |
okini3939 | 5:90f840432195 | 91 | if (write_log(csv)) { |
okini3939 | 5:90f840432195 | 92 | pc.printf("error: can't open file.\r\n"); |
okini3939 | 5:90f840432195 | 93 | } |
okini3939 | 5:90f840432195 | 94 | |
okini3939 | 5:90f840432195 | 95 | // I2C LCD or LED |
okini3939 | 1:6c7141895545 | 96 | update_display(); |
okini3939 | 0:bdb53686c194 | 97 | |
okini3939 | 0:bdb53686c194 | 98 | // in/out |
okini3939 | 0:bdb53686c194 | 99 | exec_ilip(1); |
okini3939 | 0:bdb53686c194 | 100 | |
okini3939 | 0:bdb53686c194 | 101 | LED_BUSY_OFF; |
okini3939 | 0:bdb53686c194 | 102 | |
okini3939 | 0:bdb53686c194 | 103 | while (locUpTime % 100 != 0) { |
okini3939 | 0:bdb53686c194 | 104 | pool_net(); |
okini3939 | 0:bdb53686c194 | 105 | wait_ms(100); |
okini3939 | 0:bdb53686c194 | 106 | } |
okini3939 | 0:bdb53686c194 | 107 | |
okini3939 | 0:bdb53686c194 | 108 | // interval (wait) |
okini3939 | 2:a3e5edf84f74 | 109 | while (timer.read() < conf.interval) { |
okini3939 | 2:a3e5edf84f74 | 110 | ledlevel = ledlevel + (ledflg ? -5 : 5); |
okini3939 | 2:a3e5edf84f74 | 111 | if (ledlevel > 100) { |
okini3939 | 2:a3e5edf84f74 | 112 | ledlevel = 100; |
okini3939 | 2:a3e5edf84f74 | 113 | ledflg = 1; |
okini3939 | 2:a3e5edf84f74 | 114 | } else |
okini3939 | 2:a3e5edf84f74 | 115 | if (ledlevel < 0) { |
okini3939 | 2:a3e5edf84f74 | 116 | ledlevel = 0; |
okini3939 | 2:a3e5edf84f74 | 117 | ledflg = 0; |
okini3939 | 2:a3e5edf84f74 | 118 | } |
okini3939 | 2:a3e5edf84f74 | 119 | led1 = ledlevel / 100.0; |
okini3939 | 2:a3e5edf84f74 | 120 | |
okini3939 | 0:bdb53686c194 | 121 | pool_net(); |
okini3939 | 0:bdb53686c194 | 122 | wait_ms(100); |
okini3939 | 0:bdb53686c194 | 123 | |
okini3939 | 0:bdb53686c194 | 124 | // in/out (timer) 1s |
okini3939 | 0:bdb53686c194 | 125 | if (locUpTime % 100 == 0) { |
okini3939 | 0:bdb53686c194 | 126 | exec_ilip(0); |
okini3939 | 0:bdb53686c194 | 127 | } |
okini3939 | 0:bdb53686c194 | 128 | |
okini3939 | 0:bdb53686c194 | 129 | // for debug |
okini3939 | 0:bdb53686c194 | 130 | if (pc.readable()) { |
okini3939 | 0:bdb53686c194 | 131 | int i; |
okini3939 | 0:bdb53686c194 | 132 | i = pc.getc(); |
okini3939 | 0:bdb53686c194 | 133 | if (i == ' ') { |
okini3939 | 0:bdb53686c194 | 134 | break; |
okini3939 | 0:bdb53686c194 | 135 | } else { |
okini3939 | 0:bdb53686c194 | 136 | pc.printf("( %d )\r\n", (int)timer.read()); |
okini3939 | 0:bdb53686c194 | 137 | } |
okini3939 | 0:bdb53686c194 | 138 | } |
okini3939 | 0:bdb53686c194 | 139 | } |
okini3939 | 0:bdb53686c194 | 140 | timer.reset(); |
okini3939 | 0:bdb53686c194 | 141 | } |
okini3939 | 0:bdb53686c194 | 142 | } |