Skip to content

Instantly share code, notes, and snippets.

@kitschpatrol
Created April 12, 2011 03:44
Show Gist options
  • Save kitschpatrol/914877 to your computer and use it in GitHub Desktop.
Save kitschpatrol/914877 to your computer and use it in GitHub Desktop.
#include "controlPanel.h"
controlPanel::controlPanel() {
// cout << "setting up control panel" << endl;
// Set up serial
serial.enumerateDevices();
serial.setup("/dev/tty.usbserial-A900aeQz", 115200);
bytesReceived = 0;
ageOfDeath = 0;
masterVolume = 1;
encoderPosition = 0;
}
void controlPanel::update() {
updateSerial();
aPressed = (!aWasDown && aDown) ? true : false;
aReleased = (aWasDown && !aDown) ? true : false;
bPressed = (!bWasDown && bDown) ? true : false;
bReleased = (bWasDown && !bDown) ? true : false;
cPressed = (!cWasDown && cDown) ? true : false;
cReleased = (cWasDown && !cDown) ? true : false;
dPressed = (!dWasDown && dDown) ? true : false;
dReleased = (dWasDown && !dDown) ? true : false;
aWasDown = aDown;
bWasDown = bDown;
cWasDown = cDown;
dWasDown = dDown;
// Processing
ageOfDeath = ofMap(pot2, 0, 255, 1, 2000);
if(pot2 == 255) ageOfDeath = 0; // special case for eternal life
mappedMicSlider = ofMap(micSlider, 0, 255, 0, 50, true);
mappedVhsSlider = ofMap(vhsSlider, 0, 255, 0, 10, true);
mappedMixSlider = ofMap(mixSlider, 0, 255, 0, 10, true);
masterVolume = ofMap(pot1, 0, 255, 0, 1, true);
}
void controlPanel::start() {
requestPacket();
}
void controlPanel::requestPacket() {
serial.writeByte(micLevel);
serial.writeByte(vhsLevel);
serial.writeByte(mixLevel);
}
void controlPanel::updateSerial() {
if (bytesReceived < PACKET_SIZE) {
// check for data
if (serial.available() > 0) {
//cout << serial.available() << " bytes available" << endl;
// try to read - note offset into the bytes[] array, this is so
// that we don't overwrite the bytes we already have
int packetOffset = bytesReceived;
int result = serial.readBytes( &packet[packetOffset], PACKET_SIZE - bytesReceived);
// check for error code
if (result == OF_SERIAL_ERROR) {
// something bad happened
ofLog( OF_LOG_ERROR, "unrecoverable error reading from serial" );
}
else if ( result == OF_SERIAL_NO_DATA ) {
// nothing was read, try again
}
else {
// we read some data!
bytesReceived += result;
}
}
}
else {
//cout << "got packet!" << endl;
parsePacket();
bytesReceived = 0;
// get another one
requestPacket();
}
}
void controlPanel::parsePacket() {
micSlider = (int)packet[0];
vhsSlider = (int)packet[1];
mixSlider = (int)packet[2];
pot1 = (int)packet[3];
pot2 = (int)packet[4];
encoderPosition = (int)packet[5];
// unpack the buttons
buttons = packet[6];
aDown = (getBit(buttons, 7) == 1) ? true : false;
bDown = (getBit(buttons, 6) == 1) ? true : false;
cDown = (getBit(buttons, 5) == 1) ? true : false;
dDown = (getBit(buttons, 4) == 1) ? true : false;
toggle1 = (getBit(buttons, 3) == 1) ? true : false;
toggle2 = (getBit(buttons, 2) == 1) ? true : false;
}
int controlPanel::getBit(unsigned char data, int i) {
return (data >> i) & 0x01;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment