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@133:c871de2d2b90, 2016-09-27 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Sep 27 15:28:59 2016 +0000
- Revision:
- 133:c871de2d2b90
- Parent:
- 132:45821e189dd0
- Child:
- 136:6ad7ba157b70
timer controls working
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 | 19:9bc8fabeddfa | 12 | |
jmarkel44 | 19:9bc8fabeddfa | 13 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 19:9bc8fabeddfa | 14 | |
jmarkel44 | 122:4db48b933115 | 15 | // |
jmarkel44 | 122:4db48b933115 | 16 | // method: load |
jmarkel44 | 122:4db48b933115 | 17 | // description: load the pertinents from the control file |
jmarkel44 | 128:534bf29132f8 | 18 | // |
jmarkel44 | 122:4db48b933115 | 19 | // @param _controlFile |
jmarkel44 | 122:4db48b933115 | 20 | // @return true if loaded; false otherwise |
jmarkel44 | 122:4db48b933115 | 21 | // |
jmarkel44 | 35:6235ef67faa1 | 22 | bool TimerControl::load(string _controlFile) |
jmarkel44 | 122:4db48b933115 | 23 | { |
jmarkel44 | 122:4db48b933115 | 24 | MbedJSONValue json_value; // json parsing element |
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 | 35:6235ef67faa1 | 33 | char dataBuf[1024]; |
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 | 35:6235ef67faa1 | 44 | parse(json_value, dataBuf); |
jmarkel44 | 35:6235ef67faa1 | 45 | |
jmarkel44 | 132:45821e189dd0 | 46 | // the pertinents |
jmarkel44 | 122:4db48b933115 | 47 | controlFile = _controlFile; |
jmarkel44 | 132:45821e189dd0 | 48 | id = json_value["id"].get<string>(); |
jmarkel44 | 132:45821e189dd0 | 49 | output = json_value["output"].get<string>(); |
jmarkel44 | 132:45821e189dd0 | 50 | |
jmarkel44 | 133:c871de2d2b90 | 51 | // create the schedule |
jmarkel44 | 132:45821e189dd0 | 52 | Schedule_t s; |
jmarkel44 | 132:45821e189dd0 | 53 | s.priority = atol(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 54 | s.startTime = atol(json_value["starttime"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 55 | s.duration = atol(json_value["duration"].get<string>().c_str()); |
jmarkel44 | 133:c871de2d2b90 | 56 | |
jmarkel44 | 133:c871de2d2b90 | 57 | // push it on the list |
jmarkel44 | 132:45821e189dd0 | 58 | schedule.push_back(s); |
jmarkel44 | 122:4db48b933115 | 59 | |
jmarkel44 | 122:4db48b933115 | 60 | return true; |
jmarkel44 | 122:4db48b933115 | 61 | } |
jmarkel44 | 122:4db48b933115 | 62 | |
jmarkel44 | 132:45821e189dd0 | 63 | // |
jmarkel44 | 132:45821e189dd0 | 64 | // method: start |
jmarkel44 | 132:45821e189dd0 | 65 | // description: initialize the control |
jmarkel44 | 133:c871de2d2b90 | 66 | // |
jmarkel44 | 132:45821e189dd0 | 67 | // @param none |
jmarkel44 | 132:45821e189dd0 | 68 | // @return none |
jmarkel44 | 132:45821e189dd0 | 69 | // |
jmarkel44 | 126:c85ac6a8e9af | 70 | void TimerControl::start(void) |
jmarkel44 | 126:c85ac6a8e9af | 71 | { |
jmarkel44 | 128:534bf29132f8 | 72 | currentState = STATE_OFF; |
jmarkel44 | 128:534bf29132f8 | 73 | } |
jmarkel44 | 128:534bf29132f8 | 74 | |
jmarkel44 | 132:45821e189dd0 | 75 | // |
jmarkel44 | 132:45821e189dd0 | 76 | // method: timerStart |
jmarkel44 | 132:45821e189dd0 | 77 | // description: examine the timestamp to determine if the timer control |
jmarkel44 | 132:45821e189dd0 | 78 | // should be running |
jmarkel44 | 132:45821e189dd0 | 79 | // |
jmarkel44 | 132:45821e189dd0 | 80 | // @param none |
jmarkel44 | 132:45821e189dd0 | 81 | // @return true if timer should be running; false otherwise |
jmarkel44 | 132:45821e189dd0 | 82 | // |
jmarkel44 | 132:45821e189dd0 | 83 | bool TimerControl::timerStart(void) |
jmarkel44 | 128:534bf29132f8 | 84 | { |
jmarkel44 | 132:45821e189dd0 | 85 | // schedules should be sorted in order, so always check the first |
jmarkel44 | 132:45821e189dd0 | 86 | if ( !schedule.empty() ) { |
jmarkel44 | 132:45821e189dd0 | 87 | unsigned long currentTime = time(NULL); |
jmarkel44 | 133:c871de2d2b90 | 88 | // does it fit? |
jmarkel44 | 132:45821e189dd0 | 89 | if ( currentTime < schedule.front().startTime ) { |
jmarkel44 | 132:45821e189dd0 | 90 | return false; |
jmarkel44 | 132:45821e189dd0 | 91 | } |
jmarkel44 | 132:45821e189dd0 | 92 | if ( currentTime >= schedule.front().startTime && |
jmarkel44 | 133:c871de2d2b90 | 93 | currentTime < (schedule.front().startTime + schedule.front().duration) ) { |
jmarkel44 | 133:c871de2d2b90 | 94 | logInfo("%s signals feed start", __func__); |
jmarkel44 | 132:45821e189dd0 | 95 | return true; |
jmarkel44 | 132:45821e189dd0 | 96 | } else { |
jmarkel44 | 132:45821e189dd0 | 97 | // something is wrong here, so let's toss the schedule away |
jmarkel44 | 133:c871de2d2b90 | 98 | printf("\rERROR: schedule timestamp %lu has expired\n", |
jmarkel44 | 133:c871de2d2b90 | 99 | schedule.front().startTime); |
jmarkel44 | 132:45821e189dd0 | 100 | schedule.erase(schedule.begin()); |
jmarkel44 | 132:45821e189dd0 | 101 | } |
jmarkel44 | 132:45821e189dd0 | 102 | } |
jmarkel44 | 133:c871de2d2b90 | 103 | return false; |
jmarkel44 | 132:45821e189dd0 | 104 | } |
jmarkel44 | 132:45821e189dd0 | 105 | |
jmarkel44 | 132:45821e189dd0 | 106 | // |
jmarkel44 | 132:45821e189dd0 | 107 | // method: timerStop |
jmarkel44 | 132:45821e189dd0 | 108 | // description: determines if a running timer should has reached its duration |
jmarkel44 | 132:45821e189dd0 | 109 | // |
jmarkel44 | 132:45821e189dd0 | 110 | // @param none |
jmarkel44 | 132:45821e189dd0 | 111 | // @return true if the timer has expired; false otherwise |
jmarkel44 | 132:45821e189dd0 | 112 | // |
jmarkel44 | 133:c871de2d2b90 | 113 | bool TimerControl::timerStop(void) |
jmarkel44 | 132:45821e189dd0 | 114 | { |
jmarkel44 | 133:c871de2d2b90 | 115 | // if current time is greater than start time + feed duration... |
jmarkel44 | 133:c871de2d2b90 | 116 | if ( time(NULL) >= (schedule.front().startTime + schedule.front().duration) ) { |
jmarkel44 | 133:c871de2d2b90 | 117 | logInfo("%s signals a feed stop", __func__); |
jmarkel44 | 132:45821e189dd0 | 118 | return true; |
jmarkel44 | 132:45821e189dd0 | 119 | } |
jmarkel44 | 128:534bf29132f8 | 120 | return false; |
jmarkel44 | 126:c85ac6a8e9af | 121 | } |
jmarkel44 | 126:c85ac6a8e9af | 122 | |
jmarkel44 | 132:45821e189dd0 | 123 | // |
jmarkel44 | 133:c871de2d2b90 | 124 | // method: update |
jmarkel44 | 132:45821e189dd0 | 125 | // description: run the simplified state machine |
jmarkel44 | 132:45821e189dd0 | 126 | // |
jmarkel44 | 132:45821e189dd0 | 127 | // @param none |
jmarkel44 | 132:45821e189dd0 | 128 | // @return none |
jmarkel44 | 132:45821e189dd0 | 129 | // |
jmarkel44 | 126:c85ac6a8e9af | 130 | void TimerControl::update(void) |
jmarkel44 | 126:c85ac6a8e9af | 131 | { |
jmarkel44 | 128:534bf29132f8 | 132 | switch ( this->currentState ) { |
jmarkel44 | 128:534bf29132f8 | 133 | case STATE_OFF: |
jmarkel44 | 132:45821e189dd0 | 134 | if ( this->timerStart() ) { |
jmarkel44 | 128:534bf29132f8 | 135 | currentState = STATE_RUNNING; |
jmarkel44 | 133:c871de2d2b90 | 136 | this->startFeed(); |
jmarkel44 | 128:534bf29132f8 | 137 | } |
jmarkel44 | 128:534bf29132f8 | 138 | break; |
jmarkel44 | 128:534bf29132f8 | 139 | case STATE_RUNNING: |
jmarkel44 | 132:45821e189dd0 | 140 | if ( this->timerStop() ) { |
jmarkel44 | 128:534bf29132f8 | 141 | currentState = STATE_OFF; |
jmarkel44 | 133:c871de2d2b90 | 142 | this->stopFeed(); |
jmarkel44 | 128:534bf29132f8 | 143 | } |
jmarkel44 | 128:534bf29132f8 | 144 | break; |
jmarkel44 | 128:534bf29132f8 | 145 | case STATE_DISABLED: |
jmarkel44 | 128:534bf29132f8 | 146 | // not implelmented |
jmarkel44 | 128:534bf29132f8 | 147 | default: |
jmarkel44 | 128:534bf29132f8 | 148 | break; |
jmarkel44 | 128:534bf29132f8 | 149 | } |
jmarkel44 | 126:c85ac6a8e9af | 150 | } |
jmarkel44 | 126:c85ac6a8e9af | 151 | |
jmarkel44 | 122:4db48b933115 | 152 | // |
jmarkel44 | 133:c871de2d2b90 | 153 | // method: startFeed |
jmarkel44 | 133:c871de2d2b90 | 154 | // description: signal the output thread to start a feed |
jmarkel44 | 133:c871de2d2b90 | 155 | // |
jmarkel44 | 133:c871de2d2b90 | 156 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 157 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 158 | void TimerControl::startFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 159 | { |
jmarkel44 | 133:c871de2d2b90 | 160 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 161 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 162 | |
jmarkel44 | 133:c871de2d2b90 | 163 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 164 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 165 | |
jmarkel44 | 133:c871de2d2b90 | 166 | output_mail->action = ACTION_CONTROL_ON; |
jmarkel44 | 133:c871de2d2b90 | 167 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 133:c871de2d2b90 | 168 | output_mail->priority = this->schedule.front().priority; |
jmarkel44 | 133:c871de2d2b90 | 169 | |
jmarkel44 | 133:c871de2d2b90 | 170 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 171 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 172 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 173 | } |
jmarkel44 | 133:c871de2d2b90 | 174 | |
jmarkel44 | 133:c871de2d2b90 | 175 | // |
jmarkel44 | 133:c871de2d2b90 | 176 | // method: stopFeed |
jmarkel44 | 133:c871de2d2b90 | 177 | // description: signal the output thread to stop a feed |
jmarkel44 | 133:c871de2d2b90 | 178 | // |
jmarkel44 | 133:c871de2d2b90 | 179 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 180 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 181 | void TimerControl::stopFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 182 | { |
jmarkel44 | 133:c871de2d2b90 | 183 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 184 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 185 | |
jmarkel44 | 133:c871de2d2b90 | 186 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 187 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 188 | |
jmarkel44 | 133:c871de2d2b90 | 189 | output_mail->action = ACTION_CONTROL_OFF; |
jmarkel44 | 133:c871de2d2b90 | 190 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 133:c871de2d2b90 | 191 | output_mail->priority = this->schedule.front().priority; |
jmarkel44 | 133:c871de2d2b90 | 192 | |
jmarkel44 | 133:c871de2d2b90 | 193 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 194 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 195 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 196 | } |
jmarkel44 | 133:c871de2d2b90 | 197 | |
jmarkel44 | 133:c871de2d2b90 | 198 | // |
jmarkel44 | 122:4db48b933115 | 199 | // methid: display |
jmarkel44 | 122:4db48b933115 | 200 | // description: display the elements of this timer control object |
jmarkel44 | 122:4db48b933115 | 201 | // |
jmarkel44 | 122:4db48b933115 | 202 | // @param none |
jmarkel44 | 132:45821e189dd0 | 203 | // @return none |
jmarkel44 | 132:45821e189dd0 | 204 | // |
jmarkel44 | 122:4db48b933115 | 205 | void TimerControl::display(void) |
jmarkel44 | 122:4db48b933115 | 206 | { |
jmarkel44 | 128:534bf29132f8 | 207 | string mapper[] = { "OFF", |
jmarkel44 | 128:534bf29132f8 | 208 | "RUNNING", |
jmarkel44 | 128:534bf29132f8 | 209 | "DISABLED" |
jmarkel44 | 128:534bf29132f8 | 210 | }; |
jmarkel44 | 133:c871de2d2b90 | 211 | |
jmarkel44 | 122:4db48b933115 | 212 | printf("\r controlFile : %s \n", controlFile.c_str()); |
jmarkel44 | 122:4db48b933115 | 213 | printf("\r id : %s \n", id.c_str()); |
jmarkel44 | 122:4db48b933115 | 214 | printf("\r output : %s \n", output.c_str()); |
jmarkel44 | 132:45821e189dd0 | 215 | |
jmarkel44 | 132:45821e189dd0 | 216 | std::vector<Schedule_t>::iterator pos; |
jmarkel44 | 132:45821e189dd0 | 217 | |
jmarkel44 | 132:45821e189dd0 | 218 | int counter = 0; |
jmarkel44 | 132:45821e189dd0 | 219 | for ( pos = schedule.begin(); pos != schedule.end(); ++pos ) { |
jmarkel44 | 132:45821e189dd0 | 220 | printf("\r Schedule %d\n", ++counter); |
jmarkel44 | 132:45821e189dd0 | 221 | printf("\r priority : %u \n", pos->priority); |
jmarkel44 | 132:45821e189dd0 | 222 | printf("\r start time : %lu\n", pos->startTime); |
jmarkel44 | 132:45821e189dd0 | 223 | printf("\r duration : %u \n", pos->duration); |
jmarkel44 | 132:45821e189dd0 | 224 | } |
jmarkel44 | 133:c871de2d2b90 | 225 | printf("\r current State : %s\n", mapper[currentState].c_str()); |
jmarkel44 | 132:45821e189dd0 | 226 | |
jmarkel44 | 35:6235ef67faa1 | 227 | } |