-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add example for PAT9125EL motion sensor
- Loading branch information
1 parent
d1b76f8
commit 4f25cdf
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |