#include "testApp.h" //-------------------------------------------------------------- testApp::~testApp(){ ard.disconnect(); // closes the serial port connection saveSettings("settings.xml"); } //-------------------------------------------------------------- void testApp::setup(){ loadSettings("settings.xml"); ofBackground(0,0,0); ///ofSetVerticalSync(true); //some model / light stuff glEnable (GL_DEPTH_TEST); glShadeModel (GL_SMOOTH); glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable (GL_COLOR_MATERIAL); printf("loading %s at %f,%f,%f",model_name.c_str(),model_x,model_y,model_z); //load the bottle model - the 3ds and the texture file need to be in the same folder model.loadModel(model_name, 1); model.setRotation(0, 180, 1, 0, 0); //model.setRotation(0, 90, 0, 1, 0); model.setPosition(model_x, model_y, model_z); calibrator_label.loadMovie("calibrator_label.mov"); calibrator_label.setLoopState(OF_LOOP_NORMAL); calibrator_label.play(); //ofDisableArbTex(); mode=CALIBRATE; transTime=0; transition=false; whichClip=0; //0 is black switchClip=0; //0 is black texture.loadMovie(clips[whichClip]); texture.setLoopState(OF_LOOP_NORMAL); texture.play(); light=true; mixAmount=1.0; camWidth = 320; // try to grab at this size. camHeight = 240; vidGrabber.setVerbose(true); vidGrabber.listDevices(); vidGrabber.setDeviceID(0); grabberAvailable=vidGrabber.initGrabber(camWidth,camHeight); //printf("asked for 320 by 240 - actual size is %i by %i\n", vidGrabber.width, vidGrabber.height); videoInverted = new unsigned char[camWidth*camHeight*3]; videoTexture.allocate(camWidth,camHeight, GL_RGB); bSetupArduino = ard.connect("/dev/ttyUSB0", 57600); if (bSetupArduino) ofAddListener(ard.EInitialized, this, &testApp::setupArduino); bSwitchArduino = false; //----------------avahi stuff avahiService.start("ofMrmr","_mrmrsrvr._udp",1337); oscReceiver.setup(1337); //default mrmr port } //-------------------------------------------------------------- void testApp::update(){ texture.update(); calibrator_label.update(); //mixAmount=sin(ofGetElapsedTimef())+1.0; doTransition(); if (mode==GRAB) { vidGrabber.grabFrame(); if (vidGrabber.isFrameNew()){ int totalPixels = camWidth*camHeight*3; unsigned char * pixels = vidGrabber.getPixels(); for (int i = 0; i < totalPixels; i+=3){ unsigned int bright= (pixels[i]>>2)+ (pixels[i+1]>>1)+(pixels[i+1]>>2); //(0.2126*R) + (0.7152*G) + (0.0722*B) videoInverted[i] = (unsigned char)((226*bright)>>8); videoInverted[i+1] = (unsigned char)((200*bright)>>8); videoInverted[i+2] = (unsigned char)((20*bright)>>8); } videoTexture.loadData(videoInverted, camWidth,camHeight, GL_RGB); } } updateArduino(); //----------------avahi stuff ProcessMessages(); } //-------------------------------------------------------------- void testApp::setupArduino(const int & version) { // remove listener because we don't need it anymore ofRemoveListener(ard.EInitialized, this, &testApp::setupArduino); // this is where you setup all the pins and pin modes, etc for (int i = 0; i < 13; i++){ ard.sendDigitalPinMode(i, ARD_OUTPUT); } ard.sendDigitalPinMode(7, ARD_INPUT); printf("arduino ready\n"); } //-------------------------------------------------------------- void testApp::updateArduino(){ // do not send anything until the arduino has been set up if (bSetupArduino) { // update the arduino, get any data or messages. ard.update(); if (bSwitchArduino!=ard.getDigital(7)) { bSwitchArduino=ard.getDigital(7); printf("pin 7 %i\n",bSwitchArduino); if (!bSwitchArduino) { whichClip=max(1,switchClip); transTime=ofGetElapsedTimef()+0.5; } else { switchClip=whichClip; whichClip=0; transTime=ofGetElapsedTimef()+0.1; } transition=true; } } } bool startswith(const char* _str1, const char* _start){ for(int i=0;_start[i]>0;i++){ if(_str1[i]!=_start[i]) return false; } return true; } void testApp::SendInterface(std::string targetIP, std::string interfaceString){ /* Example: "/mrmrIB mrmr_clear_all\n"\ "/mrmrIB tactilezone nil .1 1 10 1 2 1 9 _ 2\n"\ "/mrmrIB textinputview nil .1 1 10 1 1 1 1 placeholder_text_for_textinput_field\n"*/ tcpClient.close(); //just open a fresh socket to do this one-time setup/send to the new client tcpClient.setup(targetIP,31337); printf("Sending interface...\n"); int count = interfaceString.length(); tcpClient.sendRawBytes(interfaceString.c_str(),count); //don't use send() as it appends [/TCP] tag, which screws up mrmr client } void testApp::ProcessMessages(){ while( oscReceiver.hasWaitingMessages() ) { // get the next message ofxOscMessage m; oscReceiver.getNextMessage( &m ); std::string messageAddress = m.getAddress(); printf("Message: %s\n",messageAddress.c_str()); std::string rip = m.getRemoteIp(); int rp = m.getRemotePort(); std::string arg1; if(messageAddress.compare("/mrmr connect")==0){ SendInterface(rip, "/mrmrIB mrmr_clear_all\n"\ "/mrmrIB titleview nil .1 1 10 1 1 1 1 StrongbowDemo 3"\ "/mrmrIB pushbutton nil .1 2 10 1 6 1 1 < 11\n"\ "/mrmrIB pushbutton nil .1 2 10 1 6 1 1 < 11\n"\ "/mrmrIB pushbutton nil .1 2 10 2 6 1 1 > 11\n" ); } else { int ls = messageAddress.find_last_of("/"); int msg=-1; if(m.getNumArgs()) msg=m.getArgAsInt32( 0 ); if(startswith(messageAddress.c_str(),"/mrmr/pushbutton/1")) { printf("previous movie %i\n",msg); if (msg) prevMovie(); } if(startswith(messageAddress.c_str(),"/mrmr/pushbutton/2")) { printf("next movie %i\n",msg); if (msg) nextMovie(); } } } } //-------------------------------------------------------------- void testApp::draw(){ for (int i=0;i': nextMovie(); break; case '/': light=!light; printf(light?"LIGHT ON\n":"LIGHT OFF\n"); break; case '`': ard.disconnect(); // closes the serial port connection printf("arduino disconnected\n"); break; } } void testApp::prevMovie(){ whichClip--; if (whichClip<1) whichClip=numClips-1; transTime=ofGetElapsedTimef()+1.0; transition=true; } void testApp::nextMovie(){ whichClip=max(1,(whichClip+1)%numClips); transTime=ofGetElapsedTimef()+1.0; transition=true; } void testApp::doTransition(){ if (transition) { if (ofGetElapsedTimef()>transTime) { texture.stop(); texture.loadMovie(clips[whichClip]); texture.play(); transition=false; } } mixAmount=min(abs(ofGetElapsedTimef()-transTime),1.0f); if (whichClip==0&&!transition) mixAmount=0; } //-------------------------------------------------------------- void testApp::keyReleased(int key){ if (activeView<0) for (int i=0;ikeys; XML.getAttributeNames("settings", keys, 0); mapsettings; for (int k=0;kkeys; XML.getAttributeNames("settings", keys, 0); for (int k=0;k