-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add TCS3472 color sensor examples
- Loading branch information
Showing
5 changed files
with
228 additions
and
1 deletion.
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
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,92 @@ | ||
/* | ||
* Copyright (c) 2021, Thomas Sommer | ||
* | ||
* 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/driver/color/tcs3472.hpp> | ||
#include <modm/processing.hpp> | ||
|
||
using namespace modm::platform; | ||
|
||
class Sensorthread : public modm::pt::Protothread | ||
{ | ||
private: | ||
modm::ShortTimeout timeout; | ||
|
||
modm::tcs3472::Data data; | ||
modm::Tcs3472<I2cMaster> sensor{data}; | ||
using TCS3472_INT = Board::D2; | ||
|
||
public: | ||
bool | ||
update() | ||
{ | ||
PT_BEGIN(); | ||
|
||
TCS3472_INT::setInput(Gpio::InputType::PullUp); | ||
|
||
MODM_LOG_INFO << "Ping TCS34725" << modm::endl; | ||
// ping the device until it responds | ||
while (true) | ||
{ | ||
// we wait until the task started | ||
if (PT_CALL(sensor.ping())) { break; } | ||
// otherwise, try again in 100ms | ||
timeout.restart(100ms); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
MODM_LOG_INFO << "TCS34725 responded" << modm::endl; | ||
|
||
PT_CALL(sensor.initialize(modm::tcs3472::Enable::POWER_ON_INTERRUPT_AND_WAITTIME)); | ||
PT_CALL(sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4)); | ||
PT_CALL(sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20)); | ||
// Setup WaitTime to further slow down samplerate | ||
PT_CALL(sensor.setWaitTime(modm::tcs3472::WaitTime::MSEC_2_4)); | ||
|
||
// Fetch one sample ... | ||
PT_CALL(sensor.readColor()); | ||
// ...and set the high threshold 20% above current clear | ||
PT_CALL(sensor.setInterruptHighThreshold(data.getClear() * 1.2)); | ||
|
||
while (true) | ||
{ | ||
PT_CALL(sensor.reloadInterrupt()); | ||
if (PT_CALL(sensor.readColor())) | ||
{ | ||
const auto color = data.getColor(); | ||
MODM_LOG_INFO << "RGB: " << color; | ||
modm::color::HsvT<uint16_t> hsv; | ||
color.toHsv(&hsv); | ||
MODM_LOG_INFO << "HSV: " << hsv << modm::endl; | ||
} | ||
} | ||
|
||
PT_END(); | ||
} | ||
}; | ||
|
||
Sensorthread sensorthread; | ||
|
||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
I2cMaster::initialize<Board::SystemClock, 100_kHz>(); | ||
|
||
LedD13::setOutput(); | ||
modm::ShortPeriodicTimer heartbeat(500ms); | ||
|
||
while (true) | ||
{ | ||
sensorthread.update(); | ||
if (heartbeat.execute()) Board::LedD13::toggle(); | ||
} | ||
} |
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:arduino-nano</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../../build/arduino_nano/color</option> | ||
</options> | ||
<modules> | ||
<module>modm:build:scons</module> | ||
<module>modm:processing:protothread</module> | ||
<module>modm:processing:timer</module> | ||
<module>modm:platform:i2c</module> | ||
<module>modm:driver:tcs3472</module> | ||
</modules> | ||
</library> |
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,109 @@ | ||
/* | ||
* Copyright (c) 2014, Sascha Schade | ||
* Copyright (c) 2014-2018, 2021 Niklas Hauser | ||
* | ||
* 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.hpp> | ||
#include <modm/driver/color/tcs3472.hpp> | ||
|
||
class ThreadOne : public modm::pt::Protothread | ||
{ | ||
public: | ||
bool | ||
update() | ||
{ | ||
PT_BEGIN(); | ||
|
||
MODM_LOG_INFO << "Ping the device from ThreadOne" << modm::endl; | ||
|
||
// ping the device until it responds | ||
while (true) | ||
{ | ||
// we wait until the task started | ||
if (PT_CALL(sensor.ping())) { | ||
break; | ||
} | ||
// otherwise, try again in 100ms | ||
timeout.restart(100ms); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
MODM_LOG_INFO << "Device responded" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
if (PT_CALL(sensor.initialize())) { | ||
break; | ||
} | ||
// otherwise, try again in 100ms | ||
timeout.restart(100ms); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
MODM_LOG_INFO << "Device initialized" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
if (PT_CALL(sensor.configure(sensor.Gain::X4, sensor.IntegrationTime::MSEC_101))) { | ||
break; | ||
} | ||
// otherwise, try again in 100ms | ||
timeout.restart(100ms); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
MODM_LOG_INFO << "Device configured" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
if (PT_CALL(sensor.readColor())) | ||
{ | ||
const auto color = data.getColor(); | ||
MODM_LOG_INFO << "RGB: " << color; | ||
modm::color::HsvT<uint16_t> hsv; | ||
color.toHsv(&hsv); | ||
MODM_LOG_INFO << " " << hsv << modm::endl; | ||
} | ||
timeout.restart(500ms); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
PT_END(); | ||
} | ||
|
||
private: | ||
modm::ShortTimeout timeout; | ||
modm::tcs3472::Data data; | ||
modm::Tcs3472<I2cMaster1> sensor{data}; | ||
}; | ||
ThreadOne one; | ||
|
||
// ---------------------------------------------------------------------------- | ||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
Board::LedD13::setOutput(); | ||
|
||
I2cMaster1::connect<Board::D14::Sda, Board::D15::Scl>(); | ||
I2cMaster1::initialize<Board::SystemClock, 100_kHz>(); | ||
|
||
MODM_LOG_INFO << "\n\nWelcome to TCS3472 demo!\n\n"; | ||
|
||
modm::ShortPeriodicTimer tmr(500ms); | ||
while (true) | ||
{ | ||
one.update(); | ||
if (tmr.execute()) Board::LedD13::toggle(); | ||
} | ||
|
||
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:nucleo-f446re</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_f446re/color</option> | ||
</options> | ||
<modules> | ||
<module>modm:driver:tcs3472</module> | ||
<module>modm:platform:i2c:1</module> | ||
<module>modm:processing:protothread</module> | ||
<module>modm:processing:timer</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |