Skip to content

Commit

Permalink
better basetypes, improved adns9800_data.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Jul 15, 2024
1 parent 6517354 commit 57ba367
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
26 changes: 14 additions & 12 deletions src/modm/driver/motion/adns9800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <modm/platform.hpp>
#include <modm/processing/fiber.hpp>
#include <modm/processing/timer/timeout.hpp>
#include <modm/architecture/interface/spi_device.hpp>
#include <modm/architecture/interface/accessor_flash.hpp>
#include <modm/debug/logger.hpp>
Expand Down Expand Up @@ -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<PeriodType, std::ratio<1, 50_MHz>>;

// forward declarations
struct Data;
struct DataAndFaildetect;
Expand All @@ -120,7 +126,6 @@ template <class SpiMaster, class Cs>
class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
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.
Expand All @@ -133,9 +138,6 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
* @ingroup modm_driver_adns9800
*/
struct ShutterConfig {
using PeriodType = uint16_t;
using Duration = std::chrono::duration<PeriodType, std::ratio<1, 50_MHz>>;

PeriodType period_min, period_max, exposure_max;

// The datasheet refers to "wait for one frame" without better specification.
Expand Down Expand Up @@ -234,7 +236,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
}

void
set(const ShutterConfig shutter_new) {
set(ShutterConfig shutter_new) {
if (shutter_new.isValid()) {
shutter_config = shutter_new;

Expand All @@ -252,8 +254,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {

/// 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()) {
Expand All @@ -266,8 +268,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
}

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()) {
Expand All @@ -280,8 +282,8 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
}

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()) {
Expand Down Expand Up @@ -413,7 +415,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
});
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:
Expand Down
36 changes: 18 additions & 18 deletions src/modm/driver/motion/adns9800_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
#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
*
* @ingroup modm_driver_adns9800
*/
namespace modm {

/// Minimum Payload: only retrieve relative movement from the sensor
/// Bare minimum: Relative motion
struct adns9800::Data {
modm::Vector<int16_t, 2> delta;
const modm::Vector<int16_t, 2> delta;


protected:
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 57ba367

Please sign in to comment.