Skip to content

Commit e870225

Browse files
committed
better typing, better use of std::span
1 parent beff0b5 commit e870225

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

src/modm/driver/motion/adns9800.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <modm/architecture/interface/spi_device.hpp>
1818
#include <modm/architecture/interface/accessor_flash.hpp>
1919
#include <modm/debug/logger.hpp>
20+
#include <modm/ui/color/brightness.hpp>
2021

2122
EXTERN_FLASH_STORAGE(uint8_t adns9800_firmware[3070]);
2223

@@ -103,7 +104,9 @@ struct adns9800 {
103104
MODM_FLAGS8(ConfigurationII);
104105

105106

107+
using Color = modm::color::Brightness;
106108
static constexpr size_t FrameSize = 30 * 30;
109+
using FrameBuffer = std::array<Color, FrameSize>;
107110

108111
// Time periods as ticks of Adns9800 running at 50MHz
109112
using PeriodType = uint16_t;
@@ -317,26 +320,30 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
317320
requires std::is_base_of_v<Data, D>
318321
D
319322
read() {
320-
std::array<uint8_t, D::length> buffer;
323+
std::array<uint8_t, D::Span::extent> buffer;
321324

322325
readTransaction([this, &buffer]() {
323326
SpiMaster::transfer(static_cast<uint8_t>(Register::Motion_Burst));
324327
modm::this_fiber::sleep_for(shutter_config.getOneFrameTime());
325-
SpiMaster::transfer(nullptr, std::begin(buffer), buffer.size());
328+
SpiMaster::transfer(nullptr, buffer.data(), buffer.size());
326329
});
327330

328331
return D(buffer);
329332
}
330333

331334
// @todo test captureFrame
332-
void
333-
captureFrame(std::span<uint8_t, FrameSize> buffer) {
335+
FrameBuffer
336+
captureFrame() {
337+
FrameBuffer buffer{};
338+
334339
readTransaction([&buffer]() {
335340
writeRegister(Register::Frame_Capture, 0x93);
336341
writeRegister(Register::Frame_Capture, 0xc5);
337342
modm::this_fiber::poll(readRegister(Register::Motion) & Bit0); // wait for first pixel
338-
SpiMaster::transfer(nullptr, buffer, FrameSize);
343+
SpiMaster::transfer(nullptr, buffer.data(), buffer.size());
339344
});
345+
346+
return buffer;
340347
}
341348

342349
private:

src/modm/driver/motion/adns9800.lb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def prepare(module, options):
3232
module.depends(
3333
":processing:resumable",
3434
":architecture:spi.device",
35-
":math:geometry"
35+
":math:geometry",
36+
":ui:color"
3637
)
3738
return True
3839

src/modm/driver/motion/adns9800_data.hpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ namespace modm
3131
/// @brief Relative motion since the last read
3232
struct adns9800::Data
3333
{
34+
using Span = std::span<uint8_t, 6>;
35+
3436
const modm::Vector<DeltaType, 2> delta;
3537

3638
protected:
37-
static constexpr size_t length = 6;
38-
Data(std::span<uint8_t, length> buffer)
39+
Data(Span data)
3940
: delta{
40-
static_cast<DeltaType>(buffer[3] << 8 | buffer[2]),
41-
static_cast<DeltaType>(buffer[5] << 8 | buffer[4])
41+
static_cast<DeltaType>(data[3] << 8 | data[2]),
42+
static_cast<DeltaType>(data[5] << 8 | data[4])
4243
}
4344
{}
4445

@@ -54,11 +55,11 @@ struct adns9800::Data_Fails : public adns9800::Data
5455
const bool isRunningSROMCode : 1;
5556

5657
protected:
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)
58+
Data_Fails(Span data)
59+
: Data(data),
60+
LaserFaultDetected(data[0] & Bit6),
61+
LaserPowerValid(data[0] & Bit5),
62+
isRunningSROMCode(data[1] & Bit6)
6263
{}
6364

6465
template<class, class>
@@ -68,6 +69,8 @@ struct adns9800::Data_Fails : public adns9800::Data
6869
/// @brief Relative motion, laser fault detection flags and metrics from the shutter unit.
6970
struct adns9800::Data_Fails_Monitoring : public adns9800::Data_Fails
7071
{
72+
using Span = std::span<uint8_t, adns9800::Data_Fails::Span::extent + 8>;
73+
7174
struct Statistics
7275
{
7376
/**
@@ -83,12 +86,12 @@ struct adns9800::Data_Fails_Monitoring : public adns9800::Data_Fails
8386
// frame
8487
const uint8_t pixel_sum;
8588
// Minium and maximum Pixel value in current frame. Range: 0 to 127.
86-
const uint8_t max_pixel;
87-
const uint8_t min_pixel;
89+
const Color max_pixel;
90+
const Color min_pixel;
8891
} statistics;
8992

90-
uint8_t
91-
getAveragePixelValue() const
93+
Color
94+
getAveragePixel() const
9295
{
9396
return statistics.pixel_sum << 9 / FrameSize;
9497
}
@@ -113,18 +116,17 @@ struct adns9800::Data_Fails_Monitoring : public adns9800::Data_Fails
113116
};
114117

115118
protected:
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+
Data_Fails_Monitoring(Span data)
120+
: Data_Fails(data.subspan<0, Data_Fails::Span::extent>()),
119121
statistics{
120-
surface_quality : buffer[6],
121-
pixel_sum : buffer[7],
122-
max_pixel : buffer[8],
123-
min_pixel : buffer[9]
122+
surface_quality : data[6],
123+
pixel_sum : data[7],
124+
max_pixel : data[8],
125+
min_pixel : data[9]
124126
},
125127
shutter{
126-
exposure : static_cast<PeriodType>(buffer[10] << 8 | buffer[11]),
127-
period : static_cast<PeriodType>(buffer[12] << 8 | buffer[13])
128+
exposure : static_cast<PeriodType>(data[10] << 8 | data[11]),
129+
period : static_cast<PeriodType>(data[12] << 8 | data[13])
128130
}
129131
{}
130132

0 commit comments

Comments
 (0)