diff --git a/.circleci/config.yml b/.circleci/config.yml index 766b497548..7b6228d500 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/examples/arduino_nano/color/main.cpp b/examples/arduino_nano/color/main.cpp new file mode 100644 index 0000000000..c98a9bba05 --- /dev/null +++ b/examples/arduino_nano/color/main.cpp @@ -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 +#include +#include + +using namespace modm::platform; + +class Sensorthread : public modm::pt::Protothread +{ +private: + modm::ShortTimeout timeout; + + modm::tcs3472::Data data; + modm::Tcs3472 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 hsv; + color.toHsv(&hsv); + MODM_LOG_INFO << "HSV: " << hsv << modm::endl; + } + } + + PT_END(); + } +}; + +Sensorthread sensorthread; + +int +main() +{ + Board::initialize(); + I2cMaster::initialize(); + + LedD13::setOutput(); + modm::ShortPeriodicTimer heartbeat(500ms); + + while (true) + { + sensorthread.update(); + if (heartbeat.execute()) Board::LedD13::toggle(); + } +} diff --git a/examples/arduino_nano/color/project.xml b/examples/arduino_nano/color/project.xml new file mode 100644 index 0000000000..4f5027af43 --- /dev/null +++ b/examples/arduino_nano/color/project.xml @@ -0,0 +1,13 @@ + + modm:arduino-nano + + + + + modm:build:scons + modm:processing:protothread + modm:processing:timer + modm:platform:i2c + modm:driver:tcs3472 + + diff --git a/examples/nucleo_f446re/color/main.cpp b/examples/nucleo_f446re/color/main.cpp new file mode 100644 index 0000000000..a9775f1137 --- /dev/null +++ b/examples/nucleo_f446re/color/main.cpp @@ -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 +#include +#include + +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 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 sensor{data}; +}; +ThreadOne one; + +// ---------------------------------------------------------------------------- +int +main() +{ + Board::initialize(); + Board::LedD13::setOutput(); + + I2cMaster1::connect(); + I2cMaster1::initialize(); + + 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; +} diff --git a/examples/nucleo_f446re/color/project.xml b/examples/nucleo_f446re/color/project.xml new file mode 100644 index 0000000000..b70f66949f --- /dev/null +++ b/examples/nucleo_f446re/color/project.xml @@ -0,0 +1,13 @@ + + modm:nucleo-f446re + + + + + modm:driver:tcs3472 + modm:platform:i2c:1 + modm:processing:protothread + modm:processing:timer + modm:build:scons + +