First Commit

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //------------------------------------------------------------
00002 //Include Header Files
00003 //------------------------------------------------------------ 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 
00007 
00008 //------------------------------------------------------------
00009 //Definition
00010 //------------------------------------------------------------ 
00011 #define TXRX_BUF_LEN 20                     //max 20[byte]
00012 #define DEVICE_LOCAL_NAME "NampassCar"     
00013 #define ADVERTISING_INTERVAL 160            //160 * 0.625[ms] = 100[ms]
00014 #define TICKER_TIME 200000                  //200000[us] = 200[ms]
00015 #define DIGITAL_OUT_PIN P0_9
00016 #define ANALOG_IN_PIN   P0_4
00017 
00018 
00019 //------------------------------------------------------------
00020 //Command Code
00021 //------------------------------------------------------------
00022 uint8_t ForwardComm[4] = {0, 0, 0, 0};
00023 uint8_t BackComm[4] = {1, 1, 1, 1};
00024 
00025 
00026 //------------------------------------------------------------
00027 //Object generation
00028 //------------------------------------------------------------ 
00029 BLE ble;
00030 DigitalOut ForwardPin(P0_6);
00031 DigitalOut BackPin(P0_7); 
00032 AnalogIn ANALOG(ANALOG_IN_PIN);
00033 
00034 
00035 //------------------------------------------------------------
00036 //Service & Characteristic Setting
00037 //------------------------------------------------------------ 
00038 //Service UUID
00039 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00040 
00041 //Characteristic UUID
00042 static const uint8_t tx_uuid[]   = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00043 static const uint8_t rx_uuid[]   = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00044 
00045 //Characteristic Value
00046 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00047 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00048 
00049 //Characteristic Property Setting etc
00050 GattCharacteristic  txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00051 GattCharacteristic  rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00052 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic};
00053 
00054 //Service Setting
00055 GattService         myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *));
00056 
00057 
00058 //======================================================================
00059 //onDisconnection
00060 //======================================================================
00061 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00062 {
00063     ble.startAdvertising();
00064 }
00065 
00066 
00067 bool checkBuffMatch(uint8_t *buf1, uint8_t *buf2, uint8_t length){
00068     for(int i = 0; i < length; i++){
00069         if(*(buf1 + i) != *(buf2 + i)) return false;
00070     }
00071     
00072     return true;
00073 }
00074 
00075 //======================================================================
00076 //onDataWritten
00077 //======================================================================
00078 void WrittenHandler(const GattWriteCallbackParams *Handler)
00079 {   
00080     uint8_t buf[4] = {0};
00081     uint16_t bytesRead;
00082     
00083     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00084     {
00085         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00086         memset(txPayload, 0, TXRX_BUF_LEN);
00087         memcpy(txPayload, buf, TXRX_BUF_LEN); 
00088        
00089         if(checkBuffMatch(buf, ForwardComm, 4)){
00090             ForwardPin = 1;
00091             BackPin = 0;
00092             wait(1);
00093             BackPin = 0;
00094             ForwardPin = 0;
00095         }else if(checkBuffMatch(buf, BackComm, 4)){
00096             ForwardPin = 0;
00097             BackPin = 1;
00098             wait(1);
00099             BackPin = 0;
00100             ForwardPin = 0;
00101         }else{
00102             printf("failed\r\n");
00103         }
00104     }
00105 }
00106 
00107 
00108 //======================================================================
00109 //onTimeout
00110 //======================================================================
00111 void m_status_check_handle(void)
00112 {   
00113     uint8_t buf[2];
00114 
00115     //Read Analog port
00116     float s = ANALOG;
00117     uint16_t value = s*1024; 
00118     buf[0] = (value >> 8);
00119     buf[1] = value;
00120     
00121     //Send out
00122     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2); 
00123 }
00124 
00125 
00126 //======================================================================
00127 //convert reverse UUID
00128 //======================================================================
00129 void reverseUUID(const uint8_t* src, uint8_t* dst)
00130 {
00131     int i;
00132     
00133     for(i=0;i<16;i++)
00134         dst[i] = src[15 - i];
00135 }
00136 
00137 
00138 //======================================================================
00139 //main
00140 //======================================================================
00141 int main(void)
00142 {
00143     uint8_t base_uuid_rev[16];
00144 
00145     //Timer Setting [us]
00146     Ticker ticker;
00147     ticker.attach_us(m_status_check_handle, TICKER_TIME);
00148     
00149     //BLE init
00150     ble.init();
00151     
00152     //EventListener
00153     ble.onDisconnection(disconnectionCallback);
00154     ble.onDataWritten(WrittenHandler);  
00155 
00156     //------------------------------------------------------------
00157     //setup advertising 
00158     //------------------------------------------------------------
00159     //Classic BT not support
00160     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00161     
00162     //Connectable to Central
00163     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00164     
00165     //Local Name
00166     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00167                                     (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1);
00168     
00169     //GAP AdvertisingData                                
00170     reverseUUID(base_uuid, base_uuid_rev);  
00171     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00172                                     (uint8_t *)base_uuid_rev, sizeof(base_uuid));
00173                                     
00174     //Advertising Interval 
00175     ble.setAdvertisingInterval(ADVERTISING_INTERVAL);
00176 
00177     //Add Service
00178     ble.addService(myService);
00179     
00180     //Start Advertising
00181     ble.startAdvertising(); 
00182     
00183     //------------------------------------------------------------
00184     //Loop
00185     //------------------------------------------------------------
00186     while(1)
00187     {
00188         ble.waitForEvent(); 
00189     }
00190 }