Aura-Tech Solutions / Mbed 2 deprecated Connect_Test5

Dependencies:   BLE_API mbed nRF51822

Fork of Connect_Test4 by Aura-Tech Solutions

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLEDevice.h"
00003 #include "AltBeaconService.h"
00004 #include "iBeaconService.h"
00005 
00006 
00007 BLEDevice ble;
00008 DigitalOut led(LED1);
00009 
00010 /*..........................................................................
00011 uint8_t beaconID[] =    {   0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00012                             0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
00013                             0x11, 0x22, 0x33, 0x44
00014                         };
00015 uint16_t manufacturerID = 0x5900; //Nordic SIG ID
00016 int8_t rssi = -122;
00017 ..........................................................................*/
00018 
00019 
00020 uint8_t beaconID[] =    {   0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00021                             0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61
00022                         };
00023 uint16_t majorNumber = 1122;
00024 uint16_t minorNumber = 3344;
00025 uint16_t txPower = 0xC8;
00026 
00027 
00028 uint16_t customServiceUUID  = 0xA000;
00029 //uint8_t customServiceUUID[] =    {   0x68, 0xC0, 0x2F, 0xDF, 0xEC, 0x8B, 0x4C, 0xAB, 0x23, 0x08, 0x1F, 0x3C, 0xEC, 0xD6, 0x3C, 0xE7};
00030 uint16_t readCharUUID       = 0xA001;
00031 uint16_t writeCharUUID      = 0xA002;
00032 //uint8_t writeCharUUID[] =    {   0x68, 0xC0, 0x2F, 0xDF, 0xEC, 0x8B, 0x4C, 0xAB, 0x23, 0x08, 0x1F, 0x3C, 0xEC, 0xD6, 0x3C, 0xE7};
00033 uint16_t A16                = 0xA005;
00034 
00035 
00036 
00037 
00038 
00039 const uint8_t b1_id   = 0x01;
00040 const uint8_t b2_id   = 0x02;
00041 const uint8_t b3_id   = 0x03;
00042 const uint8_t b4_id   = 0x04;
00043 const uint8_t b5_id   = 0x05;
00044 const uint8_t b6_id   = 0x06;
00045 const uint8_t b7_id   = 0x07;
00046 const uint8_t b8_id   = 0x08;
00047 
00048 const uint8_t bA1_id  = 0x31;
00049 const uint8_t bA2_id  = 0x32;
00050 const uint8_t bA3_id  = 0x33;
00051 const uint8_t bA4_id  = 0x34;
00052 const uint8_t bA5_id  = 0x35;
00053 const uint8_t bA6_id  = 0x36;
00054 const uint8_t bA7_id  = 0x37;
00055 
00056 PinName flood_p     = P0_4;
00057 PinName b1_p        = P0_28;
00058 PinName b2_p        = P0_29;
00059 PinName b3_p        = P0_15;
00060 PinName b4_p        = P0_10; //CTX
00061 PinName b5_p        = P0_9;  //TXD
00062 PinName b6_p        = P0_11; //RXD
00063 PinName b7_p        = P0_8;  //RTS
00064 
00065 
00066 /*
00067 PinName b4_p             = CTX; //CTX
00068 PinName b5_p             = TXD;  //TXD
00069 PinName b6_p             = D0; //RXD
00070 PinName b7_p             = D3;  //RTS
00071 */
00072 
00073 
00074 const static char     DEVICE_NAME[]        = "PETE2"; // change this
00075 static const uint16_t uuid16_list[]        = {0xFF02}; //Custom UUID, FFFF is reserved for development
00076 
00077 
00078 //setup custom characteristics
00079 
00080 //read
00081 static uint8_t readValue[10] = {0};
00082 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
00083 //write
00084 static uint8_t writeValue[10] = {0};
00085 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
00086 
00087 
00088 // Set up custom service
00089 GattCharacteristic  *characteristics[] = {&readChar, &writeChar};
00090 GattService         customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00091 
00092 //setup led
00093 DigitalOut led1(P0_19);
00094 
00095 //setup digital outs
00096 DigitalOut b1(b1_p);
00097 DigitalOut b2(b2_p);
00098 DigitalOut b3(b3_p);
00099 DigitalOut b4(b4_p);
00100 DigitalOut b5(b5_p);
00101 //DigitalOut b6(b6_p);
00102 DigitalOut b7(b7_p);
00103 
00104 DigitalIn enable(flood_p);
00105 
00106 
00107 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
00108 void writeCharCallback(const GattCharacteristicWriteCBParams *params);
00109 void testAll(void);
00110 void blinky(void);
00111 int flip(char value);
00112 
00113 
00114 //main loop............................................................................................................
00115 int main(void)
00116 {
00117     /* initialize stuff */
00118     led1 = 1;
00119     
00120     b1 = 1;//b1_p = P0_28;
00121     b2 = 1;//b2_p = P0_29;
00122     b3 = 1;//b3_p = P0_9;
00123     b4 = 1;//b4_p = P0_10; //CTX
00124     b5 = 1;//b5_p = P0_9;  //TXD
00125     //b6 = 1;//b6_p = P0_11; //RXD
00126     b7 = 1;//b7_p = P0_8;  //RTS
00127               
00128     
00129 
00130     
00131     ble.init();
00132     
00133     //setup beacon service
00134     //AltBeaconService altbeacon(ble, manufacturerID, beaconID, rssi);
00135     iBeaconService ibeacon(ble, beaconID, majorNumber, minorNumber, txPower);
00136     
00137     
00138     ble.onDisconnection(disconnectionCallback);
00139     ble.onDataWritten(writeCharCallback);
00140 
00141 
00142     /* setup advertising */
00143     
00144     // BLE only, no classic BT
00145     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00146     // advertising type
00147     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00148     // add name
00149     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00150     // UUID's broadcast in advertising packet
00151     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00152     
00153     
00154     // set interval
00155     ble.setAdvertisingInterval(100); // 100ms. 
00156 
00157     // add our custom service
00158     ble.addService(customService);
00159 
00160     // start advertising
00161     ble.startAdvertising(); 
00162     
00163     // infinite loop waiting for BLE interrupt events
00164     while (true) {
00165         ble.waitForEvent(); //Save power
00166     }
00167 }
00168 //.......................end main............................................................................................
00169 
00170 /*
00171  *  Restart advertising when phone app disconnects
00172 */ 
00173 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00174 {
00175     ble.startAdvertising(); 
00176 }
00177 
00178 /* 
00179  *  handle writes to writeCharacteristic
00180 */
00181 void writeCharCallback(const GattCharacteristicWriteCBParams *params)
00182 {
00183     // check to see what characteristic was written, by handle
00184     if(params->charHandle == writeChar.getValueHandle()) 
00185     {
00186         //testAll();
00187         flip(params->data[0]);
00188 
00189         // update the readChar with the value of writeChar
00190         ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len);
00191     }
00192 }
00193 
00194 void testAll(void)
00195 {
00196     for(int i=0; i<4; i++)    
00197     {
00198         led1 = !led1;
00199         b1 = !b1;
00200         b2 = !b2;
00201         b3 = !b3;
00202         b4 = !b4;
00203         b5 = !b5;
00204         //b6 = !b6;
00205         b7 = !b7;
00206         wait(1);
00207     }
00208 }
00209 void blinky(void)
00210 {
00211     for(int i=0; i<20; i++)    
00212     {
00213         led1 = !led1;
00214         wait(.3);
00215     }
00216 }
00217 int flip(char value)
00218 {
00219     int ret = 0;
00220     
00221     switch(value)
00222     {
00223         case b1_id  :
00224         case bA1_id :       led1 = !led1;
00225                             b1 = !b1;
00226                             break;
00227                             
00228         case b2_id  :
00229         case bA2_id :       led1 = !led1;
00230                             b2 = !b2;
00231                             break;
00232         
00233         case b3_id  :
00234         case bA3_id :       led1 = !led1;
00235                             b3 = !b3;
00236                             break;
00237         
00238         case b4_id  :
00239         case bA4_id :       led1 = !led1;
00240                             b4 = !b4;
00241                             break;
00242         
00243         case b5_id  :
00244         case bA5_id :       led1 = !led1;
00245                             b5 = !b5;
00246                             break;
00247         
00248         case b7_id  :
00249         case bA7_id :       led1 = !led1;
00250                             b7 = !b7;
00251                             break;
00252         
00253         default :           blinky();
00254                             ret = 1;
00255                             break;
00256     }
00257     return ret;
00258 }