/** \File NvmEeprom.hpp * \copyright (c) 2025 len0rd * \date 2025-01-09 * * Very basic non-volatile memory handler to save and retrieve data * needed by the controller */ #ifndef __NVMEEPROM_H__ #define __NVMEEPROM_H__ #include struct JoystickAxisCalibration { /// Minimum value axis could be uint16_t min; /// average center point of axis uint16_t mid; /// max value axis could be uint16_t max; /// Check if the data in this struct makes sense /// Returns true if data might be valid, otherwise false bool valid() const; } __attribute__((packed)); /// calibration of min/max for single axis analog signals with no mid point /// ie: a potentiometer struct LinearAnalogCalibration { uint16_t min; uint16_t max; inline bool valid() const { return min < max; } } __attribute__((packed)); struct JoystickCalibration { JoystickAxisCalibration x; JoystickAxisCalibration y; } __attribute__((packed)); static constexpr uint32_t VALID_MARKER_DEFAULT = 0xcafecall; union NonVolatileMemory { struct Calibration { /// place hard-coded start marker at the beginning of the data /// to indicate validity uint32_t validMarker = VALID_MARKER_DEFAULT; JoystickCalibration joystickCal; LinearAnalogCalibration linearCal; } __attribute__((packed)) calData; uint8_t rawBytes[NonVolatileMemory::Calibration] = {}; inline bool valid() const { return calData.validMarker == VALID_MARKER_DEFAULT; } }; /// @brief Write the provided struct to EEPROM void writeNvm(const NonVolatileMemory &nvm); void writeCalibration(const JoystickCalibration&cal); void readNvm(NonVolatileMemory &nvm); #endif /* __NVMEEPROM_H__ */