Home > Back-end >  How to return a variable from the event function?
How to return a variable from the event function?

Time:01-31

I am new to C/C . Currently, I am working on a project in which a haptic device is used. I just want to return a value when the device button is pressed below is my code.

#ifdef  _WIN64
#pragma warning (disable:4996)
#endif

#include <stdio.h>
#include <assert.h>

#if defined(WIN32)
# include <conio.h>
#else
# include "conio.h"
#endif

#include <HD/hd.h>
#include <HL/hl.h>

#include <HDU/hduError.h>

int HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                       HLcache *cache, void *userdata);

/*******************************************************************************
 Main function.
*******************************************************************************/
int btn;
int main(int argc, char *argv[])
{
    HHD hHD;
    HHLRC hHLRC;
    HDErrorInfo error;
    HLerror frameError;

    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError())) 
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "\nPress any key to quit.\n");
        getch();
        return -1;
    }
    hdMakeCurrentDevice(hHD);    
    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC);

    /* Add a callback to handle button down in the collision thread. */
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, 0);
    hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, 0);

    printf("Move around to feel the ambient stick-slip friction.\n\n");
    printf("Press and hold the primary stylus button to feel the spring effect.\n\n");
    printf("Press the second stylus button to trigger an impulse.\n\n");

    /* Run the main loop. */
    while (!_kbhit())
    {
        hlCheckEvents();
        Sleep(3000);

    }
    hlDeleteContext(hHLRC);
    hdDisableDevice(hHD);

    return 0;
}

void HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int btn;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        btn = 1;
        printf("Button 1 pressed %d ", btn);
        return btn;
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        btn = 2;
        printf("Button 2 pressed %d ", btn);
        return btn;

    }
}

/******************************************************************************/

The buttonCB callback function is used as an event. These button events is checked in main loop using this command hlCheckEvents();. In code you can see the int HLCALLBACK buttonCB function that trigger an event when the button is pressed. I want that when the button is pressed, the event should store a value in a variable called btn that can be accessed from the main function. Please help me out with how I do this.

CodePudding user response:

You might use userData to transfer data, for example something like:

int button = 0; // Should be valid as long as event might be called.

/* Add a callback to handle button down in the collision thread. */
hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                   buttonCB, &button);
hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                   buttonCB, &button);
// ..

/* Run the main loop. */
while (!_kbhit())
{
    // button = 0; // Potentially reinit here
    hlCheckEvents();
    printf("button value: %d", button); // Do work with last value
    Sleep(3000);
}

and then your callback

void HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int* button = (int*) userdata;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        *button = 1;
        printf("Button 1 pressed %d ", btn);
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        *button = 1; = 2;
        printf("Button 2 pressed %d ", btn);
    }
}

CodePudding user response:

I am not really into event handling. But I'll try to help.

You are returning btn from the function, which is of the type int, but the function return type is void, which means the function cannot return anything. So, the fix is simple. Change the return type of the buttonCB function from void to int:

/*void*/ int HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int btn;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        btn = 1;
        printf("Button 1 pressed %d ", btn);
        return btn;
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        btn = 2;
        printf("Button 2 pressed %d ", btn);
        return btn;

    }
}

Now the return type of the function is int, that is the type of your btn variable that you're returning.

  •  Tags:  
  • Related