Example usage
// Copyright (c) 2025 Touchlab Limited. All Rights Reserved
// Unauthorized copying or modifications of this file, via any medium is strictly prohibited.
#include <chrono>
#include <iomanip>
#include <iostream>
#include <memory>
#include <numeric>
#include <vector>
#include <thread>
#include "touchlab_comm/touchlab_comm.hpp"
using touchlab_comm::TouchlabComms;
int main(int argc, char ** argv)
{
if (argc == 3 || argc == 2) {
std::cout << "Version: " << TouchlabComms::get_version() << "\n";
// Connect to the sensor
TouchlabComms com;
if (argc == 3) {
com.init(argv[2]);
} else if (argc == 2) {
com.init();
}
com.connect(argv[1]);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
com.zero();
// Set up frequency counting
std::cout << "Reading sensor data:\n";
auto start = std::chrono::system_clock::now();
int64_t count = 0;
double hz = 0.0;
// Reserve sensor data buffer
std::vector<double> raw_data;
std::vector<double> data;
raw_data.reserve(100);
data.reserve(100);
while (com.is_connected()) {
// Read data
com.read(data);
com.read_raw(raw_data, 0);
// Update time
std::chrono::duration<double> diff = std::chrono::system_clock::now() - start;
// Print update rate and raw data
std::cout << "Rate: " << std::fixed << std::setprecision(1) << hz << "Hz, Raw data: ";
for (double value : raw_data) {
std::cout << value << " ";
}
std::cout << "\n";
// Print update rate and triaxial data
std::cout << "Rate: " << std::fixed << std::setprecision(1) << hz << "Hz, Calibrated data: ";
for (double value : data) {
std::cout << value << " ";
}
std::cout << "\n";
// Update sensor rate
if (diff.count() > 0.5) {
hz = static_cast<double>(count) / diff.count();
start = std::chrono::system_clock::now();
count = 0;
}
++count;
}
} else {
std::cout << "Usage:\ntouchlab_comm_example <serial_port> [<path_to_sensor_param_file>]\n";
exit(1);
}
return 0;
}
To build this project using CMake:
cmake_minimum_required(VERSION 3.10.0)
project(touchlab_comm_example_cpp)
set(CMAKE_CXX_STANDARD 17)
find_package(touchlab_comm REQUIRED)
add_executable(${PROJECT_NAME} example.cpp)
target_link_libraries(${PROJECT_NAME} touchlab_comm)
set(INCLUDE_INSTALL_DIR include/ CACHE PATH INTERNAL)
set(BIN_INSTALL_DIR bin/ CACHE PATH INTERNAL)
set(LIB_INSTALL_DIR lib/ CACHE PATH INTERNAL)
set(SAHRE_INSTALL_DIR share/ CACHE PATH INTERNAL)
# Install
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)