Page MenuHomec4science

syncvar.h
No OneTemporary

File Metadata

Created
Tue, Apr 30, 21:24

syncvar.h

#ifndef SYNCVAR_H
#define SYNCVAR_H
#include <stdexcept>
#include <QString>
#include <QList>
#include "../../Firmware/src/definitions.h"
typedef comm_VarType VarType;
typedef comm_VarAccess VarAccess;
/**
* @addtogroup HriBoardLib
* @{
*/
/**
* @brief Variable that can be synchronized with its HRI board counterpart.
*/
class SyncVarBase
{
public:
SyncVarBase(int index, QString name, VarAccess access,
uint8_t* data, int size);
virtual ~SyncVarBase() = default;
int getIndex() const;
QString getName() const;
int getSize() const;
VarAccess getAccess() const;
QByteArray getData() const;
void setData(QByteArray newData);
bool isUpToDate() const;
void setOutOfDate();
virtual double toDouble() const = 0;
virtual void fromDouble(double value) = 0;
private:
int index; ///< Index of the SyncVar in the list.
QString name; ///< Name describing the SyncVar.
VarAccess access; ///< Access rights of the variable.
bool upToDate; ///< Indicates whether the local value is up-to-date or not.
uint8_t *const data;
const int size;
};
template<typename T>
class SyncVar : public SyncVarBase
{
public:
SyncVar(int index, QString name, VarAccess access, int varSize) :
SyncVarBase(index, name, access, (uint8_t*)&value, varSize)
{
}
~SyncVar() = default;
void setLocalValue(T value)
{
this->value = value;
}
T getLocalValue()
{
return value;
}
double toDouble() const override
{
return (double)value;
}
void fromDouble(double value) override
{
this->value = (T)value;
}
protected:
T value;
};
SyncVarBase* makeSyncVar(VarType type, int index, QString name,
VarAccess access);
/**
* @}
*/
#endif // SYNCVAR_H

Event Timeline