Keypad

Dependencies:   BLE_API mbed nRF51822

Fork of EmtpyProgram by Cataract Gemuese

Revision:
1:115afdfbaa84
Parent:
0:4817f7301801
Child:
2:343f39defab2
--- a/main.cpp	Fri Jun 24 20:32:10 2016 +0000
+++ b/main.cpp	Tue Jul 12 14:35:09 2016 +0000
@@ -7,6 +7,7 @@
 DigitalOut alivenessLED(LED1, 0);
 DigitalOut alivenessLED2(LED2, 1);
 DigitalOut alivenessLED3(LED3, 1);
+DigitalOut alivenessLED4(LED4, 1);
 
 Ticker ticker;
 
@@ -15,61 +16,116 @@
 
 Serial pc(USBTX, USBRX);
 
+SPI device(P0_14,P0_13,P0_15);
+DigitalOut cs(P0_12);
+
+char * message;
+char * cpy = (char*) malloc(21);
+
+void sendToken()
+{
+    pc.printf("Token");
+    alivenessLED4 = !alivenessLED4;
+    
+    //init SPI
+    cs = 1;
+    device.format(8,3);
+    device.frequency(2000000);
+    
+    int len = strlen(message);
+    
+    // Write the length of the message
+    cs = 0;
+    device.write( *((char*) (&len)) );
+    device.write( *((char*) (&len)+1) );
+    device.write( *((char*) (&len)+2) );
+    device.write( *((char*) (&len)+3) );
+    cs = 1;
+    
+    // Write the message
+    for(int i=0;i<len;i++){
+        cs = 0;
+        device.write(*(message+i));
+        cs = 1;
+    }
+    
+    // Free Memory
+    free(message);
+
+    // Reset board to unblock the BLE-connection
+    NVIC_SystemReset();
+}
+
+uint32_t countdata = 0; // Number of 20-byte blocks to be written.
+
+
+// Callback for the BLE connection
+void onDataWrittenCallback(const GattWriteCallbackParams* params) 
+{
+    pc.printf("read");
+    alivenessLED3 = !alivenessLED3;
+    // if countdata == 0 the data will be interpretated as the Number of 20-byte blocks to be written.
+    if (countdata == 0){
+            
+        pc.printf("%u\r\n",*(uint32_t *) params->data);
+        
+        countdata = *(uint32_t *) params->data;
+        
+        //Malloc the size of the data to be written. +1 for the 0x00 byte.
+        message = (char*) malloc(sizeof(char)*20*countdata + 1);
+    } else {
+        
+        //If countdata is set the data will be interpretated as a string and concatenated to the previously recieved message
+        pc.printf("Data: %s\r\n %d",params->data,countdata);
+        memcpy(cpy, params->data, 20);
+        cpy[20] = '\0'; 
+        strcat(message,cpy);
+        countdata--;
+        
+        // if every block was send over BLE send the token via SPI to the other board.
+        if (countdata == 0){
+            sendToken();
+        }
+    }    
+}
+
 void periodicCallback(void)
 {
     alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
 }
 
-UUID uuid = UUID("9085495d-f273-443d-9e37-e27c9194f63b");
-UUID uuid_message = UUID("8085495d-f273-443d-9e37-e27c9194f63b");
-UUID uuid_writebyte = UUID("7085495d-f273-443d-9e37-e27c9194f63b");
-
-char data[20];
-char numbermessages;
-
-char * message;
-char * cpy = (char*) malloc(21);
-
-bool recieving = false;
-
-void onDataWrittenCallback(const GattWriteCallbackParams* params) {
-    alivenessLED3 = !alivenessLED3;
-    if (!recieving){
-        pc.printf("Allocated: %d bytes\r\n", sizeof(char)*20*2*(*params->data));
-        message = (char*) malloc(sizeof(char)*20*2*(*params->data));
-        recieving = true;
-        return;
-    }
-    
-    if (*params->data == 0x00){
-        pc.printf("%s\r\n",message);
-        free(message);
-        recieving = false;
-    } else {
-        memcpy(cpy, params->data, 20);
-        cpy[20] = '\0'; 
-        strcat(message,cpy);
-    }
+//Write the "reset" byte to the other board and reset
+void resetCallback(void)
+{
+    cs = 0;
+    device.write( 0x11 );
+    cs = 1;
+    NVIC_SystemReset();
 }
 
-
-
-void connectionCallback(const Gap::ConnectionCallbackParams_t *params){
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+{
+    // Start a timer for autoreset to "unblock" the board automaticly
+    // after a bad connection
+    ticker.attach(resetCallback,30);
     alivenessLED2 = !alivenessLED2;
     pc.printf("Connected!\r\n");  
 }
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
-}
+UUID uuid = UUID("9085495d-f273-443d-9e37-e27c9194f63b");
+UUID uuid_message = UUID("8085495d-f273-443d-9e37-e27c9194f63b");
+
+//Buffer for the service
+char data[21];
 
-void initService(BLE &ble){
-    ReadWriteArrayGattCharacteristic<char, 20> messagecharacter = ReadWriteArrayGattCharacteristic<char, 20>(uuid_message,data);
-    ReadWriteArrayGattCharacteristic<char, 1> writebytecharacter = ReadWriteArrayGattCharacteristic<char, 1>(uuid_writebyte,&numbermessages);
+// Init a service with "uuid" and a "20 char" characteristic with "uuid_message"
+void initService(BLE &ble)
+{
+    //Note: 20 bytes are the max. length for BLE
+    WriteOnlyArrayGattCharacteristic<char, 20> messagecharacter = WriteOnlyArrayGattCharacteristic<char, 20>(uuid_message,data);
     
-    GattCharacteristic * chars[] = {&messagecharacter,&writebytecharacter};
-    GattService service = GattService(uuid,chars,2);
+    GattCharacteristic * chars[] = {&messagecharacter};
+    GattService service = GattService(uuid,chars,1);
     ble.gattServer().addService(service);
 }
 
@@ -81,8 +137,7 @@
     if (error != BLE_ERROR_NONE) {
         return;
     }
- 
-    ble.gap().onDisconnection(disconnectionCallback);
+    
     ble.gap().onConnection(connectionCallback);
     ble.gattServer().onDataWritten(onDataWrittenCallback);
     
@@ -99,16 +154,16 @@
 
 int main(void)
 {   
+    //Init BLE
     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
     ble.init(bleInitComplete);
     
     while (ble.hasInitialized()  == false) { /* spin loop */ }
-    
-    ticker.attach(periodicCallback, 0.5); /* Blink LED every second */
+     
+    ticker.attach(periodicCallback, 1); /* Blink LED every second */
     
-        pc.printf("init\r\n");
-    
-    while (true) {
+    pc.printf("init\r\n");
+    while (1) {
         ble.waitForEvent();
     }
 }
\ No newline at end of file