12.Interface and Application Programming

Assignment : write an application that interfaces with an input or output device

I decided to use the OpenFrameWorks for drawing sensing data of this week assignment.
Because, this software can draw beautiful design easily.
And I used a lighting sensor which I made before.
So I tried to write the c++ code.

This is header file written by c++ for the OpenFrameWorks

#include "ofMain.h"

class testApp : public ofBaseApp{
		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);
		bool bSendSerialMessage; // When serial comunication is ready, this flag become true
		bool cFlag; // when it read btye1 ~ byte4, flag become true
		char byte1;
		char byte2;
		char byte3;
		char byte4;
		char low;  // low value
		char high; // high value
		int value; //value of low + high
		int filter;
		int x; // value
		ofSerial serial; // variable for serial communication
       		ofTrueTypeFont font; // font

This is cpp file written by c++.

#include "testApp.h"

void testApp::setup(){
    // general setting
    ofSetCircleResolution(64); // smooting circle object
    font.loadFont("verdana.ttf", 14); // using font loaded
    ofSetBackgroundColor(0, 0, 0); // set background black
    // serial setting
    bSendSerialMessage = false; //serial communication is ready
    serial.listDevices(); // search port that my PC can use
    vector deviceList = serial.getDeviceList(); // show that my comuputer can use serial comunication port
    int baud = 9600; // set baud rate to 9600
    serial.setup(0, baud);
    serial.setup("/dev/tty.usbserial-FTFXE503", baud); // set serial port about my PC
    // flag
    cFlag = false // init flag that I can use reading point from serial


void testApp::update(){
    byte2 = 0;
    byte3 = 0;
    byte4 = 0;
    // loop between flag is false
    while (cFlag == false) {
        byte1 == byte2;
        byte2 == byte3;
        byte3 == byte4;
        byte4 == serial.readByte();
        if ((byte1==1)&(byte2==2)&(byte3==3)&(byte4==4)) { // when some bytes become this, flag change true, and escape loop
            cFlag = true;
    // get value from sensor
    cFlag == false; // when escape cFlag loop, cFalg is set to false
    low = serial.readByte(); // after escape flag, I insert low and high value
    high = serial.readByte();
    value = 256 * high + low; // combind low and high value
    filter = (1 - eps) * filter + eps * value; // through filter
    x = int(.2 * ofGetWidth() + (.9 - .2) * ofGetWidth() * filter / 1024.0);


void testApp::draw(){
    // smoothing
    // set circle color to white
    ofSetColor(255, 255, 255);
    // show value about sensor
    string msg; // make variable sbout message
    msg += "read" + ofToString(filter) + "\n"; // set value to message object
    font.drawString(msg, 50, 100); // show message
    // draw circle from sensor value
    ofCircle(ofGetWidth()/2, ofGetHeight()/2, filter/10);


I tried to smooth detecting data refer to the sample python data.
But I couldn't smooth data. my sensor's value were unstable.
I also tried to change a type of value from "signed" to "unsigned", but value were not stable.
And I wrote a code which is decreasing value when sensor get light.
But in either case, it was dark or light, value were decreasing.