#pragma once #include #include #include #include #include #include #include "user_signature.h" #include "proximity_algorithm.h" class HidDevice { public: explicit HidDevice(int avgLength = 8, bool logVerbose = false); ~HidDevice(); // Non-copyable, non-movable HidDevice(const HidDevice&) = delete; HidDevice& operator=(const HidDevice&) = delete; // Returns true if device was opened successfully bool Open(uint16_t vid, uint16_t pid); // Close the device (also called by destructor) void Close(); // Query state bool IsOpen() const; // --- Reader thread API --- // Launch background reader thread that handles open/read/reconnect internally void StartReading(uint16_t vid, uint16_t pid, uint16_t rateMs = 200); // Stop the reader thread and close device void StopReading(); // Lock-free cached proximity distance from latest '#' periodic report uint16_t GetProxDistance() const; // Connection state: 0=closed, 1=open, 2=reconnecting int GetConnectionState() const; // Thread-safe copy of calibration data read from user signature flash CalibrationData GetCalibration() const; // Thread-safe copy of lighthouse tracking serial from user flash (tag 0x09) std::string GetTrackingSerial() const; // Proximity algorithm results (lock-free, safe from any thread) bool GetPersonDetected() const; ProximityAlgorithm::DiagState GetAlgorithmDiag() const; private: hid_device* m_pDevice; // Reader thread state std::thread m_readerThread; std::atomic m_bStopRequested{false}; std::atomic m_lastProxDistance{0}; std::atomic m_connectionState{0}; // 0=closed, 1=open, 2=reconnecting // Calibration data (protected by mutex -- not atomic-safe) CalibrationData m_calibration; mutable std::mutex m_calMutex; // Verbose logging flag bool m_logVerbose = false; // Proximity algorithm (owned, fed from reader thread) ProximityAlgorithm m_proximityAlgorithm; // Reader thread entry point void ReaderThreadFunc(uint16_t vid, uint16_t pid, uint16_t rateMs); // HID communication helpers bool SendFeatureReport(uint8_t cmdCode, const uint8_t* data, size_t dataLen); int ReadReport(uint8_t* buf, size_t bufLen, int timeoutMs); bool SetReportRate(uint16_t rateMs); bool ReadUserSignature(uint8_t* sigOut, size_t sigLen); void ReadCalibration(); };