Page MenuHomec4science

syncvar.h
No OneTemporary

File Metadata

Created
Sat, Jun 8, 04:50

syncvar.h

/*
* Copyright (C) 2017 EPFL-LSRO (Laboratoire de Systemes Robotiques).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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