inoControl.cpp 2.25 KB
#include "inoControl.h"
#include "ofMain.h"

string inoControl::defaultSettings = "settings-dev.xml";

inoControl::inoControl(string device) {
  connect.addListener(this, &inoControl::toggleConnection);
  reset.addListener(this, &inoControl::resetSettings);
  gui.setup(device);
  gui.add(status.setup("Status", "Setup"));
  gui.add(connect.setup("Connect", false));
  gui.add(hide.setup("Hide", false));
  gui.add(settings.setup("Settings"));
  settings.add(reset.setup("Reset")); 
  settings.add(color.setup("Color",
        ofColor(100, 100, 140),
        ofColor(0, 0), ofColor(255, 255)));
  settings.add(offset.setup("Offset", ofVec2f(0,0)
        , ofVec2f(-0.5,-0.5), ofVec2f(0.5,0.5)));

  filePath = device;
  ofStringReplace(filePath, "/", "_");
  filePath = "devices/" + filePath + ".xml";

  if(!ofFile::doesFileExist(filePath)) {
    ofFile::copyFromTo(inoControl::defaultSettings, filePath);
  }
  settings.loadFromFile(filePath);

  buffer = new unsigned char[BUFFER_SIZE];

  channel.setup(device);
  ofAddListener(
      channel.statusChange, 
      this, 
      &inoControl::onStatusChanged);
}

inoControl::~inoControl() {
  channel.exit();
  channel.waitForThread();
  settings.saveToFile(filePath);
  delete[] buffer;
}

ofxGuiGroup * inoControl::getGui() {
  return &gui;
}

void inoControl::toggleConnection(bool & isSet) {
  string statusStr = status;
  cout << "Current Status: " << statusStr << endl;
  if(isSet) {
    if(statusStr == "Setup") {
      channel.startThread();
    }
    channel.startStream();
  } else {
    channel.stopStream();
  }
}

void inoControl::onStatusChanged(string & str) {
  status = str;
}

void inoControl::drawWave(float zoom) {
  if(hide) return;

  int width = ofGetViewportWidth();
  int height = ofGetViewportHeight();

  ofPushMatrix();
  
  ofTranslate(offset->x * (float)width,
      offset->y * (float)width);
  ofScale(zoom, 1);

  ofSetColor(color);
  ofSetPolyMode(OF_POLY_WINDING_ODD);
  ofNoFill();
  ofSetLineWidth(1.5);
  ofBeginShape();
  for(int i = 0; i <= BUFFER_SIZE; i++) {
    ofVertex((float)i/BUFFER_SIZE * (float)width,
        300 + sin(8* M_PI* (double)i/BUFFER_SIZE) * 100);
  }
  ofEndShape(false);

  ofPopMatrix();
}

void inoControl::resetSettings() {
  settings.loadFromFile(inoControl::defaultSettings);
}