An ANT Shared Bidirectional Slave sample implementation.

Dependencies:   bc_ant mbed

see http://mbed.org/users/MRSa/notebook/bc-ant_receiver/ .

main.cpp

Committer:
MRSa
Date:
2013-05-26
Revision:
0:516a9e85a3fb
Child:
1:05e9a83b721c

File content as of revision 0:516a9e85a3fb:

#include "mbed.h"
#include "bc_ant.h"

/** USBTX - Tranmit on USB  USBRX - receive on USB  **/
Serial pc(USBTX, USBRX);

/**  LPC1768 LEDs **/
BusOut myLeds(LED1, LED2, LED3, LED4);

/** for BC-ANT-SERIAL (LPC1768) **/
bc_ant antPort(p13, p14, p15, 9600);

/** DIP-SW (for NODE SHARED ADDRESS)  **/
DigitalIn dip1(p20);   // DIP-SW1
DigitalIn dip2(p19);   // DIP-SW2

/**  OUTPUT DEVICES  **/
DigitalOut led0(p21);

/**   ANT DEFINES   **/
#define MAX_BUFSIZE             64
#define ANT_CH                   0
#define DEVICE_NUMBER           65
#define ANT_NETWORK_NUMBER       0  // Network Number
#define TRANS_TYPE_WILDCARD      0  // Wild Card 
#define TRANS_TYPE_INDEPENDENT   1  // Independent Channel
#define TRANS_TYPE_SHARED_2BYTES 3  // Shared Channel using 2byte address
#define DEVICE_TYPE_WILDCARD     0
#define DEVICE_TYPE_ID           3  // Device Type ID

/** DIP-SWITCH ASSIGN **/
#define ID_1   0x00001   // 1:off, 2:off
#define ID_2   0x00002   // 1:on,  2:off
#define ID_3   0x00004   // 1:off, 2:on
#define ID_4   0x00008   // 1:on,  2:on


/**
 *   CHECK DIP-SW STATUS
 */
int check_DipSw()
{
    if (dip1 == 0)
    {
        return ((dip2 == 0) ? 4 : 2);
    }
    return ((dip2 == 0) ? 3 : 1);
}

/**
 *   GET LED LAMP ID
 */
int getLampId(int id)
{
    int lampId = ID_1;
    switch (id)
    {
      case 4:
        lampId = ID_4;
        break;
      case 3:
        lampId = ID_3;
        break;
      case 2:
        lampId = ID_2;
        break;
      case 1:
      default:
        lampId = ID_1;
        break;
    }
    return (lampId);
}

/**
 *  Recieved a message from ANT port.
 */
void receivedFromAnt()
{
   uint8_t receivedLength = 0;
   uint8_t receivedBuffer[MAX_BUFSIZE];
   receivedLength = antPort.BC_ANT_RecvPacket(receivedBuffer, MAX_BUFSIZE);

   printf("RX(%3d):", receivedLength);
   for (int index = 0; index < receivedLength; index++)
   {
       printf("%02x ", receivedBuffer[index]);
   }
   printf("\r\n");
}

/**
 *  Moving command received from HOST
 */
void sendToAnt(uint8_t *buffer)
{
    antPort.ANT_SendAcknowledgedData(ANT_CH, buffer);
}

/**
 *   initializes ANT port
 */
int initialize_ANTport(bool isReceive)
{
    antPort.ANT_ResetSystem();
    antPort.ANT_AssignChannel(ANT_CH, ANT_Shared_Bidirectional_Slave, ANT_NETWORK_NUMBER);
    antPort.ANT_SetChannelId(ANT_CH,  DEVICE_NUMBER, DEVICE_TYPE_ID, TRANS_TYPE_SHARED_2BYTES);
    antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 16);   // 16Hz : 32768/16 = 2048
    antPort.ANT_SetChannelRFFreq(ANT_CH, 4);       // 2404 MHz
    antPort.ANT_OpenChannel(ANT_CH);               // 
    antPort.attach(&receivedFromAnt);              // callback function

    // set device's shared address
    uint8_t buffer[MAX_BUFSIZE] =
    {
        0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    };
    buffer[0] = check_DipSw();
    antPort.ANT_SendBroadcastData(ANT_CH, buffer);
    return (0);
}

/**
 *   Main Routine
 */
int main()
{
    printf("--- INITIALIZE (ID:%d)  ----\r\n", check_DipSw());
    int statusLamp = getLampId(check_DipSw());
    pc.baud(9600);                 // set serial speed between PC and mbed.
    initialize_ANTport(true);      // initialize BC-ANT-SERIAL
    printf("--- READY ----\r\n");

    while(1)
    {
        myLeds = statusLamp;
        //led0 = 1;
        wait(0.5);
        myLeds = 0;
        //led0 = 0;
        wait(0.5);
    }
}