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

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

+ 26
- 14
antplus.cpp View File

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)
{ {
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;
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; cfg->flags.channelStatusOld = cfg->flags.channelStatus;
} }
} }
break; break;


case MESG_CAPABILITIES_ID: 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; 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.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); //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");
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;
}; };
} }

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

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

Loading…
Cancel
Save