arduino-dsmr
arduino-dsmr copied to clipboard
[Question] Can the P1Parser class be used on Windows or Linux PC hardware or on a microcontroller without Arduino library?
The parser implementation tightly depends on Arduino framework. I want to use the parser on a different microcontroller platform without Arduino libraries. Also, I would like to be able to run the parser on my development machine for unit testing purposes.
Has anyone tried to do it before? What steps would I need to take to achieve that?
You should be able to. Arduino code is really just C++ code, and I think I've written the parser class separate from the reader class to make the parser not depend on any Arduino libraries/core code.
I guess it would be a matter of including all files in your C++ project and including the right files (just don't include reader.h
, since that does depend on Arduino, but there is no reader.cpp
that you would need to exclude from the build).
I think I've written the parser class separate from the reader class to make the parser not depend on any Arduino libraries/core code.
Unfortunately, the situation is more complicated. The parser at least depends on the Arduino String
class that is defined in the WString.h with the implementation in the WString.cpp which recursively depends on other parts of the Arduino library. That doesn't allow just copy-pasting these files to the project.
@matthijskooijman, Is there a particular reason why the parser uses Arduino String instead of std::string? If not, could you give your estimate of what would need to change in the parser to switch to std::string?
Unfortunately, the situation is more complicated. The parser at least depends on the Arduino String class
Good point, I had missed that it seems.
It's because the AVR Arduino core does not have libstdc++ available, so it has not std::string
. See also https://github.com/arduino/toolchain-avr/issues/89
If not, could you give your estimate of what would need to change in the parser to switch to std::string?
From a quick glance at the code, I guess it might be sufficient to:
- Add
using String = std::string;
- Add
using __FlashStringHelper = char
- Modify the
concat_hack
function as shown below.
e.g. replaces this function: https://github.com/matthijskooijman/arduino-dsmr/blob/5a0b558e251fb6ce935f97c4b349740c594f70ee/src/dsmr/util.h#L54-L60
By:
static void concat_hack(String& s, const char *append, size_t n) {
s.append(append, n);
}
It seems the other operations on String
objects use +=
and .reserve()
, which are also supported by std::string
, so no changes needed.
I haven't tested this, so there might be more required changes. If you do test this, let me know the result and we can maybe figure out how to integrate this into the main codebase.
@matthijskooijman, I have tried your proposal. Indeed, it is possible to make the parser work. I have created a pull request with my results: https://github.com/matthijskooijman/arduino-dsmr/pull/53 Could you please take a look at it? It is not hard to make it so that on Arduino, the parser will use String, and std::string will be used in all other cases. I can help finish these changes, but I need to know if you want to review and eventually merge them.
By the way, I have also written my own DSMR parser: DsmrParserLite.