From ae072c18d15f0ab0ca0ecd6760b9ac6a1d8f453a Mon Sep 17 00:00:00 2001 From: Matt Davison Date: Sun, 26 Dec 2021 18:00:39 +0000 Subject: [PATCH 1/2] Added main class files --- hui_controller.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++ hui_controller.h | 65 ++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 hui_controller.cpp create mode 100644 hui_controller.h diff --git a/hui_controller.cpp b/hui_controller.cpp new file mode 100644 index 0000000..9631c5c --- /dev/null +++ b/hui_controller.cpp @@ -0,0 +1,123 @@ +#include "USB_HUI.h" + +//CLASS INITIATION +USB_HUI::USB_HUI(byte MIDI_channel = 1) { + //Convert MIDI channel from 1-16 to 0-15 + _MIDI_channel = MIDI_channel - 1; +} + + +//MIDI Read - returns true if changes +bool USB_HUI::read_MIDI() { + MIDI_packets[0] = MidiUSB.read(); + + //check for correct MIDI channel + if ((MIDI_packets[0].byte1 && 0x0F) != MIDI_channel) { + return; + } + + //Check for HUI sensing message + if (MIDI_packets[0].byte1 == 0x90 && e.byte2 == 0x00 && e.byte3 == 0x00) { + HUI_sense(); + return; + } + else if (e.header == 0x0A && e.byte1 == 0xA0 && e.byte2 < 0x08 && (e.byte3 & 0x0F)<0x0D && ((e.byte3 & 0xF0)>>4)<2){ + int meter_channel = e.byte2; + int meter_side = ((e.byte3 & 0xF0)>>4); + int meter_level = (e.byte3 & 0x0F); + + + + + +} + +//BUTTON SENDING +void USB_HUI::button_press(byte zone, byte port) { + HUI_button(byte zone, port, 0) +} +void USB_HUI::button_release(byte zone, byte port) { + HUI_button(zone, port, 1) +} + +void USB_HUI::send_button(byte zone, byte port) { + button_press(zone, port); + button_release(zone, port); +} + +void USB_HUI::send_mute(byte channel) { + channel = channel - 1; //Convert 1-8 to 0-7 + if (channel < 8) { //Check in range + send_button(channel, 2); + } +} +void USB_HUI::send_solo(byte channel) { + channel = channel - 1; //Convert 1-8 to 0-7 + if (channel < 8) { //Check in range + send_button(channel, 3); + } +} +void USB_HUI::send_recarm(byte channel) { + channel = channel - 1; //Convert 1-8 to 0-7 + if (channel < 8) { //Check in range + send_button(channel, 7); + } +} +void USB_HUI::send_select(byte channel) { + channel = channel - 1; //Convert 1-8 to 0-7 + if (channel < 8) { //Check in range + send_button(channel, 1); + } +} + +void USB_HUI::bank_up() { + send_button(0x0A, 3); +} +void USB_HUI::bank_down() { + send_button(0x0A, 1); +} +void USB_HUI::channel_up() { + send_button(0x0A, 2); +} +void USB_HUI::channel_down() { + send_button(0x0A, 0); +} + + + +//PRIVATE FUNCTIONS + +void USB_HUI::HUI_sense() { + MidiUSB.sendMIDI({0x09, 0x90 | _MIDI_channel, 0, 0x7F}); + MidiUSB.flush(); +} + +void USB_HUI::write_rotary(byte byte2, byte delta_val) { + int midi_rot; + if (delta_val == 0) { + return; + } + else if (delta_val > 0) { + midi_rot = 0x40 + delta_val; + } + else if (delta_val < 0) { + midi_rot = 0 - delta_val; + } + MidiUSB.sendMIDI({0x0B, 0xB0 | _MIDI_channel, byte2, midi_rot}); + MidiUSB.flush(); +} + + +void USB_HUI::HUI_button(byte zone, byte port, byte press_rel) { + int num_rel; + if (press_release == 0) { + num_rel = 0x40; + } + else { + num_rel = 0; + } + MidiUSB.sendMIDI({0x0B, 0xB0 | _MIDI_channel, 0x0F, zone}); + MidiUSB.flush(); + MidiUSB.sendMIDI({0x0B, 0xB0 | _MIDI_channel, 0x2F, num_rel + port}); + MidiUSB.flush(); +} diff --git a/hui_controller.h b/hui_controller.h new file mode 100644 index 0000000..56231ca --- /dev/null +++ b/hui_controller.h @@ -0,0 +1,65 @@ +#ifndef hui +#define hui + +#if (ARDUINO >=100) +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +class USB_HUI { + public: + //Initialising + USB_HUI(byte MIDI_Channel); //DONE + + //Reading incoming MIDI + bool read_MIDI(); + + bool get_button_state(byte zone, byte port); + + + //Variables holding states + bool mute_states[8]; + bool solo_states[8]; + bool select_states[8]; + bool recarm_states[8]; + + //Sending MIDI buttons + void button_press(byte zone, byte port); //DONE + void button_release(byte zone, byte port); //DONE + + void send_button(byte zone, byte port); //DONE + + void send_mute(byte channel); //DONE + void send_solo(byte channel); //DONE + void send_recarm(byte channel); //DONE + void send_select(byte channel); //DONE + + void bank_up(); //DONE + void bank_down(); //DONE + void channel_up(); //DONE + void channel_down(); //DONE + + + //Sending rotaries + void send_pan(byte channel, int delta); + void send_jog(int delta); + + + + + + private: + void HUI_sense(); //DONE + void write_rotary(byte byte2, byte delta_val); //DONE + void HUI_button(byte zone, byte port, byte press_rel); //DONE + byte _MIDI_channel; + bool button_states[0x1D][0x8]; + midiEventPacket_t MIDI_packets[4]; + + + +}; + + +#endif From 500b425bf4db9b6e839a155341b53bd40a60177a Mon Sep 17 00:00:00 2001 From: Matt Davison Date: Tue, 12 Mar 2024 10:31:36 +0000 Subject: [PATCH 2/2] Added midibyte class, renamed hui class --- hui_controller.cpp => HuiPeripheral.cpp | 36 ++++++------- HuiPeripheral.h | 71 +++++++++++++++++++++++++ MidiByte.h | 60 +++++++++++++++++++++ hui_controller.h | 65 ---------------------- 4 files changed, 149 insertions(+), 83 deletions(-) rename hui_controller.cpp => HuiPeripheral.cpp (70%) create mode 100644 HuiPeripheral.h create mode 100644 MidiByte.h delete mode 100644 hui_controller.h diff --git a/hui_controller.cpp b/HuiPeripheral.cpp similarity index 70% rename from hui_controller.cpp rename to HuiPeripheral.cpp index 9631c5c..9a9d9cb 100644 --- a/hui_controller.cpp +++ b/HuiPeripheral.cpp @@ -1,14 +1,14 @@ -#include "USB_HUI.h" +#include "HuiPeripheral.h" //CLASS INITIATION -USB_HUI::USB_HUI(byte MIDI_channel = 1) { +HuiPeripheral::HuiPeripheral(uint8_t MIDI_channel = 1) { //Convert MIDI channel from 1-16 to 0-15 _MIDI_channel = MIDI_channel - 1; } //MIDI Read - returns true if changes -bool USB_HUI::read_MIDI() { +bool HuiPeripheral::read_MIDI() { MIDI_packets[0] = MidiUSB.read(); //check for correct MIDI channel @@ -33,53 +33,53 @@ bool USB_HUI::read_MIDI() { } //BUTTON SENDING -void USB_HUI::button_press(byte zone, byte port) { - HUI_button(byte zone, port, 0) +void HuiPeripheral::button_press(uint8_t zone, uint8_t port) { + HUI_button(uint8_t zone, port, 0) } -void USB_HUI::button_release(byte zone, byte port) { +void HuiPeripheral::button_release(uint8_t zone, uint8_t port) { HUI_button(zone, port, 1) } -void USB_HUI::send_button(byte zone, byte port) { +void HuiPeripheral::send_button(uint8_t zone, uint8_t port) { button_press(zone, port); button_release(zone, port); } -void USB_HUI::send_mute(byte channel) { +void HuiPeripheral::send_mute(uint8_t channel) { channel = channel - 1; //Convert 1-8 to 0-7 if (channel < 8) { //Check in range send_button(channel, 2); } } -void USB_HUI::send_solo(byte channel) { +void HuiPeripheral::send_solo(uint8_t channel) { channel = channel - 1; //Convert 1-8 to 0-7 if (channel < 8) { //Check in range send_button(channel, 3); } } -void USB_HUI::send_recarm(byte channel) { +void HuiPeripheral::send_recarm(uint8_t channel) { channel = channel - 1; //Convert 1-8 to 0-7 if (channel < 8) { //Check in range send_button(channel, 7); } } -void USB_HUI::send_select(byte channel) { +void HuiPeripheral::send_select(uint8_t channel) { channel = channel - 1; //Convert 1-8 to 0-7 if (channel < 8) { //Check in range send_button(channel, 1); } } -void USB_HUI::bank_up() { +void HuiPeripheral::bank_up() { send_button(0x0A, 3); } -void USB_HUI::bank_down() { +void HuiPeripheral::bank_down() { send_button(0x0A, 1); } -void USB_HUI::channel_up() { +void HuiPeripheral::channel_up() { send_button(0x0A, 2); } -void USB_HUI::channel_down() { +void HuiPeripheral::channel_down() { send_button(0x0A, 0); } @@ -87,12 +87,12 @@ void USB_HUI::channel_down() { //PRIVATE FUNCTIONS -void USB_HUI::HUI_sense() { +void HuiPeripheral::HUI_sense() { MidiUSB.sendMIDI({0x09, 0x90 | _MIDI_channel, 0, 0x7F}); MidiUSB.flush(); } -void USB_HUI::write_rotary(byte byte2, byte delta_val) { +void HuiPeripheral::write_rotary(uint8_t byte2, uint8_t delta_val) { int midi_rot; if (delta_val == 0) { return; @@ -108,7 +108,7 @@ void USB_HUI::write_rotary(byte byte2, byte delta_val) { } -void USB_HUI::HUI_button(byte zone, byte port, byte press_rel) { +void HuiPeripheral::HUI_button(uint8_t zone, uint8_t port, uint8_t press_rel) { int num_rel; if (press_release == 0) { num_rel = 0x40; diff --git a/HuiPeripheral.h b/HuiPeripheral.h new file mode 100644 index 0000000..c82f371 --- /dev/null +++ b/HuiPeripheral.h @@ -0,0 +1,71 @@ +/* + +HuiPeripheral.h + +Author: Matt Davison +Date: 12/03/2024 + +*/ + +#pragma once + +#include + +class HuiPeripheral { + public: + //Initialising + HuiPeripheral(uint8_t MIDI_Channel); //DONE + + //Reading incoming MIDI + bool read_MIDI(); + + bool get_button_state(uint8_t zone, uint8_t port); + + + //Variables holding states + bool mute_states[NUM_CHANNEL_STRIPS]; + bool solo_states[NUM_CHANNEL_STRIPS]; + bool select_states[NUM_CHANNEL_STRIPS]; + bool recarm_states[NUM_CHANNEL_STRIPS]; + + //Sending MIDI buttons + void button_press(uint8_t zone, uint8_t port); //DONE + void button_release(uint8_t zone, uint8_t port); //DONE + + void send_button(uint8_t zone, uint8_t port); //DONE + + void send_mute(uint8_t channel); //DONE + void send_solo(uint8_t channel); //DONE + void send_recarm(uint8_t channel); //DONE + void send_select(uint8_t channel); //DONE + + void bank_up(); //DONE + void bank_down(); //DONE + void channel_up(); //DONE + void channel_down(); //DONE + + + //Sending rotaries + void send_pan(uint8_t channel, int delta); + void send_jog(int delta); + + + + + + private: + + static const uint32_t NUM_CHANNEL_STRIPS = 8; + static const uint32_t INCOMING_BYTES_BUFFER_SIZE = 256; + void HUI_sense(); //DONE + void write_rotary(uint8_t byte2, uint8_t delta_val); //DONE + void HUI_button(uint8_t zone, uint8_t port, uint8_t press_rel); //DONE + uint8_t _MIDI_channel; + bool button_states[0x1D][0x8]; + + uint8_t m_incoming_buffer[INCOMING_BYTES_BUFFER_SIZE]; + + + +}; + diff --git a/MidiByte.h b/MidiByte.h new file mode 100644 index 0000000..6b6b4be --- /dev/null +++ b/MidiByte.h @@ -0,0 +1,60 @@ +/* + +MidiByte.h + +Author: Matt Davison +Date: 12/03/2024 + +*/ + +#pragma once + +#include + +class MidiByte +{ + public: + + enum class ByteType + { + STATUS_BYTE, + DATA_BYTE + }; + + uint8_t getUpperNybble(uint8_t midi_byte) + { + return (midi_byte & 0xF0) >> 4; + } + + uint8_t getLowerNybble(uint8_t midi_byte) + { + return (midi_byte & 0x0F); + } + + enum class StatusByte + { + //Per-channel messages + NOTE_OFF = 0x80, + NOTE_ON = 0x90, + AFTERTOUCH = 0xA0, + CONTROL_CHANGE = 0xB0, + PROGRAM_CHANGE = 0xC0, + CHANNEL_PRESSURE = 0xD0, + PITCH_BEND = 0xE0, + + //Global messages + SYS_EX_START = 0xF0, + MTC_QUARTER_FRAME = 0xF1, + SONG_POSITION_POINTER = 0xF2, + SONG_SELECT = 0xF3, + TUNE_REQUEST = 0xF6, + SYS_EX_END = 0xF7, + MIDI_CLOCK = 0xF8, + MIDI_START = 0xFA, + MIDI_CONTINUE = 0xFB, + MIDI_STOP = 0xFC, + ACTIVE_SENSING = 0xFE, + SYSTEM_RESET = 0xFF + }; + +}; \ No newline at end of file diff --git a/hui_controller.h b/hui_controller.h deleted file mode 100644 index 56231ca..0000000 --- a/hui_controller.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef hui -#define hui - -#if (ARDUINO >=100) -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -class USB_HUI { - public: - //Initialising - USB_HUI(byte MIDI_Channel); //DONE - - //Reading incoming MIDI - bool read_MIDI(); - - bool get_button_state(byte zone, byte port); - - - //Variables holding states - bool mute_states[8]; - bool solo_states[8]; - bool select_states[8]; - bool recarm_states[8]; - - //Sending MIDI buttons - void button_press(byte zone, byte port); //DONE - void button_release(byte zone, byte port); //DONE - - void send_button(byte zone, byte port); //DONE - - void send_mute(byte channel); //DONE - void send_solo(byte channel); //DONE - void send_recarm(byte channel); //DONE - void send_select(byte channel); //DONE - - void bank_up(); //DONE - void bank_down(); //DONE - void channel_up(); //DONE - void channel_down(); //DONE - - - //Sending rotaries - void send_pan(byte channel, int delta); - void send_jog(int delta); - - - - - - private: - void HUI_sense(); //DONE - void write_rotary(byte byte2, byte delta_val); //DONE - void HUI_button(byte zone, byte port, byte press_rel); //DONE - byte _MIDI_channel; - bool button_states[0x1D][0x8]; - midiEventPacket_t MIDI_packets[4]; - - - -}; - - -#endif