From 7e751ac66092e1a4431adefe545db8229bc64bf5 Mon Sep 17 00:00:00 2001 From: Mateusz Drabek Date: Sat, 7 Jan 2023 17:23:53 +0100 Subject: [PATCH] =?UTF-8?q?komunikacja=20z=20urz=C4=85dzeniem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- musique/interpreter/interpreter.hh | 2 +- musique/main.cc | 25 +++++++--- musique/serialport/serialport.cc | 74 +++++++++++++++++++++++------- musique/serialport/serialport.hh | 13 +++++- 4 files changed, 89 insertions(+), 25 deletions(-) diff --git a/musique/interpreter/interpreter.hh b/musique/interpreter/interpreter.hh index d3f808b..6780213 100644 --- a/musique/interpreter/interpreter.hh +++ b/musique/interpreter/interpreter.hh @@ -26,7 +26,7 @@ struct Interpreter std::function(Interpreter&, Value)> default_action; - std::shared_ptr serialport; + std::shared_ptr serialport; Interpreter(); ~Interpreter(); diff --git a/musique/main.cc b/musique/main.cc index d8f1db4..be64364 100644 --- a/musique/main.cc +++ b/musique/main.cc @@ -165,7 +165,7 @@ struct Runner midi::Rt_Midi midi; Interpreter interpreter; - std::shared_ptr serial_state; + std::shared_ptr serial_state; std::optional serial_event_loop; /// Setup interpreter and midi connection with given port @@ -177,18 +177,27 @@ struct Runner the = this; /// Setup communication over serial - serial_state = std::make_shared(); + serial_state = std::make_shared(); + interpreter.serialport = serial_state; serialport::initialize(); serial_event_loop = std::jthread([this](std::stop_token token) mutable{ - serialport::event_loop(); + serialport::event_loop(token, *serial_state); }); interpreter.midi_connection = &midi; if (output_port) { - midi.connect_output(*output_port); - if (!quiet_mode) { - std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl; + if(output_port == 42){ + /// TODO CHANGE THE MAGIC NUMBER TO SEPARATE ARGUMENT + midi.connect_output(); + if (!quiet_mode) { + std::cout << "Created new MIDI output port 'Musique'. Ready to play!" << std::endl; + } + }else{ + midi.connect_output(*output_port); + if (!quiet_mode) { + std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl; + } } } else { bool connected_to_existing_port = midi.connect_or_create_output(); @@ -210,6 +219,10 @@ struct Runner std::cout << std::endl; return {}; }); + + Env::global->force_define("ctrl", +[](Interpreter &interpreter, std::vector args) -> Result { + return interpreter.serialport->get(std::get(args[0].data).as_int()); + }); } Runner(Runner const&) = delete; diff --git a/musique/serialport/serialport.cc b/musique/serialport/serialport.cc index 7f9bc14..ee6e9a1 100644 --- a/musique/serialport/serialport.cc +++ b/musique/serialport/serialport.cc @@ -4,15 +4,24 @@ #include namespace serialport{ - int Device::test(){ + int State::test(){ return 5; } + Number State::get(unsigned position) const + { + return Number(state[position], MAX_VALUE); + } + void State::set(unsigned position, std::uint32_t value) + { + state[position] = value; + return; + } + void initialize() { - std::cout << "initialize serial\n"; - std::jthread testowy = std::jthread(); - std::cout << "initialize serial\n"; + // std::jthread testowy = std::jthread(); + return; } std::uint8_t get_byte() @@ -20,37 +29,70 @@ namespace serialport{ return 8; } - void event_loop() + void event_loop(std::stop_token token, State &state) { - while(1){ + while(!token.stop_requested()){ try { /// Search for the right device auto const ports = serial::list_ports(); - - for (serial::PortInfo const& port : 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'; + }*/ + + int found_port = 0; + std::string connection_port = ""; + while(found_port == 0){ + for (serial::PortInfo const& port : ports) { + if(port.description.find("STM32")){ + connection_port = port.port; + found_port = 1; + break; + } + } } + /// Start connection - serial::Serial serial_conn("/dev/ttyACM0", 115200, serial::Timeout::simpleTimeout(1000)); + serial::Serial serial_conn(connection_port, 115200, serial::Timeout::simpleTimeout(1000)); - if(serial_conn.isOpen()) + + + /*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"; - + */ + + /// Set up received data buffer + while(1){ - std::string result = serial_conn.read(1); - std::cout << int(result[0]); + /// After serial connection established + + //serial_conn.flushInput(); + + std::string result = serial_conn.readline(65536, "\n"); + int control = result.at(0); + uint32_t value = std::stoi(result.substr(1, 4), nullptr, 10); + + state.set(control - 65, value); + + switch(control){ + case 68: // D + + break; + case 66: // B + + break; + } } } catch (std::exception &e) { - std::cerr << "Unhandled Exception: " << e.what() << '\n'; - std::cerr << "Terminating serial connection\n"; - return; + /// No connection to the device + + // std::cerr << "Unhandled Exception: " << e.what() << '\n'; } } return; diff --git a/musique/serialport/serialport.hh b/musique/serialport/serialport.hh index 18bc3fe..8c6dd60 100644 --- a/musique/serialport/serialport.hh +++ b/musique/serialport/serialport.hh @@ -5,14 +5,23 @@ #include #include #include +#include +#include +#include +#include namespace serialport{ - struct Device{ + constexpr std::size_t MAX_STATE_COUNT = 8; + constexpr std::size_t MAX_VALUE = 4095; + struct State{ + std::array, MAX_STATE_COUNT> state; int test(); + Number get(unsigned position) const; + void set(unsigned position, std::uint32_t value); }; void initialize(); - void event_loop(); + void event_loop(std::stop_token token, State &state); std::uint8_t get_byte(); }