Commit 4305ca77 authored by Lky's avatar Lky

adding button maintenance, resolved speed problem

parent d54445c2
#include "ofMain.h"
#include "ofApp.h"
#define WINDOWED_
#define WINDOWED
int main( )
{
......
#include "ofApp.h"
#define INTERFACE_
#define ARDUINO_
#define ARDUINO
#define WRITE_INFO
#define REBOUNCE_DELAY_MS 10
static int prev_frame = 0;
static bool db = false;
......@@ -61,12 +63,15 @@ void ofApp::setup()
#ifdef ARDUINO
// INIT ARDUINO
ard.connect("COM5", 57600);
ard.sendFirmwareVersionRequest();
ofAddListener(ard.EInitialized, this, &ofApp::setupArduino);
bSetupArduino = false; // flag so we setup arduino when its ready, you don't need to touch this :)
#endif
blendmode = 0;
count_1 = 0; count_2 = 0; count_3 = 0; count_4 = 0; count_5 = 0;
speed_ch1 = 1; speed_ch2 = 1; speed_ch3 = 1;
potar_0 = 0; potar_1 = 0; potar_2 = 0; potar_4 = 0;
......@@ -167,7 +172,7 @@ void ofApp::setup()
flickering_ch1->setPosition(x+colorParameters_ch1->getWidth()+shift_ch1->getWidth()+80,y);
geometry_ch1->setPosition(x+colorParameters_ch1->getWidth()+shift_ch1->getWidth()+flickering_ch1->getWidth()+120,y);
#endif
}
......@@ -366,6 +371,9 @@ void ofApp::draw()
#ifdef WRITE_INFO
font.drawString(ofToString(ofGetFrameRate()) + "\nspeeds: {" + ofToString(speed_ch1) + "," + ofToString(speed_ch2) + "," + ofToString(speed_ch3) + "}\nopBC: {" + ofToString(opBC_ch1) + "," + ofToString(opBC_ch2) + "," + ofToString(opBC_ch3) + "} \nblendmode::" + blendName + " \nmain_opacity::" + ofToString(main_opacity), 100, 100);
if (bSetupArduino) {
font.drawString(ofToString(list_of_buttons[0].clickCount) + "," + ofToString(list_of_buttons[1].clickCount) + "," + ofToString(list_of_buttons[2].clickCount) + "," + ofToString(list_of_buttons[3].clickCount), 200, 100);
}
#endif
}
......@@ -487,7 +495,7 @@ void ofApp::setupArduino(const int & version) {
// remove listener because we don't need it anymore
ofRemoveListener(ard.EInitialized, this, &ofApp::setupArduino);
// it is now safe to send commands to the Arduino
bSetupArduino = true;
......@@ -500,9 +508,36 @@ void ofApp::setupArduino(const int & version) {
// If using Arduino 0022 or older, then use 16 - 21.
// Firmata pin numbering changed in version 2.3 (which is included in Arduino 1.0)
buttonNumber = 5;
list_of_buttons = new buttonStruct[buttonNumber];
for (int i = 0; i < buttonNumber; i++)
{
list_of_buttons[i].isPressed = false;
list_of_buttons[i].rebounceTime = 0;
list_of_buttons[i].clickCount = 0;
}
//// Initialiser les numros spcifiques des entres utilises
list_of_buttons[0].pinNumber = 2;
list_of_buttons[1].pinNumber = 3;
list_of_buttons[2].pinNumber = 6;
list_of_buttons[3].pinNumber = 7;
list_of_buttons[4].pinNumber = 8;
//// Initialiser les PIN de tous les boutons grer
for (int i = 0; i < buttonNumber; i++)
{
ard.sendDigitalPinMode(list_of_buttons[i].pinNumber, ARD_INPUT);
// pinMode(list_of_buttons[i].entreeDigitale, INPUT);
}
// set pins D2 and A5 to digital input
ard.sendDigitalPinMode(2, ARD_INPUT);
ard.sendDigitalPinMode(19, ARD_INPUT); // pin 21 if using StandardFirmata from Arduino 0022 or older
//ard.sendDigitalPinMode(2, ARD_INPUT);
//ard.sendDigitalPinMode(3, ARD_INPUT);
ard.sendDigitalPinMode(4, ARD_INPUT);
ard.sendDigitalPinMode(5, ARD_INPUT);
//ard.sendDigitalPinMode(6, ARD_INPUT);
//ard.sendDigitalPinMode(19, ARD_INPUT); // pin 21 if using StandardFirmata from Arduino 0022 or older
// set pin A0 to analog input
ard.sendAnalogPinReporting(0, ARD_ANALOG);
......@@ -513,6 +548,7 @@ void ofApp::setupArduino(const int & version) {
ard.sendAnalogPinReporting(5, ARD_ANALOG);
ard.sendAnalogPinReporting(6, ARD_ANALOG);
ard.sendAnalogPinReporting(7, ARD_ANALOG);
// set pin D13 as digital output
ard.sendDigitalPinMode(13, ARD_OUTPUT);
// set pin A4 as digital output
......@@ -535,10 +571,18 @@ void ofApp::updateArduino(){
// update the arduino, get any data or messages.
// the call to ard.update() is required
ard.update();
// do not send anything until the arduino has been set up
if (bSetupArduino) {
// fade the led connected to pin D11
for (int i = 0; i < buttonNumber; ++i) {
stateOfButton(&(list_of_buttons[i]));
}
if (ard.getDigital(4)) {
list_of_buttons[3].clickCount++;
}
}
}
......@@ -548,6 +592,48 @@ void ofApp::digitalPinChanged(const int & pinNum) {
// do something with the digital input. here we're simply going to print the pin number and
// value to the screen each time it changes
buttonState = "digital pin: " + ofToString(pinNum) + " = " + ofToString(ard.getDigital(pinNum));
if (pinNum == 2) {
count_1++;
}
if ((pinNum == 3) && (!ard.getDigital(3))) {
count_2++;
}
if (pinNum == 4) {
count_3++;
}
if (pinNum == 5) {
count_4++;
}
if (pinNum == 6) {
count_5++;
}
//cout << "digital pin: " + ofToString(pinNum) + " = " + ofToString(ard.getDigital(pinNum));
}
void ofApp::stateOfButton(buttonStruct *someButton) {
bool signalState = !ard.getDigital(someButton->pinNumber);
if (signalState) {
if (someButton->rebounceTime != 0) {
unsigned long elapsedTime = ofGetElapsedTimeMillis() - someButton->rebounceTime;
if (elapsedTime >= REBOUNCE_DELAY_MS) {
someButton->isPressed = true;
someButton->rebounceTime = 0;
someButton->clickCount++;
}
}
else if (!someButton->isPressed) {
someButton->rebounceTime = ofGetElapsedTimeMillis();
}
}
else {
if (someButton->isPressed) {
someButton->isPressed = false;
}
else {
someButton->rebounceTime = 0;
}
}
}
//--------------------------------------------------------------
......@@ -557,9 +643,13 @@ void ofApp::analogPinChanged(const int & pinNum) {
//cout << potValue << endl;
if (pinNum == 0) {
potar_0 = ofMap(ard.getAnalog(pinNum), 0,1024,-2,2);
potar_0 = ofMap(ard.getAnalog(pinNum), 0,1024,-3,3);
//if ((int)potar_0*10 != (int)speed_ch1 * 10) {
potar_0 = roundf(potar_0 * 10)/10;
// CHECK speed != 0....
if ((potar_0 < 0.1) && (potar_0 > -0.1)) {
potar_0 = 0.1;
}
if (potar_0 != speed_ch1) {
speed_ch1 = potar_0;
hap_ch1.setSpeed(speed_ch1);
......@@ -567,9 +657,13 @@ void ofApp::analogPinChanged(const int & pinNum) {
//}
}
if (pinNum == 1) {
potar_1 = ofMap(ard.getAnalog(pinNum), 0, 1024, -2, 2);
potar_1 = ofMap(ard.getAnalog(pinNum), 0, 1024, -3, 3);
//if ((int)potar_1 * 10 != (int)speed_ch2 * 10) {
potar_1 = roundf(potar_1 * 10) / 10;
// CHECK speed != 0....
if ((potar_1 < 0.1) && (potar_1 > -0.1)) {
potar_1 = 0.1;
}
if (potar_1 != speed_ch2) {
speed_ch2 = potar_1;
hap_ch2.setSpeed(speed_ch2);
......@@ -577,9 +671,12 @@ void ofApp::analogPinChanged(const int & pinNum) {
//}
}
if (pinNum == 2) {
potar_2 = ofMap(ard.getAnalog(pinNum), 0, 1024, -2, 2);
potar_2 = ofMap(ard.getAnalog(pinNum), 0, 1024, -3, 3);
//if ((int)potar_2 * 10 != (int)speed_ch3 * 10) {
potar_2 = roundf(potar_2 * 10) / 10;
if ((potar_2 < 0.1) && (potar_2 > -0.1)) {
potar_2 = 0.1;
}
if (potar_2 != speed_ch3) {
speed_ch3 = potar_2;
hap_ch3.setSpeed(speed_ch3);
......@@ -592,8 +689,9 @@ void ofApp::analogPinChanged(const int & pinNum) {
potar_4 = ofMap(ard.getAnalog(pinNum), 0, 1024, 0, 2);
//potar_4 = roundf(potar_4 * 10) / 10;
main_opacity = potar_4;
}
//}
}
if (pinNum == 4) {
potar_10 = ofMap(ard.getAnalog(pinNum), 0, 1024, 0, 2);
//potar_10 = roundf(potar_10 * 10) / 10;
......
......@@ -37,6 +37,7 @@ public:
vector<string> video_names;
float potar_0, potar_1, potar_2, potar_4, potar_10, potar_11, potar_12, potar_14;
int count_1, count_2, count_3, count_4, count_5;
int opacity_1, opacity_2, opacity_3;
int blendmode;
ofTexture videoTexture;
......@@ -62,9 +63,23 @@ public:
int testValue;
ofShader shader;
// ARDUINO SETUP
ofArduino ard;
bool bSetupArduino; // flag variable for setting up arduino once
struct buttonStruct {
int pinNumber;
bool isPressed;
unsigned long rebounceTime;
int clickCount;
};
void stateOfButton(buttonStruct *someButton);
int buttonNumber;
buttonStruct * list_of_buttons;
// INTERFACE
ofxDatGuiFolder* colorParameters_ch1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment