#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofEnableSmoothing();
    ofEnableAlphaBlending();
    ofSetVerticalSync(true);
    ofSetFrameRate(60);
    ofBackground(0, 0, 0, 0);
    
    mouseIsPressed = false;
    int numSamples = 350;
    
    plot = new ofxHistoryPlot( NULL, "audio", numSamples, false);
    plot->setRange(0, ofGetHeight());
    plot->addHorizontalGuide(ofGetHeight()/2, ofColor(255,0,0));
    plot->setColor( ofColor(255,255,255) );
    plot->setShowNumericalInfo(true);
    plot->setRespectBorders(true);
    plot->setLineWidth(3);
    plot->setBackgroundColor(ofColor(0,220)); //custom bg color
   
    plot->setDrawGrid(false);
    //plot->setGridColor(ofColor(30)); //grid lines color
    //plot->setGridUnit(14);
    //plot->setCropToRect(true);
    
    
    int baud = 115200;
    serial.setup("/dev/cu.usbmodem14101", baud);
//
//    

    byte1 = 0;
    byte2 = 0;
    byte3 = 0;
    byte4 = 0;
    
  
}

//--------------------------------------------------------------
void ofApp::update(){
    
    currentFrameRate = ofGetFrameRate();
    if (serial.isInitialized()) {
        while(serial.available() > 2) {
            byte1 = byte2;
            byte2 = byte3;
            byte3 = byte4;
            byte4 = serial.readByte();
            
            if( (byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)) {
                result = serial.readBytes(&bytes[0], 2);
                valueSerial = bytes[0] * 256 + int(bytes[1]);
                //cout << valueSerial << endl;
                plot->update(valueSerial);
                //cout << valueSerial << endl;
            
            }
            
        }
    }
//    serial.flush();
    
    
}

//--------------------------------------------------------------
void ofApp::draw(){
    
    plot->draw(0, ofGetHeight()/2-300, ofGetWidth(), 600);
    
}

//--------------------------------------------------------------


void ofApp::keyPressed(int key){
    
    switch(key) {
        case 'f':
            ofToggleFullscreen();
        default :
            break;
    }
    
}


//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    
}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
    
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
    
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    mouseIsPressed = true;
    
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
    mouseIsPressed = false;
}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
    
}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
    
}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
    
}
