LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2005, 10:07 AM   #1
tim3090kc
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Rep: Reputation: 0
joystick


I'm trying to do some joystick programming. I've done a lot of searching with google and have come up empty.

What I'm basically looking for is to know how to read the value of the joystick axis. I've seen a lot of online posts mentioning a "js.c" example program:

Quote:
There is an example program js.c in the joystick loadable kernel module package <linux/joystick.h>.
However I have not been able to find this "js.c" anywhere. Can anyone point me in the right direction?
 
Old 07-27-2005, 10:19 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Simple Directmedia Layer handles graphics, audio, timers, keyboard, mouse and joysticks. You can also do more flexible audio with SDL_mixer, Import many images with SDL_image, do networking stuff with SDL_net. It is easier API than direct kernel stuff.
 
Old 07-27-2005, 10:30 AM   #3
tim3090kc
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry, I should have been more specific before...

The Simple Directmedia layer seems like a good solution, but the linux machine will not have that available, so I think that I will still have to resort to the linux API.

BTW, I know the joystick is working and configured, since I used jstest /dev/input/js0.

I just need to know how to read from js0. All that I am trying to do is to log joystick angles over time. Any ideas?

Last edited by tim3090kc; 07-27-2005 at 10:32 AM.
 
Old 08-08-2005, 11:22 AM   #4
paultorben
LQ Newbie
 
Registered: Aug 2005
Posts: 1

Rep: Reputation: 0
Not sure if you're still wondering about this but ...

I am also getting into joystick programming on linux - but I think most of the docs are fairly old, as I too found that reference but not the mentioned file - but I do have (in the Linux Kernel Sources)

/usr/src/linux/drivers/input/joydev.c

and a subdirectory ...

/usr/src/linux/drivers/input/joystick

I hope this helps ...
 
Old 08-08-2005, 05:04 PM   #5
tim3090kc
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
I should have come back to tell everyone that I figgured out my own solution. This will log joystick positions at about 12Hz. It also uses the curses lib. for printing output to the screen....

compile with..

gcc -o js js.c -lpthread -lncurses

Code:
#include <pthread.h>
#include <sys/timeb.h>
#include <curses.h>
#include <fcntl.h>
#include <linux/joystick.h>

#define NUM_THREADS	3
#define JOY_DEV		"/dev/js0"
#define HZ				12

const char FILENAME[] = "./joystickLog.out";
char axisDesc[15][5] = {"X", "Y", "Z", "R", "A", "B", "C", "D", "E", "F", "G", "H", "I" };
char numAxis[20], numButtons[20], value[20][20], timeSec[20], timeMilli[20];
int num_of_axis;

void write_to_log(){
	sleep(1);
	int cnt;
	FILE * daFile;
	daFile = fopen(FILENAME, "w+");

//	WRITE A HEADER ON THE OUPUT FILE
	fprintf(daFile, "#,");
	for(cnt = 0; cnt < num_of_axis; cnt++)
		if(cnt < num_of_axis -1)	fprintf(daFile, "%s,", axisDesc[cnt]);
		else								fprintf(daFile, "%s", axisDesc[cnt]);
	fprintf(daFile, "\r\n");

	while(1){
		fprintf(daFile, "%s.%s, ", timeSec, timeMilli);
		for(cnt = 0; cnt < num_of_axis; cnt++){
			if(cnt < num_of_axis -1)	fprintf(daFile, "%s, ", value[cnt]);
			else								fprintf(daFile, "%s ", value[cnt]);
		}
		fprintf(daFile, "\r\n");
		usleep(83333);
	}
	
	fprintf(daFile, "%s.%s Len = %d ", timeSec, timeMilli,strlen(timeMilli));

	fprintf(daFile, "\r\n");
	fflush(daFile);
}

void write_to_screen(){
	struct timeb seconds;
	int cnt, joy_fd, *axis=NULL, num_of_buttons=0, x;
	char *button=NULL, name_of_joystick[80];
	struct js_event js;
	time_t theTime;

	wmove(stdscr, 8, 0);
	addstr("Type \"q\" to quit.\n");

	if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 ){
		wmove(stdscr, 3,0);
		addstr( "Couldn't open joystick " );
		addstr( JOY_DEV );
		wmove(stdscr, 10,0);
		while(1)usleep(16666);
	}

	ioctl( joy_fd, JSIOCGAXES, &num_of_axis );				//	GET THE NUMBER OF AXIS ON JS
	ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );		//	GET THE NUMBER OF BUTTONS ON THE JS
	ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );	//	GET THE NAME OF THE JS

	axis = (int *) calloc( num_of_axis, sizeof( int ) );
	button = (char *) calloc( num_of_buttons, sizeof( char ) );

	sprintf(numAxis, "%d", num_of_axis);
	sprintf(numButtons, "%d", num_of_buttons);

	wmove(stdscr, 0,0);
	addstr("Joystick detected: ");

	wmove(stdscr, 0, 19);
	addstr(name_of_joystick);

	wmove(stdscr, 1, 0);
	addstr("Number of axis   :");

	wmove(stdscr, 1, 19);
	addstr(numAxis);
	
	wmove(stdscr, 2, 0);
	addstr("Number of buttons:");

	wmove (stdscr, 2, 19);
	addstr(numButtons);

//	CHANGE THE STATUS FLAG OF THE FILE DESCRIPTOR TO NON-BLOCKING MODE
	fcntl( joy_fd, F_SETFL, O_NONBLOCK );   

	while( 1 ){
		usleep(16666);
	
	//	READ THE JOYSTICK STATE, IT WILL BE RETURNED IN THE JS_EVENT STRUCT
		read(joy_fd, &js, sizeof(struct js_event));

	//	GET THE NUMBER OF SECONDS SINCE EPOCH
		ftime(&seconds);
		theTime = time(NULL);
		sprintf(timeSec, "%d", seconds.time);						
		sprintf(timeMilli, "%d", seconds.millitm);
               
	//	CHECK THE EVENT
		switch (js.type & ~JS_EVENT_INIT){
			case JS_EVENT_AXIS:
				axis   [ js.number ] = js.value;
				break;
			case JS_EVENT_BUTTON:
				button [ js.number ] = js.value;
				break;
		}
		//	ADD LEADING 0'S TO THE MILLISECOND STRING (IF NECESSARY)
			char temp[10] = "";
			while(strlen(temp) < (3 - strlen(timeMilli)))
				strcat(temp, "0");
			strcat(temp,timeMilli);
			strcpy(timeMilli, temp);

		//	PRINT THE RESULTS
			wmove(stdscr, 4, 0);
			for(cnt = 0; cnt < num_of_axis; cnt++)
				addstr("            ");
			for(cnt = 0; cnt < num_of_axis && cnt < 13; cnt++){
				sprintf(value[cnt], "%d", axis[cnt]);
				wmove(stdscr, 4, cnt * 10);
				addstr(axisDesc[cnt]);
				addstr(": ");
				wmove(stdscr, 4, (cnt * 10) + 2);
				addstr(value[cnt]);
			}
			
			wmove(stdscr, 6, 0);
			addstr(timeSec);
			wmove(stdscr, 6,10);
			addstr(".");
			wmove(stdscr, 6, 11);
			addstr(timeMilli);
			wmove(stdscr, 6, 15);
			addstr("=");
			wmove(stdscr, 6, 17);
			addstr(ctime(&theTime));
                       
			wmove(stdscr, 10, 0);
			refresh();
	}
}

void *ThreadProcs(void *threadid){
	int thread_id = (int)threadid;
	
	if(thread_id == 0){
		write_to_screen();
	}
	
	if(thread_id == 1){
	//	THIS THREAD WILL MAKE THE PROGRAM EXIT
		int ch;
		nodelay(stdscr, TRUE);				//	SETUP NON BLOCKING INPUT
		while(1) {
			if ((ch = getch()) == ERR) usleep(16666);		//	USER HASN'T RESPONDED
			else if( ch == 'q'){			
				endwin();
				exit(0);			//	QUIT ALL THREADS
			}
		}
	}
	if(thread_id == 2){
		write_to_log();
	}
}

int main(int argc, char *argv[]){
	initscr(); 									//	INIT THE SCREEN FOR CURSES
	pthread_t threads[NUM_THREADS];
	int rc, t;
	for(t = 0; t < NUM_THREADS; t++){	//	MAKE 2 NEW THREADS
		rc = pthread_create(&threads[t], NULL, ThreadProcs, (void *)t);
		if (rc){
			printf("ERROR; return code from pthread_create() is %d\n", rc);
			exit(-1);pthread_exit(NULL);
		}
	}
	
	for(t = 0; t < NUM_THREADS; t++)
		pthread_join(threads[t], NULL);		//	WAIT FOR THREADS TO EXIT OR IT WILL RACE TO HERE.

	endwin();
	return 0;
}
hope this might be helpful to someone
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Joystick Driver and API under kernel 2.6.12.3 using an USB joystick dispatcher Linux - Newbie 1 08-06-2005 09:43 AM
Joystick... Linux~Powered Linux - Games 1 07-24-2005 05:59 AM
Joystick Help sharkee Ubuntu 0 05-31-2005 05:44 AM
Joystick DaBlade Linux - Hardware 3 03-22-2005 04:58 AM
Joystick in RH 7.3?? lpriyamb Linux - General 5 10-21-2003 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration