A lean alternative for the C++ IOStream library, sized for embedded systems
The standard IOStream library works great on a Pico, but inflates the code size. In a 2022 CppCon talk "Modern C++ to Impress Your Embedded Dev Friends", Steve Bush presented a lightweight alternative: a FileStream that's perfectly sized for embedded use.
I ported it for Pico. It supports streaming to the Pico stdio supported COM ports: the Pico's USB, a PicoProbe's USB, or the Pico's UART0.
Usage example:
#include <stdio.h> #include "pico/stdio.h" #include "filestream.hpp" namespace mcu { FileStream debug(1); } int main(int, char** ) { stdio_init_all(); mcu::debug << "Hello, world!\r\n"; mcu::debug << mcu::FileStream::RadixEnum::Hexadecimal << 6 << "\r\n"; while (true) continue; }
To use it in your C++ project, you just add a few lines to your CMake file:
# add_subdirectory()
pico_add_subdirectory(lean_stream_io)
# ...
target_link_libraries(${CMAKE_PROJECT_NAME}
pico_stdlib
lean_stream_io
)
# adjust to enable stdio via usb, or uart
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 0)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 1)
Github repository, with example program: https://github.com/jancumps/pico-lean-stream-io
Steve Bush CppCon 2022 talk: Modern C++ to Impress Your Embedded Dev Friends
The code is Steve's original code written for the conference. I provided an implementation of the _write() function that uses the Pico.C SDK stdio functionality.
Steve Bush referenced material:
|