How do I add a second Device Type?

58 views
Skip to first unread message

rlib...@googlemail.com

unread,
Apr 4, 2018, 3:03:22 PM4/4/18
to ant-api

Hi, sorry for my noobish question, I just started in using C++


I was wondering how do I add a second “Device Type” using the ANT library you created? For example, I like to read Muscle Oxygen data and HR data, do I need to create a separate class for each device type?

I manage to read Muscle Oxygen using your example code “callBacks” example:

void setup()

{

    AssignChannel ac;

    ResetSystem rs;

    SetNetworkKey snk;

    ChannelId ci;

    ChannelPeriod cp;

    ChannelRfFrequency crf;

    OpenChannel oc;

    StartUpMessage sm;

 

    Serial1.begin(BAUD_RATE);

    ant.onBroadcastData(handleBroadcastMessage);

    ant.onStartUpMessage(handleStartUpMessage);

    ant.onPacketError(handleAntError);

    ant.onChannelEventResponse(handleChannelEventResponse);

    ant.onOtherResponse(handleMessageWithNoCallback);

    ant.begin(Serial1);

 

    Serial.begin(BAUD_RATE);

    delay(1000);

    // Delay to give the user time to connect on serial

 

    Serial.println("Running");

 

    ant.send(rs);

    ant.waitFor(sm, 2000, itsAlive); // wait 2s for device to start

 

    snk = SetNetworkKey();

    snk.setNetwork(0);

    snk.setKey((uint8_t*)NETWORK_KEY);

    ant.send(snk);

    if(ant.waitForStatus(snk.getMsgId(), 1000)) {

        Serial.println("No Response for setting network key");

    } else {

        ChannelEventResponse cer = ChannelEventResponse();

        ant.getResponse().getChannelEventResponseMsg(cer);

        handleChannelEventResponse(cer, 0);

    }

 

    ac = AssignChannel();

    ac.setChannel(0);

    ac.setChannelType(CHANNEL_TYPE_BIDIRECTIONAL_RECEIVE); //can't wildcard this

    ac.setChannelNetwork(0);

    if (ant.sendAndWait(ac, 2000)) {

        Serial.println("No Response for assigning channel");

    } else {

        ChannelEventResponse cer = ChannelEventResponse();

        ant.getResponse().getChannelEventResponseMsg(cer);

        handleChannelEventResponse(cer, 0);

    }

 

    ci = ChannelId();

    ci.setChannel(0);

    ci.setDeviceNumber(0);

    ci.setDeviceType(31);

    ci.setTransmissionType(0);

    ant.send(ci);

    ant.loop();

 

    cp = ChannelPeriod();

    cp.setChannel(0);

    cp.setPeriod(8192); //can't wildcard this

    ant.send(cp);

    ant.loop();

 

    crf = ChannelRfFrequency();

    crf.setChannel(0);

    crf.setRfFrequency(57); //can't wildcard this

    ant.send(crf);

    ant.loop();

 

    oc = OpenChannel();

    oc.setChannel(0);

    ant.send(oc);

    ant.loop();

}

 

Do I need to create a second class for the HR data?

 

 

PS thanks for your ANT library. 

Curtis Malainey

unread,
Apr 4, 2018, 4:40:12 PM4/4/18
to ant-api
Hello rlibralon,

No worries, we were all noobs at one point. I welcome all questions.

So what you will need to do is setup 2 different channels, one for each device. The ANT radio you have will likely support somewhere between 8-15 channels
(I think the AntVersion example can read that off radio for you.) Device Type is assigned on a per channel basis. So you will have to send another AssignChannel,
ChannelId, ChannelPeriod, ChannelRfFrequency, OpenChannel set of messages for the second channel to set it up on the radio. In your callback for the broadcast data, you can figure out which device the data came from by calling getChannelNumber() on the message. 

The library is designed to be as economical as possible with the amount of memory available so what you can do is reuse all the messages to configure the first channel
and just change the properties then resend the messages to configure the new channel. Here is stripped down an example of reusing a message for 2 channels.

void setup()
{
    AssignChannel ac;

    /* setup the driver here*/

    // setup muscle oxygen channel here here
    ci = ChannelId();
    ci.setChannel(0); // <-- Note: Channel 0 here
    ci.setDeviceNumber(0);
    ci.setDeviceType(MUSCLE_OXYGEN_DEVICE_TYPE);
    ci.setTransmissionType(0);
    ant.send(ci);
    ant.loop();

    ci.setChannel(1); // <-- Note: Channel 1 here
    ci.setDeviceNumber(0);
    ci.setDeviceType(HR_DEVICE_TYPE);
    ci.setTransmissionType(0);
    ant.send(ci);
    ant.loop();

    /* do the rest of the setup */
}

Does that make sense? Let me know if you have any other questions.

Also, I apologize, my antplus library is supposed to make connecting to multiple ANT+ devices easy but I have not added support for Muscle oxygen yet.

Curtis

Reply all
Reply to author
Forward
0 new messages