MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/schematic/StorableValue.cpp
2024-06-07 11:12:56 +03:00

110 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* StorableValue.cpp
*
* Created on: 3 сент. 2020 г.
* Author: ПО и ВТ ЭП
*/
#include "StorableValue.hh"
#include <cstring>
void schematic::StorableValue::set( float new_value ) {
current_value = new_value;
need_set = true;
}
float schematic::StorableValue::get() const {
if( value_state == read
|| value_state == waitReadComplete || not( is_value_valid ) )
return default_value;
else
return current_value;
}
const float & schematic::StorableValue::value() const {
return current_value;
}
const bool & schematic::StorableValue::valid() const {
return is_value_valid;
}
schematic::StorableValue::StorableValue( const Constraints & constraints,
peripheral::IMemoryAccess & memory,
std::size_t offset, float default_value) :
current_value(default_value), default_value(default_value), value_state(read), limits(constraints), is_value_valid(false),
memory(memory), offset(offset) {}
void schematic::StorableValue::process() {
if( need_set ) {
value_state = write;
need_set = false;
}
switch( value_state ) {
case write: {
const char * pData = reinterpret_cast<const char *>( &current_value );
std::memcpy(buff, pData, sizeof(float));
buff[sizeof(float)] = calcCrcValue(current_value);
if( memory.write( buff, offset, sizeof(buff) ) )
value_state = waitWriteComplete;
} break;
case read: {
if( memory.read( buff, offset, sizeof(buff) ) )
value_state = waitReadComplete;
} break;
case waitWriteComplete: {
if( memory.isWriteComplete() )
value_state = idle;
} break;
case waitReadComplete: {
if( memory.isReadComplete() ) {
char * pData = reinterpret_cast<char *>( &current_value );
std::memcpy(pData, buff, sizeof(float));
if( buff[sizeof(float)] == calcCrcValue(current_value) )
is_value_valid = true;
else
is_value_valid = false;
value_state = idle;
}
} break;
case idle: {
} break;
}
}
char schematic::StorableValue::calcCrcValue( float value ) {
const char * data = reinterpret_cast<const char *>( &value );
return ~( data[0] + data[1] );
}