-
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.
Add TCS34725 color sensor example for arduino-nano
- Loading branch information
Showing
2 changed files
with
122 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,109 @@ | ||
/* | ||
* Copyright (c) 2013-2014, Kevin Läufer | ||
* Copyright (c) 2013, 2016-2017, Niklas Hauser | ||
* Copyright (c) 2014, Sascha Schade | ||
* | ||
* 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/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
// Inspired by: http://arduino.cc/en/Tutorial/Blink | ||
|
||
#include <modm/board.hpp> | ||
#include <modm/driver/color/tcs3472.hpp> | ||
#include <modm/processing.hpp> | ||
|
||
using namespace modm::platform; | ||
|
||
using TCS3472_INT = Board::D2; | ||
|
||
class Sensorthread : public modm::pt::Protothread | ||
{ | ||
private: | ||
modm::ShortTimeout timeout; | ||
|
||
modm::tcs3472::Data data; | ||
modm::Tcs3472<I2cMaster> sensor{data}; | ||
|
||
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; | ||
|
||
while (!PT_CALL(sensor.initialize(modm::tcs3472::Enable::POWER_ON_INTERRUPT_AND_WAITTIME))) | ||
; | ||
while (!PT_CALL( | ||
sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4))) | ||
; | ||
while (!PT_CALL( | ||
sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20))) | ||
; | ||
// Setup WaitTime to further slow down samplerate | ||
while (!PT_CALL(sensor.setWaitTime(modm::tcs3472::WaitTime::MSEC_2_4))) | ||
; | ||
|
||
// Fetch one sample ... | ||
while (!PT_CALL(sensor.readColor())) | ||
; | ||
// ...and set the high threshold 20% above current clear | ||
while (!PT_CALL(sensor.setInterruptHighThreshold(data.getClear() * 1.2))) | ||
; | ||
|
||
while (true) | ||
{ | ||
while (!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(); | ||
TCS3472_INT::setInput(Gpio::InputType::PullUp); | ||
|
||
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> |