uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
Data Structures | Macros | Typedefs | Enumerations | Functions
midi.h File Reference

MIDI message transceiver. More...

#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include "gpio.h"
Include dependency graph for midi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  midi_event_handlers
 Pointers to MIDI message handling callbacks. More...
 
struct  midi_config
 MIDI transceiver module configuration. More...
 

Macros

#define BAUD_RATE   31250
 MIDI baudrate. More...
 
#define MIDI_UART   USARTE0
 UART used for MIDI I/O. More...
 
#define MIDI_CHANNEL_MASK   0x0f
 Bitmask for the channel in the MIDI status byte. More...
 
#define MIDI_MESSAGE_TYPE_MASK   0xf0
 Bitmask for the message type in the MIDI status byte. More...
 
#define MIDI_MAX_VALUE   127
 Maximum MIDI value. More...
 

Typedefs

typedef uint8_t midi_value_t
 Type for valid MIDI values [0..127]. More...
 

Enumerations

enum  midi_channel {
  MIDI_CHANNEL_01 = 0x00, MIDI_CHANNEL_02 = 0x01, MIDI_CHANNEL_03 = 0x02, MIDI_CHANNEL_04 = 0x03,
  MIDI_CHANNEL_05 = 0x04, MIDI_CHANNEL_06 = 0x05, MIDI_CHANNEL_07 = 0x06, MIDI_CHANNEL_08 = 0x07,
  MIDI_CHANNEL_09 = 0x08, MIDI_CHANNEL_10 = 0x09, MIDI_CHANNEL_11 = 0x0a, MIDI_CHANNEL_12 = 0x0b,
  MIDI_CHANNEL_13 = 0x0c, MIDI_CHANNEL_14 = 0x0d, MIDI_CHANNEL_15 = 0x0e, MIDI_CHANNEL_16 = 0x0f
}
 MIDI status byte values (second nibble) More...
 
enum  midi_message_type { MIDI_MSG_TYPE_CONTROL_CHANGE = 0xb0, MIDI_MSG_TYPE_NOTE_OFF = 0x80, MIDI_MSG_TYPE_NOTE_ON = 0x90, MIDI_MSG_TYPE_PROGRAM_CHANGE = 0xc0 }
 MIDI status byte values (first nibble) More...
 
enum  midi_state {
  MIDI_STATE_IDLE, MIDI_STATE_CONTROL_CHANGE_NUMBER, MIDI_STATE_CONTROL_CHANGE_VALUE, MIDI_STATE_NOTE_OFF_NUMBER,
  MIDI_STATE_NOTE_OFF_VALUE, MIDI_STATE_NOTE_ON_NUMBER, MIDI_STATE_NOTE_ON_VALUE, MIDI_STATE_PROGRAM_CHANGE
}
 States in the MIDI state machine. More...
 

Functions

static bool uart_is_ready (void)
 Checks if the UART is ready to send data. More...
 
static void uart_write (uint8_t data)
 Sends one byte of date over the UART. More...
 
void init_midi_module (const struct midi_config *config)
 Initializes the MIDI transceiver module. More...
 
enum midi_channel read_midi_channel_from_jumpers (const struct jumpers *jumpers)
 Reads the MIDI channel configuration from the solder jumpers. More...
 
void send_control_change (midi_value_t controller, midi_value_t value)
 Sends a control change message. More...
 
void send_midi_message (enum midi_channel channel, enum midi_message_type type, midi_value_t data0, midi_value_t data1)
 Sends an arbitrary MIDI message. More...
 
void send_note_off (midi_value_t note, midi_value_t velocity)
 Sends a note off message. More...
 
void send_note_on (midi_value_t note, midi_value_t velocity)
 Sends a note on message. More...
 
void send_program_change (midi_value_t program)
 Sends a program change message. More...
 
void set_midi_rx_channel (enum midi_channel channel)
 Updates the midi receive channel. More...
 
void set_midi_tx_channel (enum midi_channel channel)
 Updates the midi transmit channel. More...
 
void set_omni_mode (bool enable)
 Puts the device into Omni mode. More...
 

Detailed Description

MIDI message transceiver.

Macro Definition Documentation

◆ BAUD_RATE

#define BAUD_RATE   31250

MIDI baudrate.

◆ MIDI_CHANNEL_MASK

#define MIDI_CHANNEL_MASK   0x0f

Bitmask for the channel in the MIDI status byte.

◆ MIDI_MAX_VALUE

#define MIDI_MAX_VALUE   127

Maximum MIDI value.

◆ MIDI_MESSAGE_TYPE_MASK

#define MIDI_MESSAGE_TYPE_MASK   0xf0

Bitmask for the message type in the MIDI status byte.

◆ MIDI_UART

#define MIDI_UART   USARTE0

UART used for MIDI I/O.

Typedef Documentation

◆ midi_value_t

typedef uint8_t midi_value_t

Type for valid MIDI values [0..127].

Enumeration Type Documentation

◆ midi_channel

MIDI status byte values (second nibble)

Enumerator
MIDI_CHANNEL_01 

MIDI channel 1.

MIDI_CHANNEL_02 

MIDI channel 2.

MIDI_CHANNEL_03 

MIDI channel 3.

MIDI_CHANNEL_04 

MIDI channel 4.

MIDI_CHANNEL_05 

MIDI channel 5.

MIDI_CHANNEL_06 

MIDI channel 6.

MIDI_CHANNEL_07 

MIDI channel 7.

MIDI_CHANNEL_08 

MIDI channel 8.

MIDI_CHANNEL_09 

MIDI channel 9.

MIDI_CHANNEL_10 

MIDI channel 10.

MIDI_CHANNEL_11 

MIDI channel 11.

MIDI_CHANNEL_12 

MIDI channel 12.

MIDI_CHANNEL_13 

MIDI channel 13.

MIDI_CHANNEL_14 

MIDI channel 14.

MIDI_CHANNEL_15 

MIDI channel 15.

MIDI_CHANNEL_16 

MIDI channel 16.

◆ midi_message_type

MIDI status byte values (first nibble)

Enumerator
MIDI_MSG_TYPE_CONTROL_CHANGE 

Control change message.

MIDI_MSG_TYPE_NOTE_OFF 

Note off message.

MIDI_MSG_TYPE_NOTE_ON 

Note on message.

MIDI_MSG_TYPE_PROGRAM_CHANGE 

Program change message.

◆ midi_state

enum midi_state

States in the MIDI state machine.

Enumerator
MIDI_STATE_IDLE 

No pending MIDI message bytes.

MIDI_STATE_CONTROL_CHANGE_NUMBER 

Control change message status byte read; awaiting controller number.

MIDI_STATE_CONTROL_CHANGE_VALUE 

Control change message controller number read; awaiting value.

MIDI_STATE_NOTE_OFF_NUMBER 

Note off message status byte read; awaiting note number.

MIDI_STATE_NOTE_OFF_VALUE 

Note off message note number read; awaiting velocity.

MIDI_STATE_NOTE_ON_NUMBER 

Note on message status byte read; awaiting note number.

MIDI_STATE_NOTE_ON_VALUE 

Note on message note number read; awaiting velocity.

MIDI_STATE_PROGRAM_CHANGE 

Program change message status byte read.

Function Documentation

◆ init_midi_module()

void init_midi_module ( const struct midi_config config)

Initializes the MIDI transceiver module.

Configures the UART, enables the RXC interrupt and registers MIDI message handlers.

Parameters
configthe MIDI module configuration to apply

◆ read_midi_channel_from_jumpers()

enum midi_channel read_midi_channel_from_jumpers ( const struct jumpers jumpers)

Reads the MIDI channel configuration from the solder jumpers.

Parameters
jumpersthe the solder jumpers configuration struct
Returns
the selected MIDI channel

◆ send_control_change()

void send_control_change ( midi_value_t  controller,
midi_value_t  value 
)

Sends a control change message.

Parameters
controllerthe MIDI controller number
valuethe new value for the controller

◆ send_midi_message()

void send_midi_message ( enum midi_channel  channel,
enum midi_message_type  type,
midi_value_t  data0,
midi_value_t  data1 
)

Sends an arbitrary MIDI message.

If the second data byte is not required for the specified message type, the parameter is simply ignored.

Parameters
channelthe MIDI channel to send on
typethe type of the MIDI message to send
data0the first data byte (e.g. MIDI controller number for a control change)
data1the second data byte (optional, e.g. value for a control change)

◆ send_note_off()

void send_note_off ( midi_value_t  note,
midi_value_t  velocity 
)

Sends a note off message.

Parameters
notethe MIDI note number
velocityvelocity of the key press

◆ send_note_on()

void send_note_on ( midi_value_t  note,
midi_value_t  velocity 
)

Sends a note on message.

Parameters
notethe MIDI note number
velocityvelocity of the key press

◆ send_program_change()

void send_program_change ( midi_value_t  program)

Sends a program change message.

Parameters
programthe new MIDI program to load

◆ set_midi_rx_channel()

void set_midi_rx_channel ( enum midi_channel  channel)

Updates the midi receive channel.

Parameters
channelthe channel to listen on

◆ set_midi_tx_channel()

void set_midi_tx_channel ( enum midi_channel  channel)

Updates the midi transmit channel.

Parameters
channelthe channel to talk on

◆ set_omni_mode()

void set_omni_mode ( bool  enable)

Puts the device into Omni mode.

In Omni mode the device handles all received MIDI messages regardless of the channel. When not in Omni mode, only messages on the configured receive channel are processed.

Parameters
enableif true, Omni mode is enabled; if false, it is disabled

◆ uart_is_ready()

static bool uart_is_ready ( void  )
inlinestatic

Checks if the UART is ready to send data.

Returns
true if the UART is ready; false otherwise

◆ uart_write()

static void uart_write ( uint8_t  data)
inlinestatic

Sends one byte of date over the UART.

Parameters
datathe data byte to send