uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
midi.h
Go to the documentation of this file.
1 
4 /*
5  * Copyright 2012-2015 Sebastian Neuser
6  *
7  * This file is part of the uMIDI firmware.
8  *
9  * The uMIDI firmware is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * The uMIDI firmware is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with the uMIDI firmware. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef _MIDI_H
24 #define _MIDI_H
25 
26 
27 //---------------- includes ----------------//
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <avr/io.h>
31 
32 #include "gpio.h"
33 
34 
35 //---------------- constants ----------------//
36 
38 #define BAUD_RATE 31250
39 
41 #define MIDI_UART USARTE0
42 
44 #define MIDI_CHANNEL_MASK 0x0f
45 
47 #define MIDI_MESSAGE_TYPE_MASK 0xf0
48 
50 #define MIDI_MAX_VALUE 127
51 
52 
53 //---------------- data types ----------------//
54 
56 typedef uint8_t midi_value_t;
57 
60 {
61  MIDI_CHANNEL_01 = 0x00,
62  MIDI_CHANNEL_02 = 0x01,
63  MIDI_CHANNEL_03 = 0x02,
64  MIDI_CHANNEL_04 = 0x03,
65  MIDI_CHANNEL_05 = 0x04,
66  MIDI_CHANNEL_06 = 0x05,
67  MIDI_CHANNEL_07 = 0x06,
68  MIDI_CHANNEL_08 = 0x07,
69  MIDI_CHANNEL_09 = 0x08,
70  MIDI_CHANNEL_10 = 0x09,
71  MIDI_CHANNEL_11 = 0x0a,
72  MIDI_CHANNEL_12 = 0x0b,
73  MIDI_CHANNEL_13 = 0x0c,
74  MIDI_CHANNEL_14 = 0x0d,
75  MIDI_CHANNEL_15 = 0x0e,
77 };
78 
81 {
86 };
87 
90 {
99 };
100 
106 {
107  void (*control_change)(midi_value_t controller, midi_value_t value);
113 
114  void (*note_off)(midi_value_t note, midi_value_t velocity);
120 
121  void (*note_on)(midi_value_t note, midi_value_t velocity);
127 
132 };
133 
136 {
140  bool omni_mode;
141  bool signal_rx;
142 };
144 
145 
146 //---------------- functions and procedures ----------------//
147 
150 static inline bool uart_is_ready(void) {
151  return MIDI_UART.STATUS & USART_DREIF_bm;
152 }
153 
157 static inline void uart_write(uint8_t data) {
158  while (!uart_is_ready());
159  MIDI_UART.DATA = data;
160 }
161 
166 void init_midi_module(const struct midi_config* config);
167 
173 
179 void send_control_change(midi_value_t controller, midi_value_t value);
180 
192 void send_midi_message(enum midi_channel channel, enum midi_message_type type, midi_value_t data0, midi_value_t data1);
193 
199 void send_note_off(midi_value_t note, midi_value_t velocity);
200 
206 void send_note_on(midi_value_t note, midi_value_t velocity);
207 
212 
216 void set_midi_rx_channel(enum midi_channel channel);
217 
221 void set_midi_tx_channel(enum midi_channel channel);
222 
229 void set_omni_mode(bool enable);
230 
231 
232 //---------------- EOF ----------------//
233 #endif // _MIDI_H
Note on message note number read; awaiting velocity.
Definition: midi.h:97
static bool uart_is_ready(void)
Checks if the UART is ready to send data.
Definition: midi.h:150
void set_midi_tx_channel(enum midi_channel channel)
Updates the midi transmit channel.
Definition: midi.c:136
midi_state
States in the MIDI state machine.
Definition: midi.h:89
midi_message_type
MIDI status byte values (first nibble)
Definition: midi.h:80
void send_note_off(midi_value_t note, midi_value_t velocity)
Sends a note off message.
Definition: midi.c:116
MIDI channel 10.
Definition: midi.h:70
void send_program_change(midi_value_t program)
Sends a program change message.
Definition: midi.c:126
static enum midi_channel tx_channel
The MIDI transmit channel.
Definition: midi.c:53
uint8_t midi_value_t
Type for valid MIDI values [0..127].
Definition: midi.h:56
void(* control_change)(midi_value_t controller, midi_value_t value)
Definition: midi.h:107
MIDI channel 2.
Definition: midi.h:62
MIDI channel 8.
Definition: midi.h:68
MIDI transceiver module configuration.
Definition: midi.h:135
MIDI channel 9.
Definition: midi.h:69
GPIO configuration and service functions.
#define MIDI_UART
UART used for MIDI I/O.
Definition: midi.h:41
Control change message status byte read; awaiting controller number.
Definition: midi.h:92
This struct represents the four solder jumpers on the bottom side of the PCB.
Definition: gpio.h:86
bool omni_mode
Setting this flag enables Omni mode.
Definition: midi.h:140
Note on message.
Definition: midi.h:84
static struct midi_event_handlers * event_handlers
The MIDI event handlers.
Definition: midi.c:38
void set_omni_mode(bool enable)
Puts the device into Omni mode.
Definition: midi.c:141
MIDI channel 13.
Definition: midi.h:73
Data structure for program information.
Definition: program.h:64
static enum midi_channel rx_channel
The MIDI receive channel.
Definition: midi.c:49
Program change message status byte read.
Definition: midi.h:98
void init_midi_module(const struct midi_config *config)
Initializes the MIDI transceiver module.
Definition: midi.c:65
MIDI channel 12.
Definition: midi.h:72
No pending MIDI message bytes.
Definition: midi.h:91
void(* note_off)(midi_value_t note, midi_value_t velocity)
Definition: midi.h:114
MIDI channel 11.
Definition: midi.h:71
void(* program_change)(midi_value_t program)
Definition: midi.h:128
MIDI channel 6.
Definition: midi.h:66
Note off message status byte read; awaiting note number.
Definition: midi.h:94
MIDI channel 16.
Definition: midi.h:76
void send_note_on(midi_value_t note, midi_value_t velocity)
Sends a note on message.
Definition: midi.c:121
bool signal_rx
Definition: midi.h:141
void set_midi_rx_channel(enum midi_channel channel)
Updates the midi receive channel.
Definition: midi.c:131
Note off message.
Definition: midi.h:83
Program change message.
Definition: midi.h:85
MIDI channel 7.
Definition: midi.h:67
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.
Definition: midi.c:102
static void uart_write(uint8_t data)
Sends one byte of date over the UART.
Definition: midi.h:157
MIDI channel 1.
Definition: midi.h:61
Note off message note number read; awaiting velocity.
Definition: midi.h:95
Pointers to MIDI message handling callbacks.
Definition: midi.h:105
Control change message controller number read; awaiting value.
Definition: midi.h:93
Control change message.
Definition: midi.h:82
void send_control_change(midi_value_t controller, midi_value_t value)
Sends a control change message.
Definition: midi.c:97
void(* note_on)(midi_value_t note, midi_value_t velocity)
Definition: midi.h:121
MIDI channel 4.
Definition: midi.h:64
midi_channel
MIDI status byte values (second nibble)
Definition: midi.h:59
MIDI channel 15.
Definition: midi.h:75
enum midi_channel read_midi_channel_from_jumpers(const struct jumpers *jumpers)
Reads the MIDI channel configuration from the solder jumpers.
Definition: midi.c:89
MIDI channel 14.
Definition: midi.h:74
MIDI channel 3.
Definition: midi.h:63
Note on message status byte read; awaiting note number.
Definition: midi.h:96
MIDI channel 5.
Definition: midi.h:65