Skip to content

Commit beff0b5

Browse files
committed
impros applied
1 parent 3f9b1ed commit beff0b5

File tree

3 files changed

+110
-86
lines changed

3 files changed

+110
-86
lines changed

src/modm/driver/motion/adns9800.hpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ struct adns9800 {
109109
using PeriodType = uint16_t;
110110
using Duration = std::chrono::duration<PeriodType, std::ratio<1, 50_MHz>>;
111111

112+
using DeltaType = int16_t;
113+
112114
// forward declarations
113115
struct Data;
114116
struct Data_Fails;
@@ -315,12 +317,12 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
315317
requires std::is_base_of_v<Data, D>
316318
D
317319
read() {
318-
uint8_t buffer[D::length];
320+
std::array<uint8_t, D::length> buffer;
319321

320322
readTransaction([this, &buffer]() {
321323
SpiMaster::transfer(static_cast<uint8_t>(Register::Motion_Burst));
322324
modm::this_fiber::sleep_for(shutter_config.getOneFrameTime());
323-
SpiMaster::transfer(nullptr, buffer, sizeof(buffer));
325+
SpiMaster::transfer(nullptr, std::begin(buffer), buffer.size());
324326
});
325327

326328
return D(buffer);
@@ -341,12 +343,14 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
341343
template<class Callable>
342344
void
343345
readTransaction(Callable&& closure) {
344-
while(not ready_to_read.isExpired()) modm::this_fiber::yield();
345-
while(not this->acquireMaster()) modm::this_fiber::yield();
346+
while(not ready_to_read.isExpired())
347+
modm::this_fiber::yield();
348+
while(not this->acquireMaster())
349+
modm::this_fiber::yield();
346350
Cs::reset();
347351

348352
std::forward<Callable>(closure)();
349-
353+
350354
ready_to_write.restart(20us); // tSRW: [t]ime [S]pi between [R]rite and [W]rite
351355
ready_to_read.restart(20us); // tSRR: [t]ime [S]pi between [R]rite and [R]ead
352356

@@ -357,8 +361,10 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
357361
template<class Callable>
358362
void
359363
writeTransaction(Callable&& closure) {
360-
while(not ready_to_write.isExpired()) modm::this_fiber::yield();
361-
while(not this->acquireMaster()) modm::this_fiber::yield();
364+
while(not ready_to_write.isExpired())
365+
modm::this_fiber::yield();
366+
while(not this->acquireMaster())
367+
modm::this_fiber::yield();
362368
Cs::reset();
363369

364370
std::forward<Callable>(closure)();
@@ -386,7 +392,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
386392

387393
void
388394
writeRegister(const Register reg, const uint8_t data) {
389-
writeTransaction([=]() {
395+
writeTransaction([&]() {
390396
// Setting Bit7 indicates a write
391397
SpiMaster::transfer(static_cast<uint8_t>(reg) | Bit7);
392398
SpiMaster::transfer(data);
@@ -400,7 +406,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
400406
modm::this_fiber::sleep_for(shutter_config.getOneFrameTime());
401407
writeRegister(Register::SROM_Enable, 0x18); // Start SROM download
402408

403-
writeTransaction([=]() {
409+
writeTransaction([&]() {
404410
// Setting Bit7 indicates a write
405411
SpiMaster::transfer(static_cast<uint8_t>(Register::SROM_Load_Burst) | Bit7);
406412

@@ -413,7 +419,7 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
413419
});
414420
modm::this_fiber::sleep_for(shutter_config.getOneFrameTime());
415421

416-
// @optimize Request a CRC result of the firmware. @see datasheet P31
422+
// Additionaly, a CRC of the firmware may be requested. @see datasheet P31
417423
}
418424

419425
private:

src/modm/driver/motion/adns9800.lb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def prepare(module, options):
3232
module.depends(
3333
":processing:resumable",
3434
":architecture:spi.device",
35-
":math:geometry",
36-
":architecture:assert",
35+
":math:geometry"
3736
)
3837
return True
3938

src/modm/driver/motion/adns9800_data.hpp

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,110 @@
2525
* @author Thomas Sommer
2626
* @ingroup modm_driver_adns9800
2727
*/
28-
namespace modm {
28+
namespace modm
29+
{
2930

3031
/// @brief Relative motion since the last read
31-
struct adns9800::Data {
32-
const modm::Vector<int16_t, 2> delta;
32+
struct adns9800::Data
33+
{
34+
const modm::Vector<DeltaType, 2> delta;
3335

3436
protected:
35-
static constexpr size_t length = 6;
36-
Data(std::span<uint8_t, length> buffer)
37-
: delta{
38-
buffer[3] << 8 | buffer[2], // delta x
39-
buffer[5] << 8 | buffer[4] // delta y
40-
} {}
41-
42-
template <class, class>
43-
friend class Adns9800;
37+
static constexpr size_t length = 6;
38+
Data(std::span<uint8_t, length> buffer)
39+
: delta{
40+
static_cast<DeltaType>(buffer[3] << 8 | buffer[2]),
41+
static_cast<DeltaType>(buffer[5] << 8 | buffer[4])
42+
}
43+
{}
44+
45+
template<class, class>
46+
friend class Adns9800;
4447
};
4548

4649
/// @brief Relative motion and laser fault detection flags
47-
struct adns9800::Data_Fails : public adns9800::Data {
48-
const bool LaserFaultDetected: 1;
49-
const bool LaserPowerValid: 1;
50-
const bool isRunningSROMCode: 1;
50+
struct adns9800::Data_Fails : public adns9800::Data
51+
{
52+
const bool LaserFaultDetected : 1;
53+
const bool LaserPowerValid : 1;
54+
const bool isRunningSROMCode : 1;
5155

5256
protected:
53-
Data_Fails(std::span<uint8_t, length> buffer)
54-
: Data(buffer),
55-
LaserFaultDetected(buffer[0] & Bit6),
56-
LaserPowerValid(buffer[0] & Bit5),
57-
isRunningSROMCode(buffer[1] & Bit6) {
58-
}
59-
60-
template <class, class>
61-
friend class Adns9800;
57+
Data_Fails(std::span<uint8_t, length> buffer)
58+
: Data(buffer),
59+
LaserFaultDetected(buffer[0] & Bit6),
60+
LaserPowerValid(buffer[0] & Bit5),
61+
isRunningSROMCode(buffer[1] & Bit6)
62+
{}
63+
64+
template<class, class>
65+
friend class Adns9800;
6266
};
6367

6468
/// @brief Relative motion, laser fault detection flags and metrics from the shutter unit.
65-
struct adns9800::Data_Fails_Monitoring
66-
: public adns9800::Data_Fails {
67-
struct Statistics {
68-
/**
69-
* Number of features visible by the sensor in the current frame. Range 0 to 169.
70-
* Total number of features: 4 * surface_quality.
71-
* Changes are expected when moving over a surface.
72-
* Convergates to 0 if there is no surface below the sensor.
73-
* surface_quality remains fairly high throughout the Z-height.
74-
*/
75-
const uint8_t surface_quality;
76-
// pixel_sum containes the average pixel value. Range 0 to 223.
77-
// It reports the upper byte of a 17-bit counter which sums all 900 pixels in the current frame
78-
const uint8_t pixel_sum;
79-
// Minium and maximum Pixel value in current frame. Range: 0 to 127.
80-
const uint8_t max_pixel;
81-
const uint8_t min_pixel;
82-
} statistics;
83-
84-
uint8_t getAveragePixelValue() const {
85-
return statistics.pixel_sum << 9 / FrameSize;
86-
}
87-
88-
struct Shutter {
89-
// exposure <= ShutterConfig::exposure_max!
90-
const PeriodType exposure;
91-
// ShutterConfig::period_min <= period <= ShutterConfig::period_max!
92-
const PeriodType period;
93-
} shutter;
94-
95-
Duration getExposureTime() const { return Duration(shutter.exposure); };
96-
Duration getFrameTime() const { return Duration(shutter.period); };
69+
struct adns9800::Data_Fails_Monitoring : public adns9800::Data_Fails
70+
{
71+
struct Statistics
72+
{
73+
/**
74+
* Number of features visible by the sensor in the current frame. Range 0 to 169.
75+
* Total number of features: 4 * surface_quality.
76+
* Changes are expected when moving over a surface.
77+
* Convergates to 0 if there is no surface below the sensor.
78+
* surface_quality remains fairly high throughout the Z-height.
79+
*/
80+
const uint8_t surface_quality;
81+
// pixel_sum containes the average pixel value. Range 0 to 223.
82+
// It reports the upper byte of a 17-bit counter which sums all 900 pixels in the current
83+
// frame
84+
const uint8_t pixel_sum;
85+
// Minium and maximum Pixel value in current frame. Range: 0 to 127.
86+
const uint8_t max_pixel;
87+
const uint8_t min_pixel;
88+
} statistics;
89+
90+
uint8_t
91+
getAveragePixelValue() const
92+
{
93+
return statistics.pixel_sum << 9 / FrameSize;
94+
}
95+
96+
struct Shutter
97+
{
98+
// exposure <= ShutterConfig::exposure_max!
99+
const PeriodType exposure;
100+
// ShutterConfig::period_min <= period <= ShutterConfig::period_max!
101+
const PeriodType period;
102+
} shutter;
103+
104+
Duration
105+
getExposureTime() const
106+
{
107+
return Duration(shutter.exposure);
108+
};
109+
Duration
110+
getFrameTime() const
111+
{
112+
return Duration(shutter.period);
113+
};
114+
97115
protected:
98-
static constexpr size_t length = 14;
99-
Data_Fails_Monitoring(std::span<uint8_t, length> buffer)
100-
: Data_Fails(buffer.subspan<0, Data_Fails::length>()),
101-
statistics{
102-
surface_quality : buffer[6],
103-
pixel_sum : buffer[7],
104-
max_pixel : buffer[8],
105-
min_pixel : buffer[9]
106-
},
107-
shutter{
108-
exposure : buffer[10] << 8 | buffer[11],
109-
period : buffer[12] << 8 | buffer[13],
110-
} {}
111-
112-
template <class, class>
113-
friend class Adns9800;
116+
static constexpr size_t length = 14;
117+
Data_Fails_Monitoring(std::span<uint8_t, length> buffer)
118+
: Data_Fails(buffer.subspan<0, Data_Fails::length>()),
119+
statistics{
120+
surface_quality : buffer[6],
121+
pixel_sum : buffer[7],
122+
max_pixel : buffer[8],
123+
min_pixel : buffer[9]
124+
},
125+
shutter{
126+
exposure : static_cast<PeriodType>(buffer[10] << 8 | buffer[11]),
127+
period : static_cast<PeriodType>(buffer[12] << 8 | buffer[13])
128+
}
129+
{}
130+
131+
template<class, class>
132+
friend class Adns9800;
114133
};
115-
} // namespace modm
134+
} // namespace modm

0 commit comments

Comments
 (0)