Skip to content

Commit 6ceed8e

Browse files
committed
[driver] rewrite adns9800
1 parent 9b4b64c commit 6ceed8e

11 files changed

+1122
-450
lines changed
Lines changed: 32 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/*
2-
* Copyright (c) 2011, Fabian Greif
3-
* Copyright (c) 2013, Kevin Läufer
4-
* Copyright (c) 2013-2017, Niklas Hauser
5-
* Copyright (c) 2014, 2016, 2018, Sascha Schade
2+
* Copyright (c) 2024, Thomas Sommer
63
*
74
* This file is part of the modm project.
85
*
@@ -14,143 +11,69 @@
1411

1512
#include <modm/board.hpp>
1613
#include <modm/debug/logger.hpp>
17-
#include <modm/processing/timer.hpp>
18-
#include <modm/processing/protothread.hpp>
19-
14+
#include <modm/processing/fiber.hpp>
2015
#include <modm/driver/motion/adns9800.hpp>
16+
#include <modm/math/geometry/vector2.hpp>
2117

22-
#include <inttypes.h>
23-
24-
// ----------------------------------------------------------------------------
2518
// Set the log level
26-
#undef MODM_LOG_LEVEL
27-
#define MODM_LOG_LEVEL modm::log::DEBUG
19+
#undef MODM_LOG_LEVEL
20+
#define MODM_LOG_LEVEL modm::log::DEBUG
2821

2922
using Usart2 = BufferedUart<UsartHal2, UartTxBuffer<256>>;
3023
// Create an IODeviceWrapper around the Uart Peripheral we want to use
31-
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
24+
modm::IODeviceWrapper<Usart2, modm::IOBuffer::BlockIfFull> loggerDevice;
3225

3326
// Set all four logger streams to use the UART
3427
modm::log::Logger modm::log::debug(loggerDevice);
3528
modm::log::Logger modm::log::info(loggerDevice);
3629
modm::log::Logger modm::log::warning(loggerDevice);
3730
modm::log::Logger modm::log::error(loggerDevice);
3831

39-
class BlinkThread : public modm::pt::Protothread
40-
{
41-
public:
42-
BlinkThread()
43-
{
44-
timeout.restart(100ms);
45-
}
46-
47-
bool
48-
update()
49-
{
50-
PT_BEGIN();
51-
52-
while (true)
53-
{
54-
Board::LedGreen::reset();
32+
using Cs = GpioOutputA4;
5533

56-
PT_WAIT_UNTIL(timeout.isExpired());
57-
timeout.restart(100ms);
34+
modm::Fiber<> adns9800_fiber([]() {
35+
modm::Adns9800<SpiMaster1, Cs> adns9800;
36+
modm::Vector2i position;
5837

59-
Board::LedGreen::set();
38+
Cs::setOutput(modm::Gpio::High);
6039

61-
PT_WAIT_UNTIL(timeout.isExpired()) ;
62-
timeout.restart(4.9s);
40+
SpiMaster1::connect<GpioOutputA5::Sck, GpioInputA6::Miso, GpioOutputA7::Mosi>();
41+
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
42+
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);
6343

64-
MODM_LOG_INFO << "Seconds since reboot: " << uptime << modm::endl;
65-
66-
uptime += 5;
67-
}
68-
69-
PT_END();
44+
if(not adns9800.initialize()) {
45+
MODM_LOG_INFO << "Failed to initialize ADNS9800" << modm::endl;
46+
return;
7047
}
7148

72-
private:
73-
modm::ShortTimeout timeout;
74-
uint32_t uptime;
75-
};
76-
77-
class Adns9800Thread : public modm::pt::Protothread
78-
{
79-
public:
80-
Adns9800Thread() : timer(10ms), x(0), y(0)
81-
{
82-
}
49+
adns9800.set(modm::adns9800::Resolution<8200>{});
50+
adns9800.set(modm::adns9800::ShutterConfig{
51+
period_min: 10000,
52+
period_max: 40000,
53+
exposure_max: 50000
54+
});
8355

84-
bool
85-
update()
56+
while (true)
8657
{
87-
PT_BEGIN();
88-
89-
Cs::setOutput(modm::Gpio::High);
58+
const auto data {adns9800.read<modm::adns9800::Data_FailFlags_Monitoring>()};
59+
position += data.delta;
9060

91-
SpiMaster1::connect<GpioOutputA7::Mosi, GpioOutputA5::Sck, GpioInputA6::Miso>();
92-
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
93-
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);
61+
MODM_LOG_INFO << "delta: " << data.delta << modm::endl;
62+
MODM_LOG_INFO << "position: " << position << modm::endl;
63+
MODM_LOG_INFO << modm::endl;
9464

95-
adns9800::initialise();
96-
97-
while (true)
98-
{
99-
PT_WAIT_UNTIL(timer.execute());
100-
101-
{
102-
int16_t delta_x, delta_y;
103-
adns9800::getDeltaXY(delta_x, delta_y);
104-
MODM_LOG_INFO.printf("dx = %5" PRId16 ", dy = %5" PRId16"; x = %9" PRId32", y=%9" PRId32 "\n", delta_x, delta_y, x, y);
105-
106-
x += delta_x;
107-
y += delta_y;
108-
}
109-
}
110-
111-
PT_END();
65+
modm::this_fiber::sleep_for(100ms);
11266
}
67+
});
11368

114-
private:
115-
modm::ShortPeriodicTimer timer;
116-
int32_t x, y;
117-
118-
using Cs = GpioOutputA4;
119-
120-
using adns9800 = modm::Adns9800<
121-
/* Spi = */ SpiMaster1,
122-
/* Ncs = */ Cs >;
123-
};
124-
125-
126-
BlinkThread blinkThread;
127-
Adns9800Thread adns9800Thread;
128-
129-
130-
// ----------------------------------------------------------------------------
13169
int
13270
main()
13371
{
13472
Board::initialize();
13573

136-
// initialize Uart2 for MODM_LOG_*
13774
Usart2::connect<GpioOutputA2::Tx>();
13875
Usart2::initialize<Board::SystemClock, 115200_Bd>();
13976

140-
// Use the logging streams to print some messages.
141-
// Change MODM_LOG_LEVEL above to enable or disable these messages
142-
MODM_LOG_DEBUG << "debug" << modm::endl;
143-
MODM_LOG_INFO << "info" << modm::endl;
144-
MODM_LOG_WARNING << "warning" << modm::endl;
145-
MODM_LOG_ERROR << "error" << modm::endl;
146-
147-
MODM_LOG_INFO << "Welcome to ADNS 9800 demo." << modm::endl;
148-
149-
while (true)
150-
{
151-
blinkThread.update();
152-
adns9800Thread.update();
153-
}
154-
77+
modm::fiber::Scheduler::run();
15578
return 0;
15679
}

examples/blue_pill_f103/adns_9800/project.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<module>modm:platform:gpio</module>
1111
<module>modm:platform:spi:1</module>
1212
<module>modm:platform:uart:2</module>
13-
<module>modm:processing:protothread</module>
13+
<module>modm:processing:fiber</module>
1414
<module>modm:processing:timer</module>
1515
<module>modm:build:scons</module>
1616
</modules>

0 commit comments

Comments
 (0)