komunikacja szeregowa - zmiany robocze

This commit is contained in:
Mateusz Drabek 2022-12-07 04:41:28 +01:00
parent 58036c2357
commit 8157ba38db
4 changed files with 92 additions and 11 deletions

View File

@ -5,6 +5,7 @@
#include <musique/interpreter/context.hh>
#include <musique/value/value.hh>
#include <unordered_map>
#include <musique/serialport/serialport.hh>
/// Given program tree evaluates it into Value
struct Interpreter
@ -25,6 +26,8 @@ struct Interpreter
std::function<std::optional<Error>(Interpreter&, Value)> default_action;
std::shared_ptr<serialport::Device> serialport;
Interpreter();
~Interpreter();
Interpreter(Interpreter &&) = delete;

View File

@ -17,6 +17,7 @@
#include <musique/try.hh>
#include <musique/unicode.hh>
#include <musique/value/block.hh>
#include <musique/serialport/serialport.hh>
#include <serial/serial.h>
@ -164,6 +165,8 @@ struct Runner
midi::Rt_Midi midi;
Interpreter interpreter;
std::shared_ptr<serialport::Device> serial_state;
std::optional<std::jthread> serial_event_loop;
/// Setup interpreter and midi connection with given port
explicit Runner(std::optional<unsigned> 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::Device>();
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<Error> Main(std::span<char const*> 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()) {

View File

@ -0,0 +1,59 @@
#include <musique/serialport/serialport.hh>
#include <iostream>
#include <thread>
#include <serial/serial.h>
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;
}
}

View File

@ -0,0 +1,19 @@
#ifndef MUSIQUE_SERIALPORT_SERIALPORT_HH
#define MUSIQUE_SERIALPORT_SERIALPORT_HH
#include <string>
#include <serial/serial.h>
#include <stop_token>
#include <memory>
namespace serialport{
struct Device{
int test();
};
void initialize();
void event_loop();
std::uint8_t get_byte();
}
#endif //MUSIQUE_SERIALPORT_SERIALPORT_HH