Page 1 of 1

Use Joystick to control outputs

Posted: Sat Jan 30, 2016 9:14 pm
by cDuck28Z
Good evening,

I'm brand new to the Raspberry Pi Community. I'm working on a small little project to get my foot in the door. I'm very interested in using my HOTAS Warthog joystick to control some lights on my breadboard. Would the Raspberry Pi recognize the joystick as a plug and play device? I've scoured youtube but 'joystick' seems to turn up nothing but how to wire an analog joystick where as the Hotas is already a digital signal?

Any help would be great! Thank you in advance!

Re: Use Joystick to control outputs

Posted: Sun Jan 31, 2016 2:09 am
by Goraxium
If it shows up as a joystick on the Pi, you'll find it in /dev/input as js# (usually js0). You can use various libraries to help you get the input from it (SDL, for example), or you can read the raw data from the device (as seen below).

If it's not listed as a joystick, you can run lsusb (L, not i), to list the usb devices, at which point you can find the device number. From there, you'll usually find it in /dev/bus/usb/001/ as the number from lsusb. You should be able to open this device and read the raw data from it. Here's some simple C code to get you started (in case you're not sure):

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(){
  const int BUFFER_SIZE = 1024;
  char buffer[BUFFER_SIZE];
  int bytesRead,joystick;
  
  joystick = open("/dev/bus/usb/001/012",O_RDONLY);
  if (joystick == -1){
    printf("ERROR: Couldn't Open Joystick!\n");
    return 0;
  }
  bytesRead = read(joystick,buffer,BUFFER_SIZE);///returns -1 on error
  printf("Read %d Bytes!\n",bytesRead);
  close(joystick);
  return 0;
}