komunikacja z urządzeniem
This commit is contained in:
parent
c43836dc6f
commit
7e751ac660
@ -26,7 +26,7 @@ struct Interpreter
|
|||||||
|
|
||||||
std::function<std::optional<Error>(Interpreter&, Value)> default_action;
|
std::function<std::optional<Error>(Interpreter&, Value)> default_action;
|
||||||
|
|
||||||
std::shared_ptr<serialport::Device> serialport;
|
std::shared_ptr<serialport::State> serialport;
|
||||||
|
|
||||||
Interpreter();
|
Interpreter();
|
||||||
~Interpreter();
|
~Interpreter();
|
||||||
|
@ -165,7 +165,7 @@ struct Runner
|
|||||||
|
|
||||||
midi::Rt_Midi midi;
|
midi::Rt_Midi midi;
|
||||||
Interpreter interpreter;
|
Interpreter interpreter;
|
||||||
std::shared_ptr<serialport::Device> serial_state;
|
std::shared_ptr<serialport::State> serial_state;
|
||||||
std::optional<std::jthread> serial_event_loop;
|
std::optional<std::jthread> serial_event_loop;
|
||||||
|
|
||||||
/// Setup interpreter and midi connection with given port
|
/// Setup interpreter and midi connection with given port
|
||||||
@ -177,19 +177,28 @@ struct Runner
|
|||||||
the = this;
|
the = this;
|
||||||
|
|
||||||
/// Setup communication over serial
|
/// Setup communication over serial
|
||||||
serial_state = std::make_shared<serialport::Device>();
|
serial_state = std::make_shared<serialport::State>();
|
||||||
|
interpreter.serialport = serial_state;
|
||||||
serialport::initialize();
|
serialport::initialize();
|
||||||
|
|
||||||
serial_event_loop = std::jthread([this](std::stop_token token) mutable{
|
serial_event_loop = std::jthread([this](std::stop_token token) mutable{
|
||||||
serialport::event_loop();
|
serialport::event_loop(token, *serial_state);
|
||||||
});
|
});
|
||||||
|
|
||||||
interpreter.midi_connection = &midi;
|
interpreter.midi_connection = &midi;
|
||||||
if (output_port) {
|
if (output_port) {
|
||||||
|
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);
|
midi.connect_output(*output_port);
|
||||||
if (!quiet_mode) {
|
if (!quiet_mode) {
|
||||||
std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl;
|
std::cout << "Connected MIDI output to port " << *output_port << ". Ready to play!" << std::endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
bool connected_to_existing_port = midi.connect_or_create_output();
|
bool connected_to_existing_port = midi.connect_or_create_output();
|
||||||
if (!quiet_mode) {
|
if (!quiet_mode) {
|
||||||
@ -210,6 +219,10 @@ struct Runner
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return {};
|
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;
|
Runner(Runner const&) = delete;
|
||||||
|
@ -4,15 +4,24 @@
|
|||||||
#include <serial/serial.h>
|
#include <serial/serial.h>
|
||||||
|
|
||||||
namespace serialport{
|
namespace serialport{
|
||||||
int Device::test(){
|
int State::test(){
|
||||||
return 5;
|
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()
|
void initialize()
|
||||||
{
|
{
|
||||||
std::cout << "initialize serial\n";
|
// std::jthread testowy = std::jthread();
|
||||||
std::jthread testowy = std::jthread();
|
return;
|
||||||
std::cout << "initialize serial\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint8_t get_byte()
|
std::uint8_t get_byte()
|
||||||
@ -20,37 +29,70 @@ namespace serialport{
|
|||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_loop()
|
void event_loop(std::stop_token token, State &state)
|
||||||
{
|
{
|
||||||
while(1){
|
while(!token.stop_requested()){
|
||||||
try {
|
try {
|
||||||
/// Search for the right device
|
/// Search for the right device
|
||||||
auto const ports = serial::list_ports();
|
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 << "Port: " << port.port << '\n';
|
||||||
std::cout << "Description: " << port.description << '\n';
|
std::cout << "Description: " << port.description << '\n';
|
||||||
std::cout << "Hardware ID: " << port.hardware_id << '\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));
|
|
||||||
|
|
||||||
if(serial_conn.isOpen())
|
/// Start connection
|
||||||
|
serial::Serial serial_conn(connection_port, 115200, serial::Timeout::simpleTimeout(1000));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*if(serial_conn.isOpen())
|
||||||
std::cout << "[SERIAL] Serial open\n";
|
std::cout << "[SERIAL] Serial open\n";
|
||||||
else
|
else
|
||||||
std::cout << "[SERIAL] Serial not open\n";
|
std::cout << "[SERIAL] Serial not open\n";
|
||||||
|
|
||||||
std::cout << "[SERIAL] commence serial communication\n";
|
std::cout << "[SERIAL] commence serial communication\n";
|
||||||
|
*/
|
||||||
|
|
||||||
|
/// Set up received data buffer
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
std::string result = serial_conn.read(1);
|
/// After serial connection established
|
||||||
std::cout << int(result[0]);
|
|
||||||
|
//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) {
|
} catch (std::exception &e) {
|
||||||
std::cerr << "Unhandled Exception: " << e.what() << '\n';
|
/// No connection to the device
|
||||||
std::cerr << "Terminating serial connection\n";
|
|
||||||
return;
|
// std::cerr << "Unhandled Exception: " << e.what() << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -5,14 +5,23 @@
|
|||||||
#include <serial/serial.h>
|
#include <serial/serial.h>
|
||||||
#include <stop_token>
|
#include <stop_token>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
|
#include <atomic>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <musique/value/number.hh>
|
||||||
|
|
||||||
namespace serialport{
|
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();
|
int test();
|
||||||
|
Number get(unsigned position) const;
|
||||||
|
void set(unsigned position, std::uint32_t value);
|
||||||
};
|
};
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void event_loop();
|
void event_loop(std::stop_token token, State &state);
|
||||||
std::uint8_t get_byte();
|
std::uint8_t get_byte();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user