A C++20 header-only library for querying GPIO pin information on Effective Range hardware devices (Raspberry Pi based).
- Reads device information from the Linux device tree
- Looks up GPIO pin definitions from a JSON hardware database
- Semantic versioning support with intelligent revision matching
- JSON schema validation for hardware database
- Header-only library for easy integration
- C++20 compatible compiler
- CMake 3.22+
- Dependencies:
mkdir build && cd build
cmake ..
makeTo run tests:
make testsudo make installThis installs:
er-hwinfoCLI tool to/usr/bin/hwdb.jsonandhwdb-schema.jsonto/etc/er-hwinfo/
#include <er/hwinfo.hpp>
#include <iostream>
int main() {
// Query hardware info using default paths
auto info = er::hwinfo::get();
if (!info) {
std::cerr << "Device not found or not an ER device\n";
return 1;
}
std::cout << "Hardware type: " << info->dev.hw_type << "\n";
std::cout << "Revision: " << info->dev.hw_revision.as_string() << "\n";
for (const auto& pin : info->pins) {
std::cout << "Pin " << pin.name << " (GPIO " << pin.number << "): "
<< pin.description << "\n";
}
return 0;
}auto info = er::hwinfo::get(
"/custom/device-tree", // Device tree base path
"/custom/hwdb.json", // Hardware database path
"/custom/hwdb-schema.json" // Schema path
);er-hwinfoOutputs JSON with device info and pin definitions.
The library reads from the following device tree structure:
/proc/device-tree/
effective-range,hardware/
effective-range,type # Hardware type name (string)
effective-range,revision-major # Major version (u32, big-endian)
effective-range,revision-minor # Minor version (u32, big-endian)
effective-range,revision-patch # Patch version (u32, big-endian)
The hardware database (hwdb.json) maps device types and revisions to GPIO pin definitions:
{
"device-name": {
"1.0.0": {
"pins": {
"PIN_NAME": {
"description": "Human-readable description",
"value": 17
}
}
}
}
}| Field | Max Length | Notes |
|---|---|---|
| Device name | 64 chars | Top-level key |
| Revision | 32 chars | Semantic version (X.Y.Z) |
| Pin name | 64 chars | |
| Description | 256 chars | |
| GPIO value | 0-255 |
When looking up pin definitions, the library uses intelligent version matching:
- Exact match: If the device revision exactly matches an entry, use it
- Forward match: Use the first database revision >= device revision with the same major version
- Backward search: If no forward match exists with the same major, search backwards for the highest revision with matching major version
- No match: If no revision with the same major version exists, return empty pins
| Device | Database Entries | Selected |
|---|---|---|
| 1.5.0 | 1.2.0, 1.8.0 | 1.8.0 (forward match, same major) |
| 1.9.0 | 1.5.0, 2.0.0 | 1.5.0 (backward search, same major) |
| 1.0.0 | 1.2.3 | 1.2.3 (forward match, same major) |
| 2.0.0 | 1.5.0, 1.8.0 | (none) - different major |
This ensures devices get compatible pin definitions even when the exact revision isn't in the database.
- Returns
std::nulloptwhen device tree is missing or invalid - Returns
infowith emptypinswhen device type or compatible revision not found - Throws
std::runtime_errorfor file I/O errors or invalid JSON - Throws
std::runtime_errorwhen JSON fails schema validation
See LICENSE file for details.