12

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* 
00003         abcd olan herşeyi gönderir.
00004 */
00005 #include "mbed.h"
00006 #include "toolchain.h"
00007 #include "ble/BLE.h"
00008 #include <string>
00009 #include <stdio.h>
00010  
00011 #define RSSI_DEGER 55 // -55dBm'in altındakileri görür.
00012 #define ToplamCihaz 1// (10) hakan değiştir 01122016
00013 #define bufferSize 60
00014 
00015 //#include <stdio.h>
00016 //#include <conio.h>
00017 #include <stdlib.h>
00018 #include "MbedJSONValue.h"
00019 MbedJSONValue demo;
00020 std::string s;
00021     
00022 DigitalIn esp_baglimi(P0_11);   // ###############################
00023 DigitalOut alivenessLED(p2, 1); // ###############################
00024 
00025 DigitalOut led1(LED1, 0);
00026 DigitalOut led2(LED2, 1);
00027 DigitalOut led3(LED3, 1);
00028  
00029 Ticker     ticker;
00030 Serial pc(TX_PIN_NUMBER, RX_PIN_NUMBER);
00031 
00032 uint8_t       bulunan_index[ToplamCihaz];
00033 uint8_t buffertoSpi[100];
00034 uint8_t say=0;
00035 int boyut, byt, databoyut;
00036 int a, sayac = 1, sayac2 = 2, sayac3 = 0, sayac4 = 0;
00037 int gelen, gelene;
00038 
00039 struct Medron_Data
00040 {    
00041         uint8_t                         MacID[6];   // MAC ID leri bunun içinde tutacağız
00042         uint8_t                         RSSI;   // MAC ID leri bunun içinde tutacağız
00043         uint8_t                         LENGTH;
00044         uint8_t                         MYDATA[50];
00045         uint8_t                         PAKET_LENGTH;//SPI -> STM32 GÖNDERİLCEK DATANIN TOPLAM UZUNLUGU. 
00046 } PACKED_Data[ToplamCihaz]; 
00047 
00048 int *f;
00049 int size=0;
00050 
00051 void enqueue(int data){ // kuyruğa ekle // data
00052     int * b = (int * ) malloc(sizeof(int)*size+1);
00053     int i=0;
00054     
00055     printf("\r\n\r\nENQ_Giris = data= %d  %x", data, data);
00056     
00057     for(i = 0; i<size; i++){
00058         b[i]=f[i];
00059         printf("\r\nENQ_for b[i] = %d  %x \t_data = %d  %x", b[i], b[i], data, data);
00060     }  
00061     
00062     b[i]=data;
00063     //b[i]=*data;
00064     
00065     printf("\r\n\r\nENQ_ i = %d  b[i] = %d  %x \tENQ_data_son = %d  %x", i, b[i], b[i], data, data);
00066     
00067     size++;
00068     f=b;
00069 }
00070 
00071 int dequeue(){ // kuyruktan çıkar
00072     if(size<=0)
00073     return -1;
00074     
00075     int * b = (int * ) malloc(sizeof(int)*size-1);
00076     
00077     for(int i = 0;i<size-1;i++){
00078         b[i]=f[i+1];
00079     }   
00080      
00081     int result=f[0];
00082     size--;
00083     f=b;
00084     return result;
00085 }
00086 
00087 void printqueue(){
00088     printf("\n");
00089     for(int i = 0;i<size;i++)
00090     printf("%d_",f[i]);
00091 }
00092 
00093 void periodicCallback(void)
00094 {
00095   // Do blinky on LED1 while we're waiting for BLE events. This is optional.
00096   // alivenessLED = !alivenessLED; 
00097 }
00098  
00099 // This function is called every time we scan an advertisement.
00100 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
00101 {
00102     struct AdvertisingData_t
00103     {
00104         uint8_t                        length; // doesn't include itself
00105         GapAdvertisingData::DataType_t dataType;
00106         uint8_t                        data[0];
00107     } PACKED;
00108     
00109     struct ApplicationData_t
00110     {
00111         // An ID used to identify temperature value in the manufacture specific AD data field 
00112         uint16_t applicationSpecificId; 
00113     } PACKED;
00114                    
00115     static const uint16_t MED_KIMLIK = 0xABCD;
00116     AdvertisingData_t *pAdvData;
00117     size_t index = 0;
00118     
00119     while (index < params->advertisingDataLen) 
00120     {
00121         pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
00122         
00123         if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) 
00124         {
00125             ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
00126             
00127             if (pAppData->applicationSpecificId == MED_KIMLIK)
00128             {
00129                 alivenessLED = 1;
00130         
00131                //pc.printf("%d\r", say+1);
00132                
00133                 PACKED_Data[say].MacID[5]=params->peerAddr[5];
00134                 PACKED_Data[say].MacID[4]=params->peerAddr[4];
00135                 PACKED_Data[say].MacID[3]=params->peerAddr[3];
00136                 PACKED_Data[say].MacID[2]=params->peerAddr[2];
00137                 PACKED_Data[say].MacID[1]=params->peerAddr[1];
00138                 PACKED_Data[say].MacID[0]=params->peerAddr[0];
00139                 PACKED_Data[say].RSSI=(params->rssi) * -1;
00140                 PACKED_Data[say].LENGTH=params->advertisingDataLen;
00141                 
00142                 /*
00143                 EXAMPLE
00144                 
00145                 fa 8c b3 c7 1c 61 :MAC ; 6 BYTE
00146                 2b:RSSI;1BYTE 
00147                 0a:boyut;1BYTE-->0A Hex = 10 Dec
00148                 09  ff cd ab 01 00 4d 52 e8 00 00 00 00 00 00 00 00 00 00 00 ;DATA
00149                 1   2  3  4  5  6  7  8  9  10  X  X  X  X  X  X  X  X  X  X ;DATA SAYISI    
00150                 
00151                 c8 60 85 e2 73 e2 :MAC:6
00152                 37:RSSI:1
00153                 08:LENGTH:1
00154                 07 ff cd ab 00 00 e7 00:DATA
00155                 1  2  3  4  5  6  7  8 ;DATA SAYISI   
00156                 
00157                 */
00158                 
00159                 PACKED_Data[say].PAKET_LENGTH= 6 + 1 + 1 + params->advertisingDataLen;//MAC+RSSI+1+LENGTH
00160                 
00161                 pc.printf("\r\nTOPLAM PAKET boyut = (%d ) : ---> ",PACKED_Data[say].PAKET_LENGTH); 
00162                 
00163                 for (unsigned index = 0; index < params->advertisingDataLen; index++)
00164                 {
00165                     PACKED_Data[say].MYDATA[index]=params->advertisingData[index];
00166                 }      
00167                 
00168                 for (unsigned index = params->advertisingDataLen; index < 50; index++)
00169                 {
00170                     PACKED_Data[say].MYDATA[index]=0x00;
00171                 } 
00172                 
00173                 bulunan_index[say]=0xff;
00174                 say++;
00175         
00176                 if(say==ToplamCihaz)
00177                 {
00178                     for (unsigned i = 0; i < say; i++)
00179                     { 
00180                         //uint8_t buffertoSpi[bufferSize];
00181                         int buffertoSpi[bufferSize];
00182                         
00183                         for (unsigned a = 0; a < bufferSize; a++)
00184                         {
00185                             buffertoSpi[a]=0x00;
00186                         } 
00187                         
00188                         buffertoSpi[0]=PACKED_Data[i].PAKET_LENGTH;
00189                         
00190                         for (unsigned a = 1; a < 7; a++)
00191                         { 
00192                             buffertoSpi[a]= PACKED_Data[i].MacID[6-a];
00193                         } 
00194                         
00195                         buffertoSpi[7]=PACKED_Data[i].RSSI; 
00196                         buffertoSpi[8]=PACKED_Data[i].LENGTH;
00197                         
00198                         for (unsigned a = 0; a < PACKED_Data[i].LENGTH; a++)
00199                         {
00200                             buffertoSpi[9+a]=PACKED_Data[i].MYDATA[a];
00201                         }
00202                     
00203                         pc.printf("nRF51 to stm32f103 SENT (BEACON NUMBER = %d )   :  ",i); 
00204 
00205 byt = PACKED_Data[i].LENGTH;
00206 //databoyut
00207 
00208                         for (unsigned a = 0; a < bufferSize; a++)
00209                         {
00210                             pc.printf("\r\n%02x",buffertoSpi[a]);
00211                             
00212                             //string str[] = buffertoSpi[a];
00213                             pc.printf("\r\n%02x",buffertoSpi[a]);
00214                             demo["my_array"][0] = "demo_string";
00215                             //enqueue();
00216                         } 
00217 
00218                         pc.printf("\r\n");
00219                         
00220 ///////////////////////////// dataya 60'a kadar 0 ekle /////////////////////////
00221                         bulunan_index[i]=0x00;
00222 ////////////////////////////////////////////////////////////////////////////////                        
00223 
00224                         //Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize); // eski
00225                         //Flash_Buff_WriteBytes(bufferSize, buffertoSpi); // BU!
00226                         //databoyut = bufferSize;
00227                         //dataGndr(bufferSize, buffertoSpi);
00228                         wait_ms(20);                  
00229                         
00230                         bulunan_index[i]=0x00;                 
00231                     }
00232                 
00233                     say=0;
00234                     
00235                 } // if say==ToplamCihaz
00236 
00237                 alivenessLED = 0;
00238                 break;
00239             
00240             } // if Med Kimlik
00241         } // if MANUFACTURER_SPECIFIC_DATA
00242 
00243         index += (pAdvData->length + 1);
00244         
00245     } // while
00246 } // void advertisementCallback
00247  
00248 // This function is called when the ble initialization process has failed
00249 void onBleInitError(BLE &ble, ble_error_t error)
00250 {
00251   // Initialization error handling should go here
00252 }
00253  
00254 // Callback triggered when the ble initialization process has finished
00255 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00256 {
00257     BLE&        ble   = params->ble;
00258     ble_error_t error = params->error;
00259     
00260     if (error != BLE_ERROR_NONE) {
00261     // In case of error, forward the error handling to onBleInitError
00262         onBleInitError(ble, error);
00263         return;
00264     }
00265  
00266     // Ensure that it is the default instance of BLE
00267     if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00268         return;
00269     }
00270  
00271     // Setup and start scanning
00272     ble.gap().setScanParams(100 /* scan interval */, 100 /* scan window */);
00273     ble.gap().startScan(advertisementCallback);
00274 }
00275 
00276 int main(void)
00277 {
00278     pc.baud(115200); //local terminal baud
00279     pc.printf("\r\n\r\nSLAVE main \r\n");
00280     
00281     //mac_id_gir(); 
00282     
00283     //ticker.attach(periodicCallback, 1);  // trigger sensor polling every 2 seconds
00284   
00285     for(uint8_t i;i<ToplamCihaz;i++)
00286     {
00287         //0x00 ise bulunan mac id gönderilmiştir stm32f'e eğer ff ise gönderilmemistir.
00288         bulunan_index[i] = 0x00;
00289     }
00290 
00291     BLE &ble = BLE::Instance();
00292     ble.init(bleInitComplete);
00293 
00294 
00295 
00296 
00297     
00298 
00299     
00300     //fill the object
00301     //demo["my_array"][0] = "demo_string";
00302     demo["my_array"][1] = 10;
00303     demo["my_boolean"] = false;
00304     
00305     //serialize it into a JSON string
00306     s = demo.serialize();
00307     printf("json: %s\r\n", s.c_str());
00308 
00309 
00310 
00311     while(true){
00312         
00313         ble.waitForEvent();   
00314         
00315         //enqueue(n);
00316         //printf("\r\ndequeue -> %d",dequeue())     
00317         
00318 
00319 
00320 
00321     } // while    
00322 } // ###################### MAIN ###############
00323 
00324 //############################# SLAVE ###################################
00325 
00326