1
0
mirror of https://github.com/gdsports/USBHost_t36 synced 2025-01-30 23:00:12 -05:00

Arduino style callbacks from channel status & device ID

This commit is contained in:
PaulStoffregen 2017-10-20 07:50:38 -07:00
parent 94b2decbca
commit 1613316f7c
3 changed files with 60 additions and 21 deletions

View File

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

View File

@ -56,6 +56,8 @@ void AntPlus::init()
contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t)); contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
driver_ready_for_device(this); driver_ready_for_device(this);
callbackFunc = NULL; callbackFunc = NULL;
user_onStatusChange = NULL;
user_onDeviceID = NULL;
} }
bool AntPlus::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len) 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; cfg->flags.channelStatus = channelStatus;
if (cfg->flags.channelStatus != cfg->flags.channelStatusOld) { if (cfg->flags.channelStatus != cfg->flags.channelStatusOld) {
uint32_t status = cfg->flags.channelStatus&0x0F; //uint32_t status = cfg->flags.channelStatus&0x0F;
status |= ((cfg->channel&0x0F)<<4); //status |= ((cfg->channel&0x0F)<<4);
sendMessage(ANTP_MSG_CHANNELSTATUS, NULL, status); //sendMessage(ANTP_MSG_CHANNELSTATUS, NULL, status);
if (user_onStatusChange) {
(*user_onStatusChange)(cfg->channel, cfg->flags.channelStatus);
}
cfg->flags.channelStatusOld = cfg->flags.channelStatus; cfg->flags.channelStatusOld = cfg->flags.channelStatus;
} }
} }
@ -597,21 +602,28 @@ void AntPlus::message_event(const int channel, const int msgId,
break; break;
case MESG_CAPABILITIES_ID: case MESG_CAPABILITIES_ID:
//printf(" @ capabilities:"); printf(" @ capabilities:");
//printf(" Max ANT Channels: %i",payload[STREAM_CAP_MAXCHANNELS]); printf(" Max ANT Channels: %i",payload[STREAM_CAP_MAXCHANNELS]);
//printf(" Max ANT Networks: %i",payload[STREAM_CAP_MAXNETWORKS]); printf(" Max ANT Networks: %i",payload[STREAM_CAP_MAXNETWORKS]);
//printf(" Std. option: 0x%X",payload[STREAM_CAP_STDOPTIONS]); printf(" Std. option: 0x%X",payload[STREAM_CAP_STDOPTIONS]);
//printf(" Advanced: 0x%X",payload[STREAM_CAP_ADVANCED]); printf(" Advanced: 0x%X",payload[STREAM_CAP_ADVANCED]);
//printf(" Advanced2: 0x%X",payload[STREAM_CAP_ADVANCED2]); printf(" Advanced2: 0x%X",payload[STREAM_CAP_ADVANCED2]);
break; break;
case MESG_CHANNEL_ID_ID: case MESG_CHANNEL_ID_ID:
//TDCONFIG *cfg = (TDCONFIG*)&ant->dcfg[chan]; //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.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.deviceType = payload[STREAM_CHANNELID_DEVTYPE];
ant.dcfg[chan].dev.transType = payload[STREAM_CHANNELID_TRANTYPE]; //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); //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 0
if (cfg->dev.scidDeviceType != cfg->deviceType){ if (cfg->dev.scidDeviceType != cfg->deviceType){
printf(" @ CHANNEL ID: this is not the device we're looking for"); 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; break;
case MESG_VERSION_ID: case MESG_VERSION_ID:
//printf(" @ version: '%s'", (char*)&payload[STREAM_VERSION_STRING]); printf(" @ version: '%s'", (char*)&payload[STREAM_VERSION_STRING]);
break; break;
}; };
} }

View File

@ -10,9 +10,34 @@ void setup() {
Serial.println("Ant+ USB Test"); Serial.println("Ant+ USB Test");
myusb.begin(); myusb.begin();
ant1.begin(); ant1.begin();
ant1.onStatusChange(handleStatusChange);
ant1.onDeviceID(handleDeviceID);
} }
void loop() { void loop() {
myusb.Task(); 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);
}