Hi all,
I'm trying to write my own MIDI library for the Arduino.
Now I seem to have com across a strange problem. Apparently I'm able to call the begin() method of my object, but when I try to call read() I get an error telling me there is no reference to that method...
o: In function `loop':
E:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\build52696.tmp/Temporary_8968_8700.cpp:17: undefined reference to `MIDI::read()'Basically I can call methods that don't return anything, but I can't call methods with a return value.
code:
MIDI.h:
#ifndef MIDI_h
#define MIDI_h
#include "WConstants.h"
struct Midimsg {
byte status;
byte data1;
byte data2;
};
class MIDI {
public:
MIDI();
void begin();
bool read();
void send(byte status, byte data1, byte data2);
void send(byte status, byte data1);
void send(byte status);
byte getStatus();
byte getData1();
byte getData2();
byte getChannelFromStatus(byte status);
int getMessageSize(byte status);
private:
Midimsg msg;
byte getNextByte();
void clearMessage();
};
#endif
MIDI.cpp:
#include "WProgram.h"
#include "MIDI.h"
#include "HardwareSerial.h"
MIDI::MIDI() { }
void MIDI::begin() {
Serial.begin(31250);
}
bool MIDI::read() {
clearMessage();
if (Serial.available() > 0) {
byte sts = Serial.read();
int size = getMessageSize(sts);
switch (format) {
case 1: {
msg.status = sts;
msg.data1 = 0;
msg.data2 = 0;
}
return true;
case 2: {
byte dat1 = getNextByte();
if (dat1 != -1) {
msg.status = sts;
msg.data1 = dat1;
msg.data2 = 0;
}
}
return true;
case 3: {
byte dat1 = getNextByte();
if (dat1 != -1) {
byte dat2 = getNextByte();
if (dat2 != -1) {
msg.status = sts;
msg.data1 = dat1;
msg.data2 = dat2;
}
}
}
return true;
case -1:
return false; // invalid status byte
}
}
else {
return false;
}
return false;
}
void MIDI::send(byte status, byte data1, byte data2) {
Serial.print(status, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
void MIDI::send(byte status, byte data1) {
Serial.print(status, BYTE);
Serial.print(data1, BYTE);
}
void MIDI::send(byte status) {
Serial.print(status, BYTE);
}
byte MIDI::getStatus() {
return msg.status;
}
byte MIDI::getData1() {
return msg.data1;
}
byte MIDI::getData2() {
return msg.data2;
}
byte MIDI::getChannelFromStatus(byte status) {
return (status & 0x0F);
}
int MIDI::getMessageSize(byte status) {
if (status > 0xF5) { // 1 byte msg
return 1;
}
else if ((status > 0xC0 && status < 0xE0) || (status == 0xF3)) { // 2 byte msg
return 2;
}
else if ((status > 0x7F && status < 0xC0) || (status > 0xDF && status < 0xF0)) { // 3 byte msg
return 3;
}
else {
return -1; // invalid/unsupported message
}
}
byte MIDI::getNextByte() {
int i = 3;
while ((Serial.available() < 1) && (i > 0)) { // wait for the 2nd byte if it isn't already available
delayMicroseconds(10);
i--;
}
return Serial.read();
}
void MIDI::clearMessage() {
msg.status = 0;
msg.data1 = 0;
msg.data2 = 0;
}
I'm using version 0015 and experienced the same problem with v0013.
Great thanks in advance.