Last active
December 26, 2015 13:29
-
-
Save rc1/7158312 to your computer and use it in GitHub Desktop.
Playing with cpp11 (lambdas and ofxUI) in OpenFrameworks. Quick 24 channel DMX controller in one file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ofMain.h" | |
| #include "ofxDMX.h" | |
| #include "ofxUI.h" | |
| #define NUMBER_OF_DMX_CHANNELS 24 | |
| //======================================================================== | |
| #pragma mark - UI Utils | |
| inline void preConfigureCanvas(ofxUICanvas *ui) { | |
| ui->setFont( "GUI/inconsolata.ttf" ); | |
| ui->setColorBack( ofColor::black ); | |
| } | |
| inline void postConfigureCanvas(ofxUICanvas *ui) { | |
| ui->autoSizeToFitWidgets(); | |
| } | |
| inline void configureSlider(ofxUISlider *slider) { | |
| slider->setColorBack( ofColor::pink ); | |
| } | |
| //======================================================================== | |
| #pragma mark - ofxUILambdaTest | |
| namespace ofxUILambdaTest { | |
| class LabelButton : public ofxUILabelButton { | |
| public: | |
| // Inherit the base class constructors, yay! | |
| using ofxUILabelButton::ofxUILabelButton; | |
| // Override the standard event system ofxUI | |
| void triggerEvent(ofxUIWidget *child) { | |
| callback( getValue(), getName() ); | |
| } | |
| // Here we make our little callback, yay! | |
| typedef std::function<void (bool, string)> Functor; | |
| Functor callback; | |
| }; | |
| class Slider : public ofxUISlider { | |
| public: | |
| // Inherit the base class constructors, yay! | |
| using ofxUISlider::ofxUISlider; | |
| // Override the standard event system ofxUI | |
| void triggerEvent(ofxUIWidget *child) { | |
| callback( getScaledValue(), getName() ); | |
| } | |
| // Here we make our little callback, yay! | |
| typedef std::function<void (float, string)> Functor; | |
| Functor callback; | |
| }; | |
| } | |
| //======================================================================== | |
| #pragma mark - ofApp | |
| class ofApp : public ofBaseApp { | |
| public: | |
| ofxDmx dmx; | |
| ofxUICanvas *ui1; | |
| ofxUICanvas *ui2; | |
| ofxUICanvas *ui3; | |
| public: | |
| void update() { | |
| dmx.update(); | |
| } | |
| void setup(){ | |
| // Connect up the DMX | |
| dmx.connect( "tty.usbserial-EN137935", 24 ); | |
| // Create the UI | |
| ui1 = new ofxUICanvas( 0,0,200,412 ); | |
| ui2 = new ofxUICanvas( 200,0,200,412 ); | |
| ui3 = new ofxUICanvas( 400,0,200,412 ); | |
| // Configure the ui | |
| preConfigureCanvas( ui1 ); | |
| preConfigureCanvas( ui2 ); | |
| preConfigureCanvas( ui3 ); | |
| // Add a button for each serial device, which will then connect the dmx | |
| vector <ofSerialDeviceInfo> devices = (ofSerial()).getDeviceList(); | |
| for (auto &device : devices) { | |
| ofxUILambdaTest::LabelButton *button = new ofxUILambdaTest::LabelButton( device.getDeviceName(), false, 0, 0, 0, 0, OFX_UI_FONT_SMALL, true ); | |
| button->callback = [this](bool value, string name) { | |
| // only on down | |
| if ( value ) { | |
| if ( dmx.isConnected() ) { dmx.disconnect(); } | |
| dmx.connect(name, 24); | |
| } | |
| }; | |
| ui1->addWidgetDown( button ); | |
| } | |
| // Add a slider for each dmx channel, which will then update the relevant channel | |
| for (int i = 0; i < NUMBER_OF_DMX_CHANNELS; ++i) { | |
| ofxUILambdaTest::Slider *slider = new ofxUILambdaTest::Slider( "DMX"+ofToString(i+1), 0.0f, 255.0f, 0.0f, 200.0f, 12.0f ); | |
| slider->callback = [this, i](float value, string name) { | |
| dmx.setLevel( i+1, value ); | |
| }; | |
| configureSlider( slider ); | |
| ofxUICanvas *canvas = ( i < NUMBER_OF_DMX_CHANNELS/2 ) ? ui2 : ui3; | |
| canvas->addWidgetDown( slider ); | |
| } | |
| // Configure the ui | |
| postConfigureCanvas( ui1 ); | |
| postConfigureCanvas( ui2 ); | |
| postConfigureCanvas( ui3 ); | |
| } | |
| }; | |
| //======================================================================== | |
| int main() { | |
| ofSetDataPathRoot( "../Resources/data/" ); | |
| ofSetupOpenGL( 600, 412, OF_WINDOW ); | |
| ofRunApp( new ofApp() ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment