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@132:45821e189dd0, 2016-09-27 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Sep 27 12:18:47 2016 +0000
- Revision:
- 132:45821e189dd0
- Parent:
- 131:a290a3934132
- Child:
- 133:c871de2d2b90
timer control cleanup after whiteboard discussion;
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 | 35:6235ef67faa1 | 10 | #include <string> |
jmarkel44 | 19:9bc8fabeddfa | 11 | |
jmarkel44 | 19:9bc8fabeddfa | 12 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 19:9bc8fabeddfa | 13 | |
jmarkel44 | 122:4db48b933115 | 14 | // |
jmarkel44 | 122:4db48b933115 | 15 | // method: load |
jmarkel44 | 122:4db48b933115 | 16 | // description: load the pertinents from the control file |
jmarkel44 | 128:534bf29132f8 | 17 | // |
jmarkel44 | 122:4db48b933115 | 18 | // @param _controlFile |
jmarkel44 | 122:4db48b933115 | 19 | // @return true if loaded; false otherwise |
jmarkel44 | 122:4db48b933115 | 20 | // |
jmarkel44 | 35:6235ef67faa1 | 21 | bool TimerControl::load(string _controlFile) |
jmarkel44 | 122:4db48b933115 | 22 | { |
jmarkel44 | 122:4db48b933115 | 23 | MbedJSONValue json_value; // json parsing element |
jmarkel44 | 122:4db48b933115 | 24 | |
jmarkel44 | 19:9bc8fabeddfa | 25 | // try to open the control file |
jmarkel44 | 122:4db48b933115 | 26 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(_controlFile.c_str(), mDot::FM_RDONLY); |
jmarkel44 | 122:4db48b933115 | 27 | if ( file.fd < 0 ) { |
jmarkel44 | 35:6235ef67faa1 | 28 | return false; |
jmarkel44 | 122:4db48b933115 | 29 | } |
jmarkel44 | 35:6235ef67faa1 | 30 | |
jmarkel44 | 35:6235ef67faa1 | 31 | // read the data into a buffer |
jmarkel44 | 35:6235ef67faa1 | 32 | char dataBuf[1024]; |
jmarkel44 | 35:6235ef67faa1 | 33 | |
jmarkel44 | 35:6235ef67faa1 | 34 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 35:6235ef67faa1 | 35 | if ( bytes_read != sizeof(dataBuf) ) { |
jmarkel44 | 35:6235ef67faa1 | 36 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
jmarkel44 | 35:6235ef67faa1 | 37 | return false; |
jmarkel44 | 35:6235ef67faa1 | 38 | } |
jmarkel44 | 122:4db48b933115 | 39 | |
jmarkel44 | 122:4db48b933115 | 40 | // close the file |
jmarkel44 | 35:6235ef67faa1 | 41 | GLOBAL_mdot->closeUserFile(file); |
jmarkel44 | 35:6235ef67faa1 | 42 | |
jmarkel44 | 35:6235ef67faa1 | 43 | parse(json_value, dataBuf); |
jmarkel44 | 35:6235ef67faa1 | 44 | |
jmarkel44 | 132:45821e189dd0 | 45 | // the pertinents |
jmarkel44 | 122:4db48b933115 | 46 | controlFile = _controlFile; |
jmarkel44 | 132:45821e189dd0 | 47 | id = json_value["id"].get<string>(); |
jmarkel44 | 132:45821e189dd0 | 48 | output = json_value["output"].get<string>(); |
jmarkel44 | 132:45821e189dd0 | 49 | |
jmarkel44 | 132:45821e189dd0 | 50 | // create the schedule |
jmarkel44 | 132:45821e189dd0 | 51 | Schedule_t s; |
jmarkel44 | 132:45821e189dd0 | 52 | s.priority = atol(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 53 | s.startTime = atol(json_value["starttime"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 54 | s.duration = atol(json_value["duration"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 55 | |
jmarkel44 | 132:45821e189dd0 | 56 | // push it on the list |
jmarkel44 | 132:45821e189dd0 | 57 | schedule.push_back(s); |
jmarkel44 | 122:4db48b933115 | 58 | |
jmarkel44 | 122:4db48b933115 | 59 | return true; |
jmarkel44 | 122:4db48b933115 | 60 | } |
jmarkel44 | 122:4db48b933115 | 61 | |
jmarkel44 | 132:45821e189dd0 | 62 | // |
jmarkel44 | 132:45821e189dd0 | 63 | // method: start |
jmarkel44 | 132:45821e189dd0 | 64 | // description: initialize the control |
jmarkel44 | 132:45821e189dd0 | 65 | // |
jmarkel44 | 132:45821e189dd0 | 66 | // @param none |
jmarkel44 | 132:45821e189dd0 | 67 | // @return none |
jmarkel44 | 132:45821e189dd0 | 68 | // |
jmarkel44 | 126:c85ac6a8e9af | 69 | void TimerControl::start(void) |
jmarkel44 | 126:c85ac6a8e9af | 70 | { |
jmarkel44 | 128:534bf29132f8 | 71 | currentState = STATE_OFF; |
jmarkel44 | 128:534bf29132f8 | 72 | } |
jmarkel44 | 128:534bf29132f8 | 73 | |
jmarkel44 | 132:45821e189dd0 | 74 | // |
jmarkel44 | 132:45821e189dd0 | 75 | // method: timerStart |
jmarkel44 | 132:45821e189dd0 | 76 | // description: examine the timestamp to determine if the timer control |
jmarkel44 | 132:45821e189dd0 | 77 | // should be running |
jmarkel44 | 132:45821e189dd0 | 78 | // |
jmarkel44 | 132:45821e189dd0 | 79 | // @param none |
jmarkel44 | 132:45821e189dd0 | 80 | // @return true if timer should be running; false otherwise |
jmarkel44 | 132:45821e189dd0 | 81 | // |
jmarkel44 | 132:45821e189dd0 | 82 | bool TimerControl::timerStart(void) |
jmarkel44 | 128:534bf29132f8 | 83 | { |
jmarkel44 | 132:45821e189dd0 | 84 | // schedules should be sorted in order, so always check the first |
jmarkel44 | 132:45821e189dd0 | 85 | if ( !schedule.empty() ) { |
jmarkel44 | 132:45821e189dd0 | 86 | unsigned long currentTime = time(NULL); |
jmarkel44 | 132:45821e189dd0 | 87 | printf("\rDEBUG checking the schedule now=%lu start%lu\n", |
jmarkel44 | 132:45821e189dd0 | 88 | currentTime, schedule.front().startTime); |
jmarkel44 | 132:45821e189dd0 | 89 | // does it fit? |
jmarkel44 | 132:45821e189dd0 | 90 | if ( currentTime < schedule.front().startTime ) { |
jmarkel44 | 132:45821e189dd0 | 91 | // not ready to run yet |
jmarkel44 | 132:45821e189dd0 | 92 | return false; |
jmarkel44 | 132:45821e189dd0 | 93 | } |
jmarkel44 | 132:45821e189dd0 | 94 | if ( currentTime >= schedule.front().startTime && |
jmarkel44 | 132:45821e189dd0 | 95 | currentTime < (schedule.front().startTime + schedule.front().duration) ) |
jmarkel44 | 132:45821e189dd0 | 96 | { |
jmarkel44 | 132:45821e189dd0 | 97 | printf("START THE FEED!!! current_time = %lu startTime = %lu\n", |
jmarkel44 | 132:45821e189dd0 | 98 | time(NULL), schedule.front().startTime); |
jmarkel44 | 132:45821e189dd0 | 99 | return true; |
jmarkel44 | 132:45821e189dd0 | 100 | } else { |
jmarkel44 | 132:45821e189dd0 | 101 | // something is wrong here, so let's toss the schedule away |
jmarkel44 | 132:45821e189dd0 | 102 | printf("\rERROR: schedule timestamp %lu has expired\n", |
jmarkel44 | 132:45821e189dd0 | 103 | schedule.front().startTime); |
jmarkel44 | 132:45821e189dd0 | 104 | schedule.erase(schedule.begin()); |
jmarkel44 | 132:45821e189dd0 | 105 | } |
jmarkel44 | 132:45821e189dd0 | 106 | } |
jmarkel44 | 132:45821e189dd0 | 107 | return false; |
jmarkel44 | 132:45821e189dd0 | 108 | } |
jmarkel44 | 132:45821e189dd0 | 109 | |
jmarkel44 | 132:45821e189dd0 | 110 | // |
jmarkel44 | 132:45821e189dd0 | 111 | // method: timerStop |
jmarkel44 | 132:45821e189dd0 | 112 | // description: determines if a running timer should has reached its duration |
jmarkel44 | 132:45821e189dd0 | 113 | // |
jmarkel44 | 132:45821e189dd0 | 114 | // @param none |
jmarkel44 | 132:45821e189dd0 | 115 | // @return true if the timer has expired; false otherwise |
jmarkel44 | 132:45821e189dd0 | 116 | // |
jmarkel44 | 132:45821e189dd0 | 117 | bool TimerControl::timerStop(void) |
jmarkel44 | 132:45821e189dd0 | 118 | { |
jmarkel44 | 132:45821e189dd0 | 119 | if ( schedule.front().startTime + schedule.front().duration >= time(NULL) ) { |
jmarkel44 | 132:45821e189dd0 | 120 | return true; |
jmarkel44 | 132:45821e189dd0 | 121 | } |
jmarkel44 | 128:534bf29132f8 | 122 | return false; |
jmarkel44 | 126:c85ac6a8e9af | 123 | } |
jmarkel44 | 126:c85ac6a8e9af | 124 | |
jmarkel44 | 132:45821e189dd0 | 125 | // |
jmarkel44 | 132:45821e189dd0 | 126 | // method: update |
jmarkel44 | 132:45821e189dd0 | 127 | // description: run the simplified state machine |
jmarkel44 | 132:45821e189dd0 | 128 | // |
jmarkel44 | 132:45821e189dd0 | 129 | // @param none |
jmarkel44 | 132:45821e189dd0 | 130 | // @return none |
jmarkel44 | 132:45821e189dd0 | 131 | // |
jmarkel44 | 126:c85ac6a8e9af | 132 | void TimerControl::update(void) |
jmarkel44 | 126:c85ac6a8e9af | 133 | { |
jmarkel44 | 128:534bf29132f8 | 134 | switch ( this->currentState ) { |
jmarkel44 | 128:534bf29132f8 | 135 | case STATE_OFF: |
jmarkel44 | 132:45821e189dd0 | 136 | if ( this->timerStart() ) { |
jmarkel44 | 128:534bf29132f8 | 137 | currentState = STATE_RUNNING; |
jmarkel44 | 128:534bf29132f8 | 138 | } |
jmarkel44 | 128:534bf29132f8 | 139 | break; |
jmarkel44 | 128:534bf29132f8 | 140 | case STATE_RUNNING: |
jmarkel44 | 132:45821e189dd0 | 141 | if ( this->timerStop() ) { |
jmarkel44 | 128:534bf29132f8 | 142 | currentState = STATE_OFF; |
jmarkel44 | 132:45821e189dd0 | 143 | //TODO: need to remove the schedule from the vector here |
jmarkel44 | 128:534bf29132f8 | 144 | } |
jmarkel44 | 128:534bf29132f8 | 145 | break; |
jmarkel44 | 128:534bf29132f8 | 146 | case STATE_DISABLED: |
jmarkel44 | 128:534bf29132f8 | 147 | // not implelmented |
jmarkel44 | 128:534bf29132f8 | 148 | default: |
jmarkel44 | 128:534bf29132f8 | 149 | break; |
jmarkel44 | 128:534bf29132f8 | 150 | } |
jmarkel44 | 126:c85ac6a8e9af | 151 | } |
jmarkel44 | 126:c85ac6a8e9af | 152 | |
jmarkel44 | 122:4db48b933115 | 153 | // |
jmarkel44 | 122:4db48b933115 | 154 | // methid: display |
jmarkel44 | 122:4db48b933115 | 155 | // description: display the elements of this timer control object |
jmarkel44 | 122:4db48b933115 | 156 | // |
jmarkel44 | 122:4db48b933115 | 157 | // @param none |
jmarkel44 | 132:45821e189dd0 | 158 | // @return none |
jmarkel44 | 132:45821e189dd0 | 159 | // |
jmarkel44 | 122:4db48b933115 | 160 | void TimerControl::display(void) |
jmarkel44 | 122:4db48b933115 | 161 | { |
jmarkel44 | 128:534bf29132f8 | 162 | string mapper[] = { "OFF", |
jmarkel44 | 128:534bf29132f8 | 163 | "RUNNING", |
jmarkel44 | 128:534bf29132f8 | 164 | "DISABLED" |
jmarkel44 | 128:534bf29132f8 | 165 | }; |
jmarkel44 | 132:45821e189dd0 | 166 | |
jmarkel44 | 122:4db48b933115 | 167 | printf("\r controlFile : %s \n", controlFile.c_str()); |
jmarkel44 | 122:4db48b933115 | 168 | printf("\r id : %s \n", id.c_str()); |
jmarkel44 | 122:4db48b933115 | 169 | printf("\r output : %s \n", output.c_str()); |
jmarkel44 | 132:45821e189dd0 | 170 | |
jmarkel44 | 132:45821e189dd0 | 171 | std::vector<Schedule_t>::iterator pos; |
jmarkel44 | 132:45821e189dd0 | 172 | |
jmarkel44 | 132:45821e189dd0 | 173 | int counter = 0; |
jmarkel44 | 132:45821e189dd0 | 174 | for ( pos = schedule.begin(); pos != schedule.end(); ++pos ) { |
jmarkel44 | 132:45821e189dd0 | 175 | printf("\r Schedule %d\n", ++counter); |
jmarkel44 | 132:45821e189dd0 | 176 | printf("\r priority : %u \n", pos->priority); |
jmarkel44 | 132:45821e189dd0 | 177 | printf("\r start time : %lu\n", pos->startTime); |
jmarkel44 | 132:45821e189dd0 | 178 | printf("\r duration : %u \n", pos->duration); |
jmarkel44 | 132:45821e189dd0 | 179 | } |
jmarkel44 | 132:45821e189dd0 | 180 | printf("\r current State : %u\n", currentState); |
jmarkel44 | 132:45821e189dd0 | 181 | |
jmarkel44 | 35:6235ef67faa1 | 182 | } |