joystick control program in c++

This is a discussion on joystick control program in c++ within the C Programming forums, part of the General Programming Boards category; Hai friends i am recently to use joystick inputs for my robotic project. for that i am actually trying to ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    joystick control program in c++

    Hai friends

    i am recently to use joystick inputs for my robotic project. for that i am actually trying to run the individual testing of sub classes . when i run the code i am getting the following error

    In function int main()

    error : " calloc was declared out of scope

    The following is the code of program

    The project was compiled in ubuntu 10.10

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/joystick.h>
    
    #define JOY_DEV "/dev/js0"
    
    int main()
    {
        int joy_fd, *axis= NULL, num_of_axis = 0, num_of_buttons=0,x;
        
        char *button = NULL , name_of_joystick[80];
        
        struct js_event js;
        // input of joystick values to variable joy_fd
            
        if((joy_fd = open(JOY_DEV,O_RDONLY))== -1 )
        {
            printf(" couldn't open the joystick \n " );
            
            return -1;
        }
    
        ioctl(joy_fd, JSIOCGAXES , &num_of_axis);
        ioctl(joy_fd, JSIOCGBUTTONS , &num_of_buttons);
        ioctl(joy_fd, JSIOCGNAME(80), &name_of_joystick);
        
            axis = (int *) calloc(num_of_axis , sizeof(int));
            button = (char *) calloc( num_of_buttons , sizeof (char));
        
        printf( " Joy stick detected : %s \n \t %d axis \n\t %d buttons \n\n" ,name_of_joystick , num_of_axis , num_of_buttons);
    
        fcntl( joy_fd, F_SETFL , O_NONBLOCK ); // use non - blocking methods
    
        while(1) // infinite loop
    
        {
            // read the joystick 
            
            read (joy_fd, &js , sizeof(struct js_event));
    
            // see what to do with the event
    
            switch(js.type & ~ JS_EVENT_INIT)
            {
                case JS_EVENT_AXIS :
                
                    axis [ js.number ] = js.value;
                
                case JS_EVENT_BUTTON :
                
                    button [js.number ] = js.value;
            }
    
            // print the results
    
    
            printf( " X: %6d y: %6d ", axis[0] , axis[1]);
            
            if( num_of_axis > 2)
    
                printf( " Z: %6d " , axis[2] );
            if( num_of_axis > 3)
    
                printf( " R : %6d " , axis [3]);
     
            for( x=0 ; x<num_of_buttons ; ++x)
            
                printf( "B %d : %d " , x, button [x]);
            fflush(stdout);
    
        }
        
        close(joy_fd);
    
    return 0;
    }

    can some one help me in this regard
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    calloc is in stdlib.h.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Lets see, you write a topic title containing "c++", yet you post in the C section of the forum.

    Things improve with your post of actual C code.

    But then it all heads south again which the .cpp attachment.

    Are you entirely clear on which language you're using, or are you in the mistaken belief that they are somehow fully interchangeable?

    Oh, and your switch/cases need break statements as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. joystick program
    By Titanium in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2008, 11:19 AM
  2. how to input joystick simply?
    By denyilc in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2006, 01:35 AM
  3. Getting joystick movements.
    By Queatrix in forum C++ Programming
    Replies: 1
    Last Post: 01-04-2006, 01:58 PM
  4. Joystick
    By merlin371 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 07:15 PM
  5. I'm looking for a Joystick controller
    By Raideen in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2003, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21