#include "Launchpad.h" #include namespace midi { Launchpad::Launchpad() : midiin(), midiout(), keyPressedCallback(0), keyReleasedCallback(0) { // define callback before opening port to keep the message queue empty. midiin.setCallback(midiMessageCallback, (void*) this); for(unsigned int i=0; i(&message); if(noteMessage) { unsigned char x = noteMessage->pitch % 16; unsigned char y = 7 - noteMessage->pitch / 16; if(dynamic_cast(noteMessage)) { launchpad->keyPressed(x, y); } else { launchpad->keyReleased(x, y); } } else { ControlChangeMessage *controlChangeMessage = dynamic_cast(&message); if(controlChangeMessage) { unsigned char x = controlChangeMessage->control - 104; unsigned char y = 8; if(controlChangeMessage->value == 127) { launchpad->keyPressed(x, y); } else { launchpad->keyReleased(x, y); } } } } bool Launchpad::issetColor(unsigned char x, unsigned char y) const { return colorSet[x][y]; } const LaunchpadColor& Launchpad::getColor(unsigned char x, unsigned char y) const { return colors[x][y]; } void Launchpad::setColor(unsigned char x, unsigned char y, const LaunchpadColor& color) { if(x >= width || y >= height || (x == 8 && y == 8)) { throw "specified launchpad position is out of range"; } if(color.red > 3 || color.green > 3) { throw "specified launchpad color is out of range"; } unsigned char velocity = (color.green << 4) + color.red; if(y == 8) { midiout.sendMessage(ControlChangeMessage( 0, 104 + x, velocity )); } else { midiout.sendMessage(NoteOnMessage( 0, ((7 - y) << 4) + x, velocity )); } colors[x][y] = color; colorSet[x][y] = true; } void Launchpad::setColorAll(const LaunchpadColor& color) { for(unsigned char x = 0; x < width; x++) { for(unsigned char y = 0; y < height; y++) { if(x != 8 || y != 8) { setColor(x, y, color); } } } } void Launchpad::keyPressed(unsigned char x, unsigned char y) { if(keyPressedCallback) { keyPressedCallback(x, y, keyEventCallbackData); } } void Launchpad::keyReleased(unsigned char x, unsigned char y) { if(keyReleasedCallback) { keyReleasedCallback(x, y, keyEventCallbackData); } } } // namespace