Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return a variable from the event function?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading