Skip to content

Commit e92dc1d

Browse files
committed
[examples] Replace Protothreads with Fibers
1 parent 06ec377 commit e92dc1d

File tree

158 files changed

+2394
-4154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+2394
-4154
lines changed

examples/arduino_nano/color/main.cpp

+44-56
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,66 @@
1515

1616
using namespace modm::platform;
1717

18-
class Sensorthread : public modm::pt::Protothread
18+
modm::Fiber fiber_blink([]
1919
{
20-
private:
21-
modm::ShortTimeout timeout;
22-
23-
modm::tcs3472::Data data;
24-
modm::Tcs3472<I2cMaster> sensor{data};
25-
using TCS3472_INT = Board::D2;
26-
27-
public:
28-
bool
29-
update()
20+
LedD13::setOutput();
21+
while (true)
3022
{
31-
PT_BEGIN();
23+
LedD13::toggle();
24+
modm::this_fiber::sleep_for(0.5s);
25+
}
26+
});
3227

33-
TCS3472_INT::setInput(Gpio::InputType::PullUp);
28+
modm::tcs3472::Data data;
29+
modm::Tcs3472<I2cMaster> sensor{data};
30+
using TCS3472_INT = Board::D2;
31+
modm::Fiber fiber_sensor([]
32+
{
33+
TCS3472_INT::setInput(Gpio::InputType::PullUp);
3434

35-
MODM_LOG_INFO << "Ping TCS34725" << modm::endl;
36-
// ping the device until it responds
37-
while (true)
38-
{
39-
// we wait until the task started
40-
if (PT_CALL(sensor.ping())) { break; }
41-
// otherwise, try again in 100ms
42-
timeout.restart(100ms);
43-
PT_WAIT_UNTIL(timeout.isExpired());
44-
}
35+
MODM_LOG_INFO << "Ping TCS34725" << modm::endl;
36+
// ping the device until it responds
37+
while (true)
38+
{
39+
// we wait until the task started
40+
if (sensor.ping()) break;
41+
// otherwise, try again in 100ms
42+
modm::this_fiber::sleep_for(100ms);
43+
}
4544

46-
MODM_LOG_INFO << "TCS34725 responded" << modm::endl;
45+
MODM_LOG_INFO << "TCS34725 responded" << modm::endl;
4746

48-
PT_CALL(sensor.initialize(sensor.Enable_InterruptMode_Waittime));
49-
PT_CALL(sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4));
50-
PT_CALL(sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20));
51-
// Setup WaitTime to further slow down samplerate
52-
PT_CALL(sensor.setWaitTime(modm::tcs3472::WaitTime::MSEC_2_4));
47+
sensor.initialize(sensor.Enable_InterruptMode_Waittime);
48+
sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4);
49+
sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20);
50+
// Setup WaitTime to further slow down samplerate
51+
sensor.setWaitTime(modm::tcs3472::WaitTime::MSEC_2_4);
5352

54-
// Dummy read required
55-
PT_CALL(sensor.readColor());
56-
// Fetch one sample ...
57-
PT_CALL(sensor.readColor());
58-
// ...and set the high threshold 20% above current clear
59-
PT_CALL(sensor.setInterruptHighThreshold(data.getClear() * 1.2));
53+
// Dummy read required
54+
sensor.readColor();
55+
// Fetch one sample ...
56+
sensor.readColor();
57+
// ...and set the high threshold 20% above current clear
58+
sensor.setInterruptHighThreshold(data.getClear() * 1.2);
6059

61-
while (true)
60+
while (true)
61+
{
62+
sensor.reloadInterrupt();
63+
modm::this_fiber::poll([]{ return not TCS3472_INT::read(); });
64+
if (sensor.readColor())
6265
{
63-
PT_CALL(sensor.reloadInterrupt());
64-
PT_WAIT_UNTIL(TCS3472_INT::read() == false);
65-
if (PT_CALL(sensor.readColor()))
66-
{
67-
const auto rgb = data.getColor();
68-
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
69-
}
66+
const auto rgb = data.getColor();
67+
MODM_LOG_INFO << "RGB: " << rgb << "\tHSV: " << modm::color::Hsv(rgb) << modm::endl;
7068
}
71-
72-
PT_END();
7369
}
74-
};
75-
76-
Sensorthread sensorthread;
70+
});
7771

7872
int
7973
main()
8074
{
8175
Board::initialize();
8276
I2cMaster::initialize<Board::SystemClock, 100_kHz>();
8377

84-
LedD13::setOutput();
85-
modm::ShortPeriodicTimer heartbeat(500ms);
86-
87-
while (true)
88-
{
89-
sensorthread.update();
90-
if (heartbeat.execute()) Board::LedD13::toggle();
91-
}
78+
modm::fiber::Scheduler::run();
79+
return 0;
9280
}

examples/arduino_nano/color/project.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
</options>
66
<modules>
77
<module>modm:build:scons</module>
8-
<module>modm:processing:protothread</module>
9-
<module>modm:processing:timer</module>
8+
<module>modm:processing:fiber</module>
109
<module>modm:platform:i2c</module>
1110
<module>modm:driver:tcs3472</module>
1211
</modules>

examples/avr/block_device_mirror/main.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ main()
6161

6262
modm::BdMirror<modm::BdHeap<MemorySize>, modm::BdHeap<MemorySize>> storageDevice;
6363

64-
if(!RF_CALL_BLOCKING(storageDevice.initialize())) {
64+
if(!storageDevice.initialize()) {
6565
MODM_LOG_INFO << "Error: Unable to initialize device.";
6666
exit(1);
6767
}
@@ -71,20 +71,20 @@ main()
7171
for(uint16_t iteration = 0; iteration < 10; iteration++) {
7272
uint8_t* pattern = (iteration % 2 == 0) ? bufferA : bufferB;
7373

74-
if(!RF_CALL_BLOCKING(storageDevice.erase(0, MemorySize))) {
74+
if(!storageDevice.erase(0, MemorySize)) {
7575
MODM_LOG_INFO << "Error: Unable to erase device.";
7676
exit(1);
7777
}
7878

7979
for(uint32_t i = 0; i < MemorySize; i += BlockSize) {
80-
if(!RF_CALL_BLOCKING(storageDevice.program(pattern, i, BlockSize))) {
80+
if(!storageDevice.program(pattern, i, BlockSize)) {
8181
MODM_LOG_INFO << "Error: Unable to write data.";
8282
exit(1);
8383
}
8484
}
8585

8686
for(uint32_t i = 0; i < MemorySize; i += BlockSize) {
87-
if(!RF_CALL_BLOCKING(storageDevice.read(bufferC, i, BlockSize))) {
87+
if(!storageDevice.read(bufferC, i, BlockSize)) {
8888
MODM_LOG_INFO << "Error: Unable to read data.";
8989
exit(1);
9090
}

examples/avr/fiber/main.cpp

+37-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* Copyright (c) 2020, Erik Henriksson
2+
* Copyright (c) 2010-2011, Fabian Greif
3+
* Copyright (c) 2012-2013, 2015-2017, Niklas Hauser
4+
* Copyright (c) 2014, Sascha Schade
35
*
46
* This file is part of the modm project.
57
*
@@ -9,86 +11,57 @@
911
*/
1012
// ----------------------------------------------------------------------------
1113

12-
#include <modm/board.hpp>
13-
#include <modm/debug/logger.hpp>
14+
#include <modm/platform.hpp>
15+
#include <modm/architecture/interface/interrupt.hpp>
1416
#include <modm/processing.hpp>
1517

16-
using namespace Board;
18+
using namespace modm::platform;
1719
using namespace std::chrono_literals;
1820

19-
constexpr uint32_t cycles = 100'000;
20-
volatile uint32_t f1counter = 0, f2counter = 0;
21-
uint32_t total_counter=0;
21+
using LedGreen = GpioOutputB0;
22+
using LedRed = GpioOutputB1;
2223

23-
void
24-
fiber_function1()
24+
modm::Fiber fiber_green([]
2525
{
26-
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
27-
while (++f1counter < cycles) { modm::this_fiber::yield(); total_counter++; }
28-
}
29-
30-
void
31-
fiber_function2(uint32_t cyc)
32-
{
33-
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
34-
while (++f2counter < cyc) { modm::this_fiber::yield(); total_counter++; }
35-
}
26+
LedGreen::setOutput();
27+
LedGreen::set();
3628

37-
struct Test
38-
{
39-
void
40-
fiber_function3()
29+
while (true)
4130
{
42-
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
43-
while (++f3counter < cycles) { modm::this_fiber::yield(); total_counter++; }
31+
LedGreen::set();
32+
modm::this_fiber::sleep_for(100ms);
33+
34+
LedGreen::reset();
35+
modm::this_fiber::sleep_for(600ms);
4436
}
37+
});
38+
39+
modm::Fiber fiber_red([]
40+
{
41+
LedRed::setOutput();
42+
LedRed::set();
4543

46-
void
47-
fiber_function4(uint32_t cyc)
44+
while (true)
4845
{
49-
MODM_LOG_INFO << MODM_FILE_INFO << modm::endl;
50-
while (++f4counter < cyc) { modm::this_fiber::yield(); total_counter++; }
51-
}
46+
LedRed::set();
47+
modm::this_fiber::sleep_for(200ms);
48+
49+
LedRed::reset();
50+
modm::this_fiber::sleep_for(300ms);
5251

53-
volatile uint32_t f3counter{0};
54-
volatile uint32_t f4counter{0};
55-
} test;
52+
LedRed::set();
53+
modm::this_fiber::sleep_for(200ms);
5654

57-
modm::Fiber<> fiber1(fiber_function1);
58-
modm::Fiber<> fiber2(+[](){ fiber_function2(cycles); });
59-
modm::Fiber<> fiber3(+[](){ test.fiber_function3(); });
60-
modm::Fiber<> fiber4([cyc=uint32_t(cycles)]() mutable { cyc++; test.fiber_function4(cyc); });
55+
LedRed::reset();
56+
modm::this_fiber::sleep_for(1s);
57+
}
58+
});
6159

62-
// ATmega2560@16MHz: 239996 yields in 2492668us, 96280 yields per second, 10386ns per yield
6360
int
6461
main()
6562
{
66-
Board::initialize();
67-
Board::LedD13::setOutput();
68-
MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl;
69-
MODM_LOG_INFO.flush();
70-
71-
fiber1.stack_watermark();
72-
fiber2.stack_watermark();
73-
fiber3.stack_watermark();
74-
fiber4.stack_watermark();
63+
SystemClock::enable();
64+
enableInterrupts();
7565

76-
const modm::PreciseTimestamp start = modm::PreciseClock::now();
7766
modm::fiber::Scheduler::run();
78-
const auto diff = (modm::PreciseClock::now() - start);
79-
80-
MODM_LOG_INFO << "Benchmark done!" << modm::endl;
81-
MODM_LOG_INFO << "Executed " << total_counter << " yields in " << diff << modm::endl;
82-
MODM_LOG_INFO << uint32_t((total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count());
83-
MODM_LOG_INFO << " yields per second, ";
84-
MODM_LOG_INFO << uint32_t(std::chrono::nanoseconds(diff).count() / total_counter);
85-
MODM_LOG_INFO << "ns per yield" << modm::endl;
86-
87-
MODM_LOG_INFO << "Stack usage 1 = " << fiber1.stack_usage() << modm::endl;
88-
MODM_LOG_INFO << "Stack usage 2 = " << fiber2.stack_usage() << modm::endl;
89-
MODM_LOG_INFO << "Stack usage 3 = " << fiber3.stack_usage() << modm::endl;
90-
MODM_LOG_INFO << "Stack usage 4 = " << fiber4.stack_usage() << modm::endl;
91-
92-
while(1) ;
93-
return 0;
9467
}

examples/avr/fiber/project.xml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<library>
2-
<extends>modm:mega-2560-pro</extends>
3-
<!-- <extends>modm:arduino-nano</extends> -->
42
<options>
3+
<option name="modm:target">atmega644-20au</option>
4+
<option name="modm:platform:core:f_cpu">14.7456M</option>
55
<option name="modm:build:build.path">../../../build/avr/fiber</option>
66
</options>
77
<modules>
8-
<module>modm:build:scons</module>
9-
<module>modm:processing:timer</module>
8+
<module>modm:platform:core</module>
9+
<module>modm:platform:clock</module>
10+
<module>modm:platform:gpio</module>
1011
<module>modm:processing:fiber</module>
12+
<module>modm:build:scons</module>
1113
</modules>
1214
</library>

0 commit comments

Comments
 (0)