From 57ba3677a91e709c48578e2ecd4d14b4ce0c9d1b Mon Sep 17 00:00:00 2001 From: Thomas Sommer Date: Mon, 15 Jul 2024 12:43:49 +0200 Subject: [PATCH] better basetypes, improved adns9800_data.hpp --- src/modm/driver/motion/adns9800.hpp | 26 +++++++++-------- src/modm/driver/motion/adns9800_data.hpp | 36 ++++++++++++------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/modm/driver/motion/adns9800.hpp b/src/modm/driver/motion/adns9800.hpp index a3fa2a1b99..2da3d5c60f 100644 --- a/src/modm/driver/motion/adns9800.hpp +++ b/src/modm/driver/motion/adns9800.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -101,6 +102,11 @@ struct adns9800 { }; MODM_FLAGS8(ConfigurationII); + // Adns9800 takes periods up to 65535 + using PeriodType = uint16_t; + // The internal timer is running at 50MHz, hence the ratio: + using Duration = std::chrono::duration>; + // forward declarations struct Data; struct DataAndFaildetect; @@ -120,7 +126,6 @@ template class Adns9800 : public adns9800, public modm::SpiDevice { public: static constexpr size_t FrameSize = 30 * 30; // Size of CMOS - /** * @brief ShutterConfig boundaries which may be selected by the automatic frame rate control. * Periods are expressed as ticks of the device, running at 50MHz. @@ -133,9 +138,6 @@ class Adns9800 : public adns9800, public modm::SpiDevice { * @ingroup modm_driver_adns9800 */ struct ShutterConfig { - using PeriodType = uint16_t; - using Duration = std::chrono::duration>; - PeriodType period_min, period_max, exposure_max; // The datasheet refers to "wait for one frame" without better specification. @@ -234,7 +236,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice { } void - set(const ShutterConfig shutter_new) { + set(ShutterConfig shutter_new) { if (shutter_new.isValid()) { shutter_config = shutter_new; @@ -252,8 +254,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice { /// In fixed framerate mode (Register ConfigurationII::Fixed_FrameRate: 1), period_max selects the framerate void - setFramePeriodMax(const uint16_t period_max) { - const uint16_t recover = shutter_config.period_max; + setFramePeriodMax(const PeriodType period_max) { + const PeriodType recover = shutter_config.period_max; shutter_config.period_max = period_max; if (shutter_config.isValid()) { @@ -266,8 +268,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice { } void - setFramePeriodMin(const uint16_t period_min) { - const uint16_t recover = shutter_config.period_min; + setFramePeriodMin(const PeriodType period_min) { + const PeriodType recover = shutter_config.period_min; shutter_config.period_min = period_min; if (shutter_config.isValid()) { @@ -280,8 +282,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice { } void - setExposureMax(const uint16_t shutter_max) { - const uint16_t recover = shutter_config.shutter_max; + setExposureMax(const PeriodType shutter_max) { + const PeriodType recover = shutter_config.shutter_max; shutter_config.shutter_max = shutter_max; if (shutter_config.assert()) { @@ -413,7 +415,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice { }); modm::this_fiber::sleep_for(shutter_config.getOneFrameTime()); - // Additionaly one could request a CRC check of the firmware. @see datasheet P31 + // @optimize Request a CRC result of the firmware. @see datasheet P31 } private: diff --git a/src/modm/driver/motion/adns9800_data.hpp b/src/modm/driver/motion/adns9800_data.hpp index 70ab2982d3..6b211d7faa 100644 --- a/src/modm/driver/motion/adns9800_data.hpp +++ b/src/modm/driver/motion/adns9800_data.hpp @@ -20,9 +20,8 @@ #include "adns9800.hpp" /** - * You can stream Data in 3 levels of detail with increasing size: Data, - * DataObserve and DataAnalysis. Of course, there's a tradeof with RAM - * consumption and bus time. + * Choose the level of detail you need from your Adns9800. + * Of course, there's a tradeof with RAM and bus time. * * @author Thomas Sommer * @@ -30,9 +29,9 @@ */ namespace modm { -/// Minimum Payload: only retrieve relative movement from the sensor +/// Bare minimum: Relative motion struct adns9800::Data { - modm::Vector delta; + const modm::Vector delta; protected: @@ -47,7 +46,7 @@ struct adns9800::Data { friend class Adns9800; }; -/// Retrieve relative movement from the sensor plus Laser fault detection +/// Relative motion And Laser fault detection flags: struct adns9800::DataAndFaildetect : public adns9800::Data { // @todo Looks wastefull - use a bitfield or similar instead const bool LaserFaultDetected; @@ -66,8 +65,7 @@ struct adns9800::DataAndFaildetect : public adns9800::Data { friend class Adns9800; }; -/// Retrieve relative movement from the sensor, Laser fault detection -/// and monitoring metrics from the shutter unit. +/// Relative motion, Laser fault detection flags and metrics from the shutter unit: struct adns9800::DataAndFaildetectAndMonitoring : public adns9800::DataAndFaildetect { struct Statistics { @@ -83,7 +81,7 @@ struct adns9800::DataAndFaildetectAndMonitoring * no surface below the sensor. SQUAL remains fairly high throughout the * Z-height range which allows illumination of most pixels in the sensor. */ - uint8_t surface_quality; + const uint8_t surface_quality; /** * This register is used to find the average pixel value. It reports the * upper byte of a 17-bit counter which sums all 900 pixels in the current @@ -93,24 +91,26 @@ struct adns9800::DataAndFaildetectAndMonitoring * 223 (127 * 900 / 512 truncated to an integer). The minimum register value * is 0. The pixel sum value can change every frame. */ - uint8_t pixel_sum; + const uint8_t pixel_sum; /** * Minium and maximum Pixel value in current frame. Range: * 0 to 127. * The minimum and maximum pixel value can change every frame. */ - uint8_t max_pixel; - uint8_t min_pixel; + const uint8_t max_pixel; + const uint8_t min_pixel; } statistics; // Automated shutter selections struct Shutter { - /// The current exposure in cycles of Clock. - /// exposure <= Adns9800::Shutter::exposure_max - uint16_t exposure; - /// The Frame period in cycles of Clock. - /// Adns9800::Shutter::period_min <= period <= Adns9800::Shutter::period_max - uint16_t period; + Duration getExposureTime() const { return Duration(exposure); }; + Duration getFrameTime() const { return Duration(period); }; + + protected: + /// exposure <= ShutterConfig::exposure_max + const PeriodType exposure; + /// ShutterConfig::period_min <= period <= ShutterConfig::period_max + const PeriodType period; } shutter; protected: