I would like to add a custom tool and headers for supporting compile-time configuration, namely json2cpp. It’s converts JSON files into class header files containing firmware configuration as constants. The generated files also reference its supporting headers in order to compile.
It’s unclear how I would go about creating a platform IO dependency on json2cpp from reading the documentation. I would like to to perform the following:
- Automatically clone json2cpp as a repo from GitHub
- Build the json2cpp project using CMake.
- Install the json2cpp artifacts in the current project as a tool or locally
- Invoke json2cpp as part of the pre-build process to generate headers in src folder from a JSON file
- Reference json2cpp supporting header files during the build
Usually when PlatformIO uses a “tool”, it is uses it in a “package”, each OS (
linux_x86_64, …) has its own. For example, tool-avrdude in the registry or API. Using precompiled packages lessens the requirements on the user’s PC. But those “packages” usually get used inside of platforms.
You can e.g. at run determine the type of OS (ref), lookup the download URL from their github (ref), download it, unpack it and invoke it. Let me show you an example…
I’ve created a working showcase with an ESP32 firmware that prints the data from the autogenerated json2cpp output at
Wow! Cool! This is precisely what I’m looking for!
And just for completeness’ sake: If it’s not important that it’s firmware compile-time constant, a much easier way is to use the filesystem capabilities of the ESP32. I.e., put the configuration JSON file in the
data/ folder of the project, package it it as a e.g. LittleFS filesystem and upload it, read the file back at runtime using the LittleFS and ArduinoJSON library and act on it.
I don’t want any performance penalties when you’re down to the nanoseconds. Of course you can load it before hand. Ideally, I would like it burned in the binary so the device is configured and ready to go.