GMCStation
Dependencies: EthernetNetIf TextLCD mbed Station
GMCStation.h@2:2870a9512d73, 2012-01-06 (annotated)
- Committer:
- yamaguch
- Date:
- Fri Jan 06 06:50:24 2012 +0000
- Revision:
- 2:2870a9512d73
- Parent:
- 0:3951ca5a2511
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:3951ca5a2511 | 1 | /* |
yamaguch | 0:3951ca5a2511 | 2 | Copyright (c) 2011, Senio Networks, Inc. |
yamaguch | 0:3951ca5a2511 | 3 | |
yamaguch | 0:3951ca5a2511 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yamaguch | 0:3951ca5a2511 | 5 | of this software and associated documentation files (the "Software"), to deal |
yamaguch | 0:3951ca5a2511 | 6 | in the Software without restriction, including without limitation the rights |
yamaguch | 0:3951ca5a2511 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yamaguch | 0:3951ca5a2511 | 8 | copies of the Software, and to permit persons to whom the Software is |
yamaguch | 0:3951ca5a2511 | 9 | furnished to do so, subject to the following conditions: |
yamaguch | 0:3951ca5a2511 | 10 | |
yamaguch | 0:3951ca5a2511 | 11 | The above copyright notice and this permission notice shall be included in |
yamaguch | 0:3951ca5a2511 | 12 | all copies or substantial portions of the Software. |
yamaguch | 0:3951ca5a2511 | 13 | |
yamaguch | 0:3951ca5a2511 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yamaguch | 0:3951ca5a2511 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yamaguch | 0:3951ca5a2511 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yamaguch | 0:3951ca5a2511 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yamaguch | 0:3951ca5a2511 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yamaguch | 0:3951ca5a2511 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yamaguch | 0:3951ca5a2511 | 20 | THE SOFTWARE. |
yamaguch | 0:3951ca5a2511 | 21 | */ |
yamaguch | 0:3951ca5a2511 | 22 | |
yamaguch | 0:3951ca5a2511 | 23 | #ifndef GMCSTATION_H |
yamaguch | 0:3951ca5a2511 | 24 | #define GMCSTATION_H |
yamaguch | 0:3951ca5a2511 | 25 | |
yamaguch | 0:3951ca5a2511 | 26 | #include "Ether.h" |
yamaguch | 0:3951ca5a2511 | 27 | #include "Station.h" |
yamaguch | 0:3951ca5a2511 | 28 | #include "GMCounter.h" |
yamaguch | 0:3951ca5a2511 | 29 | #include "Location.h" |
yamaguch | 0:3951ca5a2511 | 30 | #include "NTPClient.h" |
yamaguch | 0:3951ca5a2511 | 31 | #include "TextLCD.h" |
yamaguch | 0:3951ca5a2511 | 32 | |
yamaguch | 0:3951ca5a2511 | 33 | class GMCStation : public Station { |
yamaguch | 0:3951ca5a2511 | 34 | public: |
yamaguch | 0:3951ca5a2511 | 35 | /** |
yamaguch | 0:3951ca5a2511 | 36 | * Creates a GMCStation server |
yamaguch | 0:3951ca5a2511 | 37 | * |
yamaguch | 0:3951ca5a2511 | 38 | * @param port GMCStation server prot number |
yamaguch | 0:3951ca5a2511 | 39 | * @param gmc reference to a GMCounter instance |
yamaguch | 0:3951ca5a2511 | 40 | * @param location reference to a Location instance |
yamaguch | 0:3951ca5a2511 | 41 | * @param ntpClient reference to an NTPClient instance |
yamaguch | 0:3951ca5a2511 | 42 | * @param lcd reference to a TextLCD instance |
yamaguch | 0:3951ca5a2511 | 43 | */ |
yamaguch | 0:3951ca5a2511 | 44 | GMCStation(int port, GMCounter& gmc, Location& location, NTPClient ntpClient, TextLCD& lcd) |
yamaguch | 0:3951ca5a2511 | 45 | : Station(port), gmc(gmc), location(location), ntpClient(ntpClient), lcd(lcd) { |
yamaguch | 0:3951ca5a2511 | 46 | setBuffer(buf, sizeof(buf)); |
yamaguch | 0:3951ca5a2511 | 47 | addHandler("GetCPM", this, &GMCStation::getCPM); |
yamaguch | 0:3951ca5a2511 | 48 | addHandler("GetAverageCPM", this, &GMCStation::getAverageCPM); |
yamaguch | 0:3951ca5a2511 | 49 | addHandler("GetRadiation", this, &GMCStation::getRadiation); |
yamaguch | 0:3951ca5a2511 | 50 | addHandler("GetAverageRadiation", this, &GMCStation::getAverageRadiation); |
yamaguch | 0:3951ca5a2511 | 51 | addHandler("ToggleBuzzer", this, &GMCStation::toggleBuzzer); |
yamaguch | 0:3951ca5a2511 | 52 | addHandler("ToggleLED", this, &GMCStation::toggleLED); |
yamaguch | 0:3951ca5a2511 | 53 | addHandler("GetLongitude", this, &GMCStation::getLongitude); |
yamaguch | 0:3951ca5a2511 | 54 | addHandler("GetLatitude", this, &GMCStation::getLatitude); |
yamaguch | 0:3951ca5a2511 | 55 | addHandler("GetElevation", this, &GMCStation::getElevation); |
yamaguch | 0:3951ca5a2511 | 56 | addHandler("GetTime", this, &GMCStation::getTime); |
yamaguch | 0:3951ca5a2511 | 57 | addHandler("SetTime", this, &GMCStation::setTime); |
yamaguch | 0:3951ca5a2511 | 58 | addHandler("AdjustTime", this, &GMCStation::adjustTime); |
yamaguch | 0:3951ca5a2511 | 59 | } |
yamaguch | 0:3951ca5a2511 | 60 | |
yamaguch | 0:3951ca5a2511 | 61 | /** |
yamaguch | 0:3951ca5a2511 | 62 | * displays message on the attached LCD |
yamaguch | 0:3951ca5a2511 | 63 | * |
yamaguch | 0:3951ca5a2511 | 64 | * @param message1 1st line of the message buffer |
yamaguch | 0:3951ca5a2511 | 65 | * @param message2 2nd line of the message buffer |
yamaguch | 0:3951ca5a2511 | 66 | */ |
yamaguch | 0:3951ca5a2511 | 67 | void display(char *message1, char *message2 = 0) { |
yamaguch | 0:3951ca5a2511 | 68 | lcd.cls(); |
yamaguch | 0:3951ca5a2511 | 69 | lcd.printf("%s", message1); |
yamaguch | 0:3951ca5a2511 | 70 | if (message2) { |
yamaguch | 0:3951ca5a2511 | 71 | lcd.locate(0, 1); //column, row |
yamaguch | 0:3951ca5a2511 | 72 | lcd.printf("%s", message2); |
yamaguch | 0:3951ca5a2511 | 73 | } |
yamaguch | 0:3951ca5a2511 | 74 | } |
yamaguch | 0:3951ca5a2511 | 75 | |
yamaguch | 0:3951ca5a2511 | 76 | private: |
yamaguch | 0:3951ca5a2511 | 77 | GMCounter& gmc; |
yamaguch | 0:3951ca5a2511 | 78 | Location& location; |
yamaguch | 0:3951ca5a2511 | 79 | NTPClient& ntpClient; |
yamaguch | 0:3951ca5a2511 | 80 | TextLCD& lcd; |
yamaguch | 0:3951ca5a2511 | 81 | char buf[128]; |
yamaguch | 0:3951ca5a2511 | 82 | |
yamaguch | 0:3951ca5a2511 | 83 | void getCPM() { |
yamaguch | 0:3951ca5a2511 | 84 | sprintf(buf, "GetCPM = %d\n", gmc.getCPM()); |
yamaguch | 0:3951ca5a2511 | 85 | } |
yamaguch | 0:3951ca5a2511 | 86 | |
yamaguch | 0:3951ca5a2511 | 87 | void getAverageCPM() { |
yamaguch | 0:3951ca5a2511 | 88 | sprintf(buf, "GetAverageCPM = %3.1f\n", gmc.getAverageCPM()); |
yamaguch | 0:3951ca5a2511 | 89 | } |
yamaguch | 0:3951ca5a2511 | 90 | void getRadiation() { |
yamaguch | 0:3951ca5a2511 | 91 | sprintf(buf, "GetRadiation = %f\n", gmc.getRadiation()); |
yamaguch | 0:3951ca5a2511 | 92 | } |
yamaguch | 0:3951ca5a2511 | 93 | |
yamaguch | 0:3951ca5a2511 | 94 | void getAverageRadiation() { |
yamaguch | 0:3951ca5a2511 | 95 | sprintf(buf, "GetAverageRadiation = %f\n", gmc.getAverageRadiation()); |
yamaguch | 0:3951ca5a2511 | 96 | } |
yamaguch | 0:3951ca5a2511 | 97 | |
yamaguch | 0:3951ca5a2511 | 98 | void toggleBuzzer() { |
yamaguch | 0:3951ca5a2511 | 99 | bool enabled = !gmc.getBuzzer(); |
yamaguch | 0:3951ca5a2511 | 100 | gmc.setBuzzer(enabled); |
yamaguch | 0:3951ca5a2511 | 101 | sprintf(buf, "ToggleBuzzer = %s\n", enabled ? "ON" : "OFF"); |
yamaguch | 0:3951ca5a2511 | 102 | } |
yamaguch | 0:3951ca5a2511 | 103 | |
yamaguch | 0:3951ca5a2511 | 104 | void toggleLED() { |
yamaguch | 0:3951ca5a2511 | 105 | bool enabled = !gmc.getLED(); |
yamaguch | 0:3951ca5a2511 | 106 | gmc.setLED(enabled); |
yamaguch | 0:3951ca5a2511 | 107 | sprintf(buf, "ToggleLED = %s\n", enabled ? "ON" : "OFF"); |
yamaguch | 0:3951ca5a2511 | 108 | } |
yamaguch | 0:3951ca5a2511 | 109 | |
yamaguch | 0:3951ca5a2511 | 110 | void getLongitude() { |
yamaguch | 0:3951ca5a2511 | 111 | sprintf(buf, "GetLongitude = %10.7f\n", location.getLongitude()); |
yamaguch | 0:3951ca5a2511 | 112 | } |
yamaguch | 0:3951ca5a2511 | 113 | |
yamaguch | 0:3951ca5a2511 | 114 | void getLatitude() { |
yamaguch | 0:3951ca5a2511 | 115 | sprintf(buf, "GetLatitude = %10.7f\n", location.getLatitude()); |
yamaguch | 0:3951ca5a2511 | 116 | } |
yamaguch | 0:3951ca5a2511 | 117 | |
yamaguch | 0:3951ca5a2511 | 118 | void getElevation() { |
yamaguch | 0:3951ca5a2511 | 119 | sprintf(buf, "GetElevation = %.1f\n", location.getElevation()); |
yamaguch | 0:3951ca5a2511 | 120 | } |
yamaguch | 0:3951ca5a2511 | 121 | |
yamaguch | 0:3951ca5a2511 | 122 | void getTime() { |
yamaguch | 0:3951ca5a2511 | 123 | time_t seconds = time(NULL) + 9 * 3600; |
yamaguch | 0:3951ca5a2511 | 124 | strftime(buf, sizeof(buf), "GetTime = %F %X\n", localtime(&seconds)); |
yamaguch | 0:3951ca5a2511 | 125 | } |
yamaguch | 0:3951ca5a2511 | 126 | |
yamaguch | 0:3951ca5a2511 | 127 | void setTime() { |
yamaguch | 0:3951ca5a2511 | 128 | struct tm t = {}; |
yamaguch | 0:3951ca5a2511 | 129 | sscanf(buf, "SetTime %d-%d-%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec); |
yamaguch | 0:3951ca5a2511 | 130 | t.tm_year -= 1900; |
yamaguch | 0:3951ca5a2511 | 131 | t.tm_mon -= 1; |
yamaguch | 0:3951ca5a2511 | 132 | time_t seconds = mktime(&t) - 9 * 3600; |
yamaguch | 0:3951ca5a2511 | 133 | set_time(seconds); |
yamaguch | 0:3951ca5a2511 | 134 | strftime(buf, sizeof(buf), "SetTime = %F %X\n", localtime(&seconds)); |
yamaguch | 0:3951ca5a2511 | 135 | } |
yamaguch | 0:3951ca5a2511 | 136 | |
yamaguch | 0:3951ca5a2511 | 137 | void adjustTime() { |
yamaguch | 0:3951ca5a2511 | 138 | if (ntpClient.setTime()) { |
yamaguch | 0:3951ca5a2511 | 139 | time_t seconds = time(NULL) + 9 * 3600; |
yamaguch | 0:3951ca5a2511 | 140 | strftime(buf, sizeof(buf), "AdjustTime = %F %X\n", localtime(&seconds)); |
yamaguch | 0:3951ca5a2511 | 141 | } else { |
yamaguch | 0:3951ca5a2511 | 142 | sprintf(buf, "AdjustTime failed\n"); |
yamaguch | 0:3951ca5a2511 | 143 | } |
yamaguch | 0:3951ca5a2511 | 144 | } |
yamaguch | 0:3951ca5a2511 | 145 | }; |
yamaguch | 0:3951ca5a2511 | 146 | |
yamaguch | 0:3951ca5a2511 | 147 | #endif |