Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Can the P1Parser class be used on Windows or Linux PC hardware or on a microcontroller without Arduino library? #51

Open
PolarGoose opened this issue Oct 18, 2023 · 6 comments

Comments

@PolarGoose
Copy link

PolarGoose commented Oct 18, 2023

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?

@PolarGoose PolarGoose changed the title [Question] Can the P1Parser class be used on Windows or Linux PC hardware? [Question] Can the P1Parser class be used on Windows or Linux PC hardware or on a microcontroller without Arduino library? Oct 19, 2023
@matthijskooijman
Copy link
Owner

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).

@PolarGoose
Copy link
Author

PolarGoose commented Oct 19, 2023

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.

@PolarGoose
Copy link
Author

PolarGoose commented Oct 25, 2023

@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?

@matthijskooijman
Copy link
Owner

matthijskooijman commented Oct 30, 2023

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 arduino/toolchain-avr#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:

static void concat_hack(String& s, const char *append, size_t n) {
// Add null termination. Inefficient, but it works...
char buf[n + 1];
memcpy(buf, append, n);
buf[n] = 0;
s.concat(buf);
}

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.

@PolarGoose
Copy link
Author

@matthijskooijman,
I have tried your proposal. Indeed, it is possible to make the parser work.
I have created a pull request with my results: #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.

@PolarGoose
Copy link
Author

By the way, I have also written my own DSMR parser: DsmrParserLite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants