I've really been enjoying using ChibiOS. A couple of times, I have found myself wanting to parse streams using a lightweight scanf-like function in the style of chprintf, so I have written one. I searched the forum and saw that a few years ago when the idea was raised you had said you might be interested in including it, so I figured I'd offer it to you. If you don't want to include it in the main trunk for whatever reason, no problem.
The code for actually converting characters to values is pretty straightforward. The big challenge with scanf is that you need an
Code: Select all
unget(stream, char)
Temporarily I have been using a BufferedStream class which wraps a BaseSequentialStream, and also implements the BaseSequentialStream interface itself, adding a unget function to the vmt, and corresponding macro. It has a couple of disadvantages though:
- The get method of the underlying wrapped class is not the same as the get method for the wrapper, as the former bypasses the buffering layer. This makes it more complicated for the user, and the requirement that the user initialize the buffering layer also makes things more complicated.
- The buffering is often redundant with functionality already in implementations of the BaseSequentialStream class (every implementation of the interface I could find in the HAL already used a buffer of some sort), and therefore carries some unnecessary space & time inefficiency.
The better way to do it would be to implement a pure BufferedStream interface that the relevant classes could inherit from. While thinking about how to tackle the problem, I put together a simple BufferedStream that inherits from BaseSequentialStream, adapted the Memory Streams class to inherit from BufferedStream (which was a pretty simple change), and put together unget functions for the HAL Queue and HAL Buffer Queue modules that could be used to implement the functionality for HAL Serial and HAL USB Serial.
I can see a couple of different options for getting the Serial and USB Serial drivers to implement the interface (multiple inheritance, adding the interface into the class hierarchy between the BaseSequentialStream and BaseChannel interfaces, directly modifying the BaseSequentialStream interface) but before I did more work on it, I wanted to check with you about whether this is something you would be interested in, and see which strategy you thought would be best.
Thanks,
Alex