Hardware Reference
In-Depth Information
class testApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void mousePressed(int x, int y, int button);
void arduinoSetup(const int & version); // Arduino equivalent setup function
void arduinoLoop(); // Arduino-equivalent loop function
bool ledcommand;
bool pin13; // pin13 data container
bool pin8; // pin8 data container
float analogPin0; // pin8 data container
bool isArduinoSet; // flag to know when Arduino is connected and configured
ofArduino arduino; // the Arduino object
}; // end class testApp : public ofBaseApp
In testapp.cpp of Listing 3-6, the functions arduinoSetup() and arduinoLoop() perform the same
functions of an Arduino sketch with openFrameworks on top of the Arduino-style functions. Firmata and the
openFrameworks ofArduino class make the serial communication less apparent. By carefully mimicking the same
structure as an Arduino sketch, the conversion to an actual Arduino sketch is made simpler if the conversion
becomes necessary, as when moving to a more professional setup. Keep in mind it is possible to develop code in
openFrameworks that may require more space and computing power than might be available on the Arduino. This
is especially important to remember when using Firmata as a tool in making proofs of concept to eventually be
used solely on the Arduino.
Firmata is capable of using i2C and other communication functionality; however, openFrameworks does not
currently support i2C functionality (as of version 0071).
Note
Example 3-6. testapp.cpp for Standard Firmata Communication
#include "testApp.h"
void testApp::setup() {
arduino.connect("COM7"); // remember! change this to the proper port
ofAddListener(arduino.EInitialized, this, &testApp::arduinoSetup);
/*the ofAddListener waits for the Arduino to perform a handshake telling the program that it is
ready to be configured and set up. This will call arduinoSetup*/
isArduinoSet = false; // this flag is set false until the Arduino is set up
} // end void testApp::setup()
void testApp::update() {
testApp::arduinoLoop();// perform the Arduino-style code
} // end void testApp::update()
void testApp::draw() { // objects are drawn to the screen in the order called
if (isArduinoSet){ // do not run this code until Arduino is operating
ofFill();
if(pin8 == ARD_HIGH)
ofSetColor(0,0,255);// if button on pin8 pressed, brighten the circle
else
ofSetColor(0,0,127);// blue is dim if button is released
 
 
Search WWH ::




Custom Search