summaryrefslogtreecommitdiffhomepage
path: root/tracker-trackhat/handle.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-05-11 20:42:25 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-05-11 20:45:05 +0200
commit2b19c6c532c37c2eeb2547cf92f719197957ab72 (patch)
tree26fb69b756d8ae49fb87a18956eca26dd2c55005 /tracker-trackhat/handle.cpp
parentbe42a20f03e3ccb1eaa390c072ac72cbe2d9f13b (diff)
tracker/trackhat: new tracker
Sponsored by: TrackHat
Diffstat (limited to 'tracker-trackhat/handle.cpp')
-rw-r--r--tracker-trackhat/handle.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/tracker-trackhat/handle.cpp b/tracker-trackhat/handle.cpp
new file mode 100644
index 00000000..42902e76
--- /dev/null
+++ b/tracker-trackhat/handle.cpp
@@ -0,0 +1,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_);
+ }
+}