Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/Controls/FailsafeControl.cpp

Committer:
jmarkel44
Date:
2016-10-21
Revision:
250:1cd8ec63e9e9
Child:
252:3c9863f951b7

File content as of revision 250:1cd8ec63e9e9:

/******************************************************************************
 *
 * File:                CompositeControl.cpp
 * Desciption:          ICE Composite Control Class implementation
 *
 *****************************************************************************/
#include "FailsafeControl.h"
#include "cJSON.h"
#include "mDot.h"
#include "global.h"
#include <string>
#include <iostream>
#include <iomanip>

extern mDot *GLOBAL_mdot;

//
// method:          load
// description:     load a composite control
//
// @param           none
// @return          none
//
bool FailsafeControl::load(std::string _controlFile)
{
    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 ) {
        logError("%s: failed to open %s\n", __func__, controlFile.c_str());
        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 data
    cJSON * root    = cJSON_Parse(dataBuf);

    id                  = cJSON_GetObjectItem(root, "id")->valuestring;
    priority            = atoi(cJSON_GetObjectItem(root, "priority")->valuestring);
    input               = cJSON_GetObjectItem(root, "input")->valuestring;
    output              = cJSON_GetObjectItem(root, "output")->valuestring;

    hfs_data.value      = atof(cJSON_GetObjectItem(root, "hfsValue")->valuestring);
    hfs_data.dutyCycle  = atoi(cJSON_GetObjectItem(root, "hfsDutyCycle")->valuestring);
    hfs_data.interval   = atoi(cJSON_GetObjectItem(root, "hfsInterval")->valuestring);

    lfs_data.value      = atof(cJSON_GetObjectItem(root, "lfsValue")->valuestring);
    lfs_data.dutyCycle  = atoi(cJSON_GetObjectItem(root, "lfsDutyCycle")->valuestring);
    lfs_data.interval   = atoi(cJSON_GetObjectItem(root, "lfsInterval")->valuestring);

    return true;
}

//
// method:      start
// description: start the failsafe control
//
// @param       none
// @return      none
//
void FailsafeControl::start(void)
{
    currentState = STATE_START;
}

//
// method:      update
// description: update the faisafe control
//
// @param       none
// @return      none
//
void FailsafeControl::update(void)
{
    switch ( this->currentState ) {
        case STATE_INIT:
            // do nothing
            break;
        case STATE_START:
        case STATE_CONTROL_OFF:
        case STATE_CONTROL_LFS_ON:
        case STATE_CONTROL_LFS_OFF:
        case STATE_CONTROL_HFS_ON:
        case STATE_CONTROL_HFS_OFF:
        default:
            break;
    }
}

//
// method:      display
// description: display the pertinents
//
// @param       none
// @return      none
//
void FailsafeControl::display(void)
{
    const char *mapper[] = { "INIT",
                             "START",
                             "CONTROL_OFF",
                             "LFS_ON",
                             "LFS_OFF",
                             "HFS_ON",
                             "HFS_OFF",
                             "invalid"
                           };

    printf("\r\n");
    std::cout << left << setw(10) << setfill(' ') << "failsafe: ";
    std::cout << left << setw(32) << setfill(' ') << controlFile;
    std::cout << left << setw(20) << setfill(' ') << id;
    std::cout << left << setw(6)  << setfill(' ') << priority;
    std::cout << left << setw(20) << setfill(' ') << input;
    std::cout << left << setw(20) << setfill(' ') << output;
    std::cout << left << setw(16) << setfill(' ') << mapper[currentState];
    std::cout << left  << setw(12) << setfill(' ') << "lfs-> " << lfs_data.value << ":" << lfs_data.dutyCycle << ":" << lfs_data.interval;
    std::cout << right << setw(12) << setfill(' ') << "hfs-> " << hfs_data.value << ":" << hfs_data.dutyCycle << ":" << hfs_data.interval;

    std::cout.flush();
}