Skip to content

Commit

Permalink
[driver] rewrite adns9800
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Jul 14, 2024
1 parent 5f15719 commit f1db304
Show file tree
Hide file tree
Showing 12 changed files with 1,134 additions and 469 deletions.
137 changes: 34 additions & 103 deletions examples/blue_pill_f103/adns_9800/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,145 +12,76 @@
*/
// ----------------------------------------------------------------------------

#include <inttypes.h>

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>
#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>

#include <modm/driver/motion/adns9800.hpp>

#include <inttypes.h>
#include <modm/processing/fiber.hpp>
#include <modm/processing/timer.hpp>

// ----------------------------------------------------------------------------
// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

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

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

class BlinkThread : public modm::pt::Protothread
{
public:
BlinkThread()
{
timeout.restart(100ms);
}
using Cs = GpioOutputA4;
using Adns9800 = modm::Adns9800<SpiMaster1, Cs>;

bool
update()
{
PT_BEGIN();

while (true)
{
Board::LedGreen::reset();

PT_WAIT_UNTIL(timeout.isExpired());
timeout.restart(100ms);
modm::Vector2i position;

Board::LedGreen::set();
modm::Fiber<> adns9800_fiber([]() {
Adns9800::Data data;
Adns9800 adns9800{data};

PT_WAIT_UNTIL(timeout.isExpired()) ;
timeout.restart(4.9s);
Cs::setOutput(modm::Gpio::High);

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

uptime += 5;
}

PT_END();
}

private:
modm::ShortTimeout timeout;
uint32_t uptime;
};

class Adns9800Thread : public modm::pt::Protothread
{
public:
Adns9800Thread() : timer(10ms), x(0), y(0)
{
}
adns9800.initialize();
adns9800.setResolution(Adns9800::Resolution<8200>());
Adns9800::Shutter shutter = {
period_min: 10000,
period_max: 40000,
exposure_max: 50000
};
adns9800.setShutter(shutter);

bool
update()
while (true)
{
PT_BEGIN();

Cs::setOutput(modm::Gpio::High);

SpiMaster1::connect<GpioOutputA7::Mosi, GpioOutputA5::Sck, GpioInputA6::Miso>();
SpiMaster1::initialize<Board::SystemClock, 2.25_MHz>();
SpiMaster1::setDataMode(SpiMaster1::DataMode::Mode3);

adns9800::initialise();
adns9800.read();
position += data;

while (true)
{
PT_WAIT_UNTIL(timer.execute());
MODM_LOG_INFO << "increment: " << data << modm::endl;
MODM_LOG_INFO << "position: " << position << modm::endl;
MODM_LOG_INFO << modm::endl;

{
int16_t delta_x, delta_y;
adns9800::getDeltaXY(delta_x, delta_y);
MODM_LOG_INFO.printf("dx = %5" PRId16 ", dy = %5" PRId16"; x = %9" PRId32", y=%9" PRId32 "\n", delta_x, delta_y, x, y);

x += delta_x;
y += delta_y;
}
}

PT_END();
modm::this_fiber::sleep_for(10ms);
}
});

private:
modm::ShortPeriodicTimer timer;
int32_t x, y;

using Cs = GpioOutputA4;

using adns9800 = modm::Adns9800<
/* Spi = */ SpiMaster1,
/* Ncs = */ Cs >;
};


BlinkThread blinkThread;
Adns9800Thread adns9800Thread;


// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();

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

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

MODM_LOG_INFO << "Welcome to ADNS 9800 demo." << modm::endl;

while (true)
{
blinkThread.update();
adns9800Thread.update();
}

modm::fiber::Scheduler::run();
return 0;
}
3 changes: 2 additions & 1 deletion examples/blue_pill_f103/adns_9800/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<extends>modm:blue-pill-f103</extends>
<options>
<option name="modm:build:build.path">../../../build/blue_pill_f103/adns_9800</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
Expand All @@ -10,7 +11,7 @@
<module>modm:platform:gpio</module>
<module>modm:platform:spi:1</module>
<module>modm:platform:uart:2</module>
<module>modm:processing:protothread</module>
<module>modm:processing:fiber</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
Expand Down
Loading

0 comments on commit f1db304

Please sign in to comment.