Example #1
0
void ofxGamepad::draw(int x, int y)
{
	int curX=0;
	int highestY;

	ofPushMatrix();
	ofTranslate(x, y);
	ofSetColor(255);
	ofPushMatrix();
	ofRotate(90);
	ofDrawBitmapString(name+" ("+ofToString(id)+")", 0, 0);
	ofPopMatrix();
	curX=17;

	int margin=3;
	ofRectangle axisSize(curX,0,85, 17);
	for(int i=0; i<getNumAxis(); i++) {
		ofSetColor(70);
		ofRect(axisSize);
		ofSetColor(255);
		float x =  ofMap(getAxisValue(i), -1, 1, axisSize.x, axisSize.width+axisSize.x);
		ofLine(x, axisSize.y, x, axisSize.y+axisSize.height);
		ofSetColor(20);
		ofDrawBitmapString(ofToString(i), axisSize.x, axisSize.y+axisSize.height-1);
		axisSize.y+=axisSize.height+margin;
		if(axisSize.y>highestY)
			highestY=axisSize.y;
	}

	curX+=axisSize.width+margin;
	ofRectangle btnSize(curX,0,17,17);
	for(int i=0; i<getNumButtons(); i++) {
		if(getButtonValue(i))
			ofSetColor(255);
		else
			ofSetColor(70);
		ofRect(btnSize);
		btnSize.y+=btnSize.height+margin;
		ofSetColor(20);
		ofDrawBitmapString(ofToString(i), btnSize.x, btnSize.y-4);
		if(btnSize.y>highestY)
			highestY=axisSize.y;
	}
	curX+=btnSize.width;

	ofPopMatrix();

	drawSize.x=curX;
	drawSize.y=highestY;
}
Example #2
0
ofxGamepadLinux::ofxGamepadLinux(string f):ofxGamepad() {
	filename=f;

	if ((fd = open(filename.c_str(), O_RDONLY)) < 0) {
		std::ostringstream str;
		str << filename << ": " << strerror(errno);
		throw std::runtime_error(str.str());
	} else {
		// ok
		uint8_t num_axis   = 0;
		uint8_t num_button = 0;
		ioctl(fd, JSIOCGAXES,    &num_axis);
		ioctl(fd, JSIOCGBUTTONS, &num_button);
		setNumAxis(num_axis);
		setNumButtons(num_button);

		// Get Name
		char name_c_str[1024];
		if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0) {
			std::ostringstream str;
			str << filename << ": " << strerror(errno);
			throw std::runtime_error(str.str());
		} else {
			name = name_c_str;
		}
		setName(name);

		fcntl( fd, F_SETFL, O_NONBLOCK );
	}

	string msg=name;
	msg += ": "+ofToString(getNumAxis())+" axis";
	msg += ", "+ofToString(getNumButtons())+" buttons";
	ofLog(OF_LOG_NOTICE, msg);

	// setup force feedback
	setupFF();
}