123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197 |
- /*
- This software is subject to the license described in the License.txt file
- included with this software distribution. You may not use this file except in compliance
- with this license.
- Copyright (c) Dynastream Innovations Inc. 2014
- All rights reserved.
- */
- #include "PowerRecordingExample.h"
- #include "types.h"
- #include "dsi_framer_ant.hpp"
- #include "dsi_thread.h"
- #include "dsi_serial_generic.hpp"
- #include "dsi_debug.hpp"
- extern "C" {
- #include "PowerDecoder.h"
- }
- #include <stdio.h>
- #include <assert.h>
- #include <string.h>
- #include <time.h>
- #define ENABLE_EXTENDED_MESSAGES
- #define USER_BAUDRATE (50000) // For AT3/AP2, use 57600
- #define USER_RADIOFREQ (57)
- #define USER_ANTCHANNEL (0)
- #define USER_DEVICETYPE (11)
- #define USER_TRANSTYPE (0)
- #define USER_NETWORK_KEY {0xB9, 0xA5, 0x21, 0xFB, 0xBD, 0x72, 0xC3, 0x45} //ANT+ Network Key
- #define USER_NETWORK_NUM (0) // The network key is assigned to this network number
- #define MESSAGE_TIMEOUT (1000)
- // Indexes into message recieved from ANT
- #define MESSAGE_BUFFER_DATA1_INDEX ((UCHAR) 0)
- #define MESSAGE_BUFFER_DATA2_INDEX ((UCHAR) 1)
- #define MESSAGE_BUFFER_DATA3_INDEX ((UCHAR) 2)
- #define MESSAGE_BUFFER_DATA4_INDEX ((UCHAR) 3)
- #define MESSAGE_BUFFER_DATA5_INDEX ((UCHAR) 4)
- #define MESSAGE_BUFFER_DATA6_INDEX ((UCHAR) 5)
- #define MESSAGE_BUFFER_DATA7_INDEX ((UCHAR) 6)
- #define MESSAGE_BUFFER_DATA8_INDEX ((UCHAR) 7)
- #define MESSAGE_BUFFER_DATA9_INDEX ((UCHAR) 8)
- #define MESSAGE_BUFFER_DATA10_INDEX ((UCHAR) 9)
- #define MESSAGE_BUFFER_DATA11_INDEX ((UCHAR) 10)
- #define MESSAGE_BUFFER_DATA12_INDEX ((UCHAR) 11)
- #define MESSAGE_BUFFER_DATA13_INDEX ((UCHAR) 12)
- #define MESSAGE_BUFFER_DATA14_INDEX ((UCHAR) 13)
- #define MESSAGE_BUFFER_DATA15_INDEX ((UCHAR) 14)
- #define MESSAGE_BUFFER_DATA16_INDEX ((UCHAR) 15)
- FILE *fp1; // output file
- ////////////////////////////////////////////////////////////////////////////////
- // main
- //
- // Usage:
- //
- // c:\PowerRecordingExample.exe [device_no]
- //
- // ... where
- //
- // device_no: USB Device port, starting at 0
- //
- // ... example
- //
- // c:\PowerRecordingExample.exe 0 0 1.0 0
- //
- // Comment to USB port 0, wild card device number, record at 1.0s intervals, from an event based power meter
- //
- // If the optional arguments are not supplied, the user will
- // be prompted to enter them after the program starts.
- //
- ////////////////////////////////////////////////////////////////////////////////
- int main(int argc, char **argv)
- {
- Example* pclExample = new Example();
- UCHAR ucDeviceNumber = 0xFF;
- UCHAR ucChannelType = CHANNEL_TYPE_SLAVE;
- USHORT usAntDeviceNumber = 0xFFFF;
- DOUBLE dRecordInterval = 0;
- DOUBLE dTimeBase = 0xFF;
- UCHAR ucPowerMeterType = 254;
- DOUBLE dReSyncInterval = 0;
- if (argc == 6)
- {
- ucDeviceNumber = (UCHAR)atoi(argv[1]);
- usAntDeviceNumber = (UCHAR)atoi(argv[2]);
- dRecordInterval = (DOUBLE)atoi(argv[3]);
- dTimeBase = (DOUBLE)atoi(argv[4]);
- dReSyncInterval = (DOUBLE)atoi(argv[4]);
- }
- if (argc > 6)
- {
- ucDeviceNumber = (UCHAR)atoi(argv[1]);
- usAntDeviceNumber = (UCHAR)atoi(argv[2]);
- dRecordInterval = (DOUBLE)atoi(argv[3]);
- dTimeBase = (DOUBLE)atoi(argv[4]);
- ucPowerMeterType = (UCHAR)atoi(argv[5]);
- dReSyncInterval = (DOUBLE)atoi(argv[4]);
- }
- if (pclExample->Init(ucDeviceNumber, ucChannelType, usAntDeviceNumber, dRecordInterval, dTimeBase, ucPowerMeterType, dReSyncInterval))
- pclExample->Start();
- else
- delete pclExample;
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Example
- //
- // Constructor, intializes Example class
- //
- ////////////////////////////////////////////////////////////////////////////////
- Example::Example()
- {
- ucChannelType = CHANNEL_TYPE_INVALID;
- pclSerialObject = (DSISerialGeneric*)NULL;
- pclMessageObject = (DSIFramerANT*)NULL;
- uiDSIThread = (DSI_THREAD_ID)NULL;
- bMyDone = FALSE;
- bDone = FALSE;
- bDisplay = TRUE;
- bBroadcasting = FALSE;
- bPowerDecoderInitialized = FALSE;
- ulNewEventTime = 0;
- usPreviousEventTime = 0;
- previousRxTime = time(NULL);
- ucPowerOnlyUpdateEventCount = 0;
- dRxTimeTePs = 0;
- memset(aucTransmitBuffer, 0, ANT_STANDARD_DATA_PAYLOAD_SIZE);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // ~Example
- //
- // Destructor, clean up and free memory
- //
- ////////////////////////////////////////////////////////////////////////////////
- Example::~Example()
- {
- if (pclMessageObject)
- delete pclMessageObject;
- if (pclSerialObject)
- delete pclSerialObject;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Init
- //
- // Initialize the Example and ANT Library.
- //
- // ucDeviceNumber_: USB Device Number (0 for first USB stick plugged and so on)
- // If not specified on command line, 0xFF is passed in as invalid.
- // ucChannelType_: ANT Channel Type. 0 = Master, 1 = Slave
- // If not specified, 2 is passed in as invalid.
- // usAntDeviceNumber_: The device number of the channel
- //
- // dRecordInterval_: The time in seconds between records generated by the Power Decoder
- //
- // dTimeBase_: The time in seconds at which power/torque pages are updated. Set to 0 if the power meter is event based.
- //
- // ucPowerMeterType_: The type of power meter. Knowing this ahead of time helps deal with a corner case
- // where power only messages may be decoded before torque pages at initialization.
- //
- // dResyncInterval_: The maximum time allowed for a dropout (no power messages to be received) before resetting the decoder's internal values.
- //
- ////////////////////////////////////////////////////////////////////////////////
- BOOL Example::Init(UCHAR ucDeviceNumber_, UCHAR ucChannelType_, USHORT usAntDeviceNumber_, DOUBLE dRecordInterval_, DOUBLE dTimeBase_, UCHAR ucPowerMeterType_, DOUBLE dReSyncInterval_)
- {
- BOOL bStatus;
- // Initialize condition var and mutex
- UCHAR ucCondInit = DSIThread_CondInit(&condTestDone);
- assert(ucCondInit == DSI_THREAD_ENONE);
- UCHAR ucMutexInit = DSIThread_MutexInit(&mutexTestDone);
- assert(ucMutexInit == DSI_THREAD_ENONE);
- #if defined(DEBUG_FILE)
- // Enable logging
- DSIDebug::Init();
- DSIDebug::SetDebug(TRUE);
- #endif
- // Create Serial object.
- pclSerialObject = new DSISerialGeneric();
- assert(pclSerialObject);
- // NOTE: Will fail if the module is not available.
- // If no device number was specified on the command line,
- // prompt the user for input.
- if (ucDeviceNumber_ == 255)
- {
- printf("USB Device number?\n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- sscanf(st, "%u", &ucDeviceNumber_);
- // Set USB device number to 0 by default
- if (ucDeviceNumber_ == 255)
- ucDeviceNumber_ = 0;
- }
- if (usAntDeviceNumber_ == 65535)
- {
- printf("Power Meter Device Number? (0 to wildcard)\n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- sscanf(st, "%u", &usAntDeviceNumber_);
- // Set ANT Device Number to 0 by default
- if (usAntDeviceNumber_ == 65535)
- usAntDeviceNumber_ = 0;
- }
- if (dRecordInterval_ > -0.001 && dRecordInterval_ < 0.001)
- {
- printf("Power Record Interval? (1.0s standard)\n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- dRecordInterval_ = atof(st);
- // Set Record Interval to 1 second by default
- if (dRecordInterval_ > -0.001 && dRecordInterval_ < 0.001)
- dRecordInterval_ = 1;
- }
- if (dTimeBase_ >= (255-0.001))
- {
- printf("Power Meter Timebase? (0.0s for Event Based Power Meters)\n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- dTimeBase_ = atof(st);
- // Set timebase to 0 by default
- if (dTimeBase_ >= (255 - 0.001))
- dTimeBase_ = 0;
- }
- // Prior knowledge of the power meter type prevents a corner case where the decoder may begin decoding power only messages before only decoding torque pages
- if (ucPowerMeterType_ == 254)
- {
- printf("Power Meter Type? (16-Power Only,17-Wheel Torque,18-CrankTorque,32-CTF,255-Unknown)\n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- sscanf(st, "%u", &ucPowerMeterType_);
- // Set power meter type to unknown by default
- if (ucPowerMeterType_ == 254)
- ucPowerMeterType_ = 255;
- }
- if (dReSyncInterval_ > -0.001 && dReSyncInterval_ < 0.001)
- {
- printf("Re-Sync Interval? (10s default) \n"); fflush(stdout);
- char st[1024];
- fgets(st, sizeof(st), stdin);
- dReSyncInterval_ = atof(st);
- // Set Re-Sync Interval to 10 seconds by default
- if (dReSyncInterval_ > -0.001 && dReSyncInterval_ < 0.001)
- dReSyncInterval_ = 10;
- }
- ucChannelType = ucChannelType_;
- usAntDeviceNumber = usAntDeviceNumber_;
- dRecordInterval = dRecordInterval_;
- dTimeBase = dTimeBase_;
- dReSyncInterval = dReSyncInterval_;
- SetPowerMeterType(ucPowerMeterType_);
- // Initialize Serial object.
- // The device number depends on how many USB sticks have been
- // plugged into the PC. The first USB stick plugged will be 0
- // the next 1 and so on.
- //
- // The Baud Rate depends on the ANT solution being used. AP1
- // is 50000, all others are 57600
- bStatus = pclSerialObject->Init(USER_BAUDRATE, ucDeviceNumber_);
- assert(bStatus);
- // Create Framer object.
- pclMessageObject = new DSIFramerANT(pclSerialObject);
- assert(pclMessageObject);
- // Initialize Framer object.
- bStatus = pclMessageObject->Init();
- assert(bStatus);
- // Let Serial know about Framer.
- pclSerialObject->SetCallback(pclMessageObject);
- // Open Serial.
- bStatus = pclSerialObject->Open();
- // If the Open function failed, most likely the device
- // we are trying to access does not exist, or it is connected
- // to another program
- if (!bStatus)
- {
- printf("Failed to connect to device at USB port %d\n", ucDeviceNumber_);
- return FALSE;
- }
- // Create message thread.
- uiDSIThread = DSIThread_CreateThread(&Example::RunMessageThread, this);
- assert(uiDSIThread);
- printf("Initialization was successful!\n"); fflush(stdout);
- fopen_s(&fp1, "Output.csv", "w");
- fprintf(fp1, "Record Time, Rotations, Energy, Avg Cadence, Avg Power\n");
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Close
- //
- // Close connection to USB stick.
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::Close()
- {
- //Wait for test to be done
- DSIThread_MutexLock(&mutexTestDone);
- bDone = TRUE;
- UCHAR ucWaitResult = DSIThread_CondTimedWait(&condTestDone, &mutexTestDone, DSI_THREAD_INFINITE);
- assert(ucWaitResult == DSI_THREAD_ENONE);
- DSIThread_MutexUnlock(&mutexTestDone);
- //Destroy mutex and condition var
- DSIThread_MutexDestroy(&mutexTestDone);
- DSIThread_CondDestroy(&condTestDone);
- //Close all stuff
- if (pclSerialObject)
- pclSerialObject->Close();
- #if defined(DEBUG_FILE)
- DSIDebug::Close();
- #endif
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Start
- //
- // Starts the Example
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::Start()
- {
- BOOL bStatus;
- // Print out the menu to start
- PrintMenu();
- // Start ANT channel setup
- bStatus = InitANT();
- while (!bMyDone)
- {
- UCHAR ucChar;
- char st[1024];
- fgets(st, sizeof(st), stdin);
- sscanf(st, "%c", &ucChar);
- switch (ucChar)
- {
- case 'M':
- case 'm':
- {
- // Printout options
- PrintMenu();
- break;
- }
- case 'Q':
- case 'q':
- {
- // Quit
- fclose(fp1);
- printf("Closing channel...\n");
- bBroadcasting = FALSE;
- pclMessageObject->CloseChannel(USER_ANTCHANNEL, MESSAGE_TIMEOUT);
- break;
- }
- case 'r':
- case 'R':
- {
- // Reset the system and start over the test
- bStatus = InitANT();
- break;
- }
- case 'c':
- case 'C':
- {
- // Request capabilites.
- ANT_MESSAGE_ITEM stResponse;
- pclMessageObject->SendRequest(MESG_CAPABILITIES_ID, USER_ANTCHANNEL, &stResponse, 0);
- break;
- }
- case 'v':
- case 'V':
- {
- // Request version
- ANT_MESSAGE_ITEM stResponse;
- pclMessageObject->SendRequest(MESG_VERSION_ID, USER_ANTCHANNEL, &stResponse, 0);
- break;
- }
- case 'S':
- case 's':
- {
- // Request channel status
- ANT_MESSAGE_ITEM stResponse;
- pclMessageObject->SendRequest(MESG_CHANNEL_STATUS_ID, USER_ANTCHANNEL, &stResponse, 0);
- break;
- }
- case 'I':
- case 'i':
- {
- // Request channel ID
- ANT_MESSAGE_ITEM stResponse;
- pclMessageObject->SendRequest(MESG_CHANNEL_ID_ID, USER_ANTCHANNEL, &stResponse, 0);
- break;
- }
- case 'd':
- case 'D':
- {
- // Toggle display of data messages
- bDisplay = !bDisplay;
- break;
- }
- case 'u':
- case 'U':
- {
- // Print out information about the device we are connected to
- printf("USB Device Description\n");
- USHORT usDevicePID;
- USHORT usDeviceVID;
- UCHAR aucDeviceDescription[USB_MAX_STRLEN];
- UCHAR aucDeviceSerial[USB_MAX_STRLEN];
- // Retrieve info
- if (pclMessageObject->GetDeviceUSBVID(usDeviceVID))
- {
- printf(" VID: 0x%X\n", usDeviceVID);
- }
- if (pclMessageObject->GetDeviceUSBPID(usDevicePID))
- {
- printf(" PID: 0x%X\n", usDevicePID);
- }
- if (pclMessageObject->GetDeviceUSBInfo(pclSerialObject->GetDeviceNumber(), aucDeviceDescription, aucDeviceSerial, USB_MAX_STRLEN))
- {
- printf(" Product Description: %s\n", aucDeviceDescription);
- printf(" Serial String: %s\n", aucDeviceSerial);
- }
- break;
- }
- default:
- {
- break;
- }
- }
- DSIThread_Sleep(0);
- }
- //Disconnecting from module
- printf("Disconnecting module...\n");
- this->Close();
- printf("Example has completed successfully!\n");
- return;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // InitANT
- //
- // Resets the system and starts the test
- //
- ////////////////////////////////////////////////////////////////////////////////
- BOOL Example::InitANT(void)
- {
- BOOL bStatus;
- // Reset system
- printf("Resetting module...\n");
- bStatus = pclMessageObject->ResetSystem();
- DSIThread_Sleep(1000);
- // Start the test by setting network key
- printf("Setting network key...\n");
- UCHAR ucNetKey[8] = USER_NETWORK_KEY;
- bStatus = pclMessageObject->SetNetworkKey(USER_NETWORK_NUM, ucNetKey, MESSAGE_TIMEOUT);
- return bStatus;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // RunMessageThread
- //
- // Callback function that is used to create the thread. This is a static
- // function.
- //
- ////////////////////////////////////////////////////////////////////////////////
- DSI_THREAD_RETURN Example::RunMessageThread(void *pvParameter_)
- {
- ((Example*)pvParameter_)->MessageThread();
- return NULL;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // MessageThread
- //
- // Run message thread
- ////////////////////////////////////////////////////////////////////////////////
- void Example::MessageThread()
- {
- ANT_MESSAGE stMessage;
- USHORT usSize;
- bDone = FALSE;
- while (!bDone)
- {
- if (pclMessageObject->WaitForMessage(1000))
- {
- usSize = pclMessageObject->GetMessage(&stMessage);
- if (bDone)
- break;
- if (usSize == DSI_FRAMER_ERROR)
- {
- // Get the message to clear the error
- usSize = pclMessageObject->GetMessage(&stMessage, MESG_MAX_SIZE_VALUE);
- continue;
- }
- if (usSize != DSI_FRAMER_ERROR && usSize != DSI_FRAMER_TIMEDOUT && usSize != 0)
- {
- ProcessMessage(stMessage, usSize);
- }
- }
- }
- DSIThread_MutexLock(&mutexTestDone);
- UCHAR ucCondResult = DSIThread_CondSignal(&condTestDone);
- assert(ucCondResult == DSI_THREAD_ENONE);
- DSIThread_MutexUnlock(&mutexTestDone);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // ProcessMessage
- //
- // Process ALL messages that come from ANT, including event messages.
- //
- // stMessage: Message struct containing message recieved from ANT
- // usSize_:
- ////////////////////////////////////////////////////////////////////////////////
- void Example::ProcessMessage(ANT_MESSAGE stMessage, USHORT usSize_)
- {
- BOOL bStatus;
- BOOL bPrintBuffer = FALSE;
- UCHAR ucDataOffset = MESSAGE_BUFFER_DATA2_INDEX; // For most data messages
- switch (stMessage.ucMessageID)
- {
- //RESPONSE MESG
- case MESG_RESPONSE_EVENT_ID:
- {
- //RESPONSE TYPE
- switch (stMessage.aucData[1])
- {
- case MESG_NETWORK_KEY_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error configuring network key: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Network key set.\n");
- printf("Assigning channel...\n");
- if (ucChannelType == CHANNEL_TYPE_MASTER)
- {
- bStatus = pclMessageObject->AssignChannel(USER_ANTCHANNEL, PARAMETER_TX_NOT_RX, 0, MESSAGE_TIMEOUT);
- }
- else if (ucChannelType == CHANNEL_TYPE_SLAVE)
- {
- bStatus = pclMessageObject->AssignChannel(USER_ANTCHANNEL, 0, 0, MESSAGE_TIMEOUT);
- }
- break;
- }
- case MESG_ASSIGN_CHANNEL_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error assigning channel: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Channel assigned\n");
- printf("Setting Channel ID...\n");
- bStatus = pclMessageObject->SetChannelID(USER_ANTCHANNEL, usAntDeviceNumber, USER_DEVICETYPE, USER_TRANSTYPE, MESSAGE_TIMEOUT);
- break;
- }
- case MESG_CHANNEL_ID_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error configuring Channel ID: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Channel ID set\n");
- printf("Setting Radio Frequency...\n");
- bStatus = pclMessageObject->SetChannelRFFrequency(USER_ANTCHANNEL, USER_RADIOFREQ, MESSAGE_TIMEOUT);
- break;
- }
- case MESG_CHANNEL_RADIO_FREQ_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error configuring Radio Frequency: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Radio Frequency set\n");
- printf("Setting Channel Period...\n");
- bStatus = pclMessageObject->SetChannelPeriod(USER_ANTCHANNEL, 8182, MESSAGE_TIMEOUT);
- break;
- }
- case MESG_CHANNEL_MESG_PERIOD_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error configuring Channel Period: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Channel Period set\n");
- printf("Opening channel...\n");
- bBroadcasting = TRUE;
- bStatus = pclMessageObject->OpenChannel(USER_ANTCHANNEL, MESSAGE_TIMEOUT);
- break;
- }
- case MESG_OPEN_CHANNEL_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error opening channel: Code 0%d\n", stMessage.aucData[2]);
- bBroadcasting = FALSE;
- break;
- }
- printf("Channel opened\n");
- // We register the power record receiver and initialize the bike power decoders after the channel has opened
- InitPowerDecoder(dRecordInterval, dTimeBase, dReSyncInterval, RecordReceiver);
- bPowerDecoderInitialized = TRUE;
- printf("Power record decode library initialized\n");
- #if defined (ENABLE_EXTENDED_MESSAGES)
- printf("Enabling extended messages...\n");
- pclMessageObject->SetLibConfig(ANT_LIB_CONFIG_MESG_OUT_INC_TIME_STAMP | ANT_LIB_CONFIG_MESG_OUT_INC_DEVICE_ID, MESSAGE_TIMEOUT);
- #endif
- break;
- }
- case MESG_ANTLIB_CONFIG_ID:
- {
- if (stMessage.aucData[2] == INVALID_MESSAGE)
- {
- printf("Extended messages not supported in this ANT product\n");
- break;
- }
- else if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error enabling extended messages: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Extended messages enabled\n");
- break;
- }
- case MESG_UNASSIGN_CHANNEL_ID:
- {
- if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error unassigning channel: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- printf("Channel unassigned\n");
- printf("Press enter to exit\n");
- bMyDone = TRUE;
- break;
- }
- case MESG_CLOSE_CHANNEL_ID:
- {
- if (stMessage.aucData[2] == CHANNEL_IN_WRONG_STATE)
- {
- // We get here if we tried to close the channel after the search timeout (slave)
- printf("Channel is already closed\n");
- printf("Unassigning channel...\n");
- bStatus = pclMessageObject->UnAssignChannel(USER_ANTCHANNEL, MESSAGE_TIMEOUT);
- break;
- }
- else if (stMessage.aucData[2] != RESPONSE_NO_ERROR)
- {
- printf("Error closing channel: Code 0%d\n", stMessage.aucData[2]);
- break;
- }
- // If this message was successful, wait for EVENT_CHANNEL_CLOSED to confirm channel is closed
- break;
- }
- case MESG_REQUEST_ID:
- {
- if (stMessage.aucData[2] == INVALID_MESSAGE)
- {
- printf("Requested message not supported in this ANT product\n");
- }
- break;
- }
- case MESG_EVENT_ID:
- {
- switch (stMessage.aucData[2])
- {
- case EVENT_CHANNEL_CLOSED:
- {
- printf("Channel Closed\n");
- printf("Unassigning channel...\n");
- bStatus = pclMessageObject->UnAssignChannel(USER_ANTCHANNEL, MESSAGE_TIMEOUT);
- break;
- }
- case EVENT_TX:
- {
- // This event indicates that a message has just been
- // sent over the air. We take advantage of this event to set
- // up the data for the next message period.
- static UCHAR ucIncrement = 0; // Increment the first byte of the buffer
- aucTransmitBuffer[0] = ucIncrement++;
- // Broadcast data will be sent over the air on
- // the next message period.
- if (bBroadcasting)
- {
- pclMessageObject->SendBroadcastData(USER_ANTCHANNEL, aucTransmitBuffer);
- // Echo what the data will be over the air on the next message period.
- if (bDisplay)
- {
- printf("Tx:(%d): [%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x]\n",
- USER_ANTCHANNEL,
- aucTransmitBuffer[MESSAGE_BUFFER_DATA1_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA2_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA3_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA4_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA5_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA6_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA7_INDEX],
- aucTransmitBuffer[MESSAGE_BUFFER_DATA8_INDEX]);
- }
- else
- {
- static int iIndex = 0;
- static char ac[] = { '|', '/', '-', '\\' };
- printf("Tx: %c\r", ac[iIndex++]); fflush(stdout);
- iIndex &= 3;
- }
- }
- break;
- }
- case EVENT_RX_SEARCH_TIMEOUT:
- {
- printf("Search Timeout\n");
- break;
- }
- case EVENT_RX_FAIL:
- {
- printf("Rx Fail\n");
- break;
- }
- case EVENT_TRANSFER_RX_FAILED:
- {
- printf("Burst receive has failed\n");
- break;
- }
- case EVENT_TRANSFER_TX_COMPLETED:
- {
- printf("Tranfer Completed\n");
- break;
- }
- case EVENT_TRANSFER_TX_FAILED:
- {
- printf("Tranfer Failed\n");
- break;
- }
- case EVENT_RX_FAIL_GO_TO_SEARCH:
- {
- printf("Go to Search\n");
- break;
- }
- case EVENT_CHANNEL_COLLISION:
- {
- printf("Channel Collision\n");
- break;
- }
- case EVENT_TRANSFER_TX_START:
- {
- printf("Burst Started\n");
- break;
- }
- default:
- {
- printf("Unhandled channel event: 0x%X\n", stMessage.aucData[2]);
- break;
- }
- }
- break;
- }
- default:
- {
- printf("Unhandled response 0%d to message 0x%X\n", stMessage.aucData[2], stMessage.aucData[1]);
- break;
- }
- }
- break;
- }
- case MESG_STARTUP_MESG_ID:
- {
- printf("RESET Complete, reason: ");
- UCHAR ucReason = stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX];
- if (ucReason == RESET_POR)
- printf("RESET_POR");
- if (ucReason & RESET_SUSPEND)
- printf("RESET_SUSPEND ");
- if (ucReason & RESET_SYNC)
- printf("RESET_SYNC ");
- if (ucReason & RESET_CMD)
- printf("RESET_CMD ");
- if (ucReason & RESET_WDT)
- printf("RESET_WDT ");
- if (ucReason & RESET_RST)
- printf("RESET_RST ");
- printf("\n");
- break;
- }
- case MESG_CAPABILITIES_ID:
- {
- printf("CAPABILITIES:\n");
- printf(" Max ANT Channels: %d\n", stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- printf(" Max ANT Networks: %d\n", stMessage.aucData[MESSAGE_BUFFER_DATA2_INDEX]);
- UCHAR ucStandardOptions = stMessage.aucData[MESSAGE_BUFFER_DATA3_INDEX];
- UCHAR ucAdvanced = stMessage.aucData[MESSAGE_BUFFER_DATA4_INDEX];
- UCHAR ucAdvanced2 = stMessage.aucData[MESSAGE_BUFFER_DATA5_INDEX];
- printf("Standard Options:\n");
- if (ucStandardOptions & CAPABILITIES_NO_RX_CHANNELS)
- printf("CAPABILITIES_NO_RX_CHANNELS\n");
- if (ucStandardOptions & CAPABILITIES_NO_TX_CHANNELS)
- printf("CAPABILITIES_NO_TX_CHANNELS\n");
- if (ucStandardOptions & CAPABILITIES_NO_RX_MESSAGES)
- printf("CAPABILITIES_NO_RX_MESSAGES\n");
- if (ucStandardOptions & CAPABILITIES_NO_TX_MESSAGES)
- printf("CAPABILITIES_NO_TX_MESSAGES\n");
- if (ucStandardOptions & CAPABILITIES_NO_ACKD_MESSAGES)
- printf("CAPABILITIES_NO_ACKD_MESSAGES\n");
- if (ucStandardOptions & CAPABILITIES_NO_BURST_TRANSFER)
- printf("CAPABILITIES_NO_BURST_TRANSFER\n");
- printf("Advanced Options:\n");
- if (ucAdvanced & CAPABILITIES_OVERUN_UNDERRUN)
- printf("CAPABILITIES_OVERUN_UNDERRUN\n");
- if (ucAdvanced & CAPABILITIES_NETWORK_ENABLED)
- printf("CAPABILITIES_NETWORK_ENABLED\n");
- if (ucAdvanced & CAPABILITIES_AP1_VERSION_2)
- printf("CAPABILITIES_AP1_VERSION_2\n");
- if (ucAdvanced & CAPABILITIES_SERIAL_NUMBER_ENABLED)
- printf("CAPABILITIES_SERIAL_NUMBER_ENABLED\n");
- if (ucAdvanced & CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED)
- printf("CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED\n");
- if (ucAdvanced & CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED)
- printf("CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED\n");
- if (ucAdvanced & CAPABILITIES_SCRIPT_ENABLED)
- printf("CAPABILITIES_SCRIPT_ENABLED\n");
- if (ucAdvanced & CAPABILITIES_SEARCH_LIST_ENABLED)
- printf("CAPABILITIES_SEARCH_LIST_ENABLED\n");
- if (usSize_ > 4)
- {
- printf("Advanced 2 Options 1:\n");
- if (ucAdvanced2 & CAPABILITIES_LED_ENABLED)
- printf("CAPABILITIES_LED_ENABLED\n");
- if (ucAdvanced2 & CAPABILITIES_EXT_MESSAGE_ENABLED)
- printf("CAPABILITIES_EXT_MESSAGE_ENABLED\n");
- if (ucAdvanced2 & CAPABILITIES_SCAN_MODE_ENABLED)
- printf("CAPABILITIES_SCAN_MODE_ENABLED\n");
- if (ucAdvanced2 & CAPABILITIES_RESERVED)
- printf("CAPABILITIES_RESERVED\n");
- if (ucAdvanced2 & CAPABILITIES_PROX_SEARCH_ENABLED)
- printf("CAPABILITIES_PROX_SEARCH_ENABLED\n");
- if (ucAdvanced2 & CAPABILITIES_EXT_ASSIGN_ENABLED)
- printf("CAPABILITIES_EXT_ASSIGN_ENABLED\n");
- if (ucAdvanced2 & CAPABILITIES_FS_ANTFS_ENABLED)
- printf("CAPABILITIES_FREE_1\n");
- if (ucAdvanced2 & CAPABILITIES_FIT1_ENABLED)
- printf("CAPABILITIES_FIT1_ENABLED\n");
- }
- break;
- }
- case MESG_CHANNEL_STATUS_ID:
- {
- printf("Got Status\n");
- char astrStatus[][32] = { "STATUS_UNASSIGNED_CHANNEL",
- "STATUS_ASSIGNED_CHANNEL",
- "STATUS_SEARCHING_CHANNEL",
- "STATUS_TRACKING_CHANNEL" };
- UCHAR ucStatusByte = stMessage.aucData[MESSAGE_BUFFER_DATA2_INDEX] & STATUS_CHANNEL_STATE_MASK; // MUST MASK OFF THE RESERVED BITS
- printf("STATUS: %s\n", astrStatus[ucStatusByte]);
- break;
- }
- case MESG_CHANNEL_ID_ID:
- {
- // Channel ID of the device that we just recieved a message from.
- USHORT usDeviceNumber = stMessage.aucData[MESSAGE_BUFFER_DATA2_INDEX] | (stMessage.aucData[MESSAGE_BUFFER_DATA3_INDEX] << 8);
- UCHAR ucDeviceType = stMessage.aucData[MESSAGE_BUFFER_DATA4_INDEX];
- UCHAR ucTransmissionType = stMessage.aucData[MESSAGE_BUFFER_DATA5_INDEX];
- printf("CHANNEL ID: (%d/%d/%d)\n", usDeviceNumber, ucDeviceType, ucTransmissionType);
- break;
- }
- case MESG_VERSION_ID:
- {
- printf("VERSION: %s\n", (char*)&stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- break;
- }
- case MESG_ACKNOWLEDGED_DATA_ID:
- case MESG_BURST_DATA_ID:
- case MESG_BROADCAST_DATA_ID:
- {
- // The flagged and unflagged data messages have the same
- // message ID. Therefore, we need to check the size to
- // verify of a flag is present at the end of a message.
- // To enable flagged messages, must call ANT_RxExtMesgsEnable first.
- if (usSize_ > MESG_DATA_SIZE)
- {
- UCHAR ucFlag = stMessage.aucData[MESSAGE_BUFFER_DATA10_INDEX];
- if (ucFlag & ANT_LIB_CONFIG_MESG_OUT_INC_TIME_STAMP && ucFlag & ANT_EXT_MESG_BITFIELD_DEVICE_ID)
- {
- // In case we miss messages for 2 seconds or longer, we use the system time from the standard C time library to calculate rollovers
- time_t currentRxTime = time(NULL);
- if (currentRxTime - previousRxTime >= 2)
- {
- ulNewEventTime += (currentRxTime - previousRxTime) / 2 * 32768;
- }
- previousRxTime = currentRxTime;
- unsigned short usCurrentEventTime = stMessage.aucData[MESSAGE_BUFFER_DATA15_INDEX] | (stMessage.aucData[MESSAGE_BUFFER_DATA16_INDEX] << 8);
- unsigned short usDeltaEventTime = usCurrentEventTime - usPreviousEventTime;
- ulNewEventTime += usDeltaEventTime;
- usPreviousEventTime = usCurrentEventTime;
- printf("%f-", (double)ulNewEventTime / 32768);
- // NOTE: In this example we use the incoming message timestamp as it typically has the most accuracy
- // NOTE: The library will handle the received time discrepancy caused by power only event count linked messages
- if (bPowerDecoderInitialized)
- {
- DecodePowerMessage((double)ulNewEventTime / 32768, &stMessage.aucData[ucDataOffset]);
- }
- // NOTE: We must compensate for the power only event count/rx time discrepance here, because the library does not decode Te/Ps
- // The torque effectiveness/pedal smoothness page is tied to the power only page and vice versa,
- // so both pages share the same "received time" depending on which page was received first and if the event count updated.
- if (stMessage.aucData[ucDataOffset] == ANT_TEPS || stMessage.aucData[ucDataOffset] == ANT_POWERONLY)
- {
- UCHAR ucNewPowerOnlyUpdateEventCount = stMessage.aucData[ucDataOffset + 1];
- if (ucNewPowerOnlyUpdateEventCount != ucPowerOnlyUpdateEventCount)
- {
- ucPowerOnlyUpdateEventCount = ucNewPowerOnlyUpdateEventCount;
- dRxTimeTePs = (double)ulNewEventTime / 32768;
- }
- if (stMessage.aucData[ucDataOffset] == ANT_TEPS)
- {
- // NOTE: Any value greater than 200 or 100% should be considered "INVALID"
- FLOAT fLeftTorqueEffectiveness = (float)stMessage.aucData[ucDataOffset + 2] / 2;
- FLOAT fRightTorqueEffectiveness = (float)stMessage.aucData[ucDataOffset + 3] / 2;
- FLOAT fLeftOrCombPedalSmoothness = (float)stMessage.aucData[ucDataOffset + 4] / 2;
- FLOAT fRightPedalSmoothness = (float)stMessage.aucData[ucDataOffset + 4] / 2;
- TePsReceiver(dRxTimeTePs, fLeftTorqueEffectiveness, fRightTorqueEffectiveness, fLeftOrCombPedalSmoothness, fRightPedalSmoothness);
- }
- else
- {
- // NOTE: Power only is a separate data stream containing similar power data compared to torque data pages but containing pedal power balance
- // On power only sensors, it would be valuable to average power balance between generated records
- FLOAT fPowerBalance = (float)(0x7F & stMessage.aucData[ucDataOffset + 2]);
- BOOL bPowerBalanceRightPedalIndicator = 0x80 & stMessage.aucData[ucDataOffset + 2] != 0;
- PowerBalanceReceiver(dRxTimeTePs, fPowerBalance, bPowerBalanceRightPedalIndicator);
- }
- }
- }
- if (bDisplay && ucFlag & ANT_EXT_MESG_BITFIELD_DEVICE_ID)
- {
- // Channel ID of the device that we just recieved a message from.
- USHORT usDeviceNumber = stMessage.aucData[MESSAGE_BUFFER_DATA11_INDEX] | (stMessage.aucData[MESSAGE_BUFFER_DATA12_INDEX] << 8);
- UCHAR ucDeviceType = stMessage.aucData[MESSAGE_BUFFER_DATA13_INDEX];
- UCHAR ucTransmissionType = stMessage.aucData[MESSAGE_BUFFER_DATA14_INDEX];
- printf("Chan ID(%d/%d/%d) - ", usDeviceNumber, ucDeviceType, ucTransmissionType);
- }
- }
- // Display recieved message
- bPrintBuffer = TRUE;
- ucDataOffset = MESSAGE_BUFFER_DATA2_INDEX; // For most data messages
- if (bDisplay)
- {
- if (stMessage.ucMessageID == MESG_ACKNOWLEDGED_DATA_ID)
- printf("Acked Rx:(%d): ", stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- else if (stMessage.ucMessageID == MESG_BURST_DATA_ID)
- printf("Burst(0x%02x) Rx:(%d): ", ((stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX] & 0xE0) >> 5), stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX] & 0x1F);
- else
- printf("Rx:(%d): ", stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- }
- break;
- }
- case MESG_EXT_BROADCAST_DATA_ID:
- case MESG_EXT_ACKNOWLEDGED_DATA_ID:
- case MESG_EXT_BURST_DATA_ID:
- {
- // The "extended" part of this message is the 4-byte channel
- // id of the device that we recieved this message from. This message
- // is only available on the AT3. The AP2 uses flagged versions of the
- // data messages as shown above.
- // Channel ID of the device that we just recieved a message from.
- USHORT usDeviceNumber = stMessage.aucData[MESSAGE_BUFFER_DATA2_INDEX] | (stMessage.aucData[MESSAGE_BUFFER_DATA3_INDEX] << 8);
- UCHAR ucDeviceType = stMessage.aucData[MESSAGE_BUFFER_DATA4_INDEX];
- UCHAR ucTransmissionType = stMessage.aucData[MESSAGE_BUFFER_DATA5_INDEX];
- bPrintBuffer = TRUE;
- ucDataOffset = MESSAGE_BUFFER_DATA6_INDEX; // For most data messages
- if (bDisplay)
- {
- // Display the channel id
- printf("Chan ID(%d/%d/%d) ", usDeviceNumber, ucDeviceType, ucTransmissionType);
- if (stMessage.ucMessageID == MESG_EXT_ACKNOWLEDGED_DATA_ID)
- printf("- Acked Rx:(%d): ", stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- else if (stMessage.ucMessageID == MESG_EXT_BURST_DATA_ID)
- printf("- Burst(0x%02x) Rx:(%d): ", ((stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX] & 0xE0) >> 5), stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX] & 0x1F);
- else
- printf("- Rx:(%d): ", stMessage.aucData[MESSAGE_BUFFER_DATA1_INDEX]);
- }
- // NOTE: A different source of sub-second timing is required if the device does not support received time extended messages.
- break;
- }
- default:
- {
- break;
- }
- }
- // If we recieved a data message, diplay its contents here.
- if (bPrintBuffer)
- {
- if (bDisplay)
- {
- printf("[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x]\n",
- stMessage.aucData[ucDataOffset + 0],
- stMessage.aucData[ucDataOffset + 1],
- stMessage.aucData[ucDataOffset + 2],
- stMessage.aucData[ucDataOffset + 3],
- stMessage.aucData[ucDataOffset + 4],
- stMessage.aucData[ucDataOffset + 5],
- stMessage.aucData[ucDataOffset + 6],
- stMessage.aucData[ucDataOffset + 7]);
- }
- else
- {
- static int iIndex = 0;
- static char ac[] = { '|', '/', '-', '\\' };
- printf("Rx: %c\r", ac[iIndex++]); fflush(stdout);
- iIndex &= 3;
- }
- }
- return;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // RecordReceiver
- //
- // Handle new records from power recording library.
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::RecordReceiver(double dLastRecordTime_, double dTotalRotation_, double dTotalEnergy_, float fAverageCadence_, float fAveragePower_)
- {
- fprintf(fp1, "%lf, %lf, %lf, %f, %f\n",
- dLastRecordTime_, dTotalRotation_, dTotalEnergy_, fAverageCadence_, fAveragePower_);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // TePsReceiver
- //
- // Handle new torque effectiveness and pedal smoothness data page.
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::TePsReceiver(double dRxTime_, float fLeftTorqEff_, float fRightTorqEff_, float fLeftOrCPedSmth_, float fRightPedSmth_)
- {
- fprintf(fp1, "RxTime,LTE,RTE,LCPS,RPS,%f, %f, %f, %f, %f\n",
- dRxTime_, fLeftTorqEff_, fRightTorqEff_, fLeftOrCPedSmth_, fRightPedSmth_);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // PowerBalanceReceiver
- //
- // Handle power balance from power only data page.
- // NOTE: This should be handled by the library for power only sensors using the same integrated power into energy technique.
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::PowerBalanceReceiver(double dRxTime_, float fPowerBalance_, bool bPowerBalanceRightPedalIndicator_)
- {
- fprintf(fp1, "RxTime,PwrBal,RightPedal,%f, %f, %d\n",
- dRxTime_, fPowerBalance_, bPowerBalanceRightPedalIndicator_);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // PrintMenu
- //
- // Start the Test program.
- //
- ////////////////////////////////////////////////////////////////////////////////
- void Example::PrintMenu()
- {
- // Printout options
- printf("\n");
- printf("M - Print this menu\n");
- printf("R - Reset\n");
- printf("C - Request Capabilites\n");
- printf("V - Request Version\n");
- printf("I - Request Channel ID\n");
- printf("S - Request Status\n");
- printf("U - Request USB Descriptor\n");
- printf("D - Toggle Display\n");
- printf("Q - Quit\n");
- printf("\n");
- fflush(stdout);
- }
|