Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Last active December 13, 2015 20:49
Show Gist options
  • Select an option

  • Save jvcleave/4973304 to your computer and use it in GitHub Desktop.

Select an option

Save jvcleave/4973304 to your computer and use it in GitHub Desktop.
work in progress PipeReader
#pragma once
#include "ofMain.h"
extern "C"
{
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
};
#define PIPE_BUFFER_SIZE 32
class ofxPipeListenerEventData
{
public:
ofxPipeListenerEventData(char character_)
{
character = character_;
}
char character;
};
class ofxPipeListener
{
public:
virtual void onCharacterReceived(ofxPipeListenerEventData& e) = 0;
};
class PipeReader : public ofThread{
public:
string namedPipe;
int fd;
ofxPipeListener* listener;
PipeReader()
{
listener = NULL;
}
void start(ofxPipeListener *listener_)
{
listener = listener_;
namedPipe = ofToDataPath("ofpipe", true);
ofFile file(namedPipe, ofFile::ReadWrite, false);
if (!file.exists())
{
bool didCreate = file.create();
if (didCreate)
{
ofLogVerbose() << "file creation PASS " << namedPipe;
}else
{
ofLogError() << "file creation FAIL " << namedPipe;
}
}else
{
ofLogVerbose() << namedPipe << " EXISTS";
}
mkfifo(namedPipe.c_str(), 0777);
startThread(true, false);
}
void stop()
{
stopThread();
}
void threadedFunction()
{
while( isThreadRunning())
{
ofBuffer pipeContents = ofBufferFromFile(namedPipe, false);
char * content = pipeContents.getBinaryBuffer();
if (content[0] != 0)
{
ofxPipeListenerEventData eventData(content[0]);
listener->onCharacterReceived(eventData);
pipeContents.clear();
ofBufferToFile(namedPipe, pipeContents, false);
}
}
}
};
#include "testApp.h"
string info="";
void testApp::onCharacterReceived(ofxPipeListenerEventData& e)
{
info+=ofToString(e.character)+"\n";
}
//--------------------------------------------------------------
void testApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
pipeReader.start(this);
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
ofDrawBitmapStringHighlight(info, 100, 100, ofColor::yellow, ofColor::blue);
}
#pragma once
#include "ofMain.h"
#include "PipeReader.h"
class testApp : public ofBaseApp, public ofxPipeListener{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
PipeReader pipeReader;
void onCharacterReceived(ofxPipeListenerEventData& e);
};
@jvcleave
Copy link
Author

example usage:
echo "c" > /home/pi/openFrameworks/apps/sandbox/pipeApp/bin/data/ofpipe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment