I wrote this code for isolate the problem, but I can't get the library to find the device
System info is:
Code: Select all
$uname -a
Linux rasberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11::49::46 BST 2019 armv7l GNU/Linux
Code: Select all
g++ -std=c++11 1046_reconn.cpp -L/usr/lib -lphidget21
Code: Select all
/**************************************
Name: 1046_reconn.cpp
Desc: demuestra como reconectar
un phidgets 1046_0
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;
}