Browse Source

Arduino style callbacks from channel status & device ID

main
PaulStoffregen 7 years ago
parent
commit
1613316f7c
3 changed files with 60 additions and 21 deletions
  1. +8
    -6
      USBHost_t36.h
  2. +26
    -14
      antplus.cpp
  3. +26
    -1
      examples/AntPlus/AntPlus.ino

+ 8
- 6
USBHost_t36.h View File

@@ -865,6 +865,12 @@ class AntPlus: public USBDriver {
public:
AntPlus(USBHost &host) : /* txtimer(this),*/ updatetimer(this) { init(); }
void begin(const uint8_t key=0);
void onStatusChange(void (*function)(int channel, int status)) {
user_onStatusChange = function;
}
void onDeviceID(void (*function)(int channel, int devId, int devType, int transType)) {
user_onDeviceID = function;
}
protected:
virtual void Task();
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
@@ -910,11 +916,6 @@ private:
PROFILE_CADENCE,
PROFILE_TOTAL
};
typedef struct {
uint16_t deviceId;
uint8_t deviceType;
uint8_t transType;
} TDEVICET;
typedef struct {
uint8_t channel;
uint8_t RFFreq;
@@ -927,7 +928,6 @@ private:
uint16_t channelPeriod;
uint16_t searchWaveform;
uint32_t deviceNumber; // deviceId
TDEVICET dev;
struct {
uint8_t chanIdOnce;
uint8_t keyAccepted;
@@ -944,6 +944,8 @@ private:
} TLIBANTPLUS;
TLIBANTPLUS ant;
int (*callbackFunc)(uint32_t msg, intptr_t *value1, uint32_t value2);
void (*user_onStatusChange)(int channel, int status);
void (*user_onDeviceID)(int channel, int devId, int devType, int transType);
void dispatchPayload(TDCONFIG *cfg, const uint8_t *payload, const int len);
static const uint8_t *getAntKey(const uint8_t keyIdx);
static uint8_t calcMsgChecksum (const uint8_t *buffer, const uint8_t len);

+ 26
- 14
antplus.cpp View File

@@ -56,6 +56,8 @@ void AntPlus::init()
contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
driver_ready_for_device(this);
callbackFunc = NULL;
user_onStatusChange = NULL;
user_onDeviceID = NULL;
}

bool AntPlus::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
@@ -388,9 +390,12 @@ void AntPlus::sendMessageChannelStatus(TDCONFIG *cfg, const uint32_t channelStat
{
cfg->flags.channelStatus = channelStatus;
if (cfg->flags.channelStatus != cfg->flags.channelStatusOld) {
uint32_t status = cfg->flags.channelStatus&0x0F;
status |= ((cfg->channel&0x0F)<<4);
sendMessage(ANTP_MSG_CHANNELSTATUS, NULL, status);
//uint32_t status = cfg->flags.channelStatus&0x0F;
//status |= ((cfg->channel&0x0F)<<4);
//sendMessage(ANTP_MSG_CHANNELSTATUS, NULL, status);
if (user_onStatusChange) {
(*user_onStatusChange)(cfg->channel, cfg->flags.channelStatus);
}
cfg->flags.channelStatusOld = cfg->flags.channelStatus;
}
}
@@ -597,21 +602,28 @@ void AntPlus::message_event(const int channel, const int msgId,
break;

case MESG_CAPABILITIES_ID:
//printf(" @ capabilities:");
//printf(" Max ANT Channels: %i",payload[STREAM_CAP_MAXCHANNELS]);
//printf(" Max ANT Networks: %i",payload[STREAM_CAP_MAXNETWORKS]);
//printf(" Std. option: 0x%X",payload[STREAM_CAP_STDOPTIONS]);
//printf(" Advanced: 0x%X",payload[STREAM_CAP_ADVANCED]);
//printf(" Advanced2: 0x%X",payload[STREAM_CAP_ADVANCED2]);
printf(" @ capabilities:");
printf(" Max ANT Channels: %i",payload[STREAM_CAP_MAXCHANNELS]);
printf(" Max ANT Networks: %i",payload[STREAM_CAP_MAXNETWORKS]);
printf(" Std. option: 0x%X",payload[STREAM_CAP_STDOPTIONS]);
printf(" Advanced: 0x%X",payload[STREAM_CAP_ADVANCED]);
printf(" Advanced2: 0x%X",payload[STREAM_CAP_ADVANCED2]);
break;

case MESG_CHANNEL_ID_ID:
//TDCONFIG *cfg = (TDCONFIG*)&ant->dcfg[chan];
ant.dcfg[chan].dev.deviceId = payload[STREAM_CHANNELID_DEVNO_LO] | (payload[STREAM_CHANNELID_DEVNO_HI] << 8);
ant.dcfg[chan].dev.deviceType = payload[STREAM_CHANNELID_DEVTYPE];
ant.dcfg[chan].dev.transType = payload[STREAM_CHANNELID_TRANTYPE];
//ant.dcfg[chan].dev.deviceId = payload[STREAM_CHANNELID_DEVNO_LO] | (payload[STREAM_CHANNELID_DEVNO_HI] << 8);
//ant.dcfg[chan].dev.deviceType = payload[STREAM_CHANNELID_DEVTYPE];
//ant.dcfg[chan].dev.transType = payload[STREAM_CHANNELID_TRANTYPE];
//printf(" @ CHANNEL ID: channel %i, deviceId:%i, deviceType:%i, transType:%i)", chan, cfg->dev.deviceId, cfg->dev.deviceType, cfg->dev.transType);
sendMessage(ANTP_MSG_DEVICEID, (intptr_t *)&(ant.dcfg[chan].dev), chan);
//sendMessage(ANTP_MSG_DEVICEID, (intptr_t *)&(ant.dcfg[chan].dev), chan);
if (user_onDeviceID) {
int devid = payload[STREAM_CHANNELID_DEVNO_LO];
devid |= payload[STREAM_CHANNELID_DEVNO_HI] << 8;
int devtype = payload[STREAM_CHANNELID_DEVTYPE];
int transtype = payload[STREAM_CHANNELID_TRANTYPE];
(*user_onDeviceID)(chan, devid, devtype, transtype);
}
#if 0
if (cfg->dev.scidDeviceType != cfg->deviceType){
printf(" @ CHANNEL ID: this is not the device we're looking for");
@@ -623,7 +635,7 @@ void AntPlus::message_event(const int channel, const int msgId,
break;

case MESG_VERSION_ID:
//printf(" @ version: '%s'", (char*)&payload[STREAM_VERSION_STRING]);
printf(" @ version: '%s'", (char*)&payload[STREAM_VERSION_STRING]);
break;
};
}

+ 26
- 1
examples/AntPlus/AntPlus.ino View File

@@ -10,9 +10,34 @@ void setup() {
Serial.println("Ant+ USB Test");
myusb.begin();
ant1.begin();

ant1.onStatusChange(handleStatusChange);
ant1.onDeviceID(handleDeviceID);
}

void loop() {
myusb.Task();
}

void handleStatusChange(int channel, int status) {
Serial.print("Channel ");
Serial.print(channel);
Serial.print(" status: ");
switch (status) {
case 0: Serial.println("STATUS UNASSIGNED CHANNEL"); break;
case 2: Serial.println("STATUS ASSIGNED CHANNEL"); break;
case 3: Serial.println("STATUS SEARCHING CHANNEL"); break;
case 4: Serial.println("STATUS TRACKING_CHANNEL"); break;
default: Serial.println("UNKNOWN STATUS STATE");
}
}

void handleDeviceID(int channel, int devId, int devType, int transType) {
Serial.print("Device found on channel ");
Serial.print(channel);
Serial.print(": deviceId:");
Serial.print(devId);
Serial.print(", deviceType:");
Serial.print(devType);
Serial.print(", transType:");
Serial.println(transType);
}

Loading…
Cancel
Save