Skip to content

Commit f1db304

Browse files
committed
[driver] rewrite adns9800
1 parent 5f15719 commit f1db304

12 files changed

+1134
-469
lines changed

examples/blue_pill_f103/adns_9800/main.cpp

Lines changed: 34 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,145 +12,76 @@
1212
*/
1313
// ----------------------------------------------------------------------------
1414

15+
#include <inttypes.h>
16+
1517
#include <modm/board.hpp>
1618
#include <modm/debug/logger.hpp>
17-
#include <modm/processing/timer.hpp>
18-
#include <modm/processing/protothread.hpp>
19-
2019
#include <modm/driver/motion/adns9800.hpp>
21-
22-
#include <inttypes.h>
20+
#include <modm/processing/fiber.hpp>
21+
#include <modm/processing/timer.hpp>
2322

2423
// ----------------------------------------------------------------------------
2524
// Set the log level
26-
#undef MODM_LOG_LEVEL
27-
#define MODM_LOG_LEVEL modm::log::DEBUG
25+
#undef MODM_LOG_LEVEL
26+
#define MODM_LOG_LEVEL modm::log::DEBUG
2827

2928
using Usart2 = BufferedUart<UsartHal2, UartTxBuffer<256>>;
3029
// Create an IODeviceWrapper around the Uart Peripheral we want to use
31-
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
30+
modm::IODeviceWrapper<Usart2, modm::IOBuffer::BlockIfFull> loggerDevice;
3231

3332
// Set all four logger streams to use the UART
3433
modm::log::Logger modm::log::debug(loggerDevice);
3534
modm::log::Logger modm::log::info(loggerDevice);
3635
modm::log::Logger modm::log::warning(loggerDevice);
3736
modm::log::Logger modm::log::error(loggerDevice);
3837

39-
class BlinkThread : public modm::pt::Protothread
40-
{
41-
public:
42-
BlinkThread()
43-
{
44-
timeout.restart(100ms);
45-
}
38+
using Cs = GpioOutputA4;
39+
using Adns9800 = modm::Adns9800<SpiMaster1, Cs>;
4640

47-
bool
48-
update()
49-
{
50-
PT_BEGIN();
51-
52-
while (true)
53-
{
54-
Board::LedGreen::reset();
55-
56-
PT_WAIT_UNTIL(timeout.isExpired());
57-
timeout.restart(100ms);
41+
modm::Vector2i position;
5842

59-
Board::LedGreen::set();
43+
modm::Fiber<> adns9800_fiber([]() {
44+
Adns9800::Data data;
45+
Adns9800 adns9800{data};
6046

61-
PT_WAIT_UNTIL(timeout.isExpired()) ;
62-
timeout.restart(4.9s);
47+
Cs::setOutput(modm::Gpio::High);
6348

64-
MODM_LOG_INFO << "Seconds since reboot: " << uptime << modm::endl;
49+
SpiMaster1::connect<GpioOutputA7::Mosi, GpioOutputA5::Sck, GpioInputA6::Miso>();
50+
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
51+
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);
6552

66-
uptime += 5;
67-
}
68-
69-
PT_END();
70-
}
71-
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-
}
53+
adns9800.initialize();
54+
adns9800.setResolution(Adns9800::Resolution<8200>());
55+
Adns9800::Shutter shutter = {
56+
period_min: 10000,
57+
period_max: 40000,
58+
exposure_max: 50000
59+
};
60+
adns9800.setShutter(shutter);
8361

84-
bool
85-
update()
62+
while (true)
8663
{
87-
PT_BEGIN();
88-
89-
Cs::setOutput(modm::Gpio::High);
90-
91-
SpiMaster1::connect<GpioOutputA7::Mosi, GpioOutputA5::Sck, GpioInputA6::Miso>();
92-
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
93-
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);
94-
95-
adns9800::initialise();
64+
adns9800.read();
65+
position += data;
9666

97-
while (true)
98-
{
99-
PT_WAIT_UNTIL(timer.execute());
67+
MODM_LOG_INFO << "increment: " << data << modm::endl;
68+
MODM_LOG_INFO << "position: " << position << modm::endl;
69+
MODM_LOG_INFO << modm::endl;
10070

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();
71+
modm::this_fiber::sleep_for(10ms);
11272
}
73+
});
11374

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-
// ----------------------------------------------------------------------------
13175
int
13276
main()
13377
{
13478
Board::initialize();
13579

136-
// initialize Uart2 for MODM_LOG_*
13780
Usart2::connect<GpioOutputA2::Tx>();
13881
Usart2::initialize<Board::SystemClock, 115200_Bd>();
13982

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-
14783
MODM_LOG_INFO << "Welcome to ADNS 9800 demo." << modm::endl;
14884

149-
while (true)
150-
{
151-
blinkThread.update();
152-
adns9800Thread.update();
153-
}
154-
85+
modm::fiber::Scheduler::run();
15586
return 0;
15687
}

examples/blue_pill_f103/adns_9800/project.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<extends>modm:blue-pill-f103</extends>
33
<options>
44
<option name="modm:build:build.path">../../../build/blue_pill_f103/adns_9800</option>
5+
<option name="modm:processing:protothread:use_fiber">yes</option>
56
<option name="modm:build:openocd.cfg">openocd.cfg</option>
67
</options>
78
<modules>
@@ -10,7 +11,7 @@
1011
<module>modm:platform:gpio</module>
1112
<module>modm:platform:spi:1</module>
1213
<module>modm:platform:uart:2</module>
13-
<module>modm:processing:protothread</module>
14+
<module>modm:processing:fiber</module>
1415
<module>modm:processing:timer</module>
1516
<module>modm:build:scons</module>
1617
</modules>

0 commit comments

Comments
 (0)