Pārlūkot izejas kodu

AntPlus polling and example (still incomplete)

main
PaulStoffregen pirms 7 gadiem
vecāks
revīzija
94b2decbca
3 mainītis faili ar 46 papildinājumiem un 82 dzēšanām
  1. +1
    -21
      USBHost_t36.h
  2. +27
    -61
      antplus.cpp
  3. +18
    -0
      examples/AntPlus/AntPlus.ino

+ 1
- 21
USBHost_t36.h Parādīt failu

@@ -894,6 +894,7 @@ private:
volatile uint16_t txtail;
volatile bool txready;
volatile uint8_t rxlen;
volatile bool do_polling;
private:
enum _eventi {
EVENTI_MESSAGE = 0,
@@ -935,27 +936,14 @@ private:
uint8_t channelStatusOld;
} flags;
} TDCONFIG;
//typedef struct {
//int (*cbPtr) (const int channel, const int messageId,
//const uint8_t *payLoad, const size_t dataLength, void *userPtr);
//void *uPtr; // user variable sent with each event
//} TEVENTFUNC;
//typedef struct {
//void (*cbPtr) (TDCONFIG *cfg, const uint8_t *payLoad,
//const size_t dataLength, void *userPtr);
//void *uPtr; // user variable sent with each payload
//} TPAYLOADFUNC;
typedef struct {
uint8_t initOnce;
uint8_t key; // key index
int iDevice; // index to the antplus we're interested in, if > one found
//TEVENTFUNC eventCb[EVENTI_TOTAL];
//TPAYLOADFUNC payloadCb[PROFILE_TOTAL];
TDCONFIG dcfg[PROFILE_TOTAL]; // channel config, we're using one channel per device
} TLIBANTPLUS;
TLIBANTPLUS ant;
int (*callbackFunc)(uint32_t msg, intptr_t *value1, uint32_t value2);
//int dispatchMessage(const uint8_t *stream, const int len);
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);
@@ -963,23 +951,15 @@ private:
static int msgCheckIntegrity(uint8_t *stream, const int len);
static int msgGetLength(uint8_t *stream);
int handleMessages(uint8_t *buffer, int tBytes);
//int registerEventCallback(const int which, void *eventFunc, void *userPtr);
//int registerPayloadCallback(const int profile, void *eventFunc, void *userPtr);

void setCallbackFunc(int (*func)(uint32_t msg, intptr_t *value1, uint32_t value2));
void sendMessage(uint32_t msg, intptr_t *value1, uint32_t value2);
void sendMessageChannelStatus(TDCONFIG *cfg, const uint32_t channelStatus);

void message_channel(const int chan, const int eventId,
const uint8_t *payload, const size_t dataLength);
void message_response(const int chan, const int msgId,
const uint8_t *payload, const size_t dataLength);
void message_event(const int channel, const int msgId,
const uint8_t *payload, const size_t dataLength);


//int SetPayloadHandler(const int profile, void *eventFunc, void *userPtr);
//int SetEventHandler(const int which, void *eventFunc, void *userPtr);
int ResetSystem();
int RequestMessage(const int channel, const int message);
int SetNetworkKey(const int netNumber, const uint8_t *key);

+ 27
- 61
antplus.cpp Parādīt failu

@@ -101,6 +101,7 @@ bool AntPlus::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_
updatetimer.start(500000);
queue_Data_Transfer(rxpipe, rxpacket, 64, this);
rxlen = 0;
do_polling = false;
return true;
}
return false;
@@ -135,6 +136,7 @@ void AntPlus::rx_data(const Transfer_t *transfer)
rxlen = 0;
} else {
rxlen = len; // signal arrival of data to Task()
// TODO: should someday use EventResponder to call from yield()
}
}

@@ -223,6 +225,8 @@ void AntPlus::timer_event(USBDriverTimer *whichTimer)
if (first_update) {
ResetSystem();
first_update = false;
} else {
do_polling = true;
}
//println("ant update timer");
}
@@ -245,16 +249,29 @@ void AntPlus::Task()
rxlen = 0;
NVIC_ENABLE_IRQ(IRQ_USBHS);
}



if (do_polling) {
do_polling = false;
for (int i = 0; i < PROFILE_TOTAL; i++) {
TDCONFIG *cfg = &ant.dcfg[i];
if (!(cfg->flags.profileValid)) continue;
//printf("#### %i %i: %i %i %i ####", i, cfg->channel,
// cfg->flags.channelStatus, cfg->flags.keyAccepted,
// cfg->flags.chanIdOnce);
if (cfg->flags.channelStatus) {
RequestMessage(cfg->channel, MESG_CHANNEL_STATUS_ID);
} else {
AssignChannel(cfg->channel, cfg->channelType, cfg->networkNumber);
RequestMessage(cfg->channel, MESG_CHANNEL_STATUS_ID);
if (!cfg->flags.keyAccepted && !cfg->flags.chanIdOnce) {
SetNetworkKey(cfg->networkNumber, getAntKey(ant.key));
}
}
}
}
}






enum _akeys {
KEY_ANTSPORT,
KEY_SUUNTO,
@@ -272,14 +289,6 @@ static const uint8_t antkeys[KEY_TOTAL][8] = {
};



/*int AntPlus::dispatchMessage(const uint8_t *stream, const int len)
{
return ant.eventCb[EVENTI_MESSAGE].cbPtr(stream[STREAM_CHANNEL],
stream[STREAM_MESSAGE], &stream[STREAM_DATA],
(size_t)stream[STREAM_LENGTH], ant.eventCb[EVENTI_MESSAGE].uPtr);
}*/

uint8_t AntPlus::calcMsgChecksum(const uint8_t *buffer, const uint8_t len)
{
uint8_t checksum = 0x00;
@@ -356,8 +365,6 @@ int AntPlus::handleMessages(uint8_t *buffer, int tBytes)
message_event(stream[STREAM_CHANNEL], stream[STREAM_MESSAGE],
&stream[STREAM_DATA], (size_t)stream[STREAM_LENGTH]);



int len = msgGetLength(stream);
stream += len;
tBytes -= len;
@@ -365,29 +372,6 @@ int AntPlus::handleMessages(uint8_t *buffer, int tBytes)
return 1;
}

/*int AntPlus::registerEventCallback(const int which, void *eventFunc, void *userPtr)
{
if (which < EVENTI_TOTAL) {
ant.eventCb[which].cbPtr = (int (*)(int, int, const uint8_t*, size_t, void*))eventFunc;
ant.eventCb[which].uPtr = userPtr;
return 1;
}
//printf("invalid callback id {%i}\n.", which);
return 0;
}*/

/*int AntPlus::registerPayloadCallback(const int profile, void *eventFunc, void *userPtr)
{
if (profile < PROFILE_TOTAL) {
ant.payloadCb[profile].cbPtr = (void (*)(TDCONFIG*, const uint8_t*, size_t, void*))eventFunc;
ant.payloadCb[profile].uPtr = userPtr;
return 1;
}
//printf("invalid callback id {%i}\n.", profile);
return 0;
}*/



// TODO: replace this with multiple Arduino style OnXYZ callbacks
void AntPlus::setCallbackFunc(int (*func)(uint32_t msg, intptr_t *value1, uint32_t value2))
@@ -417,7 +401,6 @@ void AntPlus::message_channel(const int chan, const int eventId,
//printf(" $ chan event: chan:%i, msgId:0x%.2X, payload:%p, dataLen:%i, uPtr:%p", chan, eventId, payload, (int)dataLength, uPtr);
//dump_hexbytes(payload, dataLength);

//TLIBANTPLUS *ant = (AntPlus::TLIBANTPLUS*)uPtr;
TDCONFIG *cfg = &(ant.dcfg[chan]);

switch (eventId){
@@ -579,7 +562,6 @@ void AntPlus::message_event(const int channel, const int msgId,
//printf(" @ msg event cb: Chan:%i, Id:0x%.2X, payload:%p, len:%i, ptr:%p", channel, msgId, payload, (int)dataLength, uPtr);
//dump_hexbytes(payload, dataLength);

//TLIBANTPLUS *ant = (TLIBANTPLUS*)uPtr;
uint8_t chan = 0;
if (channel >= 0 && channel < PROFILE_TOTAL) chan = channel;

@@ -647,16 +629,6 @@ void AntPlus::message_event(const int channel, const int msgId,
}


//int AntPlus::SetPayloadHandler(const int profile, void *eventFunc, void *userPtr)
//{
//return registerPayloadCallback(profile, eventFunc, userPtr);
//}

//int AntPlus::SetEventHandler(const int which, void *eventFunc, void *userPtr)
//{
//return registerEventCallback(which, eventFunc, userPtr);
//}

int AntPlus::ResetSystem()
{
//println("libantplus_ResetSystem");
@@ -954,12 +926,6 @@ int AntPlus::SendExtBurstTransfer(const int channel, const int devNum, const int
return ret;
}

//const uint8_t *libantplus_GetNetworkKey (const uint8_t keyIdx)
//{
//return antplus_getAntKey(keyIdx);
//}




void AntPlus::profileSetup_HRM(TDCONFIG *cfg, const uint32_t deviceId)
@@ -1170,10 +1136,10 @@ void AntPlus::begin(const uint8_t key)
int deviceId = 0; // TODO: user API to set this?
profileSetup_HRM(&ant.dcfg[PROFILE_HRM], deviceId);
profileSetup_SPDCAD(&ant.dcfg[PROFILE_SPDCAD], deviceId);
profileSetup_POWER(&ant.dcfg[PROFILE_POWER], deviceId);
profileSetup_STRIDE(&ant.dcfg[PROFILE_STRIDE], deviceId);
profileSetup_SPEED(&ant.dcfg[PROFILE_SPEED], deviceId);
profileSetup_CADENCE(&ant.dcfg[PROFILE_CADENCE], deviceId);
//profileSetup_POWER(&ant.dcfg[PROFILE_POWER], deviceId);
//profileSetup_STRIDE(&ant.dcfg[PROFILE_STRIDE], deviceId);
//profileSetup_SPEED(&ant.dcfg[PROFILE_SPEED], deviceId);
//profileSetup_CADENCE(&ant.dcfg[PROFILE_CADENCE], deviceId);

//ant.eventCb[EVENTI_MESSAGE].cbPtr = &message_event;
//SetEventHandler(EVENTI_MESSAGE, (void*)message_event, (void*)ant);

+ 18
- 0
examples/AntPlus/AntPlus.ino Parādīt failu

@@ -0,0 +1,18 @@
#include <USBHost_t36.h>

USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
AntPlus ant1(myusb);

void setup() {
while (!Serial) ; // wait for Arduino Serial Monitor
Serial.println("Ant+ USB Test");
myusb.begin();
ant1.begin();

}

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

Notiek ielāde…
Atcelt
Saglabāt