|
1 | 1 | /* |
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 |
6 | 3 | * |
7 | 4 | * This file is part of the modm project. |
8 | 5 | * |
|
14 | 11 |
|
15 | 12 | #include <modm/board.hpp> |
16 | 13 | #include <modm/debug/logger.hpp> |
17 | | -#include <modm/processing/timer.hpp> |
18 | | -#include <modm/processing/protothread.hpp> |
19 | | - |
| 14 | +#include <modm/processing/fiber.hpp> |
20 | 15 | #include <modm/driver/motion/adns9800.hpp> |
| 16 | +#include <modm/math/geometry/vector2.hpp> |
21 | 17 |
|
22 | | -#include <inttypes.h> |
23 | | - |
24 | | -// ---------------------------------------------------------------------------- |
25 | 18 | // 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 |
28 | 21 |
|
29 | 22 | using Usart2 = BufferedUart<UsartHal2, UartTxBuffer<256>>; |
30 | 23 | // 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; |
32 | 25 |
|
33 | 26 | // Set all four logger streams to use the UART |
34 | 27 | modm::log::Logger modm::log::debug(loggerDevice); |
35 | 28 | modm::log::Logger modm::log::info(loggerDevice); |
36 | 29 | modm::log::Logger modm::log::warning(loggerDevice); |
37 | 30 | modm::log::Logger modm::log::error(loggerDevice); |
38 | 31 |
|
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; |
55 | 33 |
|
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; |
58 | 37 |
|
59 | | - Board::LedGreen::set(); |
| 38 | + Cs::setOutput(modm::Gpio::High); |
60 | 39 |
|
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); |
63 | 43 |
|
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; |
70 | 47 | } |
71 | 48 |
|
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 | + }); |
83 | 55 |
|
84 | | - bool |
85 | | - update() |
| 56 | + while (true) |
86 | 57 | { |
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; |
90 | 60 |
|
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; |
94 | 64 |
|
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); |
112 | 66 | } |
| 67 | +}); |
113 | 68 |
|
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 | | -// ---------------------------------------------------------------------------- |
131 | 69 | int |
132 | 70 | main() |
133 | 71 | { |
134 | 72 | Board::initialize(); |
135 | 73 |
|
136 | | - // initialize Uart2 for MODM_LOG_* |
137 | 74 | Usart2::connect<GpioOutputA2::Tx>(); |
138 | 75 | Usart2::initialize<Board::SystemClock, 115200_Bd>(); |
139 | 76 |
|
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(); |
155 | 78 | return 0; |
156 | 79 | } |
0 commit comments