Topic: Chumby Hackerboard Joystick

Fellow Hackers,

Can someone post a brief tutorial on the joystick, or point one who is wanting to interface with the joystick in the right direction.

I have found little on the joystick in my research. I am essentially wanting to read the joystick input and do something with it. If regutil can be used, that would be awesome!

Re: Chumby Hackerboard Joystick

The joystick should show up as a standard keyboard.  I'm not at a device right now, but you should try doing this:

hexdump -C /dev/input/by-id/soc-noserial-event-kbd

As you press buttons, you should get events.  On the order of 128 bits per key.  You can ignore the first 64 bits, as it's just a timestamp.  The last 64 bits are the type (EV_KEY), the key code, and whether it's a keydown or a keyup.  There are five keys defined: Up, down, left, right, and enter.

The structure is the same as the touchscreen structure, described here.

Re: Chumby Hackerboard Joystick

Thanks ChumbyLurker!

I will give it a try this evening.

4 (edited by hack2learn 2010-10-11 16:24:29)

Re: Chumby Hackerboard Joystick

hexdump /dev/input/by-id/soc-noserial-event-kbd

The above worked out. I am able to see data spit out to the command line during the joystick operation.

I did notice that for some reason, the hexdump command does not like options. If I issue the command with a -C. I see:

hexdump 1.02
error: cannot open -C for input

However, it will continue to process the joystick input.

Re: Chumby Hackerboard Joystick

Is there a header file associated with the joystick?

What is the appropriate way to read the inputs?

#include <fcntl.h> 
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
                     
main()
{     
  int fd, status;
                 
  fd = open("/dev/input/by-id/soc-noserial-event-kbd", O_RDONLY);
  if (ioctl(fd, KDGETKEYCODE, &status) == -1)                   
  {                                         
        printf("KDGETKEYCODE failed: %s\n",
        strerror(errno));                 
  }                     
  else
  {   
        //Do Something
  }                   
   
  close(fd);
}

This is obviously not correct.

KDGETKEYCODE failed: Invalid argument

Am I getting warm though?

Re: Chumby Hackerboard Joystick

Close.  Include <linux/input.h> and allocate a 'struct input_event' on the stack.  Then use read():

struct input_event evt;
read(fd, &evt, sizeof(evt));
fprintf(stderr, "User %s %s\n", evt.value?"pressed":"release", code_to_key(evt.code));

Then code_to_key() would look like:

const char *code_to_key(int code) {
    if(code == KEY_UP)
        return "up";
    if(code == KEY_RIGHT)
        return "right";
    if(code == KEY_DOWN)
        return "down";
    if(code == KEY_LEFT)
        return "left";
    if(code == KEY_ENTER)
        return "center";
    return "unknown";
}

7 (edited by hack2learn 2010-10-17 06:32:13)

Re: Chumby Hackerboard Joystick

Thanks ChumbyLurker! Below is the finished product.

#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <linux/input.h>
#include <stdio.h>
#include <sys/io.h>
#include <stdlib.h>

struct input_event evt;
int fd, status; 

const char *code_to_key(int code) 
{
    if(code == KEY_UP)
    {
        return "up";
    }
    if(code == KEY_RIGHT)
    {
        return "right";
    }
    if(code == KEY_DOWN)
    {
        return "down";
    }
    if(code == KEY_LEFT)
    {
        return "left";
    }
    if(code == KEY_ENTER)
    {
        return "center";
    }
    
    return "unknown";
}

main()
{
  fd = open("/dev/input/by-id/soc-noserial-event-kbd", O_RDONLY);
  while(1)
  {
        if(read(fd, &evt, sizeof(evt)) > -1)
        {
                fprintf(stdout, "User %s %s\n", evt.value?"pressed":"release", code_to_key(evt.code));
        }
        else
        {
                fprintf(stderr, "An error has occured: %s", strerror(errno));
        }
  }
}