Skip to content

Commit

Permalink
[examples] Add TCS3472 color sensor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw authored and salkinium committed Mar 16, 2021
1 parent 7a87b5a commit 850b554
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jobs:
name: Examples AVR Series
when: always
command: |
(cd examples && ../tools/scripts/examples_compile.py avr arduino_uno)
(cd examples && ../tools/scripts/examples_compile.py avr arduino_uno arduino_nano)
- run:
name: Compile AVR Unittests AT90CAN
when: always
Expand Down
92 changes: 92 additions & 0 deletions examples/arduino_nano/color/main.cpp
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();
}
}
13 changes: 13 additions & 0 deletions examples/arduino_nano/color/project.xml
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>
109 changes: 109 additions & 0 deletions examples/nucleo_f446re/color/main.cpp
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;
}
13 changes: 13 additions & 0 deletions examples/nucleo_f446re/color/project.xml
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>

0 comments on commit 850b554

Please sign in to comment.