diff --git a/inc/Services/Parameter.hpp b/inc/Services/Parameter.hpp index ad2436d571666fb5b0cc69e278916b949704ef2a..a00b402181dd26e3d8e642c93a0c3d9e20f24b3f 100644 --- a/inc/Services/Parameter.hpp +++ b/inc/Services/Parameter.hpp @@ -3,7 +3,7 @@ #include "etl/bitset.h" -// Number of binary flags in every parameter. +// Number of binary flags in every parameter. Final number TBD. #define NUM_OF_FLAGS 5 /** @@ -16,23 +16,31 @@ typedef uint16_t ParamId; // parameter IDs are given sequentially typedef void(*UpdatePtr)(uint32_t*); // pointer to the update function of this parameter // (argument is a pointer to the variable where the value will be returned, in // this case currentValue) +typedef uint32_t ValueType; class Parameter { - uint8_t ptc = 0; // Packet field type code (PTC) - uint8_t pfc = 0; // Packet field format code (PFC) - UpdatePtr ptr = nullptr; // Function pointer used for updates - uint32_t currentValue = 0; // Last good value of the parameter + uint8_t ptc; // Packet field type code (PTC) + uint8_t pfc; // Packet field format code (PFC) + UpdatePtr ptr; // Function pointer used for updates etl::bitset<NUM_OF_FLAGS> flags = {false}; // Various flags (TBD which. Ideas: update with priority, do not poll, etc.) public: - Parameter(uint8_t new_ptc, uint8_t new_pfc, UpdatePtr new_ptr) { + Parameter(uint8_t new_ptc, uint8_t new_pfc, uint32_t initialValue = 0, UpdatePtr new_ptr = nullptr) { ptc = new_ptc; pfc = new_pfc; ptr = new_ptr; - (*ptr)(¤tValue); // call the update function for the initial value + if (ptr != nullptr) { + (*ptr)(¤tValue); // call the update function for the initial value + } + else { + currentValue = initialValue; + } } + + ValueType currentValue = 0; // Last good value of the parameter. TODO: Find a way to store arbitrary types (w/out + // templates preferred) }; diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp index e4093a4129fd6cfff6d346ab28062aec90e41b1f..cc2e5713e37fe37954f31014b810b4b40d11bba4 100644 --- a/inc/Services/ParameterService.hpp +++ b/inc/Services/ParameterService.hpp @@ -25,17 +25,24 @@ * corresponding Parameter structs containing the PTC, PFC and the parameter's value. */ + class ParameterService : public Service { private: - etl::map<ParamId, Parameter, MAX_PARAMS> paramsList; + static etl::map<ParamId, Parameter, MAX_PARAMS> paramsList; uint16_t numOfValidIds(Message idMsg); // count the valid ids in a given TC[20, 1] public: /** - * Initializes the parameter list with some dummy values for now. + * Initializes the parameter list. */ ParameterService(); + /** + * Adds a new parameter. If the parameter has not been added (either because the map is full or because it already + * exists in it) then returns true. + */ + static bool addParameter(uint8_t ptc, uint8_t pfc, uint32_t initialValue = 0, UpdatePtr ptr = nullptr); + /** * This function receives a TC[20, 1] packet and returns a TM[20, 2] packet * containing the current configuration diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp index a22b730e4772b827c6ae0f7bb69b96af2ec44ec6..dcbeaa44bd510595942e8e102982f3fe5e29a8b2 100644 --- a/src/Services/ParameterService.cpp +++ b/src/Services/ParameterService.cpp @@ -2,6 +2,10 @@ ParameterService::ParameterService() { + + addParameter(3, 14); + addParameter(3, 14); + // // Test code, setting up some of the parameter fields // // time_t currTime = time(nullptr); @@ -29,7 +33,11 @@ ParameterService::ParameterService() { //#endif } -void +bool ParameterService::addParameter(uint8_t ptc, uint8_t pfc, uint32_t initial_value, UpdatePtr ptr) { + Parameter param = Parameter(ptc, pfc, initial_value, ptr); + return paramsList.insert(std::make_pair(0, param)).second; + // second element of the returned std::pair is whether the given item was inserted or not +} void ParameterService::reportParameterIds(Message& paramIds) { paramIds.assertTC(20, 1); @@ -84,7 +92,8 @@ void ParameterService::setParameterIds(Message& newParamValues) { uint16_t currId = newParamValues.readUint16(); if (paramsList.find(currId) != paramsList.end()) { - paramsList[currId].currentValue = newParamValues.readUint32(); + paramsList[currId].currentValue = newParamValues.readUint32(); // TODO: add a check here with the new + // flag functionality } else { ErrorHandler::reportError(newParamValues, ErrorHandler::ExecutionStartErrorType::UnknownExecutionStartError);