Skip to content

Instantly share code, notes, and snippets.

@boxysean
Created December 6, 2013 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxysean/7829065 to your computer and use it in GitHub Desktop.
Save boxysean/7829065 to your computer and use it in GitHub Desktop.
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);
bSendSerialMessage = false;
ofBackground(255);
ofSetLogLevel(OF_LOG_ERROR);
font.loadFont("DIN.otf", 64);
serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 115200;
serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
//serial.setup("/dev/tty.usbserial-A4001JEC", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example
nTimesRead = 0;
nBytesRead = 0;
readTime = 0;
memset(bytesReadString, 0, 4);
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 3; j++) {
rgb[i][j] = 0;
}
}
channelLightMap[0] = 1;
channelLightMap[1] = 1;
channelLightMap[2] = 1;
channelLightMap[3] = 1;
channelLightMap[4] = 0;
channelLightMap[5] = 0;
channelLightMap[6] = 0;
channelLightMap[7] = 0;
channelLightMap[8] = 2;
channelLightMap[9] = 2;
channelLightMap[10] = 2;
channelLightMap[11] = 2;
channelLightMap[12] = 3;
channelLightMap[13] = 3;
channelLightMap[14] = 3;
channelLightMap[15] = 3;
channelLightIdxMap[0] = -1;
channelLightIdxMap[1] = 2;
channelLightIdxMap[2] = 0;
channelLightIdxMap[3] = 1;
channelLightIdxMap[4] = -1;
channelLightIdxMap[5] = 2;
channelLightIdxMap[6] = 0;
channelLightIdxMap[7] = 1;
channelLightIdxMap[8] = -1;
channelLightIdxMap[9] = 2;
channelLightIdxMap[10] = 0;
channelLightIdxMap[11] = 1;
channelLightIdxMap[12] = -1;
channelLightIdxMap[13] = 2;
channelLightIdxMap[14] = 0;
channelLightIdxMap[15] = 1;
steps = 256;
stepIdx = 0;
}
void testApp::writeInt(int x) {
serial.writeByte(x & 0xFF);
serial.writeByte((x >> 8) & 0xFF);
}
void testApp::Wheel(float WheelPos, int idx) {
if(WheelPos < 1.0/3) {
float r = WheelPos * 3;
float g = 1.0 - WheelPos * 3;
float b = 0;
rgb[idx][0] = (int) (r * MAX);
rgb[idx][1] = (int) (g * MAX);
rgb[idx][2] = (int) (b * MAX);
} else if(WheelPos < 2.0/3) {
WheelPos -= 1.0/3;
float r = 1.0 - WheelPos * 3;
float g = 0;
float b = WheelPos * 3;
rgb[idx][0] = (int) (r * MAX);
rgb[idx][1] = (int) (g * MAX);
rgb[idx][2] = (int) (b * MAX);
} else {
WheelPos -= 2.0/3;
float r = 0;
float g = WheelPos * 3;
float b = 1.0 - WheelPos * 3;
rgb[idx][0] = (int) (r * MAX);
rgb[idx][1] = (int) (g * MAX);
rgb[idx][2] = (int) (b * MAX);
}
}
void testApp::tick() {
for (int i = 0; i < 4; i++) {
float pos = (float) stepIdx / steps;
pos += 0.05 * i;
if (pos > 1) {
pos -= 1;
}
Wheel(pos, i);
}
if (++stepIdx == steps) {
stepIdx = 0;
}
}
void testApp::paint() {
for (int i = 0; i < 16; i++) {
int light = channelLightMap[i];
int lightIdx = channelLightIdxMap[i];
int val = lightIdx >= 0 ? rgb[light][lightIdx] : 0;
writeInt(val);
printf("%3d ", val);
}
printf("\n");
serial.flush();
}
//--------------------------------------------------------------
void testApp::update(){
/* tick();
paint();*/
}
//--------------------------------------------------------------
void testApp::draw(){
tick();
paint();
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
bSendSerialMessage = true;
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment