From 8157ba38db24ee9101703b751191acd7bfe50057 Mon Sep 17 00:00:00 2001 From: Mateusz Drabek Date: Wed, 7 Dec 2022 04:41:28 +0100 Subject: [PATCH] komunikacja szeregowa - zmiany robocze --- musique/interpreter/interpreter.hh | 3 ++ musique/main.cc | 22 +++++------ musique/serialport/serialport.cc | 59 ++++++++++++++++++++++++++++++ musique/serialport/serialport.hh | 19 ++++++++++ 4 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 musique/serialport/serialport.cc create mode 100644 musique/serialport/serialport.hh diff --git a/musique/interpreter/interpreter.hh b/musique/interpreter/interpreter.hh index e00edf1..d3f808b 100644 --- a/musique/interpreter/interpreter.hh +++ b/musique/interpreter/interpreter.hh @@ -5,6 +5,7 @@ #include #include #include +#include /// Given program tree evaluates it into Value struct Interpreter @@ -25,6 +26,8 @@ struct Interpreter std::function(Interpreter&, Value)> default_action; + std::shared_ptr serialport; + Interpreter(); ~Interpreter(); Interpreter(Interpreter &&) = delete; diff --git a/musique/main.cc b/musique/main.cc index 6f36b2e..d8f1db4 100644 --- a/musique/main.cc +++ b/musique/main.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -164,6 +165,8 @@ struct Runner midi::Rt_Midi midi; Interpreter interpreter; + std::shared_ptr serial_state; + std::optional serial_event_loop; /// Setup interpreter and midi connection with given port explicit Runner(std::optional output_port) @@ -173,6 +176,14 @@ struct Runner ensure(the == nullptr, "Only one instance of runner is supported"); the = this; + /// Setup communication over serial + serial_state = std::make_shared(); + serialport::initialize(); + + serial_event_loop = std::jthread([this](std::stop_token token) mutable{ + serialport::event_loop(); + }); + interpreter.midi_connection = &midi; if (output_port) { midi.connect_output(*output_port); @@ -543,17 +554,6 @@ static std::optional Main(std::span args) int main(int argc, char const** argv) { - auto const ports = serial::list_ports(); - - for (serial::PortInfo const& port : ports) { - std::cout << "Port: " << port.port << '\n'; - std::cout << "Description: " << port.description << '\n'; - std::cout << "Hardware ID: " << port.hardware_id << '\n'; - } - - return 0; - - auto const args = std::span(argv, argc).subspan(1); auto const result = Main(args); if (result.has_value()) { diff --git a/musique/serialport/serialport.cc b/musique/serialport/serialport.cc new file mode 100644 index 0000000..7f9bc14 --- /dev/null +++ b/musique/serialport/serialport.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +namespace serialport{ + int Device::test(){ + return 5; + } + + void initialize() + { + std::cout << "initialize serial\n"; + std::jthread testowy = std::jthread(); + std::cout << "initialize serial\n"; + } + + std::uint8_t get_byte() + { + return 8; + } + + void event_loop() + { + while(1){ + try { + /// Search for the right device + auto const ports = serial::list_ports(); + + for (serial::PortInfo const& port : ports) { + std::cout << "Port: " << port.port << '\n'; + std::cout << "Description: " << port.description << '\n'; + std::cout << "Hardware ID: " << port.hardware_id << '\n'; + } + + /// Start connection + serial::Serial serial_conn("/dev/ttyACM0", 115200, serial::Timeout::simpleTimeout(1000)); + + if(serial_conn.isOpen()) + std::cout << "[SERIAL] Serial open\n"; + else + std::cout << "[SERIAL] Serial not open\n"; + + std::cout << "[SERIAL] commence serial communication\n"; + + while(1){ + std::string result = serial_conn.read(1); + std::cout << int(result[0]); + } + } catch (std::exception &e) { + std::cerr << "Unhandled Exception: " << e.what() << '\n'; + std::cerr << "Terminating serial connection\n"; + return; + } + } + return; + } +} + diff --git a/musique/serialport/serialport.hh b/musique/serialport/serialport.hh new file mode 100644 index 0000000..18bc3fe --- /dev/null +++ b/musique/serialport/serialport.hh @@ -0,0 +1,19 @@ +#ifndef MUSIQUE_SERIALPORT_SERIALPORT_HH +#define MUSIQUE_SERIALPORT_SERIALPORT_HH + +#include +#include +#include +#include + +namespace serialport{ + struct Device{ + int test(); + }; + + void initialize(); + void event_loop(); + std::uint8_t get_byte(); +} + +#endif //MUSIQUE_SERIALPORT_SERIALPORT_HH \ No newline at end of file