12

Dependencies:   BLE_API mbed nRF51822

Revision:
16:bd87bb636883
Parent:
15:8326739d5f86
Child:
17:6abf0e1667a1
--- a/main.cpp	Fri Aug 11 07:22:03 2017 +0000
+++ b/main.cpp	Mon Oct 16 11:53:06 2017 +0000
@@ -1,265 +1,713 @@
+
 /* 
         abcd olan herşeyi gönderir.
 */
 #include "mbed.h"
 #include "toolchain.h"
 #include "ble/BLE.h"
-#include "spi_master.h"
-
+#include <SPISlave.h>
+#include <string>
+#include <stdio.h>
+ 
 #define RSSI_DEGER 55 // -55dBm'in altındakileri görür.
-//# CS      P0_14
-//# SCK     P0_25
-//# MOSI    P0_20"
-//# MISO    P0_22
-DigitalOut spi_cs(P0_7);
+#define ToplamCihaz 1// (10) hakan değiştir 01122016
+#define bufferSize 60
+
+//#include <stdio.h>
+//#include <conio.h>
+#include <stdlib.h>
 
-DigitalOut alivenessLED(p2, 1);
+//# CS      P0_14 //? 
+//# SCK     P0_25 //+
+//# MOSI    P0_20 //+
+//# MISO    P0_22 //+
+SPISlave device(p25, p28, p29, p24); // mosi, miso, sclk, ssel
+//SPISlave device(p20, p22, p25, p14); // mosi, miso, sclk, ssel
 
+DigitalIn esp_baglimi(P0_11);   // ###############################
+DigitalOut alivenessLED(p2, 1); // ###############################
+
+DigitalOut led1(LED1, 0);
+DigitalOut led2(LED2, 1);
+DigitalOut led3(LED3, 1);
+ 
 Ticker     ticker;
 Serial pc(TX_PIN_NUMBER, RX_PIN_NUMBER);
 
+uint8_t       bulunan_index[ToplamCihaz];
+uint8_t buffertoSpi[100];
+uint8_t say=0;
+int boyut, byt, databoyut;
 
-SPIClass SPI1(NRF_SPI1);
-#define ToplamCihaz 1// (10) hakan değiştir 01122016
-#define bufferSize 60
-uint8_t       bulunan_index[ToplamCihaz];
+//int y[8]= {0x08, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0F};
+///////int vx[15]= {15, 10, 2, 3, 4, 5, 6, 7,8,9,10,11,12,13, 15}; // ilk, boyut, data, 15
+//uint8_t z[8]= {0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
+//uint8_t w[8]= {8, 1, 2, 3, 4, 5, 6, 7};
+//v = {0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00};
+//v[] = [0x07; 0x01; 0x02; 0x03; 0x04; 0x05; 0x06; 0x07];
+//v[] = [7; 1; 2; 3; 4; 5; 6; 7];
+int a, sayac = 1, sayac2 = 2, sayac3 = 0, sayac4 = 0;
+int gelen, gelene;
+
 struct Medron_Data
 {    
         uint8_t                         MacID[6];   // MAC ID leri bunun içinde tutacağız
         uint8_t                         RSSI;   // MAC ID leri bunun içinde tutacağız
         uint8_t                         LENGTH;
         uint8_t                         MYDATA[50];
-        uint8_t                         PAKET_LENGTH;//SPI'YDAN STM32'YE GÖNDERİLCEK DATANIN TOPLAM
-                                                     //UZUNLUGU. 
+        uint8_t                         PAKET_LENGTH;//SPI -> STM32 GÖNDERİLCEK DATANIN TOPLAM UZUNLUGU. 
 } PACKED_Data[ToplamCihaz]; 
 
 
-
-uint8_t say=0;
-
-void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
-{
-    uint16_t index;
+void dataGndr(int DataLen, int *D){
+   
+    T:
+    
+    byt = DataLen;
+    
+    int data[byt];
+    //gelen = 5;
+    
+    //while(gelen != 3){
+        
+        //gelen = 9;
+        
+        if(device.receive()){ 
+        
+            yineal:
     
-    spi_cs = 0;
-    wait_us(20);     
-    for(index=0; index<len; index++)
-    {
-        SPI1.transfer(*pbuf);
-        pbuf++;        
+            gelen = device.read();                                
+            //pc.printf("\r\nA = %d %x", a, a);        
+        
+            //while(gelen != 3){  
+            
+            if(gelene != gelen){
+                pc.printf("\r\n");        
+            }
+            gelene = gelen;       
+                
+            //if(gelen != 9){ // ????
+                
+                pc.printf("\r\nGelen = %d %x", gelen, gelen);
+            
+                if(gelen == 0){
+                    device.reply(0);
+                    pc.printf("\r\ngelen = 0 !!!!!!!!!");
+                    //gelen = 9;
+                    goto T;
+                    //goto deneme;
+                    //reset(); //??????????????????????????????????????????????
+                }
+                else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder
+                    device.reply(D[0]);
+                    //wait_ms(500);
+                    //pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                    pc.printf("\t\tData[0] (boyut) = %d  %x", D[0], D[0]);
+                    sayac = 1;
+                }
+                else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir
+                    device.reply(D[sayac]);
+                    pc.printf("\t\tData[%d] = %d  %x", sayac, D[sayac], D[sayac]);
+                    sayac++;
+                    int s = sayac;
+                    boyut = D[0];
+                    if(s == (boyut)){ // boyut -1'e kadar olmalı
+                        s = 1;
+                        sayac = 1;
+                    }
+                }
+                else if(gelen == 3){
+                    //gelen = 9;
+                    device.reply(0);
+                    pc.printf("connection out\r\n");
+                    wait_ms(100);
+                    goto T;
+                }
+                else{
+                    pc.printf("goto al\r\n");
+                    goto yineal;
+                }
+                        
+            //} // if gelen != 9      
+        } // if device.recieve
+        else
+        {
+            pc.printf("\r\nMaster bagli degil\r\n\r\n");
+            led1 != led1;
+            //b = 0;
+            wait_ms(500);
+        } 
+    
+    //} // while     
+} // data gönder
+
+ 
+//Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize); 
+//Flash_Buff_WriteBytes(bufferSize, buffertoSpi); 
+//void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
+/*void Flash_Buff_WriteBytes(uint8_t len, uint8_t *gndr)
+{
+    tekrar:
+    int v[len];
+    
+    //pc.printf("\r\n\r\nlen = %d  %x\tGndr = %d  %x\t**Gndr = %d  %x\t&Gndr = %d  %x", len, len, gndr, gndr, *gndr, *gndr, &gndr, &gndr);
+    for(int n = 0; n <60; n++){
+        //pc.printf("\r\n%d\tlen = %d  %x\tGndr = %d  %x\t**Gndr = %d  %x\t&Gndr = %d  %x", n, len, len, gndr, gndr, *gndr, *gndr, &gndr, &gndr);
+        int d = n+2;
+        if(d < (len-1)){ // 2-58 arası
+            v[d] = *gndr;
+        }
+        
+        gndr++;
+        //wait_ms(500);
     }
     
-    wait_us(20);
-    spi_cs = 1;
+    //wait_ms(500);
+    
+    v[0] = len;
+    v[1] = 10;
+    pc.printf("\r\n");
+    for(int g = 2; g < (len-1); g++){
+        //pc.printf("\r\nV[g] = %d  %x", v[g], v[g]);
+    }
+    v[(len-1)] = 15; // dizinin son elemanı
+    
+    pc.printf("\r\n\r\n");
+    for(int j = 0; j < len; j++){
+        pc.printf("\r\n%d\tDATAM[%d] = %d  %x", j, j, v[j], v[j]);
+    }
+    pc.printf("\r\n\r\n");
+    
+    //for(int f = 0; f < len; f++){
+        //wait_ms(500);     
+        //pc.printf("\r\ndata2[len] = %d  %x \tGndr = %d  %x\t**Gndr = %d  %x\t&&Gndr = %d  %x", v[len], v[len], gndr, gndr, *gndr, *gndr, &gndr, &gndr);  
+    //}
+    
+    return(len, v);
+    //dataGndr(len, v);
+    //tekrar:
+    
+    //byt = len;
+    //int data[byt];
+/ *    
+    gelen = 5;
+    
+    while(gelen != 3){
+        
+        gelen = 9;
+        
+        if(device.receive()){ 
+    
+            gelen = device.read();                                
+            //pc.printf("\r\nA = %d %x", a, a);        
+        
+            //while(gelen != 3){  
+            
+            if(gelene != gelen){
+                pc.printf("\r\n");        
+            }
+            gelene = gelen;       
+                
+            if(gelen != 9){ // ????
+                
+                pc.printf("\r\nGelen = %d %x", gelen, gelen);
+            
+                if(gelen == 0){
+                    wait_ms(500);
+                    gelen = 9;
+                    //return;
+                    goto tekrar;
+                    //reset(); //??????????????????????????????????????????????
+                }
+                else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder
+                    device.reply(v[0]);
+                    //wait_ms(500);
+                    //pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                    pc.printf("\t\tData[0] (boyut) = %d  %x", v[0], v[0]);
+                    sayac = 1;
+                }
+                else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir
+                    device.reply(v[sayac]);
+                    pc.printf("\t\tData[%d] = %d  %x", sayac, v[sayac], v[sayac]);
+                    sayac++;
+                    int s = sayac;
+                    boyut = v[0];
+                    if(s == boyut){ // boyut -1'e kadar olmalı
+                        s = 1;
+                        sayac = 1;
+                    }
+                }
+                        
+            } // if gelen != 9      
+        } // if device.recieve
+        else
+        {
+            pc.printf("\r\nMaster bagli degil");
+            led1 != led1;
+            //b = 0;
+            wait_ms(500);
+        } 
+    
+    } // while  
+* /            
 }
-
-
+*/
+ 
 void periodicCallback(void)
 {
-  // alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
+  // Do blinky on LED1 while we're waiting for BLE events. This is optional.
+  // alivenessLED = !alivenessLED; 
 }
-
-/*
-   This function is called every time we scan an advertisement.
-*/
+ 
+// This function is called every time we scan an advertisement.
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-  struct AdvertisingData_t
-  {
-    uint8_t                        length; /* doesn't include itself */
-    GapAdvertisingData::DataType_t dataType;
-    uint8_t                        data[0];
-  } PACKED;
-
-  struct ApplicationData_t
-  {
-    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
-                                                       in the manufacture specific AD data field */
-  } PACKED;
-                       
-  static const uint16_t MED_KIMLIK = 0xABCD;
-  AdvertisingData_t *pAdvData;
-  size_t index = 0;
-  while (index < params->advertisingDataLen) 
-  {
-    pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
-    if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) 
+    struct AdvertisingData_t
     {
-      ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
-      if (pAppData->applicationSpecificId == MED_KIMLIK)
-      {
-           alivenessLED = 1;
+        uint8_t                        length; // doesn't include itself
+        GapAdvertisingData::DataType_t dataType;
+        uint8_t                        data[0];
+    } PACKED;
     
-           //pc.printf("%d\r", say+1);
-           
-           PACKED_Data[say].MacID[5]=params->peerAddr[5];
-           PACKED_Data[say].MacID[4]=params->peerAddr[4];
-           PACKED_Data[say].MacID[3]=params->peerAddr[3];
-           PACKED_Data[say].MacID[2]=params->peerAddr[2];
-           PACKED_Data[say].MacID[1]=params->peerAddr[1];
-           PACKED_Data[say].MacID[0]=params->peerAddr[0];
-           PACKED_Data[say].RSSI=(params->rssi) * -1;
-           PACKED_Data[say].LENGTH=params->advertisingDataLen;
-           /*
-            EXAMPLE
-           
-            fa 8c b3 c7 1c 61 :MAC ; 6 BYTE
-            2b:RSSI;1BYTE 
-            0a:UZUNLUK;1BYTE-->0A H=10 D
-            09  ff cd ab 01 00 4d 52 e8 00 00 00 00 00 00 00 00 00 00 00 ;DATA
-            1   2  3  4  5  6  7  8  9  10  X  X  X  X  X  X  X  X  X  X ;DATA SAYISI    
+    struct ApplicationData_t
+    {
+        // An ID used to identify temperature value in the manufacture specific AD data field 
+        uint16_t applicationSpecificId; 
+    } PACKED;
+                   
+    static const uint16_t MED_KIMLIK = 0xABCD;
+    AdvertisingData_t *pAdvData;
+    size_t index = 0;
+    
+    while (index < params->advertisingDataLen) 
+    {
+        pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
+        
+        if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) 
+        {
+            ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
             
-            c8 60 85 e2 73 e2 :MAC:6
-            37:RSSI:1
-            08:LENGTH:1
-            07 ff cd ab 00 00 e7 00:DATA
-            1  2  3  4  5  6  7  8 ;DATA SAYISI   
-               
-           */
-           
-            PACKED_Data[say].PAKET_LENGTH= 6 + 1 + 1 + params->advertisingDataLen;//MAC+RSSI+1+LENGTH
-            
-            pc.printf("TOPLAM PAKET UZUNLUK = (%d ) : ---> ",PACKED_Data[say].PAKET_LENGTH); 
-            
-            for (unsigned index = 0; index < params->advertisingDataLen; index++)
-            {
-              PACKED_Data[say].MYDATA[index]=params->advertisingData[index];
-            }      
-            
-            for (unsigned index = params->advertisingDataLen; index < 50; index++)
+            if (pAppData->applicationSpecificId == MED_KIMLIK)
             {
-              PACKED_Data[say].MYDATA[index]=0x00;
-            } 
-            
-            bulunan_index[say]=0xff;
-            say++;
-            
-            if(say==ToplamCihaz)
-            {
+                alivenessLED = 1;
+        
+               //pc.printf("%d\r", say+1);
                
-               for (unsigned i = 0; i < say; i++)
-               { 
-                 uint8_t buffertoSpi[bufferSize];
+                PACKED_Data[say].MacID[5]=params->peerAddr[5];
+                PACKED_Data[say].MacID[4]=params->peerAddr[4];
+                PACKED_Data[say].MacID[3]=params->peerAddr[3];
+                PACKED_Data[say].MacID[2]=params->peerAddr[2];
+                PACKED_Data[say].MacID[1]=params->peerAddr[1];
+                PACKED_Data[say].MacID[0]=params->peerAddr[0];
+                PACKED_Data[say].RSSI=(params->rssi) * -1;
+                PACKED_Data[say].LENGTH=params->advertisingDataLen;
+                
+                /*
+                EXAMPLE
+                
+                fa 8c b3 c7 1c 61 :MAC ; 6 BYTE
+                2b:RSSI;1BYTE 
+                0a:boyut;1BYTE-->0A Hex = 10 Dec
+                09  ff cd ab 01 00 4d 52 e8 00 00 00 00 00 00 00 00 00 00 00 ;DATA
+                1   2  3  4  5  6  7  8  9  10  X  X  X  X  X  X  X  X  X  X ;DATA SAYISI    
+                
+                c8 60 85 e2 73 e2 :MAC:6
+                37:RSSI:1
+                08:LENGTH:1
+                07 ff cd ab 00 00 e7 00:DATA
+                1  2  3  4  5  6  7  8 ;DATA SAYISI   
+                
+                */
+                
+                PACKED_Data[say].PAKET_LENGTH= 6 + 1 + 1 + params->advertisingDataLen;//MAC+RSSI+1+LENGTH
+                
+                pc.printf("\r\nTOPLAM PAKET boyut = (%d ) : ---> ",PACKED_Data[say].PAKET_LENGTH); 
+                
+                for (unsigned index = 0; index < params->advertisingDataLen; index++)
+                {
+                    PACKED_Data[say].MYDATA[index]=params->advertisingData[index];
+                }      
+                
+                for (unsigned index = params->advertisingDataLen; index < 50; index++)
+                {
+                    PACKED_Data[say].MYDATA[index]=0x00;
+                } 
+                
+                bulunan_index[say]=0xff;
+                say++;
+        
+                if(say==ToplamCihaz)
+                {
+                    for (unsigned i = 0; i < say; i++)
+                    { 
+                        //uint8_t buffertoSpi[bufferSize];
+                        int buffertoSpi[bufferSize];
+                        
+                        for (unsigned a = 0; a < bufferSize; a++)
+                        {
+                            buffertoSpi[a]=0x00;
+                        } 
+                        
+                        buffertoSpi[0]=PACKED_Data[i].PAKET_LENGTH;
+                        
+                        for (unsigned a = 1; a < 7; a++)
+                        { 
+                            buffertoSpi[a]= PACKED_Data[i].MacID[6-a];
+                        } 
+                        
+                        buffertoSpi[7]=PACKED_Data[i].RSSI; 
+                        buffertoSpi[8]=PACKED_Data[i].LENGTH;
+                        
+                        for (unsigned a = 0; a < PACKED_Data[i].LENGTH; a++)
+                        {
+                            buffertoSpi[9+a]=PACKED_Data[i].MYDATA[a];
+                        }
+                    
+                        pc.printf("nRF51 to stm32f103 SENT (BEACON NUMBER = %d )   :  ",i); 
 
-                 for (unsigned a = 0; a < bufferSize; a++)
-                 {
-                      buffertoSpi[a]=0x00;
-                 } 
-                  
-                 buffertoSpi[0]=PACKED_Data[i].PAKET_LENGTH;
-                 
-                 for (unsigned a = 1; a < 7; a++)
-                 { 
-                    buffertoSpi[a]= PACKED_Data[i].MacID[6-a];
-                 } 
-                 
-                 buffertoSpi[7]=PACKED_Data[i].RSSI; 
-                 buffertoSpi[8]=PACKED_Data[i].LENGTH;
-                 
-                 for (unsigned a = 0; a < PACKED_Data[i].LENGTH; a++)
-                 {
-                    buffertoSpi[9+a]=PACKED_Data[i].MYDATA[a];
-                 }
-    
-                 pc.printf("nRF51 to stm32f103 SENT (BEACON NUMBER = %d )   :  ",i); 
-                 
-                 for (unsigned a = 0; a < bufferSize; a++)
-                 {
-                      pc.printf("%02x",buffertoSpi[a]);
-                 } 
-                pc.printf("\r\n");
-                   
+byt = PACKED_Data[i].LENGTH;
+//databoyut
+
+                        for (unsigned a = 0; a < bufferSize; a++)
+                        {
+                            pc.printf("%02x",buffertoSpi[a]);
+                        } 
+
+                        pc.printf("\r\n");
+                        
+///////////////////////////// dataya 60'a kadar 0 ekle /////////////////////////
+                        bulunan_index[i]=0x00;
+////////////////////////////////////////////////////////////////////////////////                        
 
-                Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize);
-                wait_ms(200); // hakan ekledi 01122016
-                wait_ms(20);                    
-                bulunan_index[i]=0x00;                 
-               }
-              say=0;
-            }
-        alivenessLED = 0;
-        break;
-      }
-    }
-    index += (pAdvData->length + 1);
-  }
-  
-  
-  
-}
+                        //Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize); // eski
+                        //Flash_Buff_WriteBytes(bufferSize, buffertoSpi); // BU!
+                        //databoyut = bufferSize;
+                        dataGndr(bufferSize, buffertoSpi);
+                        wait_ms(20);                  
+                        
+                        bulunan_index[i]=0x00;                 
+                    }
+                
+                    say=0;
+                    
+                } // if say==ToplamCihaz
 
-/**
-   This function is called when the ble initialization process has failed
-*/
+                alivenessLED = 0;
+                break;
+            
+            } // if Med Kimlik
+        } // if MANUFACTURER_SPECIFIC_DATA
+
+        index += (pAdvData->length + 1);
+        
+    } // while
+} // void advertisementCallback
+ 
+// This function is called when the ble initialization process has failed
 void onBleInitError(BLE &ble, ble_error_t error)
 {
-  /* Initialization error handling should go here */
+  // Initialization error handling should go here
 }
-
-/**
-   Callback triggered when the ble initialization process has finished
-*/
+ 
+// Callback triggered when the ble initialization process has finished
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-  BLE&        ble   = params->ble;
-  ble_error_t error = params->error;
-
-  if (error != BLE_ERROR_NONE) {
-    /* In case of error, forward the error handling to onBleInitError */
-    onBleInitError(ble, error);
-    return;
-  }
-
-  /* Ensure that it is the default instance of BLE */
-  if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
-    return;
-  }
-
-  /* Setup and start scanning */
-  ble.gap().setScanParams(100 /* scan interval */, 100 /* scan window */);
-  ble.gap().startScan(advertisementCallback);
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+    
+    if (error != BLE_ERROR_NONE) {
+    // In case of error, forward the error handling to onBleInitError
+        onBleInitError(ble, error);
+        return;
+    }
+ 
+    // Ensure that it is the default instance of BLE
+    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+ 
+    // Setup and start scanning
+    ble.gap().setScanParams(100 /* scan interval */, 100 /* scan window */);
+    ble.gap().startScan(advertisementCallback);
 }
 
 int main(void)
 {
-  pc.baud(115200); //local terminal baud
-  SPI1.begin();
-  pc.printf("MEDRON SNIFFER nRF51\r\n"); 
-  //ticker.attach(periodicCallback, 1);  /* trigger sensor polling every 2 seconds */
+    pc.baud(115200); //local terminal baud
+    pc.printf("\r\n\r\nSLAVE main \r\n");
+    
+    
+
+    
+    
+    
+    //mac_id_gir();  
+      
+    //int v[byt];
+    
+    device.reply(0x00);     
+    
+    //ticker.attach(periodicCallback, 1);  // trigger sensor polling every 2 seconds
   
-  for(uint8_t i;i<ToplamCihaz;i++)
-  {
-      bulunan_index[i]=0x00;//0x00 ise bulunan mac id gönderilmiştir stm32f'e
-                            // eğer ff ise gönderilmemistir.
-  }
+    for(uint8_t i;i<ToplamCihaz;i++)
+    {
+        //0x00 ise bulunan mac id gönderilmiştir stm32f'e eğer ff ise gönderilmemistir.
+        bulunan_index[i] = 0x00;
+    }
+
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+
+    while(true){
+            
+        tekrar:
+        
+        //deneme:
+        //wait_ms(3000);
+        
+        ble.waitForEvent();        
+            
+//tekrar:
+        
+    //byt = DataLen;
+        
+        //int data[databoyutu];
+        int data[byt];
+// son
+        gelen = 5;
+        
+        while(gelen != 3){
+            
+            gelen = 9;
+            
+            if(device.receive()){ 
+        
+                gelen = device.read();                                
+                //pc.printf("\r\nA = %d %x", a, a);        
+            
+                //while(gelen != 3){  
+                
+                if(gelene != gelen){
+                    pc.printf("\r\n");        
+                }
+                gelene = gelen;       
+                    
+                if(gelen != 9){ // ????
+                    
+                    pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                
+                    if(gelen == 0){
+                        wait_ms(500);
+                        gelen = 9;
+                        //return;
+                        goto tekrar;
+                        //reset(); //??????????????????????????????????????????????
+                    }
+                    else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder
+                        device.reply(data[0]);
+                        //wait_ms(500);
+                        //pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                        pc.printf("\t\tData[0] (boyut) = %d  %x", data[0], data[0]);
+                        sayac = 1;
+                    }
+                    else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir
+                        device.reply(data[sayac]);
+                        pc.printf("\t\tData[%d] = %d  %x", sayac, data[sayac], data[sayac]);
+                        sayac++;
+                        int s = sayac;
+                        boyut = data[0];
+                        if(s == (boyut)){ // boyut -1'e kadar olmalı
+                            s = 1;
+                            sayac = 1;
+                        }
+                    }
+                            
+                } // if gelen != 9      
+            } // if device.recieve
+            else
+            {
+                pc.printf("\r\nMaster bagli degil\r\n");
+                led1 != led1;
+                //b = 0;
+                wait_ms(500);
+            }
+        
+        } // while 3 
+// son
+//* /    
+
+        //byt = DataLen;
+        
+/*****
+        if(device.receive()){ 
+        //while(device.receive()){ 
+            AL:
+            gelen = device.read();                                
+            //pc.printf("\r\nA = %d %x", a, a);
+            
+            if(gelene != gelen){ // gelen farklıysa boşluk bırak
+                gelene = gelen; 
+                pc.printf("\r\n");        
+            }
+            //gelene = gelen;       
+            
+            //if(gelen != 9){
+                //if(gelen != 3){
+
+                pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                
+                if(gelen == 0){
+                    //wait_ms(1000);
+                    device.reply(0);
+                    pc.printf("\r\ngelen = 0 !");
+                    //gelen = 9;
+                    goto tekrar;
+                    //goto deneme;
+                    //reset(); //??????????????????????????????????????????????
+                }
+                else if(gelen == 1){ // (boyut) 1 ise boyutu (vx[0]) gönder
+                    device.reply(data[0]);
+                    //wait_ms(500);
+                    //pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                    pc.printf("\t\tData[0] (boyut) = %d  %x", data[0], data[0]);
+                    sayac = 1;
+                }
+                else if(gelen == 2){ // gitmesi gereken data // vx[1] - vx[boyut-1] arasını gönderir
+                    device.reply(D[sayac]);
+                    pc.printf("\t\tData[%d] = %d  %x", sayac, data[sayac], data[sayac]);
+                    sayac++;
+                    int s = sayac;
+                    boyut = data[0];
+                    if(s == (boyut)){ // boyut -1'e kadar olmalı
+                        s = 1;
+                        sayac = 1;
+                    }
+                }
+                else if(gelen == 3){
+                    //gelen = 9;
+                    device.reply(0);
+                    pc.printf("connection out\r\n");
+                    wait_ms(100);
+                    goto tekrar;
+                }
+                else{
+                    pc.printf("goto al\r\n");
+                    goto AL;
+                }
+                
+                //}// if gelen != 3;
+                / *else{
+                    gelen = 9;
+                    device.reply(0);
+                    wait_ms(500);
+                    pc.printf("connection out\r\n");
+                    goto tekrar;
+                }* /
+              
+                
+            //} // if gelen != 9       
+            
+            //gelen = 9;  
+            
+        }// if device.recieve              
+
+// spi beacon gönderme
+*********/
+
+
 
 /*
-while(1)
-{
-    uint8_t buffertoSpi[58];
-                 buffertoSpi[0]=0x00;
-                  buffertoSpi[1]=0x01;
-                   buffertoSpi[2]=0x02;
-                     buffertoSpi[3]=0x03;
-                       buffertoSpi[4]=0x04;
-                 for (unsigned a = 5; a < 58; a++)
-                  {
-                      buffertoSpi[a]=+a;
-                      pc.printf("%02x",buffertoSpi[a]);
-                  } 
-                   Flash_Buff_WriteBytes(0, buffertoSpi, 58);
-                   pc.printf("\r\n"); 
-                    wait_ms(1000); 
-    }
+/////////   SPI sabit data gönderme ÇALIŞAN:  ///////////
+        //AL:    
+        if(device.receive()){ 
+        //while(device.receive()){ 
+            AL:
+            gelen = device.read();                                
+            //pc.printf("\r\nA = %d %x", a, a);
+            
+            if(gelene != gelen){ // gelen farklıysa boşluk bırak
+                gelene = gelen; 
+                pc.printf("\r\n");        
+            }
+            //gelene = gelen;       
+            
+            //if(gelen != 9){
+                //if(gelen != 3){
 
-*/
+                pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                
+                if(gelen == 0){
+                    //wait_ms(1000);
+                    device.reply(0);
+                    pc.printf("\r\ngelen = 0 !");
+                    //gelen = 9;
+                    goto tekrar;
+                    //goto deneme;
+                    //reset(); //??????????????????????????????????????????????
+                }
+                else if(gelen == 1){ // (boyut) 1 ise boyutu (vx[0]) gönder
+                    device.reply(vx[0]);
+                    //wait_ms(500);
+                    //pc.printf("\r\nGelen = %d %x", gelen, gelen);
+                    pc.printf("\t\tvx[0] (boyut) = %d  %x", vx[0], vx[0]);
+                    sayac = 1;
+                }
+                else if(gelen == 2){ // gitmesi gereken data // vx[1] - vx[boyut-1] arasını gönderir
+                    device.reply(vx[sayac]);
+                    pc.printf("\t\tvx[%d] = %d  %x", sayac, vx[sayac], vx[sayac]);
+                    sayac++;
+                    int s = sayac;
+                    boyut = vx[0];
+                    if(s == (boyut)){ // boyut -1'e kadar olmalı
+                        s = 1;
+                        sayac = 1;
+                    }
+                }
+                else if(gelen == 3){
+                    //gelen = 9;
+                    device.reply(0);
+                    pc.printf("connection out\r\n");
+                    wait_ms(100);
+                    goto tekrar;
+                }
+                else{
+                    pc.printf("goto al\r\n");
+                    goto AL;
+                }
+                
+                //}// if gelen != 3;
+                / *else{
+                    gelen = 9;
+                    device.reply(0);
+                    wait_ms(500);
+                    pc.printf("connection out\r\n");
+                    goto tekrar;
+                }* /
+              
+                
+            //} // if gelen != 9       
+            
+            //gelen = 9;  
+            
+        }// if device.recieve
+        / *else if(device.receive() && gelen == 3){
+            goto tekrar;
+        }
+        else if(gelen != 3){
+            goto AL;
+        }* /
+       
+        else // bağlı değilse veriyi alana kaydet (fifo yap)
+        {
+            pc.printf("\r\nMaster bagli degil");
+            led1 != led1;
+            wait_ms(200);
+            
+        } 
+*/ //spi bilinen data son
+
+    } // while    
+} // ###################### MAIN ###############
 
 
-  BLE &ble = BLE::Instance();
-  ble.init(bleInitComplete);
-
-  while (true)
- {
-    ble.waitForEvent();
-  }
-}
+//########## SLAVE ##########