Commit 02893776 authored by Jacob Vosmaer's avatar Jacob Vosmaer

More refactor

parent e2945671
......@@ -101,14 +101,14 @@ test_program_change(void)
assert_status(MIDI_PROGRAM_CHANGE, 1, 0x1200);
// a little silly, running status for program change messages
midi_parse(0);
assert_status(MIDI_PROGRAM_CHANGE, 1, 0);
midi_parse(3);
assert_status(MIDI_PROGRAM_CHANGE, 1, 0x0300);
// throw in a different message
midi_parse(0x80); // note off
midi_parse(0x12);
midi_parse(0);
assert_status(MIDI_NOTE_OFF, 1, 0x1200);
midi_parse(0x56);
assert_status(MIDI_NOTE_OFF, 1, 0x1256);
midi_parse(0xc0);
midi_parse(0x34);
......
......@@ -6,11 +6,17 @@
// Access to midi.c internals
void status_init(void);
void midi_parse(uint8_t);
uint8_t midi_available(void);
uint8_t midi_load(uint8_t *status, uint8_t *data1, uint8_t *data2);
#define assert_status(status, avail, data) assert(midi_status() == status); \
assert(!midi_available() == !avail); \
if (avail) assert(midi_data() == data)
#define assert_status(status, avail, data) { \
uint8_t _status, _avail, _data1, _data2; \
_avail = midi_load(&_status, &_data1, &_data2); \
assert((_avail != MIDI_READ_OK) == !avail); \
if (_avail == MIDI_READ_OK) { \
assert(((_data1 << 8) | _data2) == data); \
assert(_status == status); \
} \
}
// Fake uart functions to let midi.c compile
void uart_init(void) { }
......
......@@ -8,6 +8,7 @@ UART I/O and MIDI parsing.
#include "midi.h"
#include "uart.h"
#include <string.h>
uint8_t midi_channel;
......@@ -18,7 +19,12 @@ midi_set_channel(uint8_t channel)
}
uint8_t status;
uint16_t data;
enum {
MIDI_MAX_DATA_LEN = 2,
};
uint8_t data[MIDI_MAX_DATA_LEN];
uint8_t parsed_len;
const uint8_t statuses[MIDI_NUM_STATUSES] = {
......@@ -43,7 +49,7 @@ void
clear_data()
{
parsed_len = 0;
data = 0;
memset(data, 0, sizeof(data)); // TODO why is this necessary?
}
uint8_t
......@@ -52,13 +58,6 @@ midi_status(void)
return status;
}
uint16_t midi_data(void)
{
uint16_t result = data;
clear_data();
return result;
}
uint8_t
midi_available(void)
{
......@@ -103,16 +102,7 @@ midi_parse(uint8_t midi_byte)
clear_data();
}
switch (parsed_len) {
case 0:
data = midi_byte << 8;
parsed_len++;
break;
case 1:
data |= midi_byte;
parsed_len++;
break;
}
data[parsed_len++] = midi_byte;
}
void
......@@ -130,30 +120,30 @@ midi_init()
}
uint8_t
midi_read(void)
midi_load(uint8_t *status, uint8_t *data1, uint8_t *data2)
{
uint16_t uart_data = uart_read();
if (uart_data >> 8) {
// uart error
return 0;
if (!midi_available()) {
return MIDI_READ_NO_DATA;
}
midi_parse(uart_data & 0xff);
return midi_available();
*status = midi_status();
*data1 = data[0];
*data2 = data[1];
clear_data();
return MIDI_READ_OK;
}
uint8_t
midi_read_2(uint8_t *status, uint8_t *data1, uint8_t *data2)
{
if (!midi_read()) {
return MIDI_READ_NO_DATA;
uint16_t uart_data = uart_read();
if (uart_data >> 8) {
// uart error
return MIDI_READ_NO_DATA;
}
*status = midi_status();
uint16_t data_16 = midi_data();
*data1 = data_16 >> 8;
*data2 = data_16 & 0xff;
return MIDI_READ_OK;
}
midi_parse(uart_data & 0xff);
return midi_load(status, data1, data2);
}
......@@ -18,11 +18,8 @@ enum _cc {
CC_VIB_RATE = 76,
};
uint8_t midi_read(void);
void midi_init(void);
void midi_set_channel(uint8_t channel);
uint8_t midi_status(void);
uint16_t midi_data(void);
enum {
MIDI_READ_OK,
......
Markdown is supported
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