Created
September 7, 2020 19:38
-
-
Save virtuosonic/116437142c7a9b8a6eff2658ed1ae303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************** | |
Name: 1046_reconn.cpp | |
Desc: demuestra como reconectar | |
un phidgets 1046_0 | |
Autor: Gabriel Espinoza <[email protected]> | |
Date: 05-Sep-2020 | |
License: MIT | |
*/ | |
#include <iostream> | |
#include <thread> | |
#include <phidget21.h> | |
using namespace std; | |
using namespace std::chrono; | |
class ph1046ctl | |
{ | |
public: | |
ph1046ctl(); | |
~ph1046ctl(); | |
void Init(); | |
void Attach1046(); | |
private: | |
//methods | |
void phidgetErr(int result); | |
//props | |
const int timeout; | |
CPhidgetBridgeHandle bridge; | |
}; | |
ph1046ctl::ph1046ctl() : | |
timeout(5000) | |
{ | |
clog << "ph1046\n"; | |
} | |
ph1046ctl::~ph1046ctl() | |
{ | |
clog << "~ph1046\n"; | |
phidgetErr(CPhidget_close((CPhidgetHandle)bridge)); | |
phidgetErr(CPhidget_delete((CPhidgetHandle)bridge)); | |
} | |
void ph1046ctl::Init() | |
{ | |
clog << "Init\n"; | |
Attach1046(); | |
} | |
int AttachHandler(CPhidgetHandle, void*); | |
int DetachHandler(CPhidgetHandle, void* pctl); | |
int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr); | |
void ph1046ctl::Attach1046() | |
{ | |
clog << "Creating bridge object\n"; | |
phidgetErr(CPhidgetBridge_create(&bridge)); | |
phidgetErr(CPhidget_open((CPhidgetHandle)bridge, -1)); | |
//1046 handlers | |
clog << "Setting Handlers\n"; | |
phidgetErr(CPhidget_set_OnAttach_Handler( | |
(CPhidgetHandle)bridge, | |
AttachHandler,nullptr)); | |
phidgetErr(CPhidget_set_OnDetach_Handler( | |
(CPhidgetHandle)bridge, | |
DetachHandler, this)); | |
phidgetErr(CPhidget_set_OnError_Handler( | |
(CPhidgetHandle)bridge, | |
ErrorHandler, NULL)); | |
clog << "Waiting attachment\n"; | |
phidgetErr(CPhidget_waitForAttachment((CPhidgetHandle)bridge, timeout)); | |
} | |
int AttachHandler(CPhidgetHandle, void*) | |
{ | |
clog << "Attach handler\n"; | |
return 0; | |
} | |
int DetachHandler(CPhidgetHandle bridge, void* pctl) | |
{ | |
clog << ("Detach handler\n"); | |
CPhidget_close((CPhidgetHandle)bridge); | |
CPhidget_delete((CPhidgetHandle)bridge); | |
clog << "Pause\n"; | |
this_thread::sleep_for(seconds(2)); | |
auto ctl = (ph1046ctl*)pctl; | |
ctl->Attach1046(); | |
return 0; | |
} | |
int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr) | |
{ | |
clog << "Error handler: " << errorStr << endl; | |
return 0; | |
} | |
void ph1046ctl::phidgetErr(int result) | |
{ | |
if (!result) | |
return; | |
const char *err; | |
CPhidget_getErrorDescription(result, &err); | |
cerr << err <<endl; | |
throw runtime_error(err); | |
} | |
int main() | |
{ | |
clog << "Phidgets 1046 handlers test\n"; | |
ph1046ctl ctl; | |
ctl.Init(); | |
cout << "Press q key to exit\n"; | |
while(cin.get() != 'q') | |
asm("nop"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment