Added ability to supply JSON from standard input

This commit is contained in:
2026-03-30 17:28:36 +03:00
parent a80e23614d
commit 6b22d8465e
4 changed files with 40 additions and 6 deletions

View File

@@ -9,13 +9,26 @@ You will need the following packages: `build-essential` `libsdl2-dev` `libsdl2-i
Just run `make` and an executable called `trmnl_sdl` should be produced. Just run `make` and an executable called `trmnl_sdl` should be produced.
# How to use # How to use
1. You will need to write a JSON config file. ### General use
It can either be named `config.json` or you will need to pass it as the first command line parameter to the executable. 1. You will need to write a JSON config file named `config.json`.
An example structure of the config file can be found in `config_example.json`. An example structure of the config file can be found in `config_example.json`.
You can find more info on the available Widgets and their parameters at the end of this file. You can find more info on the available Widgets and their parameters at the end of this file.
2. Run the executable 2. Run the executable
3. Convert the image to a suitable format via an ImageMagick command from below. 3. Convert the image to a suitable format via an ImageMagick command from below.
### Different ways to supply JSON
1. Default
* Command: `./trmnl_sdl`
* JSON Used: `config.json`
2. Specified file
* Command: `./trmnl_sdl path/to/json/file.json`
* JSON Used: `path/to/json/file.json`
3. Standard input
* Command: `./trmnl_sdl -`
* JSON Used: Supplied on the standard input for the process.
# ImageMagick commands to prepare image for TRMNL device # ImageMagick commands to prepare image for TRMNL device
### Convert image without dithering ### Convert image without dithering
`convert trmnl.png -monochrome -colors 2 -depth 1 -strip png:output.png` `convert trmnl.png -monochrome -colors 2 -depth 1 -strip png:output.png`

View File

@@ -60,8 +60,18 @@ int main(int argc, char **argv)
cfg_filename = argv[1]; cfg_filename = argv[1];
} }
// Read JSON CFG // Read config
ok = read_config_json(cfg, cfg_filename); if ("-" == cfg_filename)
{
// Read JSON from std input
ok = read_config_json(cfg, std::cin);
}
else
{
// Read JSON from config file
ok = read_config_json(cfg, cfg_filename);
}
if (!ok) if (!ok)
{ {
result = -1; result = -1;

View File

@@ -141,15 +141,20 @@ bool read_config_json(json& cfg, const string& filename, ostream* log)
{ {
if (nullptr != log) if (nullptr != log)
{ {
*log << "Could not open config.json" << endl; *log << "Could not open config file" << endl;
} }
return false; return false;
} }
return read_config_json(cfg, cfg_file, log);
}
bool read_config_json(json& cfg, istream& in, ostream* log)
{
// Parse with comments // Parse with comments
try try
{ {
cfg = json::parse(cfg_file, nullptr, true, true); cfg = json::parse(in, nullptr, true, true);
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {

View File

@@ -90,6 +90,12 @@ SDL_Rect surface_align(const SDL_Surface* base, const SDL_Surface* applied,
// log - output errors to this ostream, silent if NULL // log - output errors to this ostream, silent if NULL
bool read_config_json(nlohmann::json& cfg, const std::string& filename, std::ostream* log = &std::cout); bool read_config_json(nlohmann::json& cfg, const std::string& filename, std::ostream* log = &std::cout);
// Reads the stream and tries to parse JSON from it with comments
// cfg - output json struct
// in - input stream to read
// log - output errors to this ostream, silent if NULL
bool read_config_json(nlohmann::json& cfg, std::istream& in, std::ostream* log = &std::cout);
// JSON Extractors - They do not override already set values if key is not present // JSON Extractors - They do not override already set values if key is not present
void json_extract(const nlohmann::json& j, const std::string& key, std::string& out); void json_extract(const nlohmann::json& j, const std::string& key, std::string& out);