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@253:ae850c19cf81, 2016-10-21 (annotated)
- Committer:
- jmarkel44
- Date:
- Fri Oct 21 18:35:29 2016 +0000
- Revision:
- 253:ae850c19cf81
- Parent:
- 242:3b0086a6d625
- Child:
- 276:851554207c77
updated filenames for preload command
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 | 242:3b0086a6d625 | 9 | #include "cJSON.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 | 19:9bc8fabeddfa | 26 | // try to open the control file |
| jmarkel44 | 122:4db48b933115 | 27 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(_controlFile.c_str(), mDot::FM_RDONLY); |
| jmarkel44 | 122:4db48b933115 | 28 | if ( file.fd < 0 ) { |
| jmarkel44 | 35:6235ef67faa1 | 29 | return false; |
| jmarkel44 | 122:4db48b933115 | 30 | } |
| jmarkel44 | 35:6235ef67faa1 | 31 | |
| jmarkel44 | 35:6235ef67faa1 | 32 | // read the data into a buffer |
| jmarkel44 | 177:9ec90c8e3ce1 | 33 | char dataBuf[350]; |
| jmarkel44 | 35:6235ef67faa1 | 34 | |
| jmarkel44 | 35:6235ef67faa1 | 35 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 35:6235ef67faa1 | 36 | if ( bytes_read != sizeof(dataBuf) ) { |
| jmarkel44 | 35:6235ef67faa1 | 37 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
| jmarkel44 | 35:6235ef67faa1 | 38 | return false; |
| jmarkel44 | 35:6235ef67faa1 | 39 | } |
| jmarkel44 | 122:4db48b933115 | 40 | |
| jmarkel44 | 122:4db48b933115 | 41 | // close the file |
| jmarkel44 | 35:6235ef67faa1 | 42 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 35:6235ef67faa1 | 43 | |
| jmarkel44 | 242:3b0086a6d625 | 44 | cJSON * root = cJSON_Parse(dataBuf); |
| jmarkel44 | 242:3b0086a6d625 | 45 | |
| jmarkel44 | 242:3b0086a6d625 | 46 | id = cJSON_GetObjectItem(root,"id")->valuestring; |
| jmarkel44 | 242:3b0086a6d625 | 47 | output = cJSON_GetObjectItem(root, "output")->valuestring; |
| jmarkel44 | 242:3b0086a6d625 | 48 | priority = atoi(cJSON_GetObjectItem(root, "priority")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 49 | startTime = atoi(cJSON_GetObjectItem(root, "starttime")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 50 | duration = atoi(cJSON_GetObjectItem(root, "duration")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 51 | |
| jmarkel44 | 122:4db48b933115 | 52 | return true; |
| jmarkel44 | 122:4db48b933115 | 53 | } |
| jmarkel44 | 122:4db48b933115 | 54 | |
| jmarkel44 | 132:45821e189dd0 | 55 | // |
| jmarkel44 | 132:45821e189dd0 | 56 | // method: start |
| jmarkel44 | 132:45821e189dd0 | 57 | // description: initialize the control |
| jmarkel44 | 133:c871de2d2b90 | 58 | // |
| jmarkel44 | 132:45821e189dd0 | 59 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 60 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 61 | // |
| jmarkel44 | 126:c85ac6a8e9af | 62 | void TimerControl::start(void) |
| jmarkel44 | 126:c85ac6a8e9af | 63 | { |
| jmarkel44 | 128:534bf29132f8 | 64 | currentState = STATE_OFF; |
| jmarkel44 | 128:534bf29132f8 | 65 | } |
| jmarkel44 | 128:534bf29132f8 | 66 | |
| jmarkel44 | 132:45821e189dd0 | 67 | // |
| jmarkel44 | 132:45821e189dd0 | 68 | // method: timerStart |
| jmarkel44 | 132:45821e189dd0 | 69 | // description: examine the timestamp to determine if the timer control |
| jmarkel44 | 132:45821e189dd0 | 70 | // should be running |
| jmarkel44 | 132:45821e189dd0 | 71 | // |
| jmarkel44 | 132:45821e189dd0 | 72 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 73 | // @return true if timer should be running; false otherwise |
| jmarkel44 | 132:45821e189dd0 | 74 | // |
| jmarkel44 | 132:45821e189dd0 | 75 | bool TimerControl::timerStart(void) |
| jmarkel44 | 128:534bf29132f8 | 76 | { |
| jmarkel44 | 156:44f87c5a83ae | 77 | unsigned long currentTime = time(NULL); |
| jmarkel44 | 156:44f87c5a83ae | 78 | |
| jmarkel44 | 156:44f87c5a83ae | 79 | if ( currentTime < startTime ) |
| jmarkel44 | 156:44f87c5a83ae | 80 | return false; |
| jmarkel44 | 156:44f87c5a83ae | 81 | |
| jmarkel44 | 156:44f87c5a83ae | 82 | if ( currentTime >= startTime && currentTime <= (startTime + duration) ) { |
| jmarkel44 | 156:44f87c5a83ae | 83 | return true; |
| jmarkel44 | 132:45821e189dd0 | 84 | } |
| jmarkel44 | 133:c871de2d2b90 | 85 | return false; |
| jmarkel44 | 132:45821e189dd0 | 86 | } |
| jmarkel44 | 132:45821e189dd0 | 87 | // |
| jmarkel44 | 132:45821e189dd0 | 88 | // method: timerStop |
| jmarkel44 | 132:45821e189dd0 | 89 | // description: determines if a running timer should has reached its duration |
| jmarkel44 | 132:45821e189dd0 | 90 | // |
| jmarkel44 | 132:45821e189dd0 | 91 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 92 | // @return true if the timer has expired; false otherwise |
| jmarkel44 | 132:45821e189dd0 | 93 | // |
| jmarkel44 | 133:c871de2d2b90 | 94 | bool TimerControl::timerStop(void) |
| jmarkel44 | 132:45821e189dd0 | 95 | { |
| jmarkel44 | 156:44f87c5a83ae | 96 | if ( time(NULL) >= startTime + duration ) { |
| jmarkel44 | 132:45821e189dd0 | 97 | return true; |
| jmarkel44 | 132:45821e189dd0 | 98 | } |
| jmarkel44 | 128:534bf29132f8 | 99 | return false; |
| jmarkel44 | 126:c85ac6a8e9af | 100 | } |
| jmarkel44 | 126:c85ac6a8e9af | 101 | |
| jmarkel44 | 132:45821e189dd0 | 102 | // |
| jmarkel44 | 133:c871de2d2b90 | 103 | // method: update |
| jmarkel44 | 132:45821e189dd0 | 104 | // description: run the simplified state machine |
| jmarkel44 | 132:45821e189dd0 | 105 | // |
| jmarkel44 | 132:45821e189dd0 | 106 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 107 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 108 | // |
| jmarkel44 | 157:0d79678ed00f | 109 | TimerError_t TimerControl::update(void) |
| jmarkel44 | 126:c85ac6a8e9af | 110 | { |
| jmarkel44 | 157:0d79678ed00f | 111 | TimerError_t rc = TIMER_CONTROL_OK; |
| jmarkel44 | 192:052a419837fa | 112 | |
| jmarkel44 | 128:534bf29132f8 | 113 | switch ( this->currentState ) { |
| jmarkel44 | 128:534bf29132f8 | 114 | case STATE_OFF: |
| jmarkel44 | 132:45821e189dd0 | 115 | if ( this->timerStart() ) { |
| jmarkel44 | 128:534bf29132f8 | 116 | currentState = STATE_RUNNING; |
| jmarkel44 | 133:c871de2d2b90 | 117 | this->startFeed(); |
| jmarkel44 | 128:534bf29132f8 | 118 | } |
| jmarkel44 | 128:534bf29132f8 | 119 | break; |
| jmarkel44 | 128:534bf29132f8 | 120 | case STATE_RUNNING: |
| jmarkel44 | 132:45821e189dd0 | 121 | if ( this->timerStop() ) { |
| jmarkel44 | 128:534bf29132f8 | 122 | currentState = STATE_OFF; |
| jmarkel44 | 133:c871de2d2b90 | 123 | this->stopFeed(); |
| jmarkel44 | 156:44f87c5a83ae | 124 | this->unregisterControl(); |
| jmarkel44 | 157:0d79678ed00f | 125 | rc = TIMER_CONTROL_DESTROY; |
| jmarkel44 | 128:534bf29132f8 | 126 | } |
| jmarkel44 | 128:534bf29132f8 | 127 | break; |
| jmarkel44 | 128:534bf29132f8 | 128 | case STATE_DISABLED: |
| jmarkel44 | 128:534bf29132f8 | 129 | // not implelmented |
| jmarkel44 | 128:534bf29132f8 | 130 | default: |
| jmarkel44 | 128:534bf29132f8 | 131 | break; |
| jmarkel44 | 128:534bf29132f8 | 132 | } |
| jmarkel44 | 157:0d79678ed00f | 133 | return rc; |
| jmarkel44 | 192:052a419837fa | 134 | |
| jmarkel44 | 126:c85ac6a8e9af | 135 | } |
| jmarkel44 | 126:c85ac6a8e9af | 136 | |
| jmarkel44 | 122:4db48b933115 | 137 | // |
| jmarkel44 | 133:c871de2d2b90 | 138 | // method: startFeed |
| jmarkel44 | 133:c871de2d2b90 | 139 | // description: signal the output thread to start a feed |
| jmarkel44 | 133:c871de2d2b90 | 140 | // |
| jmarkel44 | 133:c871de2d2b90 | 141 | // @param none |
| jmarkel44 | 133:c871de2d2b90 | 142 | // @return none |
| jmarkel44 | 133:c871de2d2b90 | 143 | void TimerControl::startFeed(void) |
| jmarkel44 | 133:c871de2d2b90 | 144 | { |
| jmarkel44 | 133:c871de2d2b90 | 145 | logInfo("%s: %s attempting to start feed on relay %s\n", |
| jmarkel44 | 133:c871de2d2b90 | 146 | __func__, controlFile.c_str(), output.c_str()); |
| jmarkel44 | 133:c871de2d2b90 | 147 | |
| jmarkel44 | 133:c871de2d2b90 | 148 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 133:c871de2d2b90 | 149 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 133:c871de2d2b90 | 150 | |
| jmarkel44 | 133:c871de2d2b90 | 151 | output_mail->action = ACTION_CONTROL_ON; |
| jmarkel44 | 133:c871de2d2b90 | 152 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 153 | output_mail->priority = priority; |
| jmarkel44 | 133:c871de2d2b90 | 154 | |
| jmarkel44 | 133:c871de2d2b90 | 155 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 133:c871de2d2b90 | 156 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 133:c871de2d2b90 | 157 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 133:c871de2d2b90 | 158 | } |
| jmarkel44 | 133:c871de2d2b90 | 159 | |
| jmarkel44 | 133:c871de2d2b90 | 160 | // |
| jmarkel44 | 133:c871de2d2b90 | 161 | // method: stopFeed |
| jmarkel44 | 133:c871de2d2b90 | 162 | // description: signal the output thread to stop a feed |
| jmarkel44 | 133:c871de2d2b90 | 163 | // |
| jmarkel44 | 133:c871de2d2b90 | 164 | // @param none |
| jmarkel44 | 133:c871de2d2b90 | 165 | // @return none |
| jmarkel44 | 133:c871de2d2b90 | 166 | void TimerControl::stopFeed(void) |
| jmarkel44 | 133:c871de2d2b90 | 167 | { |
| jmarkel44 | 133:c871de2d2b90 | 168 | logInfo("%s: %s attempting to start feed on relay %s\n", |
| jmarkel44 | 133:c871de2d2b90 | 169 | __func__, controlFile.c_str(), output.c_str()); |
| jmarkel44 | 133:c871de2d2b90 | 170 | |
| jmarkel44 | 133:c871de2d2b90 | 171 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 133:c871de2d2b90 | 172 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 133:c871de2d2b90 | 173 | |
| jmarkel44 | 133:c871de2d2b90 | 174 | output_mail->action = ACTION_CONTROL_OFF; |
| jmarkel44 | 133:c871de2d2b90 | 175 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 176 | output_mail->priority = priority; |
| jmarkel44 | 133:c871de2d2b90 | 177 | |
| jmarkel44 | 133:c871de2d2b90 | 178 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 133:c871de2d2b90 | 179 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 133:c871de2d2b90 | 180 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 133:c871de2d2b90 | 181 | } |
| jmarkel44 | 192:052a419837fa | 182 | |
| jmarkel44 | 153:0845c7bf236a | 183 | // |
| jmarkel44 | 136:6ad7ba157b70 | 184 | // Method: unregisterControl |
| jmarkel44 | 136:6ad7ba157b70 | 185 | // Description: send OFF indication to Output Master for this control's |
| jmarkel44 | 136:6ad7ba157b70 | 186 | // relay |
| jmarkel44 | 136:6ad7ba157b70 | 187 | // |
| jmarkel44 | 136:6ad7ba157b70 | 188 | // @param none |
| jmarkel44 | 136:6ad7ba157b70 | 189 | // @return none |
| jmarkel44 | 153:0845c7bf236a | 190 | // |
| jmarkel44 | 136:6ad7ba157b70 | 191 | void TimerControl::unregisterControl(void) |
| jmarkel44 | 136:6ad7ba157b70 | 192 | { |
| jmarkel44 | 136:6ad7ba157b70 | 193 | logInfo("%s: %s attempting to unregister %s\n", |
| jmarkel44 | 136:6ad7ba157b70 | 194 | __func__, controlFile.c_str()); |
| jmarkel44 | 136:6ad7ba157b70 | 195 | |
| jmarkel44 | 136:6ad7ba157b70 | 196 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 136:6ad7ba157b70 | 197 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 156:44f87c5a83ae | 198 | |
| jmarkel44 | 156:44f87c5a83ae | 199 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
| jmarkel44 | 156:44f87c5a83ae | 200 | output_mail->controlType = CONTROL_TIMER; |
| jmarkel44 | 156:44f87c5a83ae | 201 | output_mail->priority = priority; |
| jmarkel44 | 136:6ad7ba157b70 | 202 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 136:6ad7ba157b70 | 203 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 156:44f87c5a83ae | 204 | |
| jmarkel44 | 136:6ad7ba157b70 | 205 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 136:6ad7ba157b70 | 206 | } |
| jmarkel44 | 136:6ad7ba157b70 | 207 | |
| jmarkel44 | 133:c871de2d2b90 | 208 | // |
| jmarkel44 | 122:4db48b933115 | 209 | // methid: display |
| jmarkel44 | 122:4db48b933115 | 210 | // description: display the elements of this timer control object |
| jmarkel44 | 122:4db48b933115 | 211 | // |
| jmarkel44 | 122:4db48b933115 | 212 | // @param none |
| jmarkel44 | 132:45821e189dd0 | 213 | // @return none |
| jmarkel44 | 132:45821e189dd0 | 214 | // |
| jmarkel44 | 122:4db48b933115 | 215 | void TimerControl::display(void) |
| jmarkel44 | 122:4db48b933115 | 216 | { |
| jmarkel44 | 128:534bf29132f8 | 217 | string mapper[] = { "OFF", |
| jmarkel44 | 128:534bf29132f8 | 218 | "RUNNING", |
| jmarkel44 | 128:534bf29132f8 | 219 | "DISABLED" |
| jmarkel44 | 128:534bf29132f8 | 220 | }; |
| jmarkel44 | 133:c871de2d2b90 | 221 | |
| jmarkel44 | 205:3c84af5f711f | 222 | printf("\r\n"); |
| jmarkel44 | 205:3c84af5f711f | 223 | cout << left << setw(10) << setfill(' ') << "timer:"; |
| jmarkel44 | 253:ae850c19cf81 | 224 | cout << left << setw(40) << setfill(' ') << controlFile; |
| jmarkel44 | 205:3c84af5f711f | 225 | cout << left << setw(20) << setfill(' ') << id; |
| jmarkel44 | 205:3c84af5f711f | 226 | cout << left << setw(20) << setfill(' ') << output; |
| jmarkel44 | 205:3c84af5f711f | 227 | cout << "pri:" << left << setw(12) << setfill(' ') << priority; |
| jmarkel44 | 205:3c84af5f711f | 228 | cout << "start:" << left << setw(12) << setfill(' ') << startTime; |
| jmarkel44 | 205:3c84af5f711f | 229 | cout << "duration: " << left << setw(12) << setfill(' ') << duration; |
| jmarkel44 | 205:3c84af5f711f | 230 | cout << "end: " << left << setw(12) << setfill(' ') << startTime + duration; |
| jmarkel44 | 205:3c84af5f711f | 231 | cout << "remaining: " << left << setw(12) << setfill(' ') << startTime + duration - time(NULL); |
| jmarkel44 | 205:3c84af5f711f | 232 | cout << left << setw(12) << setfill(' ') << mapper[currentState]; |
| jmarkel44 | 205:3c84af5f711f | 233 | cout.flush(); |
| jmarkel44 | 35:6235ef67faa1 | 234 | } |
