Serial port can be chosen via port selection

This commit is contained in:
Robert Bendun 2023-01-09 21:49:01 +01:00
parent f3c6c0a2e4
commit 0892812933
3 changed files with 34 additions and 12 deletions

View File

@ -1638,6 +1638,7 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> 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<Value> builtin_port(Interpreter &interpreter, std::vector<Value> 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<midi::Serial_Midi>();
serial->serialport = interpreter.serialport;
interpreter.current_context->port = serial;
Context::established_connections[midi::connections::Serial_Port{}] = serial;
return {};
}
unimplemented();
}

View File

@ -13,18 +13,15 @@ std::chrono::duration<float> Context::length_to_duration(std::optional<Number> l
return std::chrono::duration<float>(float(len.num * (60.f / (float(bpm) / 4))) / len.den);
}
template<>
struct std::hash<midi::connections::Key>
std::size_t std::hash<midi::connections::Key>::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; },
[](Serial_Port) { return 0u; },
[](Established_Port port) { return port; },
}, value));
}
};
}
std::unordered_map<midi::connections::Key, std::shared_ptr<midi::Connection>> Context::established_connections;

View File

@ -18,7 +18,12 @@ namespace midi::connections
bool operator==(Virtual_Port const&) const = default;
};
using Key = std::variant<Established_Port, Virtual_Port>;
struct Serial_Port
{
bool operator==(Serial_Port const&) const = default;
};
using Key = std::variant<Established_Port, Virtual_Port, Serial_Port>;
}
/// Context holds default values for music related actions
@ -55,4 +60,10 @@ struct Context
std::shared_ptr<Context> parent;
};
template<>
struct std::hash<midi::connections::Key>
{
std::size_t operator()(midi::connections::Key const& value) const;
};
#endif