![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
initial revision
bc_antを使うサンプルコード
#include "mbed.h" #include "bc_ant.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); // USBTX - Tranmit on USB USBRX - receive on USB bc_ant antPort(p13, p14, p15); // for BC-ANT-SERIAL #define MAX_BUFSIZE 64 #define ANT_CH 0 #define DEVICE_NUMBER 65 /** * 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(); if (isReceive == true) { // receiver mode antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Slave, 0); } else { // sender mode antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Master, 0); } antPort.ANT_SetChannelId(ANT_CH, DEVICE_NUMBER, 1, 1); antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 4); antPort.ANT_SetChannelRFFreq(ANT_CH, 4); // 2404 MHz antPort.ANT_OpenChannel(ANT_CH); if (isReceive == true) { // receiver mode antPort.attach(&receivedFromAnt); } else { // sender mode uint8_t buffer[MAX_BUFSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; antPort.ANT_SendBroadcastData(ANT_CH, buffer); } return (0); } /** * Main Routine */ int main() { pc.baud(9600); // set serial speed between PC and mbed. initialize_ANTport(true); // initialize BC-ANT-SERIAL for printf("--- READY ----\r\n"); while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
main.cpp@0:bfb6e1eaa57f, 2013-03-17 (annotated)
- Committer:
- MRSa
- Date:
- Sun Mar 17 13:46:27 2013 +0000
- Revision:
- 0:bfb6e1eaa57f
BC-ANT-SERIAL Communication Sample code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MRSa | 0:bfb6e1eaa57f | 1 | #include "mbed.h" |
MRSa | 0:bfb6e1eaa57f | 2 | #include "bc_ant.h" |
MRSa | 0:bfb6e1eaa57f | 3 | |
MRSa | 0:bfb6e1eaa57f | 4 | DigitalOut myled(LED1); |
MRSa | 0:bfb6e1eaa57f | 5 | |
MRSa | 0:bfb6e1eaa57f | 6 | Serial pc(USBTX, USBRX); // USBTX - Tranmit on USB USBRX - receive on USB |
MRSa | 0:bfb6e1eaa57f | 7 | bc_ant antPort(p13, p14, p15); // for BC-ANT-SERIAL |
MRSa | 0:bfb6e1eaa57f | 8 | |
MRSa | 0:bfb6e1eaa57f | 9 | #define MAX_BUFSIZE 64 |
MRSa | 0:bfb6e1eaa57f | 10 | #define ANT_CH 0 |
MRSa | 0:bfb6e1eaa57f | 11 | #define DEVICE_NUMBER 65 |
MRSa | 0:bfb6e1eaa57f | 12 | |
MRSa | 0:bfb6e1eaa57f | 13 | /** |
MRSa | 0:bfb6e1eaa57f | 14 | * Recieved a message from ANT port. |
MRSa | 0:bfb6e1eaa57f | 15 | */ |
MRSa | 0:bfb6e1eaa57f | 16 | void receivedFromAnt() |
MRSa | 0:bfb6e1eaa57f | 17 | { |
MRSa | 0:bfb6e1eaa57f | 18 | uint8_t receivedLength = 0; |
MRSa | 0:bfb6e1eaa57f | 19 | uint8_t receivedBuffer[MAX_BUFSIZE]; |
MRSa | 0:bfb6e1eaa57f | 20 | receivedLength = antPort.BC_ANT_RecvPacket(receivedBuffer, MAX_BUFSIZE); |
MRSa | 0:bfb6e1eaa57f | 21 | |
MRSa | 0:bfb6e1eaa57f | 22 | printf("RX(%3d):", receivedLength); |
MRSa | 0:bfb6e1eaa57f | 23 | for (int index = 0; index < receivedLength; index++) |
MRSa | 0:bfb6e1eaa57f | 24 | { |
MRSa | 0:bfb6e1eaa57f | 25 | printf("%02x ", receivedBuffer[index]); |
MRSa | 0:bfb6e1eaa57f | 26 | } |
MRSa | 0:bfb6e1eaa57f | 27 | printf("\r\n"); |
MRSa | 0:bfb6e1eaa57f | 28 | } |
MRSa | 0:bfb6e1eaa57f | 29 | |
MRSa | 0:bfb6e1eaa57f | 30 | /** |
MRSa | 0:bfb6e1eaa57f | 31 | * Moving command received from HOST |
MRSa | 0:bfb6e1eaa57f | 32 | */ |
MRSa | 0:bfb6e1eaa57f | 33 | void sendToAnt(uint8_t *buffer) |
MRSa | 0:bfb6e1eaa57f | 34 | { |
MRSa | 0:bfb6e1eaa57f | 35 | antPort.ANT_SendAcknowledgedData(ANT_CH, buffer); |
MRSa | 0:bfb6e1eaa57f | 36 | } |
MRSa | 0:bfb6e1eaa57f | 37 | |
MRSa | 0:bfb6e1eaa57f | 38 | /** |
MRSa | 0:bfb6e1eaa57f | 39 | * initializes ANT port |
MRSa | 0:bfb6e1eaa57f | 40 | */ |
MRSa | 0:bfb6e1eaa57f | 41 | int initialize_ANTport(bool isReceive) |
MRSa | 0:bfb6e1eaa57f | 42 | { |
MRSa | 0:bfb6e1eaa57f | 43 | antPort.ANT_ResetSystem(); |
MRSa | 0:bfb6e1eaa57f | 44 | if (isReceive == true) |
MRSa | 0:bfb6e1eaa57f | 45 | { |
MRSa | 0:bfb6e1eaa57f | 46 | // receiver mode |
MRSa | 0:bfb6e1eaa57f | 47 | antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Slave, 0); |
MRSa | 0:bfb6e1eaa57f | 48 | } |
MRSa | 0:bfb6e1eaa57f | 49 | else |
MRSa | 0:bfb6e1eaa57f | 50 | { |
MRSa | 0:bfb6e1eaa57f | 51 | // sender mode |
MRSa | 0:bfb6e1eaa57f | 52 | antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Master, 0); |
MRSa | 0:bfb6e1eaa57f | 53 | } |
MRSa | 0:bfb6e1eaa57f | 54 | antPort.ANT_SetChannelId(ANT_CH, DEVICE_NUMBER, 1, 1); |
MRSa | 0:bfb6e1eaa57f | 55 | |
MRSa | 0:bfb6e1eaa57f | 56 | antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 4); |
MRSa | 0:bfb6e1eaa57f | 57 | antPort.ANT_SetChannelRFFreq(ANT_CH, 4); // 2404 MHz |
MRSa | 0:bfb6e1eaa57f | 58 | antPort.ANT_OpenChannel(ANT_CH); |
MRSa | 0:bfb6e1eaa57f | 59 | |
MRSa | 0:bfb6e1eaa57f | 60 | if (isReceive == true) |
MRSa | 0:bfb6e1eaa57f | 61 | { |
MRSa | 0:bfb6e1eaa57f | 62 | // receiver mode |
MRSa | 0:bfb6e1eaa57f | 63 | antPort.attach(&receivedFromAnt); |
MRSa | 0:bfb6e1eaa57f | 64 | } |
MRSa | 0:bfb6e1eaa57f | 65 | else |
MRSa | 0:bfb6e1eaa57f | 66 | { |
MRSa | 0:bfb6e1eaa57f | 67 | // sender mode |
MRSa | 0:bfb6e1eaa57f | 68 | uint8_t buffer[MAX_BUFSIZE] = |
MRSa | 0:bfb6e1eaa57f | 69 | { |
MRSa | 0:bfb6e1eaa57f | 70 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
MRSa | 0:bfb6e1eaa57f | 71 | }; |
MRSa | 0:bfb6e1eaa57f | 72 | antPort.ANT_SendBroadcastData(ANT_CH, buffer); |
MRSa | 0:bfb6e1eaa57f | 73 | } |
MRSa | 0:bfb6e1eaa57f | 74 | return (0); |
MRSa | 0:bfb6e1eaa57f | 75 | } |
MRSa | 0:bfb6e1eaa57f | 76 | |
MRSa | 0:bfb6e1eaa57f | 77 | |
MRSa | 0:bfb6e1eaa57f | 78 | /** |
MRSa | 0:bfb6e1eaa57f | 79 | * Main Routine |
MRSa | 0:bfb6e1eaa57f | 80 | */ |
MRSa | 0:bfb6e1eaa57f | 81 | int main() |
MRSa | 0:bfb6e1eaa57f | 82 | { |
MRSa | 0:bfb6e1eaa57f | 83 | pc.baud(9600); // set serial speed between PC and mbed. |
MRSa | 0:bfb6e1eaa57f | 84 | initialize_ANTport(true); // initialize BC-ANT-SERIAL for |
MRSa | 0:bfb6e1eaa57f | 85 | printf("--- READY ----\r\n"); |
MRSa | 0:bfb6e1eaa57f | 86 | |
MRSa | 0:bfb6e1eaa57f | 87 | while(1) { |
MRSa | 0:bfb6e1eaa57f | 88 | myled = 1; |
MRSa | 0:bfb6e1eaa57f | 89 | wait(0.2); |
MRSa | 0:bfb6e1eaa57f | 90 | myled = 0; |
MRSa | 0:bfb6e1eaa57f | 91 | wait(0.2); |
MRSa | 0:bfb6e1eaa57f | 92 | } |
MRSa | 0:bfb6e1eaa57f | 93 | } |