Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ConfigurationHandler/Controls/TimerControl.cpp@205:3c84af5f711f, 2016-10-07 (annotated)
- Committer:
- jmarkel44
- Date:
- Fri Oct 07 17:41:23 2016 +0000
- Revision:
- 205:3c84af5f711f
- Parent:
- 192:052a419837fa
- Child:
- 242:3b0086a6d625
updated timers;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jmarkel44 | 19:9bc8fabeddfa | 1 | /****************************************************************************** |
| jmarkel44 | 19:9bc8fabeddfa | 2 | * |
| jmarkel44 | 131:a290a3934132 | 3 | * File: TimerControl.cpp |
| jmarkel44 | 35:6235ef67faa1 | 4 | * Desciption: ICE Timer Control Class implementation |
| jmarkel44 | 19:9bc8fabeddfa | 5 | * |
| jmarkel44 | 19:9bc8fabeddfa | 6 | *****************************************************************************/ |
| jmarkel44 | 19:9bc8fabeddfa | 7 | #include "TimerControl.h" |
| jmarkel44 | 19:9bc8fabeddfa | 8 | #include "mDot.h" |
| jmarkel44 | 35:6235ef67faa1 | 9 | #include "MbedJSONValue.h" |
| jmarkel44 | 133:c871de2d2b90 | 10 | #include "global.h" |
| jmarkel44 | 35:6235ef67faa1 | 11 | #include <string> |
| jmarkel44 | 205:3c84af5f711f | 12 | #include <iostream> |
| jmarkel44 | 205:3c84af5f711f | 13 | #include <iomanip> |
| jmarkel44 | 19:9bc8fabeddfa | 14 | |
| jmarkel44 | 19:9bc8fabeddfa | 15 | extern mDot *GLOBAL_mdot; |
| jmarkel44 | 19:9bc8fabeddfa | 16 | |
| jmarkel44 | 122:4db48b933115 | 17 | // |
| jmarkel44 | 122:4db48b933115 | 18 | // method: load |
| jmarkel44 | 122:4db48b933115 | 19 | // description: load the pertinents from the control file |
| jmarkel44 | 128:534bf29132f8 | 20 | // |
| jmarkel44 | 122:4db48b933115 | 21 | // @param _controlFile |
| jmarkel44 | 122:4db48b933115 | 22 | // @return true if loaded; false otherwise |
| jmarkel44 | 122:4db48b933115 | 23 | // |
| jmarkel44 | 35:6235ef67faa1 | 24 | bool TimerControl::load(string _controlFile) |
| jmarkel44 | 122:4db48b933115 | 25 | { |
| jmarkel44 | 122:4db48b933115 | 26 | MbedJSONValue json_value; // json parsing element |
| jmarkel44 | 122:4db48b933115 | 27 | |
| jmarkel44 | 19:9bc8fabeddfa | 28 | // try to open the control file |
| jmarkel44 | 122:4db48b933115 | 29 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(_controlFile.c_str(), mDot::FM_RDONLY); |
| jmarkel44 | 122:4db48b933115 | 30 | if ( file.fd < 0 ) { |
| jmarkel44 | 35:6235ef67faa1 | 31 | return false; |
| jmarkel44 | 122:4db48b933115 | 32 | } |
| jmarkel44 | 35:6235ef67faa1 | 33 | |
| jmarkel44 | 35:6235ef67faa1 | 34 | // read the data into a buffer |
| jmarkel44 | 177:9ec90c8e3ce1 | 35 | char dataBuf[350]; |
| jmarkel44 | 35:6235ef67faa1 | 36 | |
| jmarkel44 | 35:6235ef67faa1 | 37 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 35:6235ef67faa1 | 38 | if ( bytes_read != sizeof(dataBuf) ) { |
| jmarkel44 | 35:6235ef67faa1 | 39 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
| jmarkel44 | 35:6235ef67faa1 | 40 | return false; |
| jmarkel44 | 35:6235ef67faa1 | 41 | } |
| jmarkel44 | 122:4db48b933115 | 42 | |
| jmarkel44 | 122:4db48b933115 | 43 | // close the file |
| jmarkel44 | 35:6235ef67faa1 | 44 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 35:6235ef67faa1 | 45 | |
| jmarkel44 | 35:6235ef67faa1 | 46 | parse(json_value, dataBuf); |
| jmarkel44 | 35:6235ef67faa1 | 47 | |
| jmarkel44 | 192:052a419837fa | 48 | if ( !json_value.hasMember("id") || |
| jmarkel44 | 192:052a419837fa | 49 | !json_value.hasMember("output") || |
| jmarkel44 | 192:052a419837fa | 50 | !json_value.hasMember("priority") || |
| jmarkel44 | 192:052a419837fa | 51 | !json_value.hasMember("starttime") || |
| jmarkel44 | 192:052a419837fa | 52 | !json_value.hasMember("duration") ) { |
| jmarkel44 | 192:052a419837fa | 53 | logError("Timer Control file is missing expected tags"); |
| jmarkel44 | 192:052a419837fa | 54 | return false; |
| jmarkel44 | 192:052a419837fa | 55 | } |
| jmarkel44 | 192:052a419837fa | 56 | |
| jmarkel44 | 132:45821e189dd0 | 57 | // the pertinents |
| jmarkel44 | 122:4db48b933115 | 58 | controlFile = _controlFile; |
| jmarkel44 | 183:44f7fea6b208 | 59 | id = json_value ["id"].get<string>(); |
| jmarkel44 | 183:44f7fea6b208 | 60 | output = json_value ["output"].get<string>(); |
| jmarkel44 | 156:44f87c5a83ae | 61 | priority = atoi(json_value["priority"].get<string>().c_str()); |
| jmarkel44 | 156:44f87c5a83ae | 62 | startTime = atol(json_value["starttime"].get<string>().c_str()); |
| jmarkel44 | 156:44f87c5a83ae | 63 | duration = atoi(json_value["duration"].get<string>().c_str()); |
| jmarkel44 | 122:4db48b933115 | 64 | |
| jmarkel44 | 122:4db48b933115 | 65 | return true; |
| jmarkel44 | 122:4db48b933115 | 66 | } |
| jmarkel44 | 122:4db48b933115 | 67 | |
| jmarkel44 | 132:45821e189dd0 | 68 | // |
| jmarkel44 | 132:45821e189dd0 | 69 | // method: start |
| jmarkel44 | 132:45821e189dd0 | 70 | // description: initialize the control |
| jmarkel44 | 133:c871de2d2b90 | 71 | // |
| jmarkel44 | 132:45821e189dd0 | 72 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 73 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 74 | // |
| jmarkel44 | 126:c85ac6a8e9af | 75 | void TimerControl::start(void) |
| jmarkel44 | 126:c85ac6a8e9af | 76 | { |
| jmarkel44 | 128:534bf29132f8 | 77 | currentState = STATE_OFF; |
| jmarkel44 | 128:534bf29132f8 | 78 | } |
| jmarkel44 | 128:534bf29132f8 | 79 | |
| jmarkel44 | 132:45821e189dd0 | 80 | // |
| jmarkel44 | 132:45821e189dd0 | 81 | // method: timerStart |
| jmarkel44 | 132:45821e189dd0 | 82 | // description: examine the timestamp to determine if the timer control |
| jmarkel44 | 132:45821e189dd0 | 83 | // should be running |
| jmarkel44 | 132:45821e189dd0 | 84 | // |
| jmarkel44 | 132:45821e189dd0 | 85 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 86 | // @return true if timer should be running; false otherwise |
| jmarkel44 | 132:45821e189dd0 | 87 | // |
| jmarkel44 | 132:45821e189dd0 | 88 | bool TimerControl::timerStart(void) |
| jmarkel44 | 128:534bf29132f8 | 89 | { |
| jmarkel44 | 156:44f87c5a83ae | 90 | unsigned long currentTime = time(NULL); |
| jmarkel44 | 156:44f87c5a83ae | 91 | |
| jmarkel44 | 156:44f87c5a83ae | 92 | if ( currentTime < startTime ) |
| jmarkel44 | 156:44f87c5a83ae | 93 | return false; |
| jmarkel44 | 156:44f87c5a83ae | 94 | |
| jmarkel44 | 156:44f87c5a83ae | 95 | if ( currentTime >= startTime && currentTime <= (startTime + duration) ) { |
| jmarkel44 | 156:44f87c5a83ae | 96 | return true; |
| jmarkel44 | 132:45821e189dd0 | 97 | } |
| jmarkel44 | 133:c871de2d2b90 | 98 | return false; |
| jmarkel44 | 132:45821e189dd0 | 99 | } |
| jmarkel44 | 132:45821e189dd0 | 100 | // |
| jmarkel44 | 132:45821e189dd0 | 101 | // method: timerStop |
| jmarkel44 | 132:45821e189dd0 | 102 | // description: determines if a running timer should has reached its duration |
| jmarkel44 | 132:45821e189dd0 | 103 | // |
| jmarkel44 | 132:45821e189dd0 | 104 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 105 | // @return true if the timer has expired; false otherwise |
| jmarkel44 | 132:45821e189dd0 | 106 | // |
| jmarkel44 | 133:c871de2d2b90 | 107 | bool TimerControl::timerStop(void) |
| jmarkel44 | 132:45821e189dd0 | 108 | { |
| jmarkel44 | 156:44f87c5a83ae | 109 | if ( time(NULL) >= startTime + duration ) { |
| jmarkel44 | 132:45821e189dd0 | 110 | return true; |
| jmarkel44 | 132:45821e189dd0 | 111 | } |
| jmarkel44 | 128:534bf29132f8 | 112 | return false; |
| jmarkel44 | 126:c85ac6a8e9af | 113 | } |
| jmarkel44 | 126:c85ac6a8e9af | 114 | |
| jmarkel44 | 132:45821e189dd0 | 115 | // |
| jmarkel44 | 133:c871de2d2b90 | 116 | // method: update |
| jmarkel44 | 132:45821e189dd0 | 117 | // description: run the simplified state machine |
| jmarkel44 | 132:45821e189dd0 | 118 | // |
| jmarkel44 | 132:45821e189dd0 | 119 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 120 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 121 | // |
| jmarkel44 | 157:0d79678ed00f | 122 | TimerError_t TimerControl::update(void) |
| jmarkel44 | 126:c85ac6a8e9af | 123 | { |
| jmarkel44 | 157:0d79678ed00f | 124 | TimerError_t rc = TIMER_CONTROL_OK; |
| jmarkel44 | 192:052a419837fa | 125 | |
| jmarkel44 | 128:534bf29132f8 | 126 | switch ( this->currentState ) { |
| jmarkel44 | 128:534bf29132f8 | 127 | case STATE_OFF: |
| jmarkel44 | 132:45821e189dd0 | 128 | if ( this->timerStart() ) { |
| jmarkel44 | 128:534bf29132f8 | 129 | currentState = STATE_RUNNING; |
| jmarkel44 | 133:c871de2d2b90 | 130 | this->startFeed(); |
| jmarkel44 | 128:534bf29132f8 | 131 | } |
| jmarkel44 | 128:534bf29132f8 | 132 | break; |
| jmarkel44 | 128:534bf29132f8 | 133 | case STATE_RUNNING: |
| jmarkel44 | 132:45821e189dd0 | 134 | if ( this->timerStop() ) { |
| jmarkel44 | 128:534bf29132f8 | 135 | currentState = STATE_OFF; |
| jmarkel44 | 133:c871de2d2b90 | 136 | this->stopFeed(); |
| jmarkel44 | 156:44f87c5a83ae | 137 | this->unregisterControl(); |
| jmarkel44 | 157:0d79678ed00f | 138 | rc = TIMER_CONTROL_DESTROY; |
| jmarkel44 | 128:534bf29132f8 | 139 | } |
| jmarkel44 | 128:534bf29132f8 | 140 | break; |
| jmarkel44 | 128:534bf29132f8 | 141 | case STATE_DISABLED: |
| jmarkel44 | 128:534bf29132f8 | 142 | // not implelmented |
| jmarkel44 | 128:534bf29132f8 | 143 | default: |
| jmarkel44 | 128:534bf29132f8 | 144 | break; |
| jmarkel44 | 128:534bf29132f8 | 145 | } |
| jmarkel44 | 157:0d79678ed00f | 146 | return rc; |
| jmarkel44 | 192:052a419837fa | 147 | |
| jmarkel44 | 126:c85ac6a8e9af | 148 | } |
| jmarkel44 | 126:c85ac6a8e9af | 149 | |
| jmarkel44 | 122:4db48b933115 | 150 | // |
| jmarkel44 | 133:c871de2d2b90 | 151 | // method: startFeed |
| jmarkel44 | 133:c871de2d2b90 | 152 | // description: signal the output thread to start a feed |
| jmarkel44 | 133:c871de2d2b90 | 153 | // |
| jmarkel44 | 133:c871de2d2b90 | 154 | // @param none |
| jmarkel44 | 133:c871de2d2b90 | 155 | // @return none |
| jmarkel44 | 133:c871de2d2b90 | 156 | void TimerControl::startFeed(void) |
| jmarkel44 | 133:c871de2d2b90 | 157 | { |
| jmarkel44 | 133:c871de2d2b90 | 158 | logInfo("%s: %s attempting to start feed on relay %s\n", |
| jmarkel44 | 133:c871de2d2b90 | 159 | __func__, controlFile.c_str(), output.c_str()); |
| jmarkel44 | 133:c871de2d2b90 | 160 | |
| jmarkel44 | 133:c871de2d2b90 | 161 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 133:c871de2d2b90 | 162 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 133:c871de2d2b90 | 163 | |
| jmarkel44 | 133:c871de2d2b90 | 164 | output_mail->action = ACTION_CONTROL_ON; |
| jmarkel44 | 133:c871de2d2b90 | 165 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 166 | output_mail->priority = priority; |
| jmarkel44 | 133:c871de2d2b90 | 167 | |
| jmarkel44 | 133:c871de2d2b90 | 168 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 133:c871de2d2b90 | 169 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 133:c871de2d2b90 | 170 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 133:c871de2d2b90 | 171 | } |
| jmarkel44 | 133:c871de2d2b90 | 172 | |
| jmarkel44 | 133:c871de2d2b90 | 173 | // |
| jmarkel44 | 133:c871de2d2b90 | 174 | // method: stopFeed |
| jmarkel44 | 133:c871de2d2b90 | 175 | // description: signal the output thread to stop a feed |
| jmarkel44 | 133:c871de2d2b90 | 176 | // |
| jmarkel44 | 133:c871de2d2b90 | 177 | // @param none |
| jmarkel44 | 133:c871de2d2b90 | 178 | // @return none |
| jmarkel44 | 133:c871de2d2b90 | 179 | void TimerControl::stopFeed(void) |
| jmarkel44 | 133:c871de2d2b90 | 180 | { |
| jmarkel44 | 133:c871de2d2b90 | 181 | logInfo("%s: %s attempting to start feed on relay %s\n", |
| jmarkel44 | 133:c871de2d2b90 | 182 | __func__, controlFile.c_str(), output.c_str()); |
| jmarkel44 | 133:c871de2d2b90 | 183 | |
| jmarkel44 | 133:c871de2d2b90 | 184 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 133:c871de2d2b90 | 185 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 133:c871de2d2b90 | 186 | |
| jmarkel44 | 133:c871de2d2b90 | 187 | output_mail->action = ACTION_CONTROL_OFF; |
| jmarkel44 | 133:c871de2d2b90 | 188 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 189 | output_mail->priority = priority; |
| jmarkel44 | 133:c871de2d2b90 | 190 | |
| jmarkel44 | 133:c871de2d2b90 | 191 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 133:c871de2d2b90 | 192 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 133:c871de2d2b90 | 193 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 133:c871de2d2b90 | 194 | } |
| jmarkel44 | 192:052a419837fa | 195 | |
| jmarkel44 | 153:0845c7bf236a | 196 | // |
| jmarkel44 | 136:6ad7ba157b70 | 197 | // Method: unregisterControl |
| jmarkel44 | 136:6ad7ba157b70 | 198 | // Description: send OFF indication to Output Master for this control's |
| jmarkel44 | 136:6ad7ba157b70 | 199 | // relay |
| jmarkel44 | 136:6ad7ba157b70 | 200 | // |
| jmarkel44 | 136:6ad7ba157b70 | 201 | // @param none |
| jmarkel44 | 136:6ad7ba157b70 | 202 | // @return none |
| jmarkel44 | 153:0845c7bf236a | 203 | // |
| jmarkel44 | 136:6ad7ba157b70 | 204 | void TimerControl::unregisterControl(void) |
| jmarkel44 | 136:6ad7ba157b70 | 205 | { |
| jmarkel44 | 136:6ad7ba157b70 | 206 | logInfo("%s: %s attempting to unregister %s\n", |
| jmarkel44 | 136:6ad7ba157b70 | 207 | __func__, controlFile.c_str()); |
| jmarkel44 | 136:6ad7ba157b70 | 208 | |
| jmarkel44 | 136:6ad7ba157b70 | 209 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 136:6ad7ba157b70 | 210 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 156:44f87c5a83ae | 211 | |
| jmarkel44 | 156:44f87c5a83ae | 212 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
| jmarkel44 | 156:44f87c5a83ae | 213 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 214 | output_mail->priority = priority; |
| jmarkel44 | 136:6ad7ba157b70 | 215 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 136:6ad7ba157b70 | 216 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 156:44f87c5a83ae | 217 | |
| jmarkel44 | 136:6ad7ba157b70 | 218 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 136:6ad7ba157b70 | 219 | } |
| jmarkel44 | 136:6ad7ba157b70 | 220 | |
| jmarkel44 | 133:c871de2d2b90 | 221 | // |
| jmarkel44 | 122:4db48b933115 | 222 | // methid: display |
| jmarkel44 | 122:4db48b933115 | 223 | // description: display the elements of this timer control object |
| jmarkel44 | 122:4db48b933115 | 224 | // |
| jmarkel44 | 122:4db48b933115 | 225 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 226 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 227 | // |
| jmarkel44 | 122:4db48b933115 | 228 | void TimerControl::display(void) |
| jmarkel44 | 122:4db48b933115 | 229 | { |
| jmarkel44 | 128:534bf29132f8 | 230 | string mapper[] = { "OFF", |
| jmarkel44 | 128:534bf29132f8 | 231 | "RUNNING", |
| jmarkel44 | 128:534bf29132f8 | 232 | "DISABLED" |
| jmarkel44 | 128:534bf29132f8 | 233 | }; |
| jmarkel44 | 133:c871de2d2b90 | 234 | |
| jmarkel44 | 205:3c84af5f711f | 235 | printf("\r\n"); |
| jmarkel44 | 205:3c84af5f711f | 236 | cout << left << setw(10) << setfill(' ') << "timer:"; |
| jmarkel44 | 205:3c84af5f711f | 237 | cout << left << setw(32) << setfill(' ') << controlFile; |
| jmarkel44 | 205:3c84af5f711f | 238 | cout << left << setw(20) << setfill(' ') << id; |
| jmarkel44 | 205:3c84af5f711f | 239 | cout << left << setw(20) << setfill(' ') << output; |
| jmarkel44 | 205:3c84af5f711f | 240 | cout << "pri:" << left << setw(12) << setfill(' ') << priority; |
| jmarkel44 | 205:3c84af5f711f | 241 | cout << "start:" << left << setw(12) << setfill(' ') << startTime; |
| jmarkel44 | 205:3c84af5f711f | 242 | cout << "duration: " << left << setw(12) << setfill(' ') << duration; |
| jmarkel44 | 205:3c84af5f711f | 243 | cout << "end: " << left << setw(12) << setfill(' ') << startTime + duration; |
| jmarkel44 | 205:3c84af5f711f | 244 | cout << "remaining: " << left << setw(12) << setfill(' ') << startTime + duration - time(NULL); |
| jmarkel44 | 205:3c84af5f711f | 245 | cout << left << setw(12) << setfill(' ') << mapper[currentState]; |
| jmarkel44 | 205:3c84af5f711f | 246 | cout.flush(); |
| jmarkel44 | 35:6235ef67faa1 | 247 | } |
