Skip to content

Commit

Permalink
[examples] Add example for PAT9125EL motion sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Jan 4, 2019
1 parent d1b76f8 commit 4f25cdf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
95 changes: 95 additions & 0 deletions examples/nucleo_f429zi/pat9125el/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2018 Christopher Durand
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>

#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/driver/motion/pat9125el.hpp>

using I2c = I2cMaster1;
using Scl = GpioB8;
using Sda = GpioB9;

// int pin is optional, set to void for polling mode
using Int = GpioInputA5;

class Thread : public modm::pt::Protothread
{
public:
Thread() : sensor{0x75}
{
}

bool
update()
{
PT_BEGIN();

MODM_LOG_INFO << "Ping device" << modm::endl;
// ping the device until it responds
while(true)
{
if (PT_CALL(sensor.ping())) {
break;
}
// otherwise, try again in 100ms
this->timeout.restart(100);
PT_WAIT_UNTIL(this->timeout.isExpired());
}
MODM_LOG_INFO << "Ping successful" << modm::endl;

// set x and y resolution
PT_CALL(sensor.configure(0x14, 0x14));

while (true)
{
PT_CALL(sensor.readData());
if(sensor.hasMoved()) {
position += sensor.getData();

Board::Leds::write(0b111);
MODM_LOG_INFO << "Position: " << position.x << ", " << position.y << modm::endl;
sensor.resetMoved();
} else {
Board::Leds::write(0b000);
}
}

PT_END();
}

private:
modm::ShortTimeout timeout;
modm::pat9125el::Motion2D position;
modm::Pat9125el<modm::Pat9125elI2cTransport<I2c>, Int> sensor;
};

Thread thread;

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

MODM_LOG_INFO << "\n\nPAT9125EL I2C example\n\n";

I2c::connect<Sda::Sda, Scl::Scl>();
I2c::initialize<Board::systemClock, 400'000, modm::Tolerance::TwentyPercent>();

while (1) {
thread.update();
}

return 0;
}
13 changes: 13 additions & 0 deletions examples/nucleo_f429zi/pat9125el/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:board:nucleo-f429zi</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f429zi/pat9125el</option>
</options>
<modules>
<module>modm:driver:pat9125el</module>
<module>modm:io</module>
<module>modm:platform:i2c:1</module>
<module>modm:processing:protothread</module>
<module>modm:build:scons</module>
</modules>
</library>

0 comments on commit 4f25cdf

Please sign in to comment.