-
Notifications
You must be signed in to change notification settings - Fork 0
Home
TrainerControl - is dynamic library which provide service for work with Ant+ devices like HR monitor, etc.
-
int InitAntService(void ** ant_instanance, int & max_channels)
Initialize Ant stick instance (via libUSB)
Output:
ant_instanance
- in case of success returns pointer to AntStic instancemax_channels
- max number of Ant devices which can be connected with Ant stick in same time -
int CloseAntService()
Destroy Ant stick instance
-
int RunSearch(void * ant_instanance, void ** pp_search_service, std::thread & thread, std::mutex & guard)
Run service for find and keep Ant devices
Input:
ant_instanance
- pointer to initialized AntStick instanceguard
- instance of std::mutexOutput:
pp_search_service
- in case of success returns pointer to SearchServicethread
- in case of success returns service thread -
int StopSearch(void ** pp_search_service, std::thread & thread)
Destroy SearchService
Input:
pp_search_service
- pointer to SearchServicethread
- service thread -
int AddDeviceForSearch(void * p_search_service, AntDeviceType type)
Add request for search device
Input:
p_search_service
- pointer to SearchServicetype
- type of device -
AntSession InitSession(void * ant_instanance, AntDevice ** devices, int num_devices, std::mutex & guard)
Initialize AntSession and create TelemetryServer
Input:
ant_instanance
- pointer to AntStikcdeviceы
- array of pointers to initialized AntChannel instancenum_devices
- number of pointers to initialized AntChannel instanceguard
- instance of std::mutexReturn value:
Initialized AntSession
-
int GetDeviceList(void * p_search_service, AntDevice ** devices, unsigned int & num_devices, unsigned int & num_active_devices)
Fill array of AntDevice by current connected Ant devices
Input:
p_search_service
- pointer to running SearchServiceOutput:
devices
- array of pointers to AntDevicenum_devices
- number of registered ant channels (more or equalnum_active_devices
)num_active_devices
- number of connected ant devices -
int CloseSession(AntSession & session)
Destroy AntSession (destroy TelemetryServer but not HR Monitor and "bike")
Input:
session
- AntSession for destroy -
int Run(AntSession & session, std::thread & thread)
Run TelemetryServer
Input:
session
- AntSessionthread
- thread for TelemetryServer -
int Stop(AntSession & session, std::thread & thread)
Stop TelemetryServer
Input:
session
- AntSessionthread
- thread for TelemetryServer -
Telemetry GetTelemetry(AntSession & session)
Returns current value of telemetry
Input:
session
- AntSessionReturn value:
current value of telemetry
struct AntSession
{
void * m_AntStick;
void * m_TelemtryServer;
bool m_bIsRun;
};
struct Telemetry
{
Telemetry()
: hr(-1), cad(-1), spd(-1), pwr(-1) {}
double hr;
};
struct AntDevice
{
AntDevice() : m_type(NONE_Type), m_device(nullptr) {}
AntDeviceType m_type;
void * m_device;
};
Application thread:
{
// parent thread for SearchService and TelemertyServer threads
}
SearchService thread:
{
// finding Ant devices, create instance and keep them
// read all messages from AntStick, send messages from device to right instance
// check current status of connected devices, destroy instance if need
}
TelemertyServer thread:
{
// check status, telemetry instances of Ant devices (which stored by SeachService)
// handling user's actions (change configuration of device, etc)
}
int main()
{
// create instance AntStick instance
void * ant_handle;
int max_channels = 0;
InitAntService(&ant_handle, max_channels);
// Run SearchService
std::thread search_thread;
std::mutex guard;
void * search_service;
RunSearch(ant_handle, &search_service, search_thread, guard);
// add channel for search HR monitor
AddDeviceForSearch(search_service, HRM_Type);
// wait for connection HR monitor
AntDevice ** device_list = new AntDevice*[max_channels];
for (int i = 0; i < max_channels; i++)
device_list[i] = new AntDevice();
int num_hrm = 0;
int id_hrm[max_channels];
memset(id_hrm, -1, max_channels);
while (num_hrm == 0)
{
int num_devices = 0;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
GetDeviceList(search_service, device_list, num_devices);
for (int i = 0; i < num_devices; i++)
if (device_list[i]->m_type == HRM_Type)
{
id_hrm[num_hrm] = i;
num_hrm++;
}
}
// Init AntSession with connected HR monitor
AntSession ant_session = InitSession(ant_handle, &device_list[id_hrm[0]], 1, guard);
// Run TelemetryServer
std::thread server_thread;
Run(ant_session, server_thread);
//while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
Telemetry t = GetTelemetry(ant_session);
printf("HR = %lf\n", t.hr);
}
// stop TelemetryServer
Stop(ant_session, server_thread);
// destroy AntSession
CloseSession(ant_session);
// destroy SearchService
StopSearch(&search_service, search_thread);
// destroy AntStick instance
CloseAntService();
return 0;
}