Let the user build a serial port: which port, the baud rate, how it is read and whether the board restarts when the port opens.
248{
249 struct PortChoice {
250 std::string path;
251 std::string label;
252 };
253 struct RateChoice {
254 uint32_t rate;
255 std::string label;
256 };
257 struct ProtocolChoice {
258 std::string label;
259 Core::SerialProtocolSettings settings;
260 };
261 struct RestartChoice {
262 std::string label;
263 uint32_t settle_ms;
264 };
265
266 std::vector<PortChoice> ports;
268 ports.push_back({ port.path.string(), std::format("{} {}", port.name, port.path.string()) });
269 }
270
272 std::string path;
273 if (!ports.empty()) {
274 ports.push_back({ "", "Another path, such as a virtual port" });
276 [](const PortChoice& choice) { return choice.label; });
277 if (!port) {
278 return std::nullopt;
279 }
280 path = port->path;
281 }
282 if (path.empty()) {
283 const auto typed =
detail::ask_one(
"Serial port, a device path",
"", is_device_path);
284 if (!typed) {
285 return std::nullopt;
286 }
287 path = *typed;
288 }
290
291 const std::vector<RateChoice> rates {
292 { 9600, "9600" },
293 { 19200, "19200" },
294 { 31250, "31250, MIDI" },
295 { 38400, "38400" },
296 { 57600, "57600" },
297 { 115200, "115200" },
298 { 230400, "230400" },
299 { 250000, "250000, DMX" },
300 { 460800, "460800" },
301 { 921600, "921600" },
302 { 0, "Another rate" },
303 };
305 [](const RateChoice& choice) { return choice.label; });
306 if (!rate) {
307 return std::nullopt;
308 }
309 config.line.baud_rate = rate->rate;
310 if (rate->rate == 0) {
311 const auto typed =
detail::ask_one(
"Baud rate",
"115200", is_baud_rate);
312 if (!typed) {
313 return std::nullopt;
314 }
315 std::from_chars(typed->data(), typed->data() + typed->size(), config.line.baud_rate);
316 }
317
336 gap.gap_ms = 5;
340
341 const std::vector<ProtocolChoice> protocols {
342 { "Lines of numbers (Serial.println)", numbers },
343 { "Lines of text, as bytes", text },
344 { "MIDI byte stream", midi },
345 { "COBS frames", cobs },
346 { "SLIP frames", slip },
347 { "Raw bytes, as they arrive", raw },
348 { "A message ends after a pause", gap },
349 { "Messages ended by a terminator", delimited },
350 };
352 [](const ProtocolChoice& choice) { return choice.label; });
353 if (!protocol) {
354 return std::nullopt;
355 }
356 config.protocol = protocol->settings;
357
359 const auto typed =
detail::ask_one(
"Terminator, with \\r \\n \\t for control characters",
"\\r\\n",
360 [](std::string_view candidate) { return !unescape(candidate).empty(); });
361 if (!typed) {
362 return std::nullopt;
363 }
364 config.protocol.terminator = unescape(*typed);
365 }
366
367 const std::vector<RestartChoice> restarts {
368 { "Board restarts on open (Arduino): wait 2 s", 2000 },
369 { "No restart: read at once", 0 },
370 };
372 [](const RestartChoice& choice) { return choice.label; });
373 if (!restart) {
374 return std::nullopt;
375 }
376 config.line.settle_ms = restart->settle_ms;
377
378 return config;
379}
std::optional< std::string > ask_one(std::string title, std::string initial, Portal::Forma::AskValidator validator)
Open a text prompt and wait for the text.
std::optional< T > pick_one(std::string title, std::vector< T > items, std::function< std::string(const T &)> label)
Open a picker and wait for the choice.
std::string port_name
e.g., "/dev/ttyUSB0" or "COM3"
One serial port, the line setup it opens with and how it is read.
How a port is read, as plain data a config can hold.