komunikacja z urządzeniem

This commit is contained in:
Mateusz Drabek 2023-01-07 17:23:53 +01:00
parent c43836dc6f
commit 7e751ac660
4 changed files with 89 additions and 25 deletions

View File

@ -26,7 +26,7 @@ struct Interpreter
std::function<std::optional<Error>(Interpreter&, Value)> default_action;
std::shared_ptr<serialport::Device> serialport;
std::shared_ptr<serialport::State> serialport;
Interpreter();
~Interpreter();

View File

@ -165,7 +165,7 @@ struct Runner
midi::Rt_Midi midi;
Interpreter interpreter;
std::shared_ptr<serialport::Device> serial_state;
std::shared_ptr<serialport::State> serial_state;
std::optional<std::jthread> 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<serialport::Device>();
serial_state = std::make_shared<serialport::State>();
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<Value> args) -> Result<Value> {
return interpreter.serialport->get(std::get<Number>(args[0].data).as_int());
});
}
Runner(Runner const&) = delete;

View File

@ -4,15 +4,24 @@
#include <serial/serial.h>
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;

View File

@ -5,14 +5,23 @@
#include <serial/serial.h>
#include <stop_token>
#include <memory>
#include <array>
#include <atomic>
#include <cstdint>
#include <musique/value/number.hh>
namespace serialport{
struct Device{
constexpr std::size_t MAX_STATE_COUNT = 8;
constexpr std::size_t MAX_VALUE = 4095;
struct State{
std::array<std::atomic<std::uint32_t>, 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();
}