diff --git a/HuiPeripheral.cpp b/HuiPeripheral.cpp new file mode 100644 index 0000000..9a9d9cb --- /dev/null +++ b/HuiPeripheral.cpp @@ -0,0 +1,123 @@ +#include "HuiPeripheral.h" + +//CLASS INITIATION +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 HuiPeripheral::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 HuiPeripheral::button_press(uint8_t zone, uint8_t port) { + HUI_button(uint8_t zone, port, 0) +} +void HuiPeripheral::button_release(uint8_t zone, uint8_t port) { + HUI_button(zone, port, 1) +} + +void HuiPeripheral::send_button(uint8_t zone, uint8_t port) { + button_press(zone, port); + button_release(zone, port); +} + +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 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 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 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 HuiPeripheral::bank_up() { + send_button(0x0A, 3); +} +void HuiPeripheral::bank_down() { + send_button(0x0A, 1); +} +void HuiPeripheral::channel_up() { + send_button(0x0A, 2); +} +void HuiPeripheral::channel_down() { + send_button(0x0A, 0); +} + + + +//PRIVATE FUNCTIONS + +void HuiPeripheral::HUI_sense() { + MidiUSB.sendMIDI({0x09, 0x90 | _MIDI_channel, 0, 0x7F}); + MidiUSB.flush(); +} + +void HuiPeripheral::write_rotary(uint8_t byte2, uint8_t 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 HuiPeripheral::HUI_button(uint8_t zone, uint8_t port, uint8_t 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/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