With the release of distortos 0.7.0 I decided to share my project here in the hope that it may be useful to someone. Or at least that it will be a good start to the endless debate on "why using C++ on a microcontroller is just impossible and should never be tried" (; It shoud be a good controversy starter, as it has (almost) all of the things "loved" so much by C++-haters - templates, inheritance, virtual functions, ... - and yet it works correctly, reliably and fits into flash and RAM (; And you can create threads with as many arguments of whatever types you want, all of that type-checked statically during compilation. Or do the same for virtual timers. Or use RAII for locking mutexes. Or pass non-trivial objects via queues. Or just any of those things which are quite normal in C++, but yet so extremely hard to do in all of these C-only RTOSes (assuming they are possible at all, given the level of support such C-only RTOSes offer to object-oriented design).
http://distortos.org/#include "distortos/board/leds.hpp"
#include "distortos/chip/ChipOutputPin.hpp"
#include "distortos/ThisThread.hpp"
int main()
{
while (1)
{
distortos::board::leds[0].set(!distortos::board::leds[0].get());
distortos::ThisThread::sleepFor(std::chrono::milliseconds{500});
}
}
This is a COMPLETE file, which uses one thread, as main() is a thread (why wouldn't it be?).
#include "distortos/board/leds.hpp"
#include "distortos/chip/ChipOutputPin.hpp"
#include "distortos/DynamicThread.hpp"
#include "distortos/ThisThread.hpp"
int main()
{
auto lambda = [](distortos::devices::OutputPin& led, const std::chrono::milliseconds milliseconds)
{
while (1)
{
led.set(!led.get());
distortos::ThisThread::sleepFor(milliseconds);
}
};
const auto thread0 = distortos::makeAndStartDynamicThread({512, 1},
lambda, std::ref(distortos::board::leds[0]), std::chrono::milliseconds{500});
const auto thread1 = distortos::makeAndStartDynamicThread({512, 1},
lambda, std::ref(distortos::board::leds[1]), std::chrono::milliseconds{1234});
const auto thread2 = distortos::makeAndStartDynamicThread({512, 1},
lambda, std::ref(distortos::board::leds[2]), std::chrono::milliseconds{333});
const auto thread3 = distortos::makeAndStartDynamicThread({512, 1},
lambda, std::ref(distortos::board::leds[3]), std::chrono::milliseconds{10});
while (1)
distortos::ThisThread::sleepFor(std::chrono::seconds{1});
}
Here's another one (assuming the board has 4 LEDs), with 4 additional threads, each built with a lambda which expects 2 arguments - a reference to LED which will be blinked and the duration of one "blink". Try to do the same with a C-only RTOS (;