Ryoji Sakai / Mbed 2 deprecated bc_ant-sample

Dependencies:   bc_ant mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "bc_ant.h"
00003 
00004 DigitalOut myled(LED1);
00005 
00006 Serial pc(USBTX, USBRX);          // USBTX - Tranmit on USB  USBRX - receive on USB
00007 bc_ant antPort(p13, p14, p15);    // for BC-ANT-SERIAL
00008 
00009 #define MAX_BUFSIZE   64
00010 #define ANT_CH         0
00011 #define DEVICE_NUMBER 65
00012 
00013 /**
00014  *  Recieved a message from ANT port.
00015  */
00016 void receivedFromAnt()
00017 {
00018    uint8_t receivedLength = 0;
00019    uint8_t receivedBuffer[MAX_BUFSIZE];
00020    receivedLength = antPort.BC_ANT_RecvPacket(receivedBuffer, MAX_BUFSIZE);
00021 
00022    printf("RX(%3d):", receivedLength);
00023    for (int index = 0; index < receivedLength; index++)
00024    {
00025        printf("%02x ", receivedBuffer[index]);
00026    }
00027    printf("\r\n");
00028 }
00029 
00030 /**
00031  *  Moving command received from HOST
00032  */
00033 void sendToAnt(uint8_t *buffer)
00034 {
00035     antPort.ANT_SendAcknowledgedData(ANT_CH, buffer);
00036 }
00037 
00038 /**
00039  *   initializes ANT port
00040  */
00041 int initialize_ANTport(bool isReceive)
00042 {
00043     antPort.ANT_ResetSystem();
00044     if (isReceive == true)
00045     {
00046         // receiver mode
00047         antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Slave, 0);
00048     }
00049     else
00050     {
00051         // sender mode
00052         antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Master, 0);    
00053     }
00054     antPort.ANT_SetChannelId(ANT_CH,  DEVICE_NUMBER, 1, 1);
00055 
00056     antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 4);
00057     antPort.ANT_SetChannelRFFreq(ANT_CH, 4);       // 2404 MHz
00058     antPort.ANT_OpenChannel(ANT_CH);
00059 
00060     if (isReceive == true)
00061     {
00062         // receiver mode
00063         antPort.attach(&receivedFromAnt);
00064     }
00065     else
00066     {
00067         // sender mode
00068         uint8_t buffer[MAX_BUFSIZE] =
00069         {
00070             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
00071         };
00072         antPort.ANT_SendBroadcastData(ANT_CH, buffer);
00073     }
00074     return (0);
00075 }
00076 
00077 
00078 /**
00079  *   Main Routine
00080  */
00081 int main()
00082 {
00083     pc.baud(9600);                  // set serial speed between PC and mbed.
00084     initialize_ANTport(true);      // initialize BC-ANT-SERIAL for 
00085     printf("--- READY ----\r\n");
00086 
00087     while(1) {
00088         myled = 1;
00089         wait(0.2);
00090         myled = 0;
00091         wait(0.2);
00092     }
00093 }