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/ManualControl.cpp@242:3b0086a6d625, 2016-10-20 (annotated)
- Committer:
- jmarkel44
- Date:
- Thu Oct 20 15:30:21 2016 +0000
- Revision:
- 242:3b0086a6d625
- Parent:
- 217:d5a2ff093319
- Child:
- 253:ae850c19cf81
use of new JSON parser
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jmarkel44 | 96:807f04bd5256 | 1 | /****************************************************************************** |
| jmarkel44 | 96:807f04bd5256 | 2 | * |
| jmarkel44 | 96:807f04bd5256 | 3 | * File: ManualControl.cpp |
| jmarkel44 | 96:807f04bd5256 | 4 | * Desciption: ICE Manual Control Class implementation |
| jmarkel44 | 96:807f04bd5256 | 5 | * |
| jmarkel44 | 96:807f04bd5256 | 6 | *****************************************************************************/ |
| jmarkel44 | 96:807f04bd5256 | 7 | #include "ManualControl.h" |
| jmarkel44 | 96:807f04bd5256 | 8 | #include "mDot.h" |
| jmarkel44 | 242:3b0086a6d625 | 9 | #include "cJSON.h" |
| jmarkel44 | 96:807f04bd5256 | 10 | #include "ModbusMasterApi.h" |
| jmarkel44 | 96:807f04bd5256 | 11 | #include "global.h" |
| jmarkel44 | 96:807f04bd5256 | 12 | #include <string> |
| jmarkel44 | 205:3c84af5f711f | 13 | #include <iostream> |
| jmarkel44 | 205:3c84af5f711f | 14 | #include <iomanip> |
| jmarkel44 | 96:807f04bd5256 | 15 | |
| jmarkel44 | 96:807f04bd5256 | 16 | extern mDot *GLOBAL_mdot; |
| jmarkel44 | 96:807f04bd5256 | 17 | |
| jmarkel44 | 217:d5a2ff093319 | 18 | // |
| jmarkel44 | 192:052a419837fa | 19 | // method: load |
| jmarkel44 | 192:052a419837fa | 20 | // description: open the configuration file and assign data to the |
| jmarkel44 | 96:807f04bd5256 | 21 | // setpoint control object |
| jmarkel44 | 96:807f04bd5256 | 22 | // |
| jmarkel44 | 96:807f04bd5256 | 23 | // @param controlFile -> name of the control file |
| jmarkel44 | 96:807f04bd5256 | 24 | // @return true if data is assigned; false on error |
| jmarkel44 | 217:d5a2ff093319 | 25 | // |
| jmarkel44 | 96:807f04bd5256 | 26 | bool ManualControl::load(string _controlFile) |
| jmarkel44 | 96:807f04bd5256 | 27 | { |
| jmarkel44 | 97:5cf6ab71dcd0 | 28 | controlFile = _controlFile; |
| jmarkel44 | 97:5cf6ab71dcd0 | 29 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 30 | // open and read from the control file |
| jmarkel44 | 97:5cf6ab71dcd0 | 31 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY); |
| jmarkel44 | 97:5cf6ab71dcd0 | 32 | if ( file.fd < 0 ) |
| jmarkel44 | 97:5cf6ab71dcd0 | 33 | return false; |
| jmarkel44 | 97:5cf6ab71dcd0 | 34 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 35 | // read the data into a buffer |
| jmarkel44 | 177:9ec90c8e3ce1 | 36 | char dataBuf[MAX_FILE_SIZE]; |
| jmarkel44 | 97:5cf6ab71dcd0 | 37 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 38 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 97:5cf6ab71dcd0 | 39 | if ( bytes_read != sizeof(dataBuf) ) { |
| jmarkel44 | 97:5cf6ab71dcd0 | 40 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
| jmarkel44 | 97:5cf6ab71dcd0 | 41 | // caller should destroy the object |
| jmarkel44 | 97:5cf6ab71dcd0 | 42 | return false; |
| jmarkel44 | 97:5cf6ab71dcd0 | 43 | } |
| jmarkel44 | 97:5cf6ab71dcd0 | 44 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 45 | // close the file |
| jmarkel44 | 97:5cf6ab71dcd0 | 46 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 97:5cf6ab71dcd0 | 47 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 48 | // parse the json data |
| jmarkel44 | 242:3b0086a6d625 | 49 | cJSON * root = cJSON_Parse(dataBuf); |
| jmarkel44 | 192:052a419837fa | 50 | |
| jmarkel44 | 242:3b0086a6d625 | 51 | id = cJSON_GetObjectItem(root,"id")->valuestring; |
| jmarkel44 | 242:3b0086a6d625 | 52 | output = cJSON_GetObjectItem(root,"output")->valuestring; |
| jmarkel44 | 242:3b0086a6d625 | 53 | type = atoi(cJSON_GetObjectItem(root,"type")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 54 | priority = atoi(cJSON_GetObjectItem(root, "priority")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 55 | duration = atoi(cJSON_GetObjectItem(root, "duration")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 56 | setpoint = atof(cJSON_GetObjectItem(root, "setpoint")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 57 | state = atoi(cJSON_GetObjectItem(root, "state")->valuestring); |
| jmarkel44 | 242:3b0086a6d625 | 58 | percent = atoi(cJSON_GetObjectItem(root, "percent")->valuestring); |
| jmarkel44 | 97:5cf6ab71dcd0 | 59 | |
| jmarkel44 | 96:807f04bd5256 | 60 | return true; |
| jmarkel44 | 96:807f04bd5256 | 61 | } |
| jmarkel44 | 96:807f04bd5256 | 62 | |
| jmarkel44 | 217:d5a2ff093319 | 63 | // |
| jmarkel44 | 192:052a419837fa | 64 | // method: start |
| jmarkel44 | 192:052a419837fa | 65 | // description: start the manual control |
| jmarkel44 | 96:807f04bd5256 | 66 | // |
| jmarkel44 | 96:807f04bd5256 | 67 | // @param none |
| jmarkel44 | 96:807f04bd5256 | 68 | // @return none |
| jmarkel44 | 217:d5a2ff093319 | 69 | // |
| jmarkel44 | 97:5cf6ab71dcd0 | 70 | void ManualControl::start(void) |
| jmarkel44 | 96:807f04bd5256 | 71 | { |
| jmarkel44 | 97:5cf6ab71dcd0 | 72 | currentState = STATE_STARTUP; |
| jmarkel44 | 96:807f04bd5256 | 73 | } |
| jmarkel44 | 96:807f04bd5256 | 74 | |
| jmarkel44 | 217:d5a2ff093319 | 75 | // |
| jmarkel44 | 192:052a419837fa | 76 | // method: update |
| jmarkel44 | 195:21df85341cb3 | 77 | // description: the setpoint control's state machine |
| jmarkel44 | 96:807f04bd5256 | 78 | // |
| jmarkel44 | 96:807f04bd5256 | 79 | // @param none |
| jmarkel44 | 96:807f04bd5256 | 80 | // @return none |
| jmarkel44 | 217:d5a2ff093319 | 81 | // |
| jmarkel44 | 96:807f04bd5256 | 82 | int ManualControl::update(void) |
| jmarkel44 | 96:807f04bd5256 | 83 | { |
| jmarkel44 | 97:5cf6ab71dcd0 | 84 | int rc = 0; |
| jmarkel44 | 97:5cf6ab71dcd0 | 85 | switch ( this->currentState ) { |
| jmarkel44 | 131:a290a3934132 | 86 | case STATE_INIT: |
| jmarkel44 | 131:a290a3934132 | 87 | // do nothing |
| jmarkel44 | 131:a290a3934132 | 88 | break; |
| jmarkel44 | 97:5cf6ab71dcd0 | 89 | case STATE_STARTUP: |
| jmarkel44 | 97:5cf6ab71dcd0 | 90 | if ( state ) { |
| jmarkel44 | 97:5cf6ab71dcd0 | 91 | this->currentState = STATE_CONTROL_ON; |
| jmarkel44 | 97:5cf6ab71dcd0 | 92 | } else { |
| jmarkel44 | 97:5cf6ab71dcd0 | 93 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 97:5cf6ab71dcd0 | 94 | } |
| jmarkel44 | 164:7cecd731882e | 95 | this->powerOutput(); |
| jmarkel44 | 97:5cf6ab71dcd0 | 96 | break; |
| jmarkel44 | 97:5cf6ab71dcd0 | 97 | case STATE_CONTROL_ON: |
| jmarkel44 | 97:5cf6ab71dcd0 | 98 | if ( !state ) { |
| jmarkel44 | 97:5cf6ab71dcd0 | 99 | this->currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 164:7cecd731882e | 100 | this->powerOutput(); |
| jmarkel44 | 97:5cf6ab71dcd0 | 101 | } |
| jmarkel44 | 97:5cf6ab71dcd0 | 102 | break; |
| jmarkel44 | 97:5cf6ab71dcd0 | 103 | case STATE_CONTROL_OFF: |
| jmarkel44 | 97:5cf6ab71dcd0 | 104 | if ( state ) { |
| jmarkel44 | 97:5cf6ab71dcd0 | 105 | this->currentState = STATE_CONTROL_ON; |
| jmarkel44 | 164:7cecd731882e | 106 | this->powerOutput(); |
| jmarkel44 | 97:5cf6ab71dcd0 | 107 | } |
| jmarkel44 | 97:5cf6ab71dcd0 | 108 | break; |
| jmarkel44 | 97:5cf6ab71dcd0 | 109 | default: |
| jmarkel44 | 97:5cf6ab71dcd0 | 110 | logError("%s unknown state %d\n", __func__, this->currentState); |
| jmarkel44 | 97:5cf6ab71dcd0 | 111 | rc = -1; |
| jmarkel44 | 97:5cf6ab71dcd0 | 112 | break; |
| jmarkel44 | 97:5cf6ab71dcd0 | 113 | } |
| jmarkel44 | 97:5cf6ab71dcd0 | 114 | return rc; |
| jmarkel44 | 96:807f04bd5256 | 115 | } |
| jmarkel44 | 96:807f04bd5256 | 116 | |
| jmarkel44 | 217:d5a2ff093319 | 117 | // |
| jmarkel44 | 192:052a419837fa | 118 | // method: unregisterControl |
| jmarkel44 | 192:052a419837fa | 119 | // description: unregister this control with the output master |
| jmarkel44 | 96:807f04bd5256 | 120 | // |
| jmarkel44 | 96:807f04bd5256 | 121 | // @param none |
| jmarkel44 | 96:807f04bd5256 | 122 | // @return none |
| jmarkel44 | 217:d5a2ff093319 | 123 | // |
| jmarkel44 | 96:807f04bd5256 | 124 | int ManualControl::unregisterControl(void) |
| jmarkel44 | 96:807f04bd5256 | 125 | { |
| jmarkel44 | 115:1558e01d04c6 | 126 | logInfo("%s: Attempting to unregister %s\n", |
| jmarkel44 | 115:1558e01d04c6 | 127 | __func__, controlFile.c_str()); |
| jmarkel44 | 97:5cf6ab71dcd0 | 128 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 129 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 97:5cf6ab71dcd0 | 130 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 192:052a419837fa | 131 | |
| jmarkel44 | 121:650205ffa656 | 132 | output_mail->controlType = CONTROL_MANUAL; |
| jmarkel44 | 121:650205ffa656 | 133 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
| jmarkel44 | 121:650205ffa656 | 134 | output_mail->priority = this->priority; |
| jmarkel44 | 115:1558e01d04c6 | 135 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 97:5cf6ab71dcd0 | 136 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 192:052a419837fa | 137 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 138 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 97:5cf6ab71dcd0 | 139 | return 0; |
| jmarkel44 | 97:5cf6ab71dcd0 | 140 | } |
| jmarkel44 | 97:5cf6ab71dcd0 | 141 | |
| jmarkel44 | 192:052a419837fa | 142 | // |
| jmarkel44 | 192:052a419837fa | 143 | // method: powerOutput |
| jmarkel44 | 192:052a419837fa | 144 | // description: send a message to the output thread to power the relay |
| jmarkel44 | 192:052a419837fa | 145 | // |
| jmarkel44 | 192:052a419837fa | 146 | // @param none |
| jmarkel44 | 192:052a419837fa | 147 | // @return -1 on error; 0 otherwise |
| jmarkel44 | 192:052a419837fa | 148 | // |
| jmarkel44 | 164:7cecd731882e | 149 | int ManualControl::powerOutput(void) |
| jmarkel44 | 97:5cf6ab71dcd0 | 150 | { |
| jmarkel44 | 97:5cf6ab71dcd0 | 151 | printf("%s: %s attempting to manually turn %s relay %s\n", |
| jmarkel44 | 97:5cf6ab71dcd0 | 152 | __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str()); |
| jmarkel44 | 97:5cf6ab71dcd0 | 153 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 154 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 97:5cf6ab71dcd0 | 155 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 192:052a419837fa | 156 | |
| jmarkel44 | 121:650205ffa656 | 157 | output_mail->controlType = CONTROL_MANUAL; |
| jmarkel44 | 115:1558e01d04c6 | 158 | output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF; |
| jmarkel44 | 115:1558e01d04c6 | 159 | output_mail->priority = this->priority; |
| jmarkel44 | 115:1558e01d04c6 | 160 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 97:5cf6ab71dcd0 | 161 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 192:052a419837fa | 162 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 163 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 96:807f04bd5256 | 164 | return 0; |
| jmarkel44 | 96:807f04bd5256 | 165 | } |
| jmarkel44 | 96:807f04bd5256 | 166 | |
| jmarkel44 | 192:052a419837fa | 167 | // |
| jmarkel44 | 192:052a419837fa | 168 | // method: display |
| jmarkel44 | 192:052a419837fa | 169 | // description: display the pertinents |
| jmarkel44 | 192:052a419837fa | 170 | // |
| jmarkel44 | 192:052a419837fa | 171 | // @param none |
| jmarkel44 | 192:052a419837fa | 172 | // @return none |
| jmarkel44 | 192:052a419837fa | 173 | // |
| jmarkel44 | 96:807f04bd5256 | 174 | void ManualControl::display(void) |
| jmarkel44 | 217:d5a2ff093319 | 175 | { |
| jmarkel44 | 131:a290a3934132 | 176 | string mapper[] = { "INIT", |
| jmarkel44 | 131:a290a3934132 | 177 | "STARTUP", |
| jmarkel44 | 97:5cf6ab71dcd0 | 178 | "CONTROL_ON", |
| jmarkel44 | 97:5cf6ab71dcd0 | 179 | "CONTROL_OFF" |
| jmarkel44 | 97:5cf6ab71dcd0 | 180 | }; |
| jmarkel44 | 205:3c84af5f711f | 181 | |
| jmarkel44 | 97:5cf6ab71dcd0 | 182 | printf("\r\n"); |
| jmarkel44 | 205:3c84af5f711f | 183 | std::cout << left << setw(10) << setfill(' ') << "manual: "; |
| jmarkel44 | 205:3c84af5f711f | 184 | std::cout << left << setw(32) << setfill(' ') << controlFile; |
| jmarkel44 | 205:3c84af5f711f | 185 | std::cout << left << setw(20) << setfill(' ') << id; |
| jmarkel44 | 205:3c84af5f711f | 186 | std::cout << left << setw(20) << setfill(' ') << output; |
| jmarkel44 | 205:3c84af5f711f | 187 | std::cout << left << setw(6) << setfill(' ') << type; |
| jmarkel44 | 205:3c84af5f711f | 188 | std::cout << left << setw(6) << setfill(' ') << priority; |
| jmarkel44 | 205:3c84af5f711f | 189 | std::cout << left << setw(8) << setfill(' ') << duration; |
| jmarkel44 | 205:3c84af5f711f | 190 | std::cout << left << setw(8) << setfill(' ') << percent; |
| jmarkel44 | 205:3c84af5f711f | 191 | std::cout << left << setw(16) << setfill(' ') << mapper[currentState]; |
| jmarkel44 | 205:3c84af5f711f | 192 | std::cout.flush(); |
| jmarkel44 | 96:807f04bd5256 | 193 | } |
