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.