report errors on serial connection failure
This commit is contained in:
parent
0892812933
commit
926fc84138
@ -150,6 +150,7 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
|
||||
[](errors::Unexpected_Keyword const&) { return "Unexpected keyword"; },
|
||||
[](errors::Unrecognized_Character const&) { return "Unrecognized character"; },
|
||||
[](errors::Wrong_Arity_Of const&) { return "Different arity then expected"; },
|
||||
[](errors::Temporary_Error_Serial_Port const&) { return "Serial port connection failed"; },
|
||||
[](errors::internal::Unexpected_Token const&) { return "Unexpected token"; },
|
||||
[](errors::Arithmetic const& err) {
|
||||
switch (err.type) {
|
||||
@ -271,6 +272,15 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
|
||||
}
|
||||
},
|
||||
|
||||
[&](errors::Temporary_Error_Serial_Port const& error) {
|
||||
os << "Error serial port: " << error.message << '\n';
|
||||
os << "\n";
|
||||
|
||||
pretty::begin_comment(os);
|
||||
os << "This error message is temporary\n";
|
||||
pretty::end(os);
|
||||
},
|
||||
|
||||
[&](errors::Unsupported_Types_For const& err) {
|
||||
switch (err.type) {
|
||||
break; case errors::Unsupported_Types_For::Function:
|
||||
|
@ -169,6 +169,11 @@ namespace errors
|
||||
};
|
||||
}
|
||||
|
||||
struct Temporary_Error_Serial_Port
|
||||
{
|
||||
std::string message;
|
||||
};
|
||||
|
||||
/// All possible error types
|
||||
using Details = std::variant<
|
||||
Arithmetic,
|
||||
@ -180,6 +185,7 @@ namespace errors
|
||||
Not_Callable,
|
||||
Operation_Requires_Midi_Connection,
|
||||
Out_Of_Range,
|
||||
Temporary_Error_Serial_Port,
|
||||
Undefined_Operator,
|
||||
Unexpected_Empty_Source,
|
||||
Unexpected_Keyword,
|
||||
|
@ -1615,6 +1615,18 @@ static Result<Value> builtin_peers(Interpreter &interpreter, std::vector<Value>)
|
||||
return Number(interpreter.starter.peers());
|
||||
}
|
||||
|
||||
static std::optional<Error> ensure_serial_available(Interpreter &interpreter)
|
||||
{
|
||||
if (interpreter.serialport->error_message->empty())
|
||||
return std::nullopt;
|
||||
|
||||
return Error{
|
||||
errors::Temporary_Error_Serial_Port {
|
||||
.message = *std::exchange(interpreter.serialport->error_message, std::nullopt),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//: Ustaw wyjście MIDI w danym kontekście na dany port
|
||||
//:
|
||||
//: Dostępne opcje to numer portu oraz symbol `'virtual` tworzacy port wirtualny MIDI
|
||||
@ -1660,6 +1672,7 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> a
|
||||
}
|
||||
|
||||
if (port_type == "serial") {
|
||||
Try(ensure_serial_available(interpreter));
|
||||
if (auto it = Context::established_connections.find(midi::connections::Serial_Port{}); it != Context::established_connections.end()) {
|
||||
interpreter.current_context->port = it->second;
|
||||
return {};
|
||||
@ -1685,7 +1698,7 @@ static Result<Value> builtin_port(Interpreter &interpreter, std::vector<Value> a
|
||||
//: Kolejne parametry tworzą zbiór z którego równomiernie wejście będzie wybierane na podstawie wartości
|
||||
static Result<Value> builtin_ctrl(Interpreter &interpreter, std::vector<Value> args)
|
||||
{
|
||||
if (args.size() == 1) {
|
||||
if (args.empty()) {
|
||||
error:
|
||||
return errors::Unsupported_Types_For {
|
||||
.type = errors::Unsupported_Types_For::Function,
|
||||
@ -1694,6 +1707,7 @@ static Result<Value> builtin_ctrl(Interpreter &interpreter, std::vector<Value> a
|
||||
},
|
||||
};
|
||||
}
|
||||
Try(ensure_serial_available(interpreter));
|
||||
|
||||
auto input_index = 0;
|
||||
if (auto a = get_if<Number>(args.front())) {
|
||||
|
@ -102,8 +102,13 @@ namespace serialport{
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
/// No connection to the device
|
||||
|
||||
state.error_message = e.what();
|
||||
// std::cerr << "Unhandled Exception: " << e.what() << '\n';
|
||||
//
|
||||
// Sleep until error message is cleared since we don't need to try always to reconnect
|
||||
while (!state.error_message->empty()) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
@ -1,13 +1,14 @@
|
||||
#ifndef MUSIQUE_SERIALPORT_SERIALPORT_HH
|
||||
#define MUSIQUE_SERIALPORT_SERIALPORT_HH
|
||||
|
||||
#include <string>
|
||||
#include <serial/serial.h>
|
||||
#include <memory>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <musique/value/number.hh>
|
||||
#include <optional>
|
||||
#include <serial/serial.h>
|
||||
#include <string>
|
||||
|
||||
namespace serialport{
|
||||
constexpr std::size_t MAX_STATE_COUNT = 8;
|
||||
@ -18,6 +19,9 @@ namespace serialport{
|
||||
std::array<std::atomic<std::uint8_t>, 128> message_data;
|
||||
std::atomic<std::uint8_t> head = 0;
|
||||
std::atomic<std::uint8_t> tail = 0;
|
||||
|
||||
std::optional<std::string> error_message;
|
||||
|
||||
int test();
|
||||
Number get(unsigned position) const;
|
||||
void send(uint8_t message_type, uint8_t note_number);
|
||||
|
Loading…
Reference in New Issue
Block a user