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) { if (port == interpreter.current_context->port) {
return std::visit(Overloaded { return std::visit(Overloaded {
[](midi::connections::Virtual_Port) { return Value(Symbol("virtual")); }, [](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)); }, [](midi::connections::Established_Port port) { return Value(Number(port)); },
}, key); }, key);
} }
@ -1658,6 +1659,19 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> a
return {}; 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(); 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); return std::chrono::duration<float>(float(len.num * (60.f / (float(bpm) / 4))) / len.den);
} }
template<> std::size_t std::hash<midi::connections::Key>::operator()(midi::connections::Key const& value) const
struct std::hash<midi::connections::Key>
{ {
std::size_t operator()(midi::connections::Key const& value) const using namespace midi::connections;
{ return hash_combine(value.index(), std::visit(Overloaded {
using namespace midi::connections; [](Virtual_Port) { return 0u; },
return hash_combine(value.index(), std::visit(Overloaded { [](Serial_Port) { return 0u; },
[](Virtual_Port) { return 0u; }, [](Established_Port port) { return port; },
[](Established_Port port) { return port; }, }, value));
}, value)); }
}
};
std::unordered_map<midi::connections::Key, std::shared_ptr<midi::Connection>> Context::established_connections; 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; 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 /// Context holds default values for music related actions
@ -55,4 +60,10 @@ struct Context
std::shared_ptr<Context> parent; std::shared_ptr<Context> parent;
}; };
template<>
struct std::hash<midi::connections::Key>
{
std::size_t operator()(midi::connections::Key const& value) const;
};
#endif #endif