/* * DeviceHandler.h * * Created on: 02/06/2021 * Author: Marco Modè * */ #ifndef MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_ #define MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_ #include #include #include #include /** * @brief Basic device handler to test device commanding without a physical device. * @details * This test device handler provided a basic demo for the device handler object. * It can also be commanded with the following PUS services, using * the specified object ID of the test device handler. * * 1. PUS Service 8 - Functional commanding * 2. PUS Service 2 - Device access, raw commanding * 3. PUS Service 20 - Parameter Management * 4. PUS Service 3 - Housekeeping * @author R. Mueller * @ingroup devices */ class ArduinoDH: public DeviceHandlerBase { public: /** * Build the test device in the factory. * @param objectId This ID will be assigned to the test device handler. * @param comIF The ID of the Communication IF used by test device handler. * @param cookie Cookie object used by the test device handler. This is * also used and passed to the comIF object. * @param onImmediately This will start a transition to MODE_ON immediately * so the device handler jumps into #doStartUp. Should only be used * in development to reduce need of commanding while debugging. */ ArduinoDH(object_id_t objectId, object_id_t comIF, CookieIF *cookie); /** * This can be used to enable and disable a lot of demo print output. * @param enable */ void enableFullDebugOutput(bool enable); virtual ~ ArduinoDH(); //! Size of internal buffer used for communication. static constexpr uint8_t MAX_BUFFER_SIZE = 255; //! Unique index if the device handler is created multiple times. /*testdevice::DeviceIndex deviceIdx = testdevice::DeviceIndex::DEVICE_0;*/ // Definiton of data structure for SPC communication. Three different structures are defined for measurements of: // - Temperature, // - Environmental data, // - Accelerometer data. struct Temperature { char start_string[8]; uint8_t Typ; uint8_t SPCChNumber; uint8_t Value_Cnt; float temperature; unsigned int Timestamp; char end_string[8]; Temperature() = default; Temperature(const char *_start_string, uint8_t _Typ, uint8_t _SPCChNumber, uint8_t _Value_Cnt, float _temperature, unsigned int _Timestamp, const char *_end_string) : Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt), temperature( _temperature), Timestamp(_Timestamp) { strncpy(start_string, _start_string, sizeof(start_string) - 1); strncpy(end_string, _end_string, sizeof(end_string) - 1); } }; struct Environmental { char start_string[8]; uint8_t Typ; uint8_t SPCChNumber; uint8_t Value_Cnt; float Value; unsigned int Timestamp; char end_string[8]; Environmental() = default; Environmental(const char *_start_string, uint8_t _Typ, uint8_t _SPCChNumber, uint8_t _Value_Cnt, float _Value, unsigned int _Timestamp, const char *_end_string) : Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt), Value( _Value), Timestamp(_Timestamp) { strncpy(start_string, _start_string, sizeof(start_string) - 1); strncpy(end_string, _end_string, sizeof(end_string) - 1); } }; struct Accelerometer { char start_string[8]; uint8_t Typ; uint8_t SPCChNumber; uint8_t Value_Cnt; float Value[9]; //max buffer unsigned int Timestamp[9]; //max buffer char end_string[8]; Accelerometer() = default; Accelerometer(const char *_start_string, uint8_t _Typ, uint8_t _SPCChNumber, uint8_t _Value_Cnt, const float *_Value, const unsigned int *_Timestamp, const char *_end_string) : Typ(_Typ), SPCChNumber(_SPCChNumber), Value_Cnt(_Value_Cnt) { strncpy(start_string, _start_string, sizeof(start_string) - 1); memcpy(&Value, _Value, sizeof(Value) - 1); memcpy(&Timestamp, _Timestamp, sizeof(Timestamp) - 1); strncpy(end_string, _end_string, sizeof(end_string) - 1); } }; // Three vectors are defined to store the three type of classes sequentially // during the phase of reading copying data from the buffers std::vector vecTemp; std::vector vecEnv; std::vector vecAcc; // Three dummy child structures are defined. They are used to store the three // different types of data during the measurement loop and then the data are // copied in the vectors above. // Then, they are overwritten by the data of next iteration and the process is // repeated ,until all the data from the buffer are copied to the three vectors // using the three different structures. Temperature Temp_ch; Environmental Env_ch; Accelerometer Acc_ch; protected: /*DeviceCommandId_t bufferId = 0x01; size_t bufferLen = 2034; DeviceCommandId_t *foundId; size_t *foundLen;*/ //testdevice::TestDataSet dataset; //! This is used to reset the dataset after a commanded change has been made. bool resetAfterChange = false; bool commandSent = false; /** DeviceHandlerBase overrides (see DHB documentation) */ /** * Hook into the DHB #performOperation call which is executed * periodically. */ /*void buildNormalModeCommands() override;*/ virtual void doStartUp() override; virtual void doShutDown() override; virtual ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override; virtual ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override; virtual ReturnValue_t buildCommandFromCommand( DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen) override; virtual void fillCommandAndReplyMap() override; virtual ReturnValue_t scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId, size_t *foundLen) override; virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override; virtual uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override; virtual void doTransition(Mode_t modeFrom, Submode_t subModeFrom) override; virtual void setNormalDatapoolEntriesInvalid() override; /*virtual ReturnValue_t initializeLocalDataPool( localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) override;*/ /*virtual LocalPoolObjectBase* getPoolObjectHandle(lp_id_t localPoolId) override;*/ /* HasParametersIF overrides */ /*virtual ReturnValue_t getParameter(uint8_t domainId, uint8_t uniqueId, ParameterWrapper *parameterWrapper, const ParameterWrapper *newValues, uint16_t startAtIndex) override;*/ uint8_t commandBuffer[MAX_BUFFER_SIZE]; // ****************************************************************** // delete this stuff bool oneShot = true; /* Variables for parameter service */ uint32_t testParameter0 = 0; int32_t testParameter1 = -2; float vectorFloatParams2[3] = { }; /* Change device handler functionality, changeable via parameter service */ uint8_t periodicPrintout = false; /*ReturnValue_t buildNormalModeCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen);*/ /*ReturnValue_t buildTestCommand0(DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen);*/ /*ReturnValue_t buildTestCommand1(DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen);*/ void passOnCommand(DeviceCommandId_t command, const uint8_t *commandData, size_t commandDataLen); ReturnValue_t interpretingNormalModeReply(); /*ReturnValue_t interpretingTestReply0(DeviceCommandId_t id, const uint8_t *packet);*/ /*ReturnValue_t interpretingTestReply1(DeviceCommandId_t id, const uint8_t *packet);*/ /*ReturnValue_t interpretingTestReply2(DeviceCommandId_t id, const uint8_t *packet);*/ /* Some timer utilities */ static constexpr uint8_t divider1 = 2; PeriodicOperationDivider opDivider1 = PeriodicOperationDivider(divider1); static constexpr uint8_t divider2 = 10; PeriodicOperationDivider opDivider2 = PeriodicOperationDivider(divider2); static constexpr uint32_t initTimeout = 2000; Countdown countdown1 = Countdown(initTimeout); // ******************************************************************************* }; #endif /* MISSION_DEVICEHANDLER_ARDUINODEVICEHANDLER_H_ */