Hardware Reference
In-Depth Information
Listing 3-2. main.cpp
#include "ofMain.h" // include files
#include "testApp.h" // declarations for the testapp class
#include "ofAppGlutWindow.h" // for using OpenGL and creating windows
int main() {
ofAppGlutWindow window; // sets up an OpenGL window object
ofSetupOpenGL(&window,200,100, OF_WINDOW); //sets window size in pixels
ofRunApp(new testApp()); // create testapp object & enter program loop
} // end int main()
openFrameworks code is set up to be event driven and window based, the same as other graphical-interface
programs. The main.cpp file contains the main() function, which is the entry point to the openFrameworks programs.
The main() function sets parameters for the window, including the window size and window mode. It is rare to make
many changes in main.cpp ; most of the time the only thing that will change is the window size.
Listing 3-3. testapp.h
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup(); // for setting initial parameters
void update(); // code in this function is constantly run, events will interrupt
void draw(); // runs after update,this updates & creates the window objects
void mousePressed(int x, int y, int button); // on event function
bool SendSerialMessage; // signals that data needs to be sent
char ledcommand ; // hold what state the LED is in
char Returned; // hold returned char from Arduino
ofSerial serial; // this is the object to handle serial
};// end class testApp : public ofBaseApp
The testApp class inherits common functionality from the ofBaseApp class. This is where the function prototypes
are created. Variables that will be used in many functions can be declared here. There is a set of functions that are
called when events occur, such as mouse movement or using the keyboard. Note the line where you need to change
COM4 to match your Arduino setup.
Listing 3-4. testapp.cpp
#include "testApp.h"
void testApp::setup(){
ofSetVerticalSync(true); // helps to smooth out object drawing
ofBackground(255,255,255); // set background color to an RGB value
serial.setup("COM7", 9600); // change "COM7" to match where the Arduino is
ledcommand = 's'; // set initial state of the LED
serial.writeByte(ledcommand); // tell Arduino of the initial state
SendSerialMessage = false; // nothing to send yet
} // end void testApp::setup()
void testApp::update(){
if (SendSerialMessage) // is there serial information that needs to be sent
serial.writeByte(ledcommand); // tell the Arduino to change LED state
if (serial.available()) // check to see if there is incoming data
Returned = serial.readByte(); // save the incoming data
 
Search WWH ::




Custom Search