Page MenuHomec4science

syncvar.cpp
No OneTemporary

File Metadata

Created
Sun, Jul 28, 00:34

syncvar.cpp

#include "syncvar.h"
#include "hriboard.h"
#include <QThread>
VarType getType(const bool*) { return VarType::BOOL; }
VarType getType(const uint8_t*) { return VarType::UINT8; }
VarType getType(const int8_t*) { return VarType::INT8; }
VarType getType(const uint16_t*) { return VarType::UINT16; }
VarType getType(const int16_t*) { return VarType::INT16; }
VarType getType(const uint32_t*) { return VarType::UINT32; }
VarType getType(const int32_t*) { return VarType::INT32; }
VarType getType(const uint64_t*) { return VarType::UINT64; }
VarType getType(const int64_t*) { return VarType::INT64; }
VarType getType(const float*) { return VarType::FLOAT32; }
VarType getType(const double*) { return VarType::FLOAT64; }
const int VAR_SIZE[] = { 1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8 };
/**
* @brief Constructor
* @param index index in the SyncVars list.
* @param name name of the SyncVar.
* @param type type of the SyncVar value.
* @param access access right of the SyncVar.
*/
SyncVar::SyncVar(int index, QString name, VarType type, VarAccess access) :
index(index), name(name), type(type), access(access)
{
data.resize(VAR_SIZE[(int)type]);
upToDate = false;
}
/**
* @brief Gets the index in the SyncVars list.
* @return the index.
*/
int SyncVar::getIndex() const
{
return index;
}
/**
* @brief Gets the name of the SyncVar.
* @return the name.
*/
QString SyncVar::getName() const
{
return name;
}
/**
* @brief Gets the size of the SyncVar value.
* @return the value size [byte].
*/
int SyncVar::getSize() const
{
return data.size();
}
/**
* @brief Gets the variable type.
* @return the variable type.
*/
VarType SyncVar::getType() const
{
return type;
}
/**
* @brief Gets the SyncVar access rights.
* @return the SyncVar access rights.
*/
VarAccess SyncVar::getAccess() const
{
return access;
}
/**
* @brief Gets the local value in raw bytes form.
* @return a copy of the value data.
* @remark this value may not match the actual value of the variable on the
* board, because this function does not perform the synchronisation.
*/
QByteArray SyncVar::getData() const
{
return data;
}
/**
* @brief Sets the local value with raw bytes.
* @param newData the new value data.
* @remark this function only sets the local value of the SyncVar, not the value
* of the one on the board (no synchronisation performed).
*/
void SyncVar::setData(QByteArray newData)
{
if(newData.size() != data.size())
throw std::runtime_error("SyncVar::setData(): size do not match.");
data = newData;
upToDate = true;
}
/**
* @brief Gets if the variable is up-to-date.
* @return true if the variable value has been set, false if it has not been set
* since the beginning, or the call to setOutOfDate().
*/
bool SyncVar::isUpToDate() const
{
return upToDate;
}
/**
* @brief Sets the variable as out-of-date.
* This function is useful when the user need to know when the variable value
* has been updated.
*/
void SyncVar::setOutOfDate()
{
upToDate = false;
}
/**
* @brief Constructor.
*/
SyncVarPointerBase::SyncVarPointerBase()
{
syncVar = nullptr;
hriBoard = nullptr;
}
/**
* @brief Associates a SyncVarPointer to a SyncVar.
* @param hriBoard the HRI board object.
* @param syncVar the SyncVar to associate to the SyncVarPointer.
* @remark This method should only be called by HriBoard.
*/
void SyncVarPointerBase::associate(HriBoard *hriBoard,
SyncVar *syncVar)
{
this->hriBoard = hriBoard;
this->syncVar = syncVar;
}
/**
* @brief Gets if the SyncVarPointer can be "dereferenced".
* @return true if the pointer has been associated with a SyncVar, false
* otherwise.
*/
bool SyncVarPointerBase::isValid() const
{
return hriBoard != nullptr;
}
/**
* @brief Gets the SyncVar associated to the SyncVarPointer.
* @return a pointer to the associated SyncVar, or nullptr if it was not
* associated yet.
*/
SyncVar *SyncVarPointerBase::getVar() const
{
return syncVar;
}
/**
* @brief Updates the SyncVar value from the one on the board.
* This method requests the HRI board to send the value of the SyncVar. The
* local value will be updated later, when the "SyncVar value packet" will be
* received.
* To check if the value has actually been received, call isUpToDate().
* @remark This method does nothing if the SyncVar is WRITEONLY.
*/
void SyncVarPointerBase::readValue()
{
if(syncVar->getAccess() != WRITEONLY)
{
syncVar->setOutOfDate();
hriBoard->readRemoteVar(syncVar);
}
}
/**
* @brief Updates the SyncVar value on the board from local value.
* @remark This method does nothing if the SyncVar is READONLY.
*/
void SyncVarPointerBase::writeValue()
{
if(syncVar->getAccess() != READONLY)
hriBoard->writeRemoteVar(syncVar);
}

Event Timeline