Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[driver] Added TCS3472 missing features #578

Merged
merged 2 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
37 changes: 19 additions & 18 deletions examples/stm32f4_discovery/colour_tcs3414/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ typedef I2cMaster2 MyI2cMaster;
class ThreadOne : public modm::pt::Protothread
{
public:
ThreadOne() :
colorSensor()
{
}

bool
update()
{
Expand All @@ -57,7 +52,7 @@ class ThreadOne : public modm::pt::Protothread
while (true)
{
// we wait until the task started
if (PT_CALL(colorSensor.ping())) {
if (PT_CALL(sensor.ping())) {
break;
}
// otherwise, try again in 100ms
Expand All @@ -69,7 +64,7 @@ class ThreadOne : public modm::pt::Protothread

while (true)
{
if (PT_CALL(colorSensor.initialize())) {
if (PT_CALL(sensor.initialize())) {
break;
}
// otherwise, try again in 100ms
Expand All @@ -81,11 +76,16 @@ class ThreadOne : public modm::pt::Protothread

while (true)
{
if (PT_CALL(colorSensor.configure(
if (PT_CALL(sensor.configure(
modm::tcs3414::Gain::X16,
modm::tcs3414::Prescaler::DEFAULT,
modm::tcs3414::IntegrationMode::INTERNAL,
static_cast<uint8_t>(modm::tcs3414::NominalIntegrationTime::MSEC_100)))){
modm::tcs3414::Prescaler::D1)))
{
if (PT_CALL(sensor.setIntegrationTime(
modm::tcs3414::IntegrationMode::INTERNAL,
modm::tcs3414::NominalIntegrationTime::MSEC_100)))
{
break;
}
break;
}
// otherwise, try again in 100ms
Expand All @@ -97,12 +97,12 @@ class ThreadOne : public modm::pt::Protothread

while (true)
{
if (PT_CALL(colorSensor.refreshAllColors())) {
auto colors = colorSensor.getOldColors();
stream.printf("RGB: %5d %5d %5d", colors.red, colors.green, colors.blue);
modm::color::HsvT<modm::tcs3414::UnderlyingType> hsv;
colors.toHsv(&hsv);
stream.printf(" %5d\n", hsv.hue);
if (PT_CALL(sensor.readColor())) {
auto color = data.getColor();
stream << "RGB: " << color;
modm::color::HsvT<uint16_t> hsv;
color.toHsv(&hsv);
stream << " " << hsv << modm::endl;
}
timeout.restart(500ms);
PT_WAIT_UNTIL(timeout.isExpired());
Expand All @@ -113,7 +113,8 @@ class ThreadOne : public modm::pt::Protothread

private:
modm::ShortTimeout timeout;
modm::Tcs3414<MyI2cMaster> colorSensor;
modm::tcs3414::Data data;
modm::Tcs3414<MyI2cMaster> sensor{data};
};

ThreadOne one;
Expand Down
Loading