It always bothers me when I see ugly, unmaintainable MCU code, which seems to be prevalent in the embedded programming world. Everywhere I look, I see ugly code where all sorts of bugs can easily lurk!

I am an advocate of modern C++. I can use high-level constructs such as higher-order functions without ever sacrificing on performance. The code is just as fast and tight as, ugly, low-level C. C++ can be ugly too, but if you do it right, it can be elegant and beautiful!
Here’s an example of how I write mine, and I am proud of it! It’s one of my test programs: the obligatory blink LED sample code, but notice that here, we’re using three real-time threads, each blinking an LED at different rates (Aside: The observant reader might note this can also be done using timers instead of delays. Sure! But this particular example is about threads). In my opinion, using real-time threads is a must for any serious MCU development. This code runs on an STM32H7 480Mhz MCU.
int main()
{
auto led1 = out<snq::led1>();
auto led2 = out<snq::led2>();
auto led3 = out<snq::led3>();
auto f1 = snq::thread(
[&]
{
while (true)
{
snq::delay_ms(1000);
led1 = !led1;
}
}
);
auto f2 = snq::thread(
[&]
{
while (true)
{
snq::delay_ms(500);
led2 = !led2;
}
}
);
while (true)
{
snq::delay_ms(250);
led3 = !led3;
}
}
