giuliomoro@181: /* giuliomoro@181: * Midi.cpp giuliomoro@181: * giuliomoro@181: * Created on: 15 Jan 2016 giuliomoro@181: * Author: giulio giuliomoro@181: */ giuliomoro@181: giuliomoro@181: #include "Midi.h" giuliomoro@181: #include giuliomoro@181: #include giuliomoro@181: giuliomoro@181: bool Midi::staticConstructed; giuliomoro@181: AuxiliaryTask Midi::midiInputTask; giuliomoro@181: AuxiliaryTask Midi::midiOutputTask; giuliomoro@181: std::vector Midi::objAddrs(0); giuliomoro@181: giuliomoro@181: Midi::Midi(){ giuliomoro@181: outputPort = -1; giuliomoro@181: inputPort = -1; giuliomoro@181: size_t inputBytesInitialSize = 1000; giuliomoro@181: inputBytes.resize(inputBytesInitialSize); giuliomoro@181: inputBytesWritePointer = 0; giuliomoro@181: inputBytesReadPointer = inputBytes.size() - 1; giuliomoro@181: if(!staticConstructed){ giuliomoro@181: staticConstructor(); giuliomoro@181: } giuliomoro@181: } giuliomoro@181: giuliomoro@181: void Midi::staticConstructor(){ giuliomoro@181: staticConstructed = true; giuliomoro@181: midiInputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiInput"); giuliomoro@181: midiOutputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiOutupt"); giuliomoro@181: } giuliomoro@181: giuliomoro@181: Midi::~Midi(){} giuliomoro@181: giuliomoro@181: void Midi::midiInputLoop(){ giuliomoro@181: printf("Midi input loop %d\n", objAddrs.size()); giuliomoro@181: for(unsigned int n = 0; n < objAddrs.size(); n++){ giuliomoro@181: printf("In the for\n"); giuliomoro@181: objAddrs[n] -> readInputLoop(); giuliomoro@181: } giuliomoro@181: } giuliomoro@181: giuliomoro@181: void Midi::readInputLoop(){ giuliomoro@181: while(!gShouldStop){ giuliomoro@181: int maxBytesToRead = inputBytes.size() - inputBytesWritePointer; giuliomoro@181: int ret = read(inputPort, &inputBytes[inputBytesWritePointer], sizeof(midi_byte_t)*maxBytesToRead); giuliomoro@181: static int count = 0; giuliomoro@181: count++; giuliomoro@181: if(ret < 0){ giuliomoro@181: if(errno != EAGAIN){ // read() would return EAGAIN when no data are available to read just now giuliomoro@181: rt_printf("Error while reading midi %d\n", errno); giuliomoro@181: } giuliomoro@181: usleep(1000); giuliomoro@181: continue; giuliomoro@181: } giuliomoro@181: inputBytesWritePointer += ret; giuliomoro@181: if(inputBytesWritePointer == inputBytes.size()){ //wrap pointer around giuliomoro@181: inputBytesWritePointer = 0; giuliomoro@181: } giuliomoro@181: if(ret < maxBytesToRead){ //no more data to retrieve at the moment giuliomoro@181: usleep(1000); giuliomoro@181: } // otherwise there might be more data ready to be read, so don't sleep giuliomoro@181: } giuliomoro@181: } giuliomoro@181: giuliomoro@181: int Midi::readFrom(int port){ giuliomoro@181: objAddrs.push_back(this); giuliomoro@181: inputPort = open("/dev/midi1", O_RDONLY | O_NONBLOCK | O_NOCTTY); giuliomoro@181: if(inputPort < 0){ giuliomoro@181: printf("Error occurred while opening midi port %d: %d", port, inputPort); giuliomoro@181: return -1; giuliomoro@181: } else { giuliomoro@181: printf("Reading from port %d\n", inputPort); giuliomoro@181: BeagleRT_scheduleAuxiliaryTask(midiInputTask); giuliomoro@181: return 1; giuliomoro@181: } giuliomoro@181: } giuliomoro@181: giuliomoro@181: int Midi::getInput(){ giuliomoro@181: if(inputPort < 0) giuliomoro@181: return -2; giuliomoro@181: if(inputBytesReadPointer == inputBytesWritePointer){ giuliomoro@181: return -1; // no bytes to read giuliomoro@181: } giuliomoro@181: midi_byte_t inputMessage = inputBytes[inputBytesReadPointer++]; giuliomoro@181: if(inputBytesReadPointer == inputBytes.size()){ // wrap pointer giuliomoro@181: inputBytesReadPointer = 0; giuliomoro@181: } giuliomoro@181: return inputMessage; giuliomoro@181: }