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
- Committer:
- jmarkel44
- Date:
- 2016-10-07
- Revision:
- 205:3c84af5f711f
- Parent:
- 195:21df85341cb3
- Child:
- 217:d5a2ff093319
File content as of revision 205:3c84af5f711f:
/******************************************************************************
*
* File: ManualControl.cpp
* Desciption: ICE Manual Control Class implementation
*
*****************************************************************************/
#include "ManualControl.h"
#include "mDot.h"
#include "MbedJSONValue.h"
#include "ModbusMasterApi.h"
#include "global.h"
#include <string>
#include <iostream>
#include <iomanip>
extern mDot *GLOBAL_mdot;
// method: load
// description: open the configuration file and assign data to the
// setpoint control object
//
// @param controlFile -> name of the control file
// @return true if data is assigned; false on error
bool ManualControl::load(string _controlFile)
{
MbedJSONValue json_value;
controlFile = _controlFile;
// open and read from the control file
mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
if ( file.fd < 0 )
return false;
// read the data into a buffer
char dataBuf[MAX_FILE_SIZE];
int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
if ( bytes_read != sizeof(dataBuf) ) {
logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
// caller should destroy the object
return false;
}
// close the file
GLOBAL_mdot->closeUserFile(file);
// parse the json data
parse(json_value, dataBuf);
if ( !json_value.hasMember("id") ||
!json_value.hasMember("output") ||
!json_value.hasMember("type") ||
!json_value.hasMember("priority") ||
!json_value.hasMember("duration") ||
!json_value.hasMember("setpoint") ||
!json_value.hasMember("state") ||
!json_value.hasMember("percent") ) {
logError("Manual Control file is missing expected tags");
return false;
}
id = json_value ["id"].get<string>();
output = json_value ["output"].get<string>();
type = atoi(json_value["type"].get<string>().c_str());
priority = atoi(json_value["priority"].get<string>().c_str());
duration = atoi(json_value["duration"].get<string>().c_str());
setpoint = atof(json_value["setpoint"].get<string>().c_str());
state = atoi(json_value["state"].get<string>().c_str());
percent = atoi(json_value["percent"].get<string>().c_str());
return true;
}
// method: start
// description: start the manual control
//
// @param none
// @return none
void ManualControl::start(void)
{
currentState = STATE_STARTUP;
}
// method: update
// description: the setpoint control's state machine
//
// @param none
// @return none
int ManualControl::update(void)
{
int rc = 0;
switch ( this->currentState ) {
case STATE_INIT:
// do nothing
break;
case STATE_STARTUP:
if ( state ) {
this->currentState = STATE_CONTROL_ON;
} else {
this->currentState = STATE_CONTROL_OFF;
}
this->powerOutput();
break;
case STATE_CONTROL_ON:
if ( !state ) {
this->currentState = STATE_CONTROL_OFF;
this->powerOutput();
}
break;
case STATE_CONTROL_OFF:
if ( state ) {
this->currentState = STATE_CONTROL_ON;
this->powerOutput();
}
break;
default:
logError("%s unknown state %d\n", __func__, this->currentState);
rc = -1;
break;
}
return rc;
}
// method: unregisterControl
// description: unregister this control with the output master
//
// @param none
// @return none
int ManualControl::unregisterControl(void)
{
logInfo("%s: Attempting to unregister %s\n",
__func__, controlFile.c_str());
OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
memset(output_mail, 0, sizeof(OutputControlMsg_t));
output_mail->controlType = CONTROL_MANUAL;
output_mail->action = ACTION_CONTROL_UNREGISTER;
output_mail->priority = this->priority;
strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
OutputMasterMailBox.put(output_mail);
return 0;
}
//
// method: powerOutput
// description: send a message to the output thread to power the relay
//
// @param none
// @return -1 on error; 0 otherwise
//
int ManualControl::powerOutput(void)
{
printf("%s: %s attempting to manually turn %s relay %s\n",
__func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());
OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
memset(output_mail, 0, sizeof(OutputControlMsg_t));
output_mail->controlType = CONTROL_MANUAL;
output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
output_mail->priority = this->priority;
strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
OutputMasterMailBox.put(output_mail);
return 0;
}
//
// method: display
// description: display the pertinents
//
// @param none
// @return none
//
void ManualControl::display(void)
{
string mapper[] = { "INIT",
"STARTUP",
"CONTROL_ON",
"CONTROL_OFF"
};
printf("\r\n");
std::cout << left << setw(10) << setfill(' ') << "manual: ";
std::cout << left << setw(32) << setfill(' ') << controlFile;
std::cout << left << setw(20) << setfill(' ') << id;
std::cout << left << setw(20) << setfill(' ') << output;
std::cout << left << setw(6) << setfill(' ') << type;
std::cout << left << setw(6) << setfill(' ') << priority;
std::cout << left << setw(8) << setfill(' ') << duration;
std::cout << left << setw(8) << setfill(' ') << percent;
std::cout << left << setw(16) << setfill(' ') << mapper[currentState];
std::cout.flush();
}
