Skip to content

Commit

Permalink
[examples] Enable all protothread as fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 16, 2023
1 parent 68c84e7 commit 40d4bde
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 63 deletions.
29 changes: 20 additions & 9 deletions examples/nucleo_g071rb/amnb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class Thread : public modm::pt::Protothread
Result<uint8_t, uint8_t> res2;

public:
bool inline
update()
bool
run()
{
PT_BEGIN();

Expand All @@ -98,6 +98,23 @@ class Thread : public modm::pt::Protothread
}
thread;

class NodeThread : public modm::pt::Protothread
{
public:
bool run()
{
while (true)
{
node1.update();
node2.update();
node3.update();
modm::fiber::yield();
}
return false;
}
}
nodeThread;

// ----------------------------------------------------------------------------
int
main()
Expand Down Expand Up @@ -132,13 +149,7 @@ main()
USART4->CR3 = USART_CR3_HDSEL;
USART4->CR1 |= USART_CR1_UE;

while (true)
{
node1.update();
node2.update();
node3.update();
thread.update();
}
modm::fiber::Scheduler::run();

return 0;
}
1 change: 1 addition & 0 deletions examples/nucleo_g071rb/amnb/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<option name="modm:platform:uart:3:buffer.rx">16</option>
<option name="modm:platform:uart:4:buffer.rx">0</option>
<option name="modm:communication:amnb:with_heap">False</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
</options>
<modules>
<module>modm:platform:gpio</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
101 changes: 52 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,56 +39,55 @@ Board::l3g::Gyroscope::Data data;
Board::l3g::Gyroscope gyro(data);


class ReaderThread : public modm::pt::Protothread
void thread1()
{
public:
bool
update()
// initialize with limited range of 250 degrees per second
gyro.configure(gyro.Scale::Dps250);

modm::filter::MovingAverage<float, 25> averageZ;
while (true)
{
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());

// update the moving average
averageZ.update(gyro.getData().getZ());

{
float value = averageZ.getValue();
// normalize rotation and scale by 5 leds
uint16_t leds = abs(value / 200 * 5);
leds = (1ul << leds) - 1;

// 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);
}
}

// repeat every 5 ms
timeout.restart(5ms);
PT_WAIT_UNTIL(timeout.isExpired());
// read out the sensor
gyro.readRotation();

// update the moving average
averageZ.update(gyro.getData().getZ());

float value = averageZ.getValue();
// normalize rotation and scale by 5 leds
uint16_t leds = abs(value / 200 * 5);
leds = (1ul << leds) - 1;

// 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::ShortTimeout timeout(5ms);
while(not timeout.execute())
modm::fiber::yield();
}
}
modm_faststack modm::fiber::Stack<512> stack1;
modm_fastdata modm::Fiber fiber1(stack1, thread1);

private:
modm::ShortTimeout timeout;
modm::filter::MovingAverage<float, 25> averageZ;
};
modm_faststack modm::fiber::Stack<256> stack2;
modm_fastdata modm::Fiber fiber2(stack2, []()
{
modm::ShortPeriodicTimer timer(1s);
while (true)
{
if(timer.execute()) Board::LedSouth::toggle();
modm::fiber::yield();
}
});

ReaderThread reader;


int
Expand All @@ -97,10 +96,14 @@ 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 40d4bde

Please sign in to comment.