report errors on serial connection failure

This commit is contained in:
Robert Bendun 2023-01-20 02:38:48 +01:00
parent 0892812933
commit 926fc84138
5 changed files with 44 additions and 5 deletions

View File

@ -150,6 +150,7 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
[](errors::Unexpected_Keyword const&) { return "Unexpected keyword"; }, [](errors::Unexpected_Keyword const&) { return "Unexpected keyword"; },
[](errors::Unrecognized_Character const&) { return "Unrecognized character"; }, [](errors::Unrecognized_Character const&) { return "Unrecognized character"; },
[](errors::Wrong_Arity_Of const&) { return "Different arity then expected"; }, [](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::internal::Unexpected_Token const&) { return "Unexpected token"; },
[](errors::Arithmetic const& err) { [](errors::Arithmetic const& err) {
switch (err.type) { 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) { [&](errors::Unsupported_Types_For const& err) {
switch (err.type) { switch (err.type) {
break; case errors::Unsupported_Types_For::Function: break; case errors::Unsupported_Types_For::Function:

View File

@ -169,6 +169,11 @@ namespace errors
}; };
} }
struct Temporary_Error_Serial_Port
{
std::string message;
};
/// All possible error types /// All possible error types
using Details = std::variant< using Details = std::variant<
Arithmetic, Arithmetic,
@ -180,6 +185,7 @@ namespace errors
Not_Callable, Not_Callable,
Operation_Requires_Midi_Connection, Operation_Requires_Midi_Connection,
Out_Of_Range, Out_Of_Range,
Temporary_Error_Serial_Port,
Undefined_Operator, Undefined_Operator,
Unexpected_Empty_Source, Unexpected_Empty_Source,
Unexpected_Keyword, Unexpected_Keyword,

View File

@ -1615,6 +1615,18 @@ static Result<Value> builtin_peers(Interpreter &interpreter, std::vector<Value>)
return Number(interpreter.starter.peers()); 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 //: Ustaw wyjście MIDI w danym kontekście na dany port
//: //:
//: Dostępne opcje to numer portu oraz symbol `'virtual` tworzacy port wirtualny MIDI //: 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") { 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()) { if (auto it = Context::established_connections.find(midi::connections::Serial_Port{}); it != Context::established_connections.end()) {
interpreter.current_context->port = it->second; interpreter.current_context->port = it->second;
return {}; 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 //: 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) static Result<Value> builtin_ctrl(Interpreter &interpreter, std::vector<Value> args)
{ {
if (args.size() == 1) { if (args.empty()) {
error: error:
return errors::Unsupported_Types_For { return errors::Unsupported_Types_For {
.type = errors::Unsupported_Types_For::Function, .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; auto input_index = 0;
if (auto a = get_if<Number>(args.front())) { if (auto a = get_if<Number>(args.front())) {

View File

@ -102,8 +102,13 @@ namespace serialport{
} }
} catch (std::exception &e) { } catch (std::exception &e) {
/// No connection to the device /// No connection to the device
state.error_message = e.what();
// std::cerr << "Unhandled Exception: " << e.what() << '\n'; // 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; return;

View File

@ -1,13 +1,14 @@
#ifndef MUSIQUE_SERIALPORT_SERIALPORT_HH #ifndef MUSIQUE_SERIALPORT_SERIALPORT_HH
#define MUSIQUE_SERIALPORT_SERIALPORT_HH #define MUSIQUE_SERIALPORT_SERIALPORT_HH
#include <string>
#include <serial/serial.h>
#include <memory>
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
#include <memory>
#include <musique/value/number.hh> #include <musique/value/number.hh>
#include <optional>
#include <serial/serial.h>
#include <string>
namespace serialport{ namespace serialport{
constexpr std::size_t MAX_STATE_COUNT = 8; 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::array<std::atomic<std::uint8_t>, 128> message_data;
std::atomic<std::uint8_t> head = 0; std::atomic<std::uint8_t> head = 0;
std::atomic<std::uint8_t> tail = 0; std::atomic<std::uint8_t> tail = 0;
std::optional<std::string> error_message;
int test(); int test();
Number get(unsigned position) const; Number get(unsigned position) const;
void send(uint8_t message_type, uint8_t note_number); void send(uint8_t message_type, uint8_t note_number);