Commit e67618a2 authored by philip's avatar philip
Browse files

updates (fixes) of USBMIDI and some refactoring

parent ef392147
# aggregat controller 02
https://gitlab.zhdk.ch/aggregat-02/a01
https://aggregat.zhdk.ch/
## Hardware
### dev (philip)
- Base board: [ST NUCLEO-H743ZI2](https://www.st.com/en/evaluation-tools/nucleo-h743zi.html) (also see [@mbed](https://os.mbed.com/platforms/ST-Nucleo-H743ZI2/))
*note* can possibly be scaled down to a cheaper version
*note* can possibly be scaled down to a (marginally) cheaper version
- MIDI-Module: [midi-uart-adapter](https://github.com/tschiemer/midi-uart-adapter), to be tested
## Building
## Software
- [Mbed Studio](https://os.mbed.com/studio/) as IDE
......@@ -18,6 +22,13 @@ Make sure to install all dependencies:
git submodule update --init --recursive
```
### Problems
- as of 2020-07-02 the official USBMIDI of mbed-os has some (reported) bugs, please update these files:
- [USBMIDI.h](https://github.com/tschiemer/mbed-os/blob/usbmidi-writeplus-fixes/drivers/USBMIDI.h)
- [USBMIDI.cpp](https://github.com/tschiemer/mbed-os/blob/usbmidi-writeplus-fixes/drivers/source/usb/USBMIDI.cpp)
- [MIDIMessage.h](https://github.com/tschiemer/mbed-os/blob/usbmidi-writeplus-fixes/drivers/internal/MIDIMessage.h)
## Configuration options
See file `config.h`
#ifndef USBMIDI_AGGREGAT_H
#define USBMIDI_AGGREGAT_H
#include "USBMIDI.h"
#include "PinNames.h"
#include "InterruptIn.h"
......@@ -26,3 +29,5 @@ class UsbMidiAggregat : public USBMIDI {
const uint8_t *string_iinterface_desc();
};
#endif //USBMIDI_AGGREGAT_H
\ No newline at end of file
......@@ -8,16 +8,23 @@
#define ENABLE_SYSTEM_RESET 1
#define ENABLE_CONTROLLER_LOGIC 0
/************ MOTORS ************/
#define MOTOR_REFRESH_RATE_HZ 60
// #define MOTOR_REFRESH_RATE_HZ 60
// #define MOTOR_PULSEWIDTH_MIN_USEC 556
// #define MOTOR_PULSEWIDTH_MAX_USEC 2410
#define MOTOR_REFRESH_RATE_HZ 50
#define MOTOR_PULSEWIDTH_MIN_USEC 556
#define MOTOR_PULSEWIDTH_MAX_USEC 2410
#define MOTOR_PULSEWIDTH_MIN_USEC 0
#define MOTOR_PULSEWIDTH_MAX_USEC 20000000
#define MOTOR_COUNT 1
#define MOTOR_1_PIN LED1
#define MOTOR_1_PIN PWM_OUT // PA_15
#define MOTOR_2_PIN LED2
#define MOTOR_3_PIN LED3
#define MOTOR_4_PIN LED4
......@@ -29,7 +36,7 @@
#define USB_POWER_PIN PA_9
#define USB_CONNECTED_LED LED1
#define USB_CONNECTED_LED LED2
// default value
#define USBMIDI_FORWARD_TO_MIDI true
......@@ -42,24 +49,24 @@
#define MIDI_TX_PIN PD_5
#define MIDI_RX_PIN PD_6
// #define MIDI_BAUD 31250
#define MIDI_BAUD 9600
#define MIDI_BAUD 31250
// #define MIDI_BAUD 9600
#define MIDI_BITS 8
#define MIDI_PARITY BufferedSerial::None
#define MIDI_STOP 1
// default value
#define MIDI_FORWARD_TO_USB false
#define MIDI_FORWARD_TO_NET false
#define MIDI_FORWARD_TO_USB true
#define MIDI_FORWARD_TO_NET true
/************ NETWORK BASED MIDI ************/
#define USE_NETMIDI 1
#define USE_NETMIDI 0
// default value
#define NETMIDI_FORWARD_TO_MIDI false
#define NETMIDI_FORWARD_TO_MIDI true
#define NETMIDI_FORWARD_TO_USB false
#define NET_HOSTNAME_FMT "Aggregat-02-%08x"
......
......@@ -19,25 +19,12 @@
/************ TYPES ************/
// typedef enum {
// StateStopped,
// StateStarting,
// StateStarted,
// StateStopping
// } State;
typedef enum {
SourceUsb,
SourceMidi,
SourceNet
} Source;
typedef enum {
ExclusiveSourceNone,
ExclusiveSourceUsb,
ExclusiveSourceNet
} ExclusiveSource;
typedef enum {
ConfigStateValid = 0x1337BEEF
} ConfigState;
......@@ -101,8 +88,9 @@ void eth_ifup();
/************ LOCAL VARIABLES ************/
// will be overwritten immediately by load_config()
Config config = {
.device_id = 1
.device_id = 0,
};
volatile bool motors_running = false;
......@@ -113,8 +101,6 @@ AggregatMotor motors[MOTOR_COUNT] = {
volatile int32_t channel_offset = CHANNEL_OFFSET;
volatile int32_t controller_offset = CONTROLLER_OFFSET;
Mutex exclusive_source_mutex;
ExclusiveSource exclusive_source = ExclusiveSourceNone;
#if USE_USBMIDI == 1
DigitalOut usbmidi_led(USB_CONNECTED_LED);
......@@ -238,6 +224,9 @@ void controller_handle_msg(uint8_t * buffer, size_t length, Source source)
}
printf("\n");
// return;
#if ENABLE_CONTROLLER_LOGIC == 1
MIDIMessage msg;
msg.from_raw(buffer, length);
......@@ -288,6 +277,8 @@ void controller_handle_msg(uint8_t * buffer, size_t length, Source source)
}
#endif //ENABLE_CONTROLLER_LOGIC == 1
if (source == SourceUsb){
if (usb_to_midi){
midi_tx(buffer, length);
......@@ -327,32 +318,11 @@ void usbmidi_run()
if (usbmidi.connected() == false){
// if (exclusive_source == ExclusiveSourceUsb){
// exclusive_source = ExclusiveSourceNone;
// }
// if (exclusive_source != ExclusiveSourceNone){
// return;
// }
// if (exclusive_source_mutex.trylock() == false){
// return;
// }
if (usbmidi.just_reconnected()){
// midi_tx((uint8_t*)"1\n", 2);
usbmidi.connect();
}
// while(usbmidi.connected() == false){
// printf("1\n");
// ThisThread::sleep_for(100);
// }
// exclusive_source = ExclusiveSourceUsb;
// exclusive_source_mutex.unlock();
return;
}
......@@ -369,14 +339,14 @@ void usbmidi_run()
MIDIMessage msg;
usbmidi.read(&msg);
usbmidi.read(msg);
if (msg.length <= 1){
if (msg.length == 0){
return;
}
MidiMessage::simpleparser_receivedData(&usbmidi_parser, &msg.data[1], (uint8_t)msg.length -1);
// controller_handle_msg(&msg.data[1], msg.length - 1, SourceUsb);
// MIDIMessage can contain multiple messages, thus we have to parse them like a stream...
MidiMessage::simpleparser_receivedData(&usbmidi_parser, msg.data, (uint8_t)msg.length);
}
}
......@@ -390,17 +360,8 @@ void usbmidi_tx(uint8_t * buffer, size_t length)
if (usbmidi.ready() == false){
return;
}
// if (usbmidi.writable() == false){
// printf("usbmidi not writable!\n");
// return;
// }
MIDIMessage msg;
msg.from_raw(buffer, length);
usbmidi.write(msg);
usbmidi.write(buffer, length);
}
#endif //USE_USBMIDI == 1
......@@ -435,12 +396,18 @@ void midi_run()
if (rlen > 0){
// echo
// midi_tx(buffer,rlen);
// printf("midi rx %d\n", rlen);
MidiMessage::simpleparser_receivedData(&midi_parser, buffer, (uint8_t)rlen);
}
}
}
void midi_rx(uint8_t * buffer, uint8_t length, void * context)
{
controller_handle_msg(buffer, length, SourceMidi);
}
void midi_tx(uint8_t * buffer, size_t length)
{
if (midi.writable() == false){
......@@ -449,37 +416,6 @@ void midi_tx(uint8_t * buffer, size_t length)
midi.write(buffer, length);
}
void midi_rx(uint8_t * buffer, uint8_t length, void * context)
{
controller_handle_msg(buffer, length, SourceMidi);
}
// void midi_start()
// {
// if (midi_state != StateStopped){
// return;
// }
// midi_state = StateStarting;
// midi_thread.start(midi_run);
// }
// void midi_stop()
// {
// if (midi_state != StateStarted){
// return;
// }
// midi_state = StateStopping;
// do {
// ThisThread::sleep_for(100);
// } while( midi_state == StateStopped);
// }
#endif //USE_MIDI == 1
......@@ -637,6 +573,7 @@ void eth_ifup()
eth_reconnect = true;
while (1){
// if (exclusive_source == ExclusiveSourceNone){
......@@ -685,26 +622,6 @@ void eth_ifup()
void netmidi_run()
{
if (eth.get_connection_status() != NSAPI_STATUS_GLOBAL_UP){
// if (eth_reconnect){
// eth_reconnect = false;
// // net_thread.terminate();
// // eth.disconnect();
// // Thread t;
// // t.start(eth_ifup);
// // if (net_thread_ran_before){
// // net_thread_ran_before = false;
// // net_thread.join();
// // }
// // net_thread.start(eth_ifup);
// // net_thread_ran_before = true;
// eth_ifup();
// }
return;
}
......@@ -725,14 +642,10 @@ void netmidi_run()
udp_sock.sendto(addr, data, res);
}
}
// void netmidi_deinit()
// {
// // mdns_resp_remove_netif();
// }
void netmidi_tx(uint8_t * buffer, size_t length)
{
//TODO
}
#endif //USE_NETMIDI == 1
......@@ -761,10 +674,45 @@ int main()
motors_resume();
// Thread thr;
// thr.start([](){
// while(1){
// printf("midi_tx\n");
// static uint8_t controller = 1;
// static uint8_t value = 127;
// value = (value + 1) % 128;
// uint8_t cc[3] = {0xB1, controller, value};
// usbmidi_tx(cc, 3);
// wait_us(1000000);
// }
// });
while (true) {
usbmidi_run();
midi_run();
netmidi_run();
// if (usbmidi.ready()){
// static uint8_t controller = 1;
// static uint8_t value = 127;
// value = (value + 1) % 128;
// uint8_t cc[3] = {0xB1, controller, value};
// usbmidi_tx(cc, 3);
// wait_us(1000000);
// }
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment