MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ choose_serial_port()

MAYAFLUX_API std::optional< Core::SerialPortConfig > MayaFlux::choose_serial_port ( )

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.

A Portal::Forma picker per step, as in choose_endpoint() and choose_camera(). Blocks until the user finishes or closes a window, so it must not be called from the event thread. Portal::Forma must be initialized and the engine running. A system that exposes no ports is asked for a device path instead.

Returns
The port for open_serial_port(), or nothing on cancellation.

Definition at line 247 of file Input.cpp.

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;
267 for (const auto& port : Platform::list_serial_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" });
275 const auto port = detail::pick_one<PortChoice>("Choose a serial port", ports,
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 }
289 config.port_name = std::move(path);
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 };
304 const auto rate = detail::pick_one<RateChoice>("Baud rate", rates,
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
323 midi.parse = Core::SerialParse::MIDI;
326 cobs.parse = Core::SerialParse::BYTES;
329 slip.parse = Core::SerialParse::BYTES;
332 raw.parse = Core::SerialParse::BYTES;
335 gap.parse = Core::SerialParse::BYTES;
336 gap.gap_ms = 5;
339 delimited.parse = Core::SerialParse::BYTES;
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 };
351 const auto protocol = detail::pick_one<ProtocolChoice>("How the port is read", protocols,
352 [](const ProtocolChoice& choice) { return choice.label; });
353 if (!protocol) {
354 return std::nullopt;
355 }
356 config.protocol = protocol->settings;
357
358 if (config.protocol.framing == Core::SerialFraming::DELIMITED) {
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 };
371 const auto restart = detail::pick_one<RestartChoice>("When the port opens", restarts,
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::vector< SerialPortEntry > list_serial_ports()
The serial ports the system exposes, sorted by name.
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.

References MayaFlux::detail::ask_one(), MayaFlux::Core::SerialLineSettings::baud_rate, MayaFlux::Core::BYTES, MayaFlux::Core::COBS, MayaFlux::Core::DELIMITED, MayaFlux::Core::SerialProtocolSettings::framing, MayaFlux::Core::SerialProtocolSettings::gap_ms, MayaFlux::Core::IDLE_GAP, MayaFlux::Core::SerialPortConfig::line, MayaFlux::Platform::list_serial_ports(), MayaFlux::Core::MIDI, MayaFlux::Core::SerialProtocolSettings::parse, MayaFlux::detail::pick_one(), MayaFlux::Core::SerialPortConfig::port_name, MayaFlux::Core::SerialPortConfig::protocol, MayaFlux::Core::RAW, MayaFlux::Core::SerialLineSettings::settle_ms, MayaFlux::Core::SLIP, and MayaFlux::Core::SerialProtocolSettings::terminator.

Referenced by MayaFlux::Creator::read_serial().

Here is the call graph for this function:
Here is the caller graph for this function: