workss

Dependencies:   mbed BLE_API nRF51822 VL53L0X

Revision:
36:fb7f31e53ed9
Parent:
35:daf134714cee
Child:
37:5792986fea5d
--- a/main.cpp	Thu Mar 14 19:42:18 2019 +0000
+++ b/main.cpp	Thu Mar 14 20:18:38 2019 +0000
@@ -51,48 +51,12 @@
  
 HeartRateService         *hrService;
 uint8_t hrmCounter = 0; 
- 
-void connectionCallback(const Gap::ConnectionCallbackParams_t *) {
-    printf("Bluetooth connected at %i\n", current_time);
-}
- 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
-{
-    printf("Bluetooth disconnected at %i\n", current_time);
-    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
-}
- 
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
-{
-    BLE &ble          = params->ble;
-    ble_error_t error = params->error;
-    
-    if (error != BLE_ERROR_NONE) {
-        return;
-    }
-    
-    ble.gap().onConnection(connectionCallback);
-    ble.gap().onDisconnection(disconnectionCallback);
-    hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
- 
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().setAdvertisingInterval(100);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
- 
-    ble.gap().startAdvertising();
-}
- 
 int format_dist(int distance) {
    int result;
- 
-   if (distance > 1270)
-      result = 127;
+   if (distance > 2550)
+      result = 255;
    else
       result = distance/10;
- 
    return result; 
 }
 
@@ -115,7 +79,6 @@
         b *= 2;
         temp /= 10;
     }
-//    printf("The decimal equivalent of %i is %i", num, dec);
     return dec;
 }
 
@@ -165,10 +128,59 @@
     packet1 = bin_to_dec(binary1);
     packet2 = bin_to_dec(binary2);
 }
+
+void send_packet(int direction, int time) {     
+    int encoding [16] = {0};
+    encode_bin(direction, time, encoding);
+    double packet1, packet2;
+    create_packets(encoding, packet1, packet2);
+    int p1 = (int)packet1;
+    int p2 = (int)packet2;
+    
+    hrmCounter = p1;
+    hrService->updateHeartRate(hrmCounter);
+    printf("sent packet %i\n", p1);
+    hrmCounter = p2;
+    hrService->updateHeartRate(hrmCounter);
+    printf("sent packet %i\n", p2);
+}
+
+void connectionCallback(const Gap::ConnectionCallbackParams_t *) {
+    printf("Bluetooth connected at %i\n", current_time);
+    send_packet(-1, current_time);
+}
+ 
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
+{
+    printf("Bluetooth disconnected at %i\n", current_time);
+    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
+}
+ 
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE &ble          = params->ble;
+    ble_error_t error = params->error;
+    
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+    
+    ble.gap().onConnection(connectionCallback);
+    ble.gap().onDisconnection(disconnectionCallback);
+    hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+ 
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().setAdvertisingInterval(100);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
+ 
+    ble.gap().startAdvertising();
+}
  
 void wakeup_event_cb() {
-         
-    int encoding [16] = {0};
+
     led != led;
     timestamp++;
     ::current_time = timestamp / time_cycle;