![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
An ANT Shared Bidirectional Slave sample implementation.
see http://mbed.org/users/MRSa/notebook/bc-ant_receiver/ .
main.cpp@3:ce23e131fdbd, 2013-06-05 (annotated)
- Committer:
- MRSa
- Date:
- Wed Jun 05 15:39:59 2013 +0000
- Revision:
- 3:ce23e131fdbd
- Parent:
- 2:691ccdef6f1e
reformat
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MRSa | 0:516a9e85a3fb | 1 | #include "mbed.h" |
MRSa | 0:516a9e85a3fb | 2 | #include "bc_ant.h" |
MRSa | 0:516a9e85a3fb | 3 | |
MRSa | 0:516a9e85a3fb | 4 | /** USBTX - Tranmit on USB USBRX - receive on USB **/ |
MRSa | 0:516a9e85a3fb | 5 | Serial pc(USBTX, USBRX); |
MRSa | 0:516a9e85a3fb | 6 | |
MRSa | 0:516a9e85a3fb | 7 | /** LPC1768 LEDs **/ |
MRSa | 0:516a9e85a3fb | 8 | BusOut myLeds(LED1, LED2, LED3, LED4); |
MRSa | 0:516a9e85a3fb | 9 | |
MRSa | 0:516a9e85a3fb | 10 | /** for BC-ANT-SERIAL (LPC1768) **/ |
MRSa | 0:516a9e85a3fb | 11 | bc_ant antPort(p13, p14, p15, 9600); |
MRSa | 0:516a9e85a3fb | 12 | |
MRSa | 0:516a9e85a3fb | 13 | /** DIP-SW (for NODE SHARED ADDRESS) **/ |
MRSa | 0:516a9e85a3fb | 14 | DigitalIn dip1(p20); // DIP-SW1 |
MRSa | 0:516a9e85a3fb | 15 | DigitalIn dip2(p19); // DIP-SW2 |
MRSa | 0:516a9e85a3fb | 16 | |
MRSa | 0:516a9e85a3fb | 17 | /** OUTPUT DEVICES **/ |
MRSa | 0:516a9e85a3fb | 18 | DigitalOut led0(p21); |
MRSa | 1:05e9a83b721c | 19 | DigitalOut led1(p22); |
MRSa | 0:516a9e85a3fb | 20 | |
MRSa | 0:516a9e85a3fb | 21 | /** ANT DEFINES **/ |
MRSa | 0:516a9e85a3fb | 22 | #define MAX_BUFSIZE 64 |
MRSa | 0:516a9e85a3fb | 23 | #define ANT_CH 0 |
MRSa | 0:516a9e85a3fb | 24 | #define DEVICE_NUMBER 65 |
MRSa | 0:516a9e85a3fb | 25 | #define ANT_NETWORK_NUMBER 0 // Network Number |
MRSa | 0:516a9e85a3fb | 26 | #define TRANS_TYPE_SHARED_2BYTES 3 // Shared Channel using 2byte address |
MRSa | 0:516a9e85a3fb | 27 | #define DEVICE_TYPE_ID 3 // Device Type ID |
MRSa | 0:516a9e85a3fb | 28 | |
MRSa | 0:516a9e85a3fb | 29 | /** DIP-SWITCH ASSIGN **/ |
MRSa | 0:516a9e85a3fb | 30 | #define ID_1 0x00001 // 1:off, 2:off |
MRSa | 0:516a9e85a3fb | 31 | #define ID_2 0x00002 // 1:on, 2:off |
MRSa | 0:516a9e85a3fb | 32 | #define ID_3 0x00004 // 1:off, 2:on |
MRSa | 0:516a9e85a3fb | 33 | #define ID_4 0x00008 // 1:on, 2:on |
MRSa | 0:516a9e85a3fb | 34 | |
MRSa | 0:516a9e85a3fb | 35 | |
MRSa | 0:516a9e85a3fb | 36 | /** |
MRSa | 0:516a9e85a3fb | 37 | * CHECK DIP-SW STATUS |
MRSa | 0:516a9e85a3fb | 38 | */ |
MRSa | 0:516a9e85a3fb | 39 | int check_DipSw() |
MRSa | 0:516a9e85a3fb | 40 | { |
MRSa | 0:516a9e85a3fb | 41 | if (dip1 == 0) |
MRSa | 0:516a9e85a3fb | 42 | { |
MRSa | 0:516a9e85a3fb | 43 | return ((dip2 == 0) ? 4 : 2); |
MRSa | 0:516a9e85a3fb | 44 | } |
MRSa | 0:516a9e85a3fb | 45 | return ((dip2 == 0) ? 3 : 1); |
MRSa | 0:516a9e85a3fb | 46 | } |
MRSa | 0:516a9e85a3fb | 47 | |
MRSa | 0:516a9e85a3fb | 48 | /** |
MRSa | 0:516a9e85a3fb | 49 | * GET LED LAMP ID |
MRSa | 0:516a9e85a3fb | 50 | */ |
MRSa | 0:516a9e85a3fb | 51 | int getLampId(int id) |
MRSa | 0:516a9e85a3fb | 52 | { |
MRSa | 0:516a9e85a3fb | 53 | int lampId = ID_1; |
MRSa | 0:516a9e85a3fb | 54 | switch (id) |
MRSa | 0:516a9e85a3fb | 55 | { |
MRSa | 0:516a9e85a3fb | 56 | case 4: |
MRSa | 0:516a9e85a3fb | 57 | lampId = ID_4; |
MRSa | 0:516a9e85a3fb | 58 | break; |
MRSa | 0:516a9e85a3fb | 59 | case 3: |
MRSa | 0:516a9e85a3fb | 60 | lampId = ID_3; |
MRSa | 0:516a9e85a3fb | 61 | break; |
MRSa | 0:516a9e85a3fb | 62 | case 2: |
MRSa | 0:516a9e85a3fb | 63 | lampId = ID_2; |
MRSa | 0:516a9e85a3fb | 64 | break; |
MRSa | 0:516a9e85a3fb | 65 | case 1: |
MRSa | 0:516a9e85a3fb | 66 | default: |
MRSa | 0:516a9e85a3fb | 67 | lampId = ID_1; |
MRSa | 0:516a9e85a3fb | 68 | break; |
MRSa | 0:516a9e85a3fb | 69 | } |
MRSa | 0:516a9e85a3fb | 70 | return (lampId); |
MRSa | 0:516a9e85a3fb | 71 | } |
MRSa | 0:516a9e85a3fb | 72 | |
MRSa | 0:516a9e85a3fb | 73 | /** |
MRSa | 2:691ccdef6f1e | 74 | * |
MRSa | 2:691ccdef6f1e | 75 | */ |
MRSa | 2:691ccdef6f1e | 76 | void executeAction(uint8_t commandType, uint8_t channel, uint8_t command) |
MRSa | 2:691ccdef6f1e | 77 | { |
MRSa | 2:691ccdef6f1e | 78 | if (commandType != 0x4e) |
MRSa | 2:691ccdef6f1e | 79 | { |
MRSa | 2:691ccdef6f1e | 80 | // do nothing! |
MRSa | 2:691ccdef6f1e | 81 | return; |
MRSa | 2:691ccdef6f1e | 82 | } |
MRSa | 2:691ccdef6f1e | 83 | // execute! |
MRSa | 2:691ccdef6f1e | 84 | if ((command & 0x40) != 0) |
MRSa | 2:691ccdef6f1e | 85 | { |
MRSa | 2:691ccdef6f1e | 86 | led0 = 1; |
MRSa | 2:691ccdef6f1e | 87 | } |
MRSa | 2:691ccdef6f1e | 88 | else |
MRSa | 2:691ccdef6f1e | 89 | { |
MRSa | 2:691ccdef6f1e | 90 | led0 = 0; |
MRSa | 2:691ccdef6f1e | 91 | } |
MRSa | 3:ce23e131fdbd | 92 | if ((command & 0x04) != 0) |
MRSa | 2:691ccdef6f1e | 93 | { |
MRSa | 2:691ccdef6f1e | 94 | led1 = 1; |
MRSa | 2:691ccdef6f1e | 95 | } |
MRSa | 2:691ccdef6f1e | 96 | else |
MRSa | 2:691ccdef6f1e | 97 | { |
MRSa | 2:691ccdef6f1e | 98 | led1 = 0; |
MRSa | 2:691ccdef6f1e | 99 | } |
MRSa | 2:691ccdef6f1e | 100 | |
MRSa | 2:691ccdef6f1e | 101 | } |
MRSa | 2:691ccdef6f1e | 102 | |
MRSa | 2:691ccdef6f1e | 103 | /** |
MRSa | 0:516a9e85a3fb | 104 | * Recieved a message from ANT port. |
MRSa | 0:516a9e85a3fb | 105 | */ |
MRSa | 0:516a9e85a3fb | 106 | void receivedFromAnt() |
MRSa | 0:516a9e85a3fb | 107 | { |
MRSa | 0:516a9e85a3fb | 108 | uint8_t receivedLength = 0; |
MRSa | 0:516a9e85a3fb | 109 | uint8_t receivedBuffer[MAX_BUFSIZE]; |
MRSa | 0:516a9e85a3fb | 110 | receivedLength = antPort.BC_ANT_RecvPacket(receivedBuffer, MAX_BUFSIZE); |
MRSa | 0:516a9e85a3fb | 111 | |
MRSa | 2:691ccdef6f1e | 112 | executeAction(receivedBuffer[2], receivedBuffer[4], receivedBuffer[6]); |
MRSa | 2:691ccdef6f1e | 113 | |
MRSa | 3:ce23e131fdbd | 114 | // Dump received message to USB |
MRSa | 0:516a9e85a3fb | 115 | printf("RX(%3d):", receivedLength); |
MRSa | 0:516a9e85a3fb | 116 | for (int index = 0; index < receivedLength; index++) |
MRSa | 0:516a9e85a3fb | 117 | { |
MRSa | 0:516a9e85a3fb | 118 | printf("%02x ", receivedBuffer[index]); |
MRSa | 0:516a9e85a3fb | 119 | } |
MRSa | 0:516a9e85a3fb | 120 | printf("\r\n"); |
MRSa | 0:516a9e85a3fb | 121 | } |
MRSa | 0:516a9e85a3fb | 122 | |
MRSa | 0:516a9e85a3fb | 123 | /** |
MRSa | 0:516a9e85a3fb | 124 | * initializes ANT port |
MRSa | 0:516a9e85a3fb | 125 | */ |
MRSa | 3:ce23e131fdbd | 126 | int initialize_ANTport() |
MRSa | 0:516a9e85a3fb | 127 | { |
MRSa | 0:516a9e85a3fb | 128 | antPort.ANT_ResetSystem(); |
MRSa | 0:516a9e85a3fb | 129 | antPort.ANT_AssignChannel(ANT_CH, ANT_Shared_Bidirectional_Slave, ANT_NETWORK_NUMBER); |
MRSa | 0:516a9e85a3fb | 130 | antPort.ANT_SetChannelId(ANT_CH, DEVICE_NUMBER, DEVICE_TYPE_ID, TRANS_TYPE_SHARED_2BYTES); |
MRSa | 0:516a9e85a3fb | 131 | antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 16); // 16Hz : 32768/16 = 2048 |
MRSa | 0:516a9e85a3fb | 132 | antPort.ANT_SetChannelRFFreq(ANT_CH, 4); // 2404 MHz |
MRSa | 0:516a9e85a3fb | 133 | antPort.ANT_OpenChannel(ANT_CH); // |
MRSa | 0:516a9e85a3fb | 134 | antPort.attach(&receivedFromAnt); // callback function |
MRSa | 0:516a9e85a3fb | 135 | |
MRSa | 0:516a9e85a3fb | 136 | // set device's shared address |
MRSa | 0:516a9e85a3fb | 137 | uint8_t buffer[MAX_BUFSIZE] = |
MRSa | 0:516a9e85a3fb | 138 | { |
MRSa | 0:516a9e85a3fb | 139 | 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
MRSa | 0:516a9e85a3fb | 140 | }; |
MRSa | 0:516a9e85a3fb | 141 | buffer[0] = check_DipSw(); |
MRSa | 0:516a9e85a3fb | 142 | antPort.ANT_SendBroadcastData(ANT_CH, buffer); |
MRSa | 0:516a9e85a3fb | 143 | return (0); |
MRSa | 0:516a9e85a3fb | 144 | } |
MRSa | 0:516a9e85a3fb | 145 | |
MRSa | 0:516a9e85a3fb | 146 | /** |
MRSa | 0:516a9e85a3fb | 147 | * Main Routine |
MRSa | 0:516a9e85a3fb | 148 | */ |
MRSa | 0:516a9e85a3fb | 149 | int main() |
MRSa | 0:516a9e85a3fb | 150 | { |
MRSa | 3:ce23e131fdbd | 151 | pc.baud(9600); // set serial speed between PC and mbed. |
MRSa | 0:516a9e85a3fb | 152 | printf("--- INITIALIZE (ID:%d) ----\r\n", check_DipSw()); |
MRSa | 0:516a9e85a3fb | 153 | int statusLamp = getLampId(check_DipSw()); |
MRSa | 3:ce23e131fdbd | 154 | initialize_ANTport(); // initialize BC-ANT-SERIAL |
MRSa | 0:516a9e85a3fb | 155 | printf("--- READY ----\r\n"); |
MRSa | 0:516a9e85a3fb | 156 | |
MRSa | 0:516a9e85a3fb | 157 | while(1) |
MRSa | 0:516a9e85a3fb | 158 | { |
MRSa | 0:516a9e85a3fb | 159 | myLeds = statusLamp; |
MRSa | 0:516a9e85a3fb | 160 | wait(0.5); |
MRSa | 0:516a9e85a3fb | 161 | myLeds = 0; |
MRSa | 0:516a9e85a3fb | 162 | wait(0.5); |
MRSa | 0:516a9e85a3fb | 163 | } |
MRSa | 0:516a9e85a3fb | 164 | } |