Skip to content
Kokoshnikov Alexey edited this page Mar 23, 2019 · 8 revisions

Welcome to the TrainerControl wiki!

TrainerControl - is dynamic library which provide service for work with Ant+ devices like HR monitor, etc.

API (not stable)

Functions declared in TrainerCondrol.h

InitAntService(void ** ant_instanance, int & max_channels)

Initialize Ant stick instance (via libUSB)

Output:

ant_instanance - in case of success returns pointer to AntStic instance

max_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 instance

guard - instance of std::mutex

Output:

pp_search_service - in case of success returns pointer to SearchService

thread - in case of success returns service thread

int StopSearch(void ** pp_search_service, std::thread & thread)

Destroy SearchService

Input:

pp_search_service - pointer to SearchService

thread - service thread

AddDeviceForSearch(void * p_search_service, AntDeviceType type)

Add request for search device

Input:

p_search_service - pointer to SearchService

type - 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 AntStikc

deviceы - array of pointers to initialized AntChannel instance

num_devices - number of pointers to initialized AntChannel instance

guard - instance of std::mutex

Return value:

Initialized AntSession

int GetDeviceList(void * p_search_service, AntDevice ** devices, int & num_devices)

Fill array of AntDevice by current connected Ant devices

Input:

p_search_service - pointer to running SearchService

Output:

devices - array of pointers to AntDevice

num_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 - AntSession

thread - thread for TelemetryServer

int Stop(AntSession & session, std::thread & thread)

Stop TelemetryServer

Input:

session - AntSession

thread - thread for TelemetryServer

Telemetry GetTelemetry(AntSession & session)

Returns current value of telemetry Input:

session - AntSession

Return value:

current value of telemetry

Structures declared in separate header - structures.h


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;
};

Design

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)
    }

Use case

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;
}

Clone this wiki locally