0

I'm trying to control a joystick, written some code using the API to do so. I've read the number of axes on the joystick in function a using ioctl(fd, JSIOCGAXES, &axes); and want to then print the axis that was moved in the event-handling function (function b):

char whichaxis[axes] = {'X','Y','Y','X'};
printf("%c%c |%8hd\n",whichjs,whichaxis[jse.number],jse.value);

This code should print something like LX| -32768, saying that the left joystick has moved in the x direction.

However, this returns an error, as I am calling axes in function b but it is not defined in function b. So my question is, how can I use axes despite it not being defined in function b?

Here is the code

// Returns info about the joystick device
void print_device_info(int fd) {
    int axes=0, buttons=0;
    char name[128];
    ioctl(fd, JSIOCGAXES, &axes);
    ioctl(fd, JSIOCGBUTTONS, &buttons);
    ioctl(fd, JSIOCGNAME(sizeof(name)), &name);
    printf("%s\n  %d Axes %d Buttons\n", name, axes, buttons);
}

// Event handler
void process_event(struct js_event jse) {
     // Define which axis is which
     //        Axis number {0,1,2,3} */
     char whichaxis[axes] = {'X','Y','Y','X'};
     //Define which joystick is moved
     char whichjs = '*';
     switch(jse.number) {
         case 0: case 1:
             whichjs = 'L';
             break;
         case 2: case 3:
             whichjs = 'R';
             break;
         default:
             whichjs = '*';
             break;
     }
     // Print which joystick, axis and value of the joystick position
     printf("%c%c |%8hd\n",whichjs,whichaxis[jse.number],jse.value);
}
3
  • 1
    Please post some details (function where it is defined and function b). Commented Nov 5, 2013 at 7:48
  • 1
    In printf("%c%c |%8hd\n",whichjs,whichaxis,jse.value); whichaxis should be whichaxis[jse.number]. whichaxis is a pointer. Commented Nov 5, 2013 at 8:32
  • good point- changed in code Commented Nov 6, 2013 at 20:09

2 Answers 2

2

Either, you can use a global variable axes or pass axes as a parameter in both or one of the function.

If it is not possible to pass as a parameter (because it is a callback), then you can make function such as GetAxes() to give current axes or use a global variable.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd say use a global or a non-const pointer to axes, so you can modify it.
Also, how do I pass axes as a parameter?
@theoB610: In your case, it seems a problem to pass as parameter to handler function. You can use a global variable.
1

axes is a local variable that is declared inside a function. A local variable can only be used in the function where it is declared. A global variable is a variable that is declared outside all functions. So make axes a global variable which can be used in all functions.

int axes; // Global declaration makes axes which as scope in all below functions

void print_device_info(int fd) {
    ...
    ioctl(fd, JSIOCGAXES, &axes);
    ...

void process_event(struct js_event jse) {
    char whichaxis[axes] = {'X','Y','Y','X'};
    ...

2 Comments

how? logically it makes sense to define it in the device info function
@theoB610 int axes in function print_device_info() has local scope, making the variable is valid and can use within that function. If this is declared as global it is accessible in multiple scopes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.