Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Wed Sep 21 16:17:12 2016 +0000
Revision:
97:5cf6ab71dcd0
Parent:
96:807f04bd5256
Child:
98:8eab18d03ac2
initial spin of manual controls ; included create-manual command; verifies the output task map of vectors data struct; ;

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 96:807f04bd5256 13
jmarkel44 96:807f04bd5256 14 extern mDot *GLOBAL_mdot;
jmarkel44 96:807f04bd5256 15
jmarkel44 96:807f04bd5256 16 // Method: load
jmarkel44 96:807f04bd5256 17 // Description: open the configuration file and assign data to the
jmarkel44 96:807f04bd5256 18 // setpoint control object
jmarkel44 96:807f04bd5256 19 //
jmarkel44 96:807f04bd5256 20 // @param controlFile -> name of the control file
jmarkel44 96:807f04bd5256 21 // @return true if data is assigned; false on error
jmarkel44 96:807f04bd5256 22
jmarkel44 96:807f04bd5256 23 bool ManualControl::load(string _controlFile)
jmarkel44 96:807f04bd5256 24 {
jmarkel44 97:5cf6ab71dcd0 25 MbedJSONValue json_value;
jmarkel44 97:5cf6ab71dcd0 26 controlFile = _controlFile;
jmarkel44 97:5cf6ab71dcd0 27
jmarkel44 97:5cf6ab71dcd0 28 // open and read from the control file
jmarkel44 97:5cf6ab71dcd0 29 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
jmarkel44 97:5cf6ab71dcd0 30 if ( file.fd < 0 )
jmarkel44 97:5cf6ab71dcd0 31 return false;
jmarkel44 97:5cf6ab71dcd0 32
jmarkel44 97:5cf6ab71dcd0 33 // read the data into a buffer
jmarkel44 97:5cf6ab71dcd0 34 char dataBuf[1024];
jmarkel44 97:5cf6ab71dcd0 35
jmarkel44 97:5cf6ab71dcd0 36 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 97:5cf6ab71dcd0 37 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 97:5cf6ab71dcd0 38 logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 39 // caller should destroy the object
jmarkel44 97:5cf6ab71dcd0 40 return false;
jmarkel44 97:5cf6ab71dcd0 41 }
jmarkel44 97:5cf6ab71dcd0 42
jmarkel44 97:5cf6ab71dcd0 43 // close the file
jmarkel44 97:5cf6ab71dcd0 44 GLOBAL_mdot->closeUserFile(file);
jmarkel44 97:5cf6ab71dcd0 45
jmarkel44 97:5cf6ab71dcd0 46 // parse the json data
jmarkel44 97:5cf6ab71dcd0 47 parse(json_value, dataBuf);
jmarkel44 97:5cf6ab71dcd0 48
jmarkel44 97:5cf6ab71dcd0 49 id = json_value["id"].get<string>();
jmarkel44 97:5cf6ab71dcd0 50 output = json_value["id"].get<string>();
jmarkel44 97:5cf6ab71dcd0 51 type = atoi(json_value["type"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 52 priority = atoi(json_value["priority"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 53 duration = atoi(json_value["duration"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 54 setpoint = atof(json_value["setpoint"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 55 state = atoi(json_value["state"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 56 percent = atoi(json_value["percent"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 57
jmarkel44 96:807f04bd5256 58 return true;
jmarkel44 96:807f04bd5256 59 }
jmarkel44 96:807f04bd5256 60
jmarkel44 96:807f04bd5256 61 // Method: start
jmarkel44 96:807f04bd5256 62 // Description: start the manual control
jmarkel44 96:807f04bd5256 63 //
jmarkel44 96:807f04bd5256 64 // @param none
jmarkel44 96:807f04bd5256 65 // @return none
jmarkel44 96:807f04bd5256 66
jmarkel44 97:5cf6ab71dcd0 67 void ManualControl::start(void)
jmarkel44 96:807f04bd5256 68 {
jmarkel44 97:5cf6ab71dcd0 69 currentState = STATE_STARTUP;
jmarkel44 97:5cf6ab71dcd0 70
jmarkel44 96:807f04bd5256 71 }
jmarkel44 96:807f04bd5256 72
jmarkel44 96:807f04bd5256 73 // Method: update
jmarkel44 96:807f04bd5256 74 // Description:
jmarkel44 96:807f04bd5256 75 //
jmarkel44 96:807f04bd5256 76 // @param none
jmarkel44 96:807f04bd5256 77 // @return none
jmarkel44 96:807f04bd5256 78
jmarkel44 96:807f04bd5256 79 int ManualControl::update(void)
jmarkel44 96:807f04bd5256 80 {
jmarkel44 97:5cf6ab71dcd0 81 int rc = 0;
jmarkel44 97:5cf6ab71dcd0 82 switch ( this->currentState ) {
jmarkel44 97:5cf6ab71dcd0 83 case STATE_STARTUP:
jmarkel44 97:5cf6ab71dcd0 84 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 85 this->currentState = STATE_CONTROL_ON;
jmarkel44 97:5cf6ab71dcd0 86 } else {
jmarkel44 97:5cf6ab71dcd0 87 this->currentState = STATE_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 88 }
jmarkel44 97:5cf6ab71dcd0 89 this->triggerOutput();
jmarkel44 97:5cf6ab71dcd0 90 break;
jmarkel44 97:5cf6ab71dcd0 91 case STATE_CONTROL_ON:
jmarkel44 97:5cf6ab71dcd0 92 if ( !state ) {
jmarkel44 97:5cf6ab71dcd0 93 this->currentState = STATE_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 94 this->triggerOutput();
jmarkel44 97:5cf6ab71dcd0 95 }
jmarkel44 97:5cf6ab71dcd0 96 break;
jmarkel44 97:5cf6ab71dcd0 97 case STATE_CONTROL_OFF:
jmarkel44 97:5cf6ab71dcd0 98 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 99 this->currentState = STATE_CONTROL_ON;
jmarkel44 97:5cf6ab71dcd0 100 this->triggerOutput();
jmarkel44 97:5cf6ab71dcd0 101 }
jmarkel44 97:5cf6ab71dcd0 102 break;
jmarkel44 97:5cf6ab71dcd0 103 default:
jmarkel44 97:5cf6ab71dcd0 104 logError("%s unknown state %d\n", __func__, this->currentState);
jmarkel44 97:5cf6ab71dcd0 105 rc = -1;
jmarkel44 97:5cf6ab71dcd0 106 break;
jmarkel44 97:5cf6ab71dcd0 107 }
jmarkel44 97:5cf6ab71dcd0 108 return rc;
jmarkel44 96:807f04bd5256 109 }
jmarkel44 96:807f04bd5256 110
jmarkel44 96:807f04bd5256 111 // Method: unregisterControl
jmarkel44 96:807f04bd5256 112 // Description: unregister this control with the output master
jmarkel44 96:807f04bd5256 113 //
jmarkel44 96:807f04bd5256 114 // @param none
jmarkel44 96:807f04bd5256 115 // @return none
jmarkel44 96:807f04bd5256 116 int ManualControl::unregisterControl(void)
jmarkel44 96:807f04bd5256 117 {
jmarkel44 97:5cf6ab71dcd0 118 logInfo("%s: %s attempting to stop feed on relay %s\n",
jmarkel44 97:5cf6ab71dcd0 119 __func__, controlFile.c_str(), output.c_str());
jmarkel44 97:5cf6ab71dcd0 120
jmarkel44 97:5cf6ab71dcd0 121 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 122 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 97:5cf6ab71dcd0 123 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 97:5cf6ab71dcd0 124 output_mail->priority = this->priority;
jmarkel44 97:5cf6ab71dcd0 125 strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1);
jmarkel44 97:5cf6ab71dcd0 126 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 97:5cf6ab71dcd0 127 OutputMasterMailBox.put(output_mail);
jmarkel44 97:5cf6ab71dcd0 128 return 0;
jmarkel44 97:5cf6ab71dcd0 129 }
jmarkel44 97:5cf6ab71dcd0 130
jmarkel44 97:5cf6ab71dcd0 131 int ManualControl::triggerOutput(void)
jmarkel44 97:5cf6ab71dcd0 132 {
jmarkel44 97:5cf6ab71dcd0 133 printf("%s: %s attempting to manually turn %s relay %s\n",
jmarkel44 97:5cf6ab71dcd0 134 __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());
jmarkel44 97:5cf6ab71dcd0 135
jmarkel44 97:5cf6ab71dcd0 136 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 137 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 97:5cf6ab71dcd0 138 output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 139 output_mail->priority = this->priority;
jmarkel44 97:5cf6ab71dcd0 140 strncpy(output_mail->output, this->output.c_str(), sizeof(output_mail->output)-1);
jmarkel44 97:5cf6ab71dcd0 141 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 97:5cf6ab71dcd0 142 OutputMasterMailBox.put(output_mail);
jmarkel44 96:807f04bd5256 143 return 0;
jmarkel44 96:807f04bd5256 144 }
jmarkel44 96:807f04bd5256 145
jmarkel44 96:807f04bd5256 146 void ManualControl::display(void)
jmarkel44 96:807f04bd5256 147 {
jmarkel44 97:5cf6ab71dcd0 148 string mapper[] = { "STARTUP",
jmarkel44 97:5cf6ab71dcd0 149 "CONTROL_ON",
jmarkel44 97:5cf6ab71dcd0 150 "CONTROL_OFF"
jmarkel44 97:5cf6ab71dcd0 151 };
jmarkel44 97:5cf6ab71dcd0 152
jmarkel44 97:5cf6ab71dcd0 153 printf("\r controlFile : %s \n", controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 154 printf("\r id : %s \n", id.c_str());
jmarkel44 97:5cf6ab71dcd0 155 printf("\r type : %u \n", type);
jmarkel44 97:5cf6ab71dcd0 156 printf("\r priority : %u \n", priority);
jmarkel44 97:5cf6ab71dcd0 157 printf("\r duration : %u \n", duration);
jmarkel44 97:5cf6ab71dcd0 158 printf("\r state : %u \n", state);
jmarkel44 97:5cf6ab71dcd0 159 printf("\r percent : %u \n", percent);
jmarkel44 97:5cf6ab71dcd0 160 printf("\r currentState: %s \n", mapper[currentState].c_str());
jmarkel44 97:5cf6ab71dcd0 161
jmarkel44 97:5cf6ab71dcd0 162 printf("\r\n");
jmarkel44 96:807f04bd5256 163
jmarkel44 96:807f04bd5256 164 }