<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><description>C++ has embedded friendly mechanisms that can help to structure your firmware. While keeping the design resource-lean.In this post, I use classes, C++ stl containers and algorithms to manage close-to-the-hardware resources with object...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><pubDate>Tue, 18 Nov 2025 08:03:41 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:b09f7aa5-7af2-4c81-ab34-7659d23f672e</guid><dc:creator>pvit</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;I&amp;#39;d suggest you take a look at the Embedded Templates Library &lt;a href="https://www.etlcpp.com/" rel="noopener noreferrer nofollow" target="_blank" data-e14adj="t"&gt;https://www.etlcpp.com/&lt;/a&gt;. It&amp;#39;s more focused on embedded-specific demands and portability between c++ standards.&lt;/p&gt;
&lt;p&gt;~ the same as std, but without dynamic allocations everywhere, with backports of new features like string_view to ancient C++11 compilers when nothing better is available on your target MCU. And of course, both can be used at the same time, if needed.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29330&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><pubDate>Sun, 09 Nov 2025 20:17:04 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:b09f7aa5-7af2-4c81-ab34-7659d23f672e</guid><dc:creator>colporteur</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;JC I do enjoy your posts but start gasping for air shortly after I start reading. I really need tanks of knowledge to go to your depth. I can spell C. Keep thinking I can learn more by exposure but come up gasping:)&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29330&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><pubDate>Sat, 08 Nov 2025 19:02:21 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:b09f7aa5-7af2-4c81-ab34-7659d23f672e</guid><dc:creator>DAB</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Great post Jan.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29330&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><pubDate>Fri, 07 Nov 2025 22:52:55 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:b09f7aa5-7af2-4c81-ab34-7659d23f672e</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;the mechanism becomes more interesting when you&amp;#39;re doing more with these motors. &lt;br /&gt;In my firmware, I want that each of those motors throws an interrupt when it has executed a command (a command is a number of steps, clockwise or counter-clockwise).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Registering that interrupt becomes simple, because I have the infrastructure set up already:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:c6ad339e-98cf-4e5c-9961-cf19c4b783d3:type=csharp&amp;text=%20%20%20%20for%20%28auto%20%26h%3A%20handlers%29%20%7B%0D%0A%20%20%20%20%20%20%20%20pio_irq_manager_t%3A%3Aregister_interrupt%28IRQ_CHAN%2C%20h.pio%2C%20h.sm%2C%20true%29%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20]&lt;/p&gt;
&lt;p&gt;This is true for any logic that applies to more than one motor.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;disable all motors: use a for loop&lt;/li&gt;
&lt;li&gt;slow down all motors on PIO 0: use a loop with filter predicate&lt;/li&gt;
&lt;li&gt;stop all motors except 1: same filter mechanism ...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is my full code, where I program PIOs, initialise state machines, enable interrupt, register interrupt&amp;nbsp;handler and start the state machines:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:f5900ca0-1cf5-4e75-b110-7cde94824d16:type=csharp&amp;text=%20%20%20%20%2F%2F%20program%20each%20PIO%2C%20and%20initialise%20each%20sm%20-%20only%20if%20they%20are%20used%0D%0A%20%20%20%20for%20%28uint%20u%20%3D%200u%3B%20u%20%3C%20NUM_PIOS%3B%20u%2B%2B%29%20%7B%0D%0A%20%20%20%20%20%20%20%20if%20%28std%3A%3Aranges%3A%3Aany_of%28handlers%2C%20%5Bu%5D%28auto%26%20h%29%7B%20return%20h.pio%20%3D%3D%20PIO_INSTANCE%28u%29%3B%20%7D%29%29%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20printf%28%22PIO%20%25u%20programmed%5Cn%22%2C%20u%29%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20uint%20offset%20%20%3D%20pio_add_program%28PIO_INSTANCE%28u%29%2C%20%26run_program%29%3B%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%28auto%20%26h%20%3A%20handlers%20%7C%20std%3A%3Aviews%3A%3Afilter%28%5Bu%5D%28const%20auto%26%20h%29%7B%20return%20h.pio%20%3D%3D%20PIO_INSTANCE%28u%29%3B%7D%29%29%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20run_program_init%28h.pio%2C%20h.sm%2C%20offset%29%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf%28%22SM%20%25u%20on%20PIO%20%25u%20initialised%5Cn%22%2C%20h.sm%2C%20PIO_NUM%28h.pio%29%29%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%0D%0A%20%20%20%20%2F%2F%20tell%20each%20sm%20that%20it%20has%20to%20forward%20interrupt%20on%20channel%200%0D%0A%20%20%20%20for%20%28auto%20%26h%3A%20handlers%29%20%7B%0D%0A%20%20%20%20%20%20%20%20pio_irq_manager_t%3A%3Aregister_interrupt%28IRQ_CHAN%2C%20h.pio%2C%20h.sm%2C%20true%29%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%0D%0A%20%20%20%20for%20%28auto%20%26h%3A%20handlers%29%20%7B%0D%0A%20%20%20%20%20%20%20%20pio_irq_manager_t%3A%3Aregister_handler%28h.pio%2C%20h.sm%2C%20%26h%2C%20true%29%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%0D%0A%20%20%20%20%2F%2F%20enable%20and%20start%20each%20sm%0D%0A%20%20%20%20for%20%28auto%20%26h%3A%20handlers%29%20%7B%0D%0A%20%20%20%20%20%20%20%20pio_run_forever%28h.pio%2C%20h.sm%2C%201%29%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20]&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29330&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly</title><link>https://community.element14.com/products/raspberry-pi/b/blog/posts/modern-c-on-a-pico-use-c-standard-library-algorithms---embedded-friendly</link><pubDate>Fri, 07 Nov 2025 21:36:57 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:b09f7aa5-7af2-4c81-ab34-7659d23f672e</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;if you prefer C, or you know exactly what motors you&amp;#39;ll connect to which PIO state machine: this code is doing the same:&lt;/p&gt;
&lt;pre&gt; offset = pio_add_program(pio0, &amp;amp;run_program);&lt;br /&gt; run_program_init(pio0, 0, offset);&lt;br /&gt; run_program_init(pio0, 1, offset);&lt;br /&gt; run_program_init(pio0, 2, offset);&lt;br /&gt; run_program_init(pio0, 3, offset);&lt;br /&gt; offset = pio_add_program(pio1, &amp;amp;run_program);&lt;br /&gt; run_program_init(pio1, 0, offset);&lt;br /&gt; run_program_init(pio1, 1, offset);&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;/* these Pico PIO resources aren&amp;#39;t used, so not programmed / initiated:&lt;br /&gt; run_program_init(pio1, 2, offset);&lt;br /&gt; run_program_init(pio1, 3, offset);&lt;br /&gt;#if (NUM_PIOS &amp;gt; 2) // pico 2&lt;br /&gt; offset = pio_add_program(pio2, &amp;amp;run_program);&lt;br /&gt; run_program_init(pio2, 0, offset);&lt;br /&gt; run_program_init(pio2, 1, offset);&lt;br /&gt; run_program_init(pio2, 2, offset);&lt;br /&gt; run_program_init(pio2, 3, offset);&lt;br /&gt;#endif&lt;br /&gt;*/&lt;/pre&gt;
&lt;p&gt;In this case, it&amp;#39;s the programmer that drives the decisions, not the data set ...&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29330&amp;AppID=86&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>