#ifndef QNBOT_CPP_EXAMPLE_SUPPORT_HPP #define QNBOT_CPP_EXAMPLE_SUPPORT_HPP #include #include #include #include #include #include #include namespace example { constexpr const char* default_target_name = "openxr_hand"; struct SerialOptions { std::string port; std::optional side; double seconds{1.0}; std::uint64_t updates{10}; double hold{1.0}; std::string package_id; }; enum class SerialExample { manual, background, foreground, haptics, imu, }; inline void validate_no_options(int argc, char** argv) { if (argc == 1) return; if (argc > 1) { throw std::invalid_argument("unknown argument: " + std::string(argv[1])); } throw std::invalid_argument("invalid argument count"); } inline std::string require_value(int argc, char** argv, int& index, const std::string& option) { if (++index >= argc) throw std::invalid_argument(option + " requires a value"); const std::string value = argv[index]; if (value.rfind("--", 0) == 0) { throw std::invalid_argument(option + " requires a value"); } return value; } inline double parse_positive_double(const std::string& text, const std::string& option, bool allow_zero = false) { if (text.empty() || text.front() == '-') { throw std::invalid_argument(option + " has an invalid value"); } std::size_t consumed = 0; double value = 0.0; try { value = std::stod(text, &consumed); } catch (const std::invalid_argument&) { throw std::invalid_argument(option + " has an invalid value"); } catch (const std::out_of_range&) { throw std::invalid_argument(option + " has an invalid value"); } if (consumed != text.size() || !std::isfinite(value) || (allow_zero ? value < 0.0 : value <= 0.0)) { throw std::invalid_argument(option + " has an invalid value"); } return value; } inline std::uint64_t parse_positive_count(const std::string& text, const std::string& option) { if (text.empty() || text.front() == '-') { throw std::invalid_argument(option + " has an invalid value"); } std::size_t consumed = 0; std::uint64_t value = 0; try { value = std::stoull(text, &consumed); } catch (const std::exception&) { throw std::invalid_argument(option + " has an invalid value"); } if (consumed != text.size() || value == 0) { throw std::invalid_argument(option + " has an invalid value"); } return value; } inline qnbot::Side parse_side(const std::string& text) { if (text == "left") return qnbot::Side::left; if (text == "right") return qnbot::Side::right; throw std::invalid_argument("--side must be left or right"); } inline SerialOptions parse_serial_options(int argc, char** argv, SerialExample example) { SerialOptions options; const bool requires_algorithm = example != SerialExample::haptics && example != SerialExample::imu; for (int index = 1; index < argc; ++index) { const std::string argument = argv[index]; if (argument == "--port") { options.port = require_value(argc, argv, index, argument); } else if (argument == "--side") { options.side = parse_side(require_value(argc, argv, index, argument)); } else if (argument == "--seconds" && example == SerialExample::background) { options.seconds = parse_positive_double( require_value(argc, argv, index, argument), argument); } else if (argument == "--updates" && example == SerialExample::manual) { options.updates = parse_positive_count( require_value(argc, argv, index, argument), argument); } else if (argument == "--hold" && example == SerialExample::haptics) { options.hold = parse_positive_double( require_value(argc, argv, index, argument), argument, true); } else if (argument == "--package-id" && requires_algorithm) { options.package_id = require_value(argc, argv, index, argument); } else { throw std::invalid_argument("unknown argument: " + argument); } } if (options.port.empty()) { throw std::invalid_argument("--port is required"); } if (!options.side) { throw std::invalid_argument("--side is required with --port"); } if (requires_algorithm && options.package_id.empty()) { throw std::invalid_argument("--package-id is required"); } return options; } inline qnbot::SdkConfig serial_config(const std::string& port, qnbot::Side side) { qnbot::SerialConnection connection; connection.port = port; qnbot::GloveConfig glove{side, connection}; qnbot::SdkConfig config; config.devices.push_back(glove); return config; } inline void print_output(const qnbot::Sample& sample) { std::cout << "retargeting sequence=" << sample.sequence << " target=" << sample.value.target << " joints={"; bool first = true; for (const auto& joint : sample.value.joints) { if (!first) std::cout << ", "; std::cout << joint.first << ": " << joint.second; first = false; } std::cout << "}\n"; } inline int report_error(const std::exception& error) { std::cerr << "qnbot example failed: " << error.what() << '\n'; return EXIT_FAILURE; } } // namespace example #endif