From 0892812933ba01544c0ae9eb2dc0fccb424c4bb6 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 9 Jan 2023 21:49:01 +0100 Subject: [PATCH] Serial port can be chosen via port selection --- musique/interpreter/builtin_functions.cc | 14 ++++++++++++++ musique/interpreter/context.cc | 19 ++++++++----------- musique/interpreter/context.hh | 13 ++++++++++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/musique/interpreter/builtin_functions.cc b/musique/interpreter/builtin_functions.cc index addcf6d..0ae2bd9 100644 --- a/musique/interpreter/builtin_functions.cc +++ b/musique/interpreter/builtin_functions.cc @@ -1638,6 +1638,7 @@ static Result builtin_port(Interpreter &interpreter, std::vector a if (port == interpreter.current_context->port) { return std::visit(Overloaded { [](midi::connections::Virtual_Port) { return Value(Symbol("virtual")); }, + [](midi::connections::Serial_Port) { return Value(Symbol("serial")); }, [](midi::connections::Established_Port port) { return Value(Number(port)); }, }, key); } @@ -1658,6 +1659,19 @@ static Result builtin_port(Interpreter &interpreter, std::vector a return {}; } + if (port_type == "serial") { + if (auto it = Context::established_connections.find(midi::connections::Serial_Port{}); it != Context::established_connections.end()) { + interpreter.current_context->port = it->second; + return {}; + } + + auto serial = std::make_shared(); + serial->serialport = interpreter.serialport; + interpreter.current_context->port = serial; + Context::established_connections[midi::connections::Serial_Port{}] = serial; + return {}; + } + unimplemented(); } diff --git a/musique/interpreter/context.cc b/musique/interpreter/context.cc index c2cc51f..15e5ca0 100644 --- a/musique/interpreter/context.cc +++ b/musique/interpreter/context.cc @@ -13,18 +13,15 @@ std::chrono::duration Context::length_to_duration(std::optional l return std::chrono::duration(float(len.num * (60.f / (float(bpm) / 4))) / len.den); } -template<> -struct std::hash +std::size_t std::hash::operator()(midi::connections::Key const& value) const { - std::size_t operator()(midi::connections::Key const& value) const - { - using namespace midi::connections; - return hash_combine(value.index(), std::visit(Overloaded { - [](Virtual_Port) { return 0u; }, - [](Established_Port port) { return port; }, - }, value)); - } -}; + using namespace midi::connections; + return hash_combine(value.index(), std::visit(Overloaded { + [](Virtual_Port) { return 0u; }, + [](Serial_Port) { return 0u; }, + [](Established_Port port) { return port; }, + }, value)); +} std::unordered_map> Context::established_connections; diff --git a/musique/interpreter/context.hh b/musique/interpreter/context.hh index 5ab8412..8bb134e 100644 --- a/musique/interpreter/context.hh +++ b/musique/interpreter/context.hh @@ -18,7 +18,12 @@ namespace midi::connections bool operator==(Virtual_Port const&) const = default; }; - using Key = std::variant; + struct Serial_Port + { + bool operator==(Serial_Port const&) const = default; + }; + + using Key = std::variant; } /// Context holds default values for music related actions @@ -55,4 +60,10 @@ struct Context std::shared_ptr parent; }; +template<> +struct std::hash +{ + std::size_t operator()(midi::connections::Key const& value) const; +}; + #endif