2015年10月20日 星期二

openFrameworks: Serial Read String From Arduino Simulated Sensor

since: 2015/10/20
update: 2015/10/22
reference:
1. Serial read string from Arduino - 【oF】openFrameworks

A. Arduino 程式: 
// arduino_sensor.ino
long randNumber;
float randFloat;

String xAxis = "X: ";
String yAxis = "Y: ";
String zAxis = "Z: ";

String xValue = "";
String yValue = "";
String zValue = "";

void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.

  randomSeed(analogRead(0));
}

void loop() {

  // ex: X: 359.81252 Y: 11.81252 Z: 56.00005
  randNumber = random(0, 36000000);
  randFloat = randNumber / 100000.0; //
produce float random value: 0 ~ 360
  xValue = String(randFloat, 5);
 
  randNumber = random(-3600000, 3600000);
  randFloat = randNumber / 100000.0; //
produce float random value: -36 ~ 36
  yValue = String(randFloat, 5); 
 
  randNumber = random(0, 18000000);
  randFloat = randNumber / 100000.0;  //
produce float random value: 0 ~ 180
  zValue = String(randFloat, 5); 

  Serial.println(xAxis + xValue + " " + yAxis + yValue + " " + zAxis + zValue);
  delay(50);
}


-----------------------------------------------------------------------------------------------

B. openFrameworks 程式:
    1. main.cpp
#include "ofMain.h"
#include "ofApp.h"

//========================================================================
int main( ){
    ofSetupOpenGL(1024,768,OF_WINDOW);            // <-------- setup the GL context

    // this kicks off the running of my app
    // can be OF_WINDOW or OF_FULLSCREEN
    // pass in width and height too:

    ofRunApp(new ofApp());
}

-----------------------------------------------------------------------------------------------

    2. ofApp.h
#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

    public:
        void setup();
        void update();
        void draw();

        void keyPressed(int key);
        void keyReleased(int key);
        void mouseMoved(int x, int y );
        void mouseDragged(int x, int y, int button);
        void mousePressed(int x, int y, int button);
        void mouseReleased(int x, int y, int button);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);
   
        //@add 2015/10/20 ########################################
        string ofxGetSerialString(ofSerial &serial, char until);
        string ofxTrimStringRight(string str);
        string ofxTrimStringLeft(string str);
        string ofxTrimString(string str);
   
        ofTrueTypeFont  font;
        float           readTime;           // when did we last read?
        ofSerial        serial;
        string          serialString;
};


-----------------------------------------------------------------------------------------------

    3. ofApp.cpp
#include "ofApp.h"

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

//@add 2015/10/20 ########################################
string ofApp::ofxGetSerialString(ofSerial &serial, char until) {
    static string str;
    stringstream ss;
    char ch;
    int ttl=1000;
    while ((ch=serial.readByte())>0 && ttl-->0 && ch!=until) {
        ss << ch;
    }
    str+=ss.str();
    if (ch==until) {
        string tmp=str;
        str="";
        return ofxTrimString(tmp);
    } else {
        return "";
    }
}


//@add 2015/10/20 ########################################
// trim right trailing spaces

string ofApp::ofxTrimStringRight(string str) {
    size_t endpos = str.find_last_not_of(" \t\r\n");
    return (string::npos != endpos) ? str.substr( 0, endpos+1) : str;
}


//@add 2015/10/20 ########################################
// trim left trailing spaces

string ofApp::ofxTrimStringLeft(string str) {
    size_t startpos = str.find_first_not_of(" \t\r\n");
    return (string::npos != startpos) ? str.substr(startpos) : str;
}


//@add 2015/10/20 ########################################
// trim trailing spaces

string ofApp::ofxTrimString(string str) {
    return ofxTrimStringLeft(ofxTrimStringRight(str));;
}


void ofApp::setup(){
    //@add 2015/10/20 ########################################
    ofSetVerticalSync(true);
   
    ofBackground(255);
    // http://openframeworks.cc/documentation/utils/ofLog.html
    ofSetLogLevel(OF_LOG_VERBOSE);
   
    ofSetColor(0);
    font.loadFont("DIN.otf", 32);
   
    serial.listDevices();
    /*
     [notice ] ofSerial: [0] = tty.usbmodem1441
     [notice ] ofSerial: [1] = cu.usbmodem1441
     [notice ] ofSerial: [2] = tty.Bluetooth-Incoming-Port
     [notice ] ofSerial: [3] = cu.Bluetooth-Incoming-Port
     */
   
    /*
    vector<ofSerialDeviceInfo> deviceList = serial.getDeviceList();
    vector<ofSerialDeviceInfo>::iterator it = deviceList.begin();
   
    while( it != deviceList.end() ){
        ofLogVerbose() << (*it).getDeviceID() << ": " << (*it).getDeviceName();
        ++it;
    }
    */

   
    int baud = 9600;
    // Tells the computer which port to be listening to
    serial.setup(0, baud); //open the first device
   
    readTime = 0;
    serialString = "";
}

//--------------------------------------------------------------
void ofApp::update(){
    //@add 2015/10/20 ########################################
    serialString = "";
    serialString = ofxGetSerialString(serial,'\n'); //read until end of line
    if (serialString.length() > 0) {
        readTime = ofGetElapsedTimef();
        ofLogVerbose() << "serialString = " << serialString << "\n";
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    string msg;
    msg += "read: " + serialString + "\n";
    msg += "(at time " + ofToString(readTime, 3) + ")";
    font.drawString(msg, 50, 100);
}

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

//--------------------------------------------------------------
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){
}

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

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

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

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


       備註:
       記得將 .... /openFrameworks/of_v0.8.4_osx_release/examples/ communication/
       serialExample/bin/data
內的 DIN.otf copy 到相對的位置內.


-----------------------------------------------------------------------------------------------

C. 執行結果:

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。