Skip to content

Commit f08654c

Browse files
committed
finished Verification type and its embedding
1 parent ba9a743 commit f08654c

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

examples/blue_pill_f103/adns_9800/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ modm::Fiber<> adns9800_fiber([]() {
4444
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
4545
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);
4646

47-
adns9800.initialize();
47+
if(not adns9800.initialize()) {
48+
MODM_LOG_INFO << "Failed to initialize ADNS9800" << modm::endl;
49+
return;
50+
}
51+
4852
adns9800.set(Adns9800::Resolution<8200>());
4953
adns9800.set(Adns9800::ShutterConfig{
5054
period_min: 10000,

src/modm/driver/motion/adns9800.hpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,29 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
158158
/// @endcond
159159
};
160160

161-
union VerificationResult {
162-
uint8_t value;
163-
struct {
164-
bool ProductId: 1;
165-
bool ProductIdInverse: 1;
166-
bool RevisionId: 1;
167-
};
168-
static constexpr uint8_t Ok = 0b111;
161+
struct Verification {
162+
bool ProductId: 1;
163+
bool ProductIdInverse: 1;
164+
bool RevisionId: 1;
165+
166+
// @todo allFine() maybe a better name
167+
// bool allFine() const {
168+
operator bool() const {
169+
return ProductId and ProductIdInverse and RevisionId;
170+
}
169171
};
170172

171-
void
173+
Verification
172174
initialize() {
173175
powerUp();
174-
const VerificationResult result = verifyIdentity();
175-
if(result.value == VerificationResult::Ok) {
176+
177+
const Verification verification = verify();
178+
if(verification) {
176179
writeFirmware();
177180
laserEnable();
178181
}
182+
183+
return verification;
179184
}
180185

181186
/// @brief Reset the device's internal state after power-loss or user invoked shutdown().
@@ -201,12 +206,12 @@ class Adns9800 : public adns9800, public modm::SpiDevice<SpiMaster> {
201206
/**
202207
* @brief Verify presence of the device by validating various id registers.
203208
*/
204-
VerificationResult
205-
verifyIdentity() {
206-
return VerificationResult{
207-
ProductId: readRegister(Register::Product_ID) == uint8_t(0x33),
208-
ProductIdInverse: readRegister(Register::Inverse_Product_ID) == uint8_t(~0x33),
209-
RevisionId: readRegister(Register::Revision_ID) == uint8_t(0x03)
209+
Verification
210+
verify() {
211+
return {
212+
ProductId: readRegister(Register::Product_ID) == static_cast<uint8_t>(0x33),
213+
ProductIdInverse: readRegister(Register::Inverse_Product_ID) == static_cast<uint8_t>(~0x33),
214+
RevisionId: readRegister(Register::Revision_ID) == static_cast<uint8_t>(0x03)
210215
};
211216
}
212217

0 commit comments

Comments
 (0)