Skip to content

Commit

Permalink
[examples] Adapt all protothreads to fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 17, 2023
1 parent 917f558 commit 7b813b5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 108 deletions.
101 changes: 47 additions & 54 deletions examples/arduino_nano/color/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,61 @@

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;

modm::tcs3472::Data data;
modm::Tcs3472<I2cMaster> sensor{data};
using TCS3472_INT = Board::D2;
void sensor_fiber()
{
TCS3472_INT::setInput(Gpio::InputType::PullUp);

public:
bool
update()
MODM_LOG_INFO << "Ping TCS34725" << modm::endl;
// ping the device until it responds
while (true)
{
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());
}
// we wait until the task started
if (sensor.ping()) break;
// otherwise, try again in 100ms
modm::fiber::sleep(100ms);
}

MODM_LOG_INFO << "TCS34725 responded" << modm::endl;
MODM_LOG_INFO << "TCS34725 responded" << modm::endl;

PT_CALL(sensor.initialize(sensor.Enable_InterruptMode_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));
sensor.initialize(sensor.Enable_InterruptMode_Waittime);
sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4);
sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20);
// Setup WaitTime to further slow down samplerate
sensor.setWaitTime(modm::tcs3472::WaitTime::MSEC_2_4);

// Dummy read required
PT_CALL(sensor.readColor());
// Fetch one sample ...
PT_CALL(sensor.readColor());
// ...and set the high threshold 20% above current clear
PT_CALL(sensor.setInterruptHighThreshold(data.getClear() * 1.2));
// Dummy read required
sensor.readColor();
// Fetch one sample ...
sensor.readColor();
// ...and set the high threshold 20% above current clear
sensor.setInterruptHighThreshold(data.getClear() * 1.2);

while (true)
while (true)
{
sensor.reloadInterrupt();
while(TCS3472_INT::read() == false)
modm::fiber::yield();
if (sensor.readColor())
{
PT_CALL(sensor.reloadInterrupt());
PT_WAIT_UNTIL(TCS3472_INT::read() == false);
if (PT_CALL(sensor.readColor()))
{
const auto rgb = data.getColor();
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
}
const auto rgb = data.getColor();
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
}

PT_END();
}
};
}

Sensorthread sensorthread;
modm::StackFiber<> fiber1(sensor_fiber);
modm::StackFiber<> fiber2([]()
{
while(true)
{
Board::LedD13::toggle();
modm::fiber::sleep(0.5s);
}
}

int
main()
Expand All @@ -82,11 +78,8 @@ main()
I2cMaster::initialize<Board::SystemClock, 100_kHz>();

LedD13::setOutput();
modm::ShortPeriodicTimer heartbeat(500ms);

while (true)
{
sensorthread.update();
if (heartbeat.execute()) Board::LedD13::toggle();
}
modm::fiber::Scheduler::run();

return 0;
}
1 change: 1 addition & 0 deletions examples/arduino_nano/color/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<extends>modm:arduino-nano</extends>
<options>
<option name="modm:build:build.path">../../../build/arduino_nano/color</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
</options>
<modules>
<module>modm:build:scons</module>
Expand Down
7 changes: 2 additions & 5 deletions examples/stm32f3_discovery/accelerometer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ReaderThread : public modm::pt::Protothread
{
public:
bool
update()
run()
{
PT_BEGIN();

Expand Down Expand Up @@ -83,10 +83,7 @@ main()
Board::initialize();
Board::initializeLsm3();

while (true)
{
reader.update();
}
modm::fiber::Scheduler::run();

return 0;
}
1 change: 1 addition & 0 deletions examples/stm32f3_discovery/accelerometer/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<extends>modm:disco-f303vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f3_discovery/accelerometer</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
</options>
<modules>
<module>modm:math:filter</module>
Expand Down
93 changes: 44 additions & 49 deletions examples/stm32f3_discovery/rotation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ using namespace Board;

// maps arbitrary gpios to a bit
using LedRingLeft = SoftwareGpioPort<
Board::LedSouth, // 4
// Board::LedSouth, // 4
Board::LedSouthWest, // 3
Board::LedWest, // 2
Board::LedNorthWest, // 1
Board::LedNorth // 0
>;
// Symmetry \o/
using LedRingRight = SoftwareGpioPort<
Board::LedSouth, // 4
// Board::LedSouth, // 4
Board::LedSouthEast, // 3
Board::LedEast, // 2
Board::LedNorthEast, // 1
Expand All @@ -39,68 +39,63 @@ Board::l3g::Gyroscope::Data data;
Board::l3g::Gyroscope gyro(data);


class ReaderThread : public modm::pt::Protothread
void thread1()
{
public:
bool
update()
{
PT_BEGIN();

// initialize with limited range of 250 degrees per second
PT_CALL(gyro.configure(gyro.Scale::Dps250));

while (true)
{
// read out the sensor
PT_CALL(gyro.readRotation());
// initialize with limited range of 250 degrees per second
gyro.configure(gyro.Scale::Dps250);

// update the moving average
averageZ.update(gyro.getData().getZ());
modm::filter::MovingAverage<float, 25> averageZ;
while (true)
{
// read out the sensor
gyro.readRotation();

{
float value = averageZ.getValue();
// normalize rotation and scale by 5 leds
uint16_t leds = abs(value / 200 * 5);
leds = (1ul << leds) - 1;
// update the moving average
averageZ.update(gyro.getData().getZ());

// use left or right half ring depending on sign
if (value < 0) {
LedRingRight::write(0);
LedRingLeft::write(leds);
}
else {
LedRingLeft::write(0);
LedRingRight::write(leds);
}
}
float value = averageZ.getValue();
// normalize rotation and scale by 5 leds
uint16_t leds = abs(value / 200 * 5);
leds = (1ul << leds) - 1;

// repeat every 5 ms
timeout.restart(5ms);
PT_WAIT_UNTIL(timeout.isExpired());
// use left or right half ring depending on sign
if (value < 0) {
LedRingRight::write(0);
LedRingLeft::write(leds);
}
else {
LedRingLeft::write(0);
LedRingRight::write(leds);
}

PT_END();
// repeat every 5 ms
modm::fiber::sleep(5ms);
}

private:
modm::ShortTimeout timeout;
modm::filter::MovingAverage<float, 25> averageZ;
};

ReaderThread reader;

}
modm::StackFiber<> fiber1(thread1);
modm::StackFiber<> fiber2([]()
{
while (true)
{
Board::LedSouth::toggle();
modm::fiber::sleep(1s);
}
});

int
main()
{
Board::initialize();
Board::initializeL3g();

while (true)
{
reader.update();
}
// You can also call fibers in the main function, before the scheduler
// has started, in which case the yield function simply returns immediately.
// This then behaves identically to RF_CALL_BLOCKING.
// thread1();

// However, if your program progress depends on other fibers, then you
// need to start the scheduler.
modm::fiber::Scheduler::run();

return 0;
}
1 change: 1 addition & 0 deletions examples/stm32f3_discovery/rotation/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<extends>modm:disco-f303vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f3_discovery/rotation</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
</options>
<modules>
<module>modm:math:filter</module>
Expand Down

0 comments on commit 7b813b5

Please sign in to comment.