Doly C++ SDK v1.00
Loading...
Searching...
No Matches
TofControl/main.cpp

Demonstrates:

  • Initializing TofControl
  • Receiving distance measurements
  • Handling ToF sensor events
#include <spdlog/spdlog.h>
#include <thread>
#include "TofControl.h"
#include "Helper.h"
void onProximityGesture(TofGesture left, TofGesture right)
{
spdlog::info("TOF GESTURE L[{}] R[{}] ", static_cast<int>(left.type), static_cast<int>(right.type));
}
void onProximityThreshold(TofData left, TofData right)
{
spdlog::warn("TOF PROXIMITY L[{}] R[{}] ", static_cast<int>(left.range_mm), static_cast<int>(right.range_mm));
}
int main()
{
// Setup spdlog
spdlog::set_level(spdlog::level::info); // Set as needed
spdlog::flush_on(spdlog::level::trace); // flush everything
// *** IMPORTANT ***
// Stop doly service if running,
// otherwise instance of libraries cause conflict
spdlog::error("Doly service stop failed");
return -1;
}
// Initialize ToF Control
if (TofControl::init() < 0)
{
spdlog::error("TofControl init failed");
return -2;
}
// Setup continues ToF Control with 50ms interval and proximity threshold 40mm (enabled)
{
spdlog::error("TofControl setup failed");
return -3;
}
// Get TofControl version
spdlog::info("TofControl Version:{:.3f}", TofControl::getVersion());
// Add event listener for touch state changes and activities
// Run for 30 seconds to test sensors
std::this_thread::sleep_for(std::chrono::seconds(30));
// Stop continuous reading and disable events
// Single read example
std::vector<TofData> sensors = TofControl::getSensorsData();
spdlog::info("Single Read Sensor[{}] Range[{}mm] Error[{}]", static_cast<int>(sensors[0].side), sensors[0].range_mm, static_cast<int>(sensors[0].error));
spdlog::info("Single Read Sensor[{}] Range[{}mm] Error[{}]", static_cast<int>(sensors[1].side), sensors[1].range_mm, static_cast<int>(sensors[1].error));
// Cleanup
return 0;
}
Shared helper utilities and common types used across the Doly SDK.
Public API for Doly ToF (Time of Flight) sensor control.
int8_t stopDolyService()
Stop the background Doly service (if running).
int8_t init(int8_t offset_left=0, int8_t offset_right=0)
Initialize ToF sensors.
int8_t setup_continuous(uint16_t interval_ms=50, uint8_t distance=0)
Configure continuous reading for gesture detection and threshold events.
float getVersion()
Get current library version.
int8_t dispose()
Dispose/stop the ToF subsystem and release resources.
std::vector< TofData > getSensorsData()
Read both sensors once and return the latest values.
void AddListenerOnProximityGesture(void(*onEvent)(TofGesture left, TofGesture right))
Register a static/free function to receive proximity gesture events.
void RemoveListenerOnProximityGesture(void(*onEvent)(TofGesture left, TofGesture right))
Unregister a previously registered proximity gesture callback.
void RemoveListenerOnProximityThreshold(void(*onEvent)(TofData left, TofData right))
Unregister a previously registered proximity threshold callback.
void AddListenerOnProximityThreshold(void(*onEvent)(TofData left, TofData right))
Register a static/free function to receive proximity threshold events.
Latest ToF sample snapshot for one sensor.
Definition TofEventListener.h:89
int range_mm
Definition TofEventListener.h:93
Gesture description for a ToF sensor.
Definition TofEventListener.h:78
TofGestureType type
Definition TofEventListener.h:80