Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Tue Oct 11 12:24:28 2016 +0000
Revision:
217:d5a2ff093319
Parent:
205:3c84af5f711f
Child:
242:3b0086a6d625
more general cleanup;

Who changed what in which revision?

UserRevisionLine numberNew 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 96:807f04bd5256 9 #include "MbedJSONValue.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 MbedJSONValue json_value;
jmarkel44 97:5cf6ab71dcd0 29 controlFile = _controlFile;
jmarkel44 97:5cf6ab71dcd0 30
jmarkel44 97:5cf6ab71dcd0 31 // open and read from the control file
jmarkel44 97:5cf6ab71dcd0 32 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
jmarkel44 97:5cf6ab71dcd0 33 if ( file.fd < 0 )
jmarkel44 97:5cf6ab71dcd0 34 return false;
jmarkel44 97:5cf6ab71dcd0 35
jmarkel44 97:5cf6ab71dcd0 36 // read the data into a buffer
jmarkel44 177:9ec90c8e3ce1 37 char dataBuf[MAX_FILE_SIZE];
jmarkel44 97:5cf6ab71dcd0 38
jmarkel44 97:5cf6ab71dcd0 39 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 97:5cf6ab71dcd0 40 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 97:5cf6ab71dcd0 41 logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 42 // caller should destroy the object
jmarkel44 97:5cf6ab71dcd0 43 return false;
jmarkel44 97:5cf6ab71dcd0 44 }
jmarkel44 97:5cf6ab71dcd0 45
jmarkel44 97:5cf6ab71dcd0 46 // close the file
jmarkel44 97:5cf6ab71dcd0 47 GLOBAL_mdot->closeUserFile(file);
jmarkel44 97:5cf6ab71dcd0 48
jmarkel44 97:5cf6ab71dcd0 49 // parse the json data
jmarkel44 97:5cf6ab71dcd0 50 parse(json_value, dataBuf);
jmarkel44 195:21df85341cb3 51
jmarkel44 192:052a419837fa 52 if ( !json_value.hasMember("id") ||
jmarkel44 192:052a419837fa 53 !json_value.hasMember("output") ||
jmarkel44 192:052a419837fa 54 !json_value.hasMember("type") ||
jmarkel44 192:052a419837fa 55 !json_value.hasMember("priority") ||
jmarkel44 192:052a419837fa 56 !json_value.hasMember("duration") ||
jmarkel44 192:052a419837fa 57 !json_value.hasMember("setpoint") ||
jmarkel44 192:052a419837fa 58 !json_value.hasMember("state") ||
jmarkel44 192:052a419837fa 59 !json_value.hasMember("percent") ) {
jmarkel44 192:052a419837fa 60 logError("Manual Control file is missing expected tags");
jmarkel44 192:052a419837fa 61 return false;
jmarkel44 192:052a419837fa 62 }
jmarkel44 192:052a419837fa 63
jmarkel44 98:8eab18d03ac2 64 id = json_value ["id"].get<string>();
jmarkel44 98:8eab18d03ac2 65 output = json_value ["output"].get<string>();
jmarkel44 97:5cf6ab71dcd0 66 type = atoi(json_value["type"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 67 priority = atoi(json_value["priority"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 68 duration = atoi(json_value["duration"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 69 setpoint = atof(json_value["setpoint"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 70 state = atoi(json_value["state"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 71 percent = atoi(json_value["percent"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 72
jmarkel44 96:807f04bd5256 73 return true;
jmarkel44 96:807f04bd5256 74 }
jmarkel44 96:807f04bd5256 75
jmarkel44 217:d5a2ff093319 76 //
jmarkel44 192:052a419837fa 77 // method: start
jmarkel44 192:052a419837fa 78 // description: start the manual control
jmarkel44 96:807f04bd5256 79 //
jmarkel44 96:807f04bd5256 80 // @param none
jmarkel44 96:807f04bd5256 81 // @return none
jmarkel44 217:d5a2ff093319 82 //
jmarkel44 97:5cf6ab71dcd0 83 void ManualControl::start(void)
jmarkel44 96:807f04bd5256 84 {
jmarkel44 97:5cf6ab71dcd0 85 currentState = STATE_STARTUP;
jmarkel44 96:807f04bd5256 86 }
jmarkel44 96:807f04bd5256 87
jmarkel44 217:d5a2ff093319 88 //
jmarkel44 192:052a419837fa 89 // method: update
jmarkel44 195:21df85341cb3 90 // description: the setpoint control's state machine
jmarkel44 96:807f04bd5256 91 //
jmarkel44 96:807f04bd5256 92 // @param none
jmarkel44 96:807f04bd5256 93 // @return none
jmarkel44 217:d5a2ff093319 94 //
jmarkel44 96:807f04bd5256 95 int ManualControl::update(void)
jmarkel44 96:807f04bd5256 96 {
jmarkel44 97:5cf6ab71dcd0 97 int rc = 0;
jmarkel44 97:5cf6ab71dcd0 98 switch ( this->currentState ) {
jmarkel44 131:a290a3934132 99 case STATE_INIT:
jmarkel44 131:a290a3934132 100 // do nothing
jmarkel44 131:a290a3934132 101 break;
jmarkel44 97:5cf6ab71dcd0 102 case STATE_STARTUP:
jmarkel44 97:5cf6ab71dcd0 103 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 104 this->currentState = STATE_CONTROL_ON;
jmarkel44 97:5cf6ab71dcd0 105 } else {
jmarkel44 97:5cf6ab71dcd0 106 this->currentState = STATE_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 107 }
jmarkel44 164:7cecd731882e 108 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 109 break;
jmarkel44 97:5cf6ab71dcd0 110 case STATE_CONTROL_ON:
jmarkel44 97:5cf6ab71dcd0 111 if ( !state ) {
jmarkel44 97:5cf6ab71dcd0 112 this->currentState = STATE_CONTROL_OFF;
jmarkel44 164:7cecd731882e 113 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 114 }
jmarkel44 97:5cf6ab71dcd0 115 break;
jmarkel44 97:5cf6ab71dcd0 116 case STATE_CONTROL_OFF:
jmarkel44 97:5cf6ab71dcd0 117 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 118 this->currentState = STATE_CONTROL_ON;
jmarkel44 164:7cecd731882e 119 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 120 }
jmarkel44 97:5cf6ab71dcd0 121 break;
jmarkel44 97:5cf6ab71dcd0 122 default:
jmarkel44 97:5cf6ab71dcd0 123 logError("%s unknown state %d\n", __func__, this->currentState);
jmarkel44 97:5cf6ab71dcd0 124 rc = -1;
jmarkel44 97:5cf6ab71dcd0 125 break;
jmarkel44 97:5cf6ab71dcd0 126 }
jmarkel44 97:5cf6ab71dcd0 127 return rc;
jmarkel44 96:807f04bd5256 128 }
jmarkel44 96:807f04bd5256 129
jmarkel44 217:d5a2ff093319 130 //
jmarkel44 192:052a419837fa 131 // method: unregisterControl
jmarkel44 192:052a419837fa 132 // description: unregister this control with the output master
jmarkel44 96:807f04bd5256 133 //
jmarkel44 96:807f04bd5256 134 // @param none
jmarkel44 96:807f04bd5256 135 // @return none
jmarkel44 217:d5a2ff093319 136 //
jmarkel44 96:807f04bd5256 137 int ManualControl::unregisterControl(void)
jmarkel44 96:807f04bd5256 138 {
jmarkel44 115:1558e01d04c6 139 logInfo("%s: Attempting to unregister %s\n",
jmarkel44 115:1558e01d04c6 140 __func__, controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 141
jmarkel44 97:5cf6ab71dcd0 142 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 143 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 144
jmarkel44 121:650205ffa656 145 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 121:650205ffa656 146 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 121:650205ffa656 147 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 148 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 149 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 150
jmarkel44 97:5cf6ab71dcd0 151 OutputMasterMailBox.put(output_mail);
jmarkel44 97:5cf6ab71dcd0 152 return 0;
jmarkel44 97:5cf6ab71dcd0 153 }
jmarkel44 97:5cf6ab71dcd0 154
jmarkel44 192:052a419837fa 155 //
jmarkel44 192:052a419837fa 156 // method: powerOutput
jmarkel44 192:052a419837fa 157 // description: send a message to the output thread to power the relay
jmarkel44 192:052a419837fa 158 //
jmarkel44 192:052a419837fa 159 // @param none
jmarkel44 192:052a419837fa 160 // @return -1 on error; 0 otherwise
jmarkel44 192:052a419837fa 161 //
jmarkel44 164:7cecd731882e 162 int ManualControl::powerOutput(void)
jmarkel44 97:5cf6ab71dcd0 163 {
jmarkel44 97:5cf6ab71dcd0 164 printf("%s: %s attempting to manually turn %s relay %s\n",
jmarkel44 97:5cf6ab71dcd0 165 __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());
jmarkel44 97:5cf6ab71dcd0 166
jmarkel44 97:5cf6ab71dcd0 167 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 168 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 169
jmarkel44 121:650205ffa656 170 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 115:1558e01d04c6 171 output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
jmarkel44 115:1558e01d04c6 172 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 173 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 174 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 175
jmarkel44 97:5cf6ab71dcd0 176 OutputMasterMailBox.put(output_mail);
jmarkel44 96:807f04bd5256 177 return 0;
jmarkel44 96:807f04bd5256 178 }
jmarkel44 96:807f04bd5256 179
jmarkel44 192:052a419837fa 180 //
jmarkel44 192:052a419837fa 181 // method: display
jmarkel44 192:052a419837fa 182 // description: display the pertinents
jmarkel44 192:052a419837fa 183 //
jmarkel44 192:052a419837fa 184 // @param none
jmarkel44 192:052a419837fa 185 // @return none
jmarkel44 192:052a419837fa 186 //
jmarkel44 96:807f04bd5256 187 void ManualControl::display(void)
jmarkel44 217:d5a2ff093319 188 {
jmarkel44 131:a290a3934132 189 string mapper[] = { "INIT",
jmarkel44 131:a290a3934132 190 "STARTUP",
jmarkel44 97:5cf6ab71dcd0 191 "CONTROL_ON",
jmarkel44 97:5cf6ab71dcd0 192 "CONTROL_OFF"
jmarkel44 97:5cf6ab71dcd0 193 };
jmarkel44 205:3c84af5f711f 194
jmarkel44 97:5cf6ab71dcd0 195 printf("\r\n");
jmarkel44 205:3c84af5f711f 196 std::cout << left << setw(10) << setfill(' ') << "manual: ";
jmarkel44 205:3c84af5f711f 197 std::cout << left << setw(32) << setfill(' ') << controlFile;
jmarkel44 205:3c84af5f711f 198 std::cout << left << setw(20) << setfill(' ') << id;
jmarkel44 205:3c84af5f711f 199 std::cout << left << setw(20) << setfill(' ') << output;
jmarkel44 205:3c84af5f711f 200 std::cout << left << setw(6) << setfill(' ') << type;
jmarkel44 205:3c84af5f711f 201 std::cout << left << setw(6) << setfill(' ') << priority;
jmarkel44 205:3c84af5f711f 202 std::cout << left << setw(8) << setfill(' ') << duration;
jmarkel44 205:3c84af5f711f 203 std::cout << left << setw(8) << setfill(' ') << percent;
jmarkel44 205:3c84af5f711f 204 std::cout << left << setw(16) << setfill(' ') << mapper[currentState];
jmarkel44 205:3c84af5f711f 205 std::cout.flush();
jmarkel44 96:807f04bd5256 206 }