blob: 42902e764f67b9812950848b18dc34797d4f74c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#undef NDEBUG
#include "trackhat.hpp"
#include "compat/sleep.hpp"
#include "compat/timer.hpp"
#include <cassert>
bool camera_handle::ensure_connected()
{
if (state_ >= st_streaming)
return true;
else if (state_ == st_stopped)
return false;
Timer t;
constexpr int max_attempts = 5;
if (!ensure_device_exists())
goto error;
for (int i = 0; i < max_attempts; i++)
{
if (!th_check(trackHat_Connect(&device_, TH_FRAME_EXTENDED)))
{
state_ = st_streaming;
if (int ms = (int)t.elapsed_ms(); ms > 1000)
qDebug() << "tracker/trackhat: connecting took" << ms << "ms";
return true;
}
auto dbg = qDebug();
dbg << "tracker/trackhat: connect failed, retry";
dbg.space(); dbg.nospace();
dbg << (i+1) << "/" << max_attempts;
portable::sleep(50);
}
error:
disconnect();
return false;
}
bool camera_handle::ensure_device_exists()
{
switch (state_)
{
case st_streaming:
return true;
case st_detected:
disconnect();
[[fallthrough]];
case st_stopped:
assert(!th_check(trackHat_Initialize(&device_)) && device_.m_pInternal);
if (auto error = th_check(trackHat_DetectDevice(&device_)); error)
{
disconnect();
return false;
}
state_ = st_detected;
return true;
}
}
void camera_handle::disconnect()
{
state_ = st_stopped;
if (device_.m_pInternal)
{
(void)!th_check(trackHat_Disconnect(&device_));
trackHat_Deinitialize(&device_);
}
}
|