diff --git a/README.md b/README.md index fea8c48..2aaf6ad 100644 --- a/README.md +++ b/README.md @@ -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. # How to use -1. You will need to write a JSON config file. -It can either be named `config.json` or you will need to pass it as the first command line parameter to the executable. +### General use +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`. You can find more info on the available Widgets and their parameters at the end of this file. 2. Run the executable 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 ### Convert image without dithering `convert trmnl.png -monochrome -colors 2 -depth 1 -strip png:output.png` diff --git a/main.cpp b/main.cpp index 5973348..e22b277 100644 --- a/main.cpp +++ b/main.cpp @@ -60,8 +60,18 @@ int main(int argc, char **argv) cfg_filename = argv[1]; } - // Read JSON CFG - ok = read_config_json(cfg, cfg_filename); + // Read config + 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) { result = -1; diff --git a/sdl_helpers.cpp b/sdl_helpers.cpp index e6e2d84..d0c71d7 100644 --- a/sdl_helpers.cpp +++ b/sdl_helpers.cpp @@ -141,15 +141,20 @@ bool read_config_json(json& cfg, const string& filename, ostream* log) { if (nullptr != log) { - *log << "Could not open config.json" << endl; + *log << "Could not open config file" << endl; } return false; } + return read_config_json(cfg, cfg_file, log); +} + +bool read_config_json(json& cfg, istream& in, ostream* log) +{ // Parse with comments try { - cfg = json::parse(cfg_file, nullptr, true, true); + cfg = json::parse(in, nullptr, true, true); } catch (const std::exception &e) { diff --git a/sdl_helpers.h b/sdl_helpers.h index 9a47469..9eca5df 100644 --- a/sdl_helpers.h +++ b/sdl_helpers.h @@ -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 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 void json_extract(const nlohmann::json& j, const std::string& key, std::string& out);