No changes

Dependencies:   BLE_API mbed nRF51822

Fork of SDP_Version3_Abdul by Michael Galis

Revision:
4:caab577334f0
Parent:
3:7dc284221369
--- a/main.cpp	Sun Apr 10 19:05:36 2016 +0000
+++ b/main.cpp	Sun Feb 26 02:33:32 2017 +0000
@@ -1,96 +1,83 @@
+/* Senior Project Bluetooth bicycle speedometer
+Author: Michael Galis
+This is the main file, it implements both Acceleration Service and Reed Switch
+Service to send all the necessary data over bluetooth in organized packages. 
+Also there is code to reed from the accelerometer over an I2C connection.
+*/
+
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "MMA8452Q.h"
-#include "Service.h"
-
-MMA8452Q accel(P0_0, P0_1, 0x1D); //deklaracja obiektu akcelerometru (P0_0 -> SDA, P0_1 -> SCL, 0x1D -> ID urzadzenia)
-Ticker ticker;
-float x,y,z; //zmienne do których przypisywane są wartosci odczytu x y z z akcelerometru
+#include "AcclerationService.h"
+#include "ReedSwitchService.h"
+#include "BLE_Init.h"
 
-const static char     DEVICE_NAME[] = "BLE_Accel"; 
-static const uint16_t uuid16_list[] = {Service::SERVICE_UUID};
+#define REED_SWITCH     P0_5
 
-
-static Service *ServicePtr;
+MMA8452Q accel(P0_10, P0_8, 0x1D);  //Declare accel object at I2C pins(P0_0 -> SDA, P0_1 -> SCL, 0x1D -> Accelerometer Address )
+Ticker ticker;
+float x,y,z;                        //variables assigned values to read x, y, and z accelerometer numbers
+InterruptIn button(REED_SWITCH);
 
-void f(){    
-    /*
-    * Odczyt x y z 
-    */
-    x=accel.readX(); 
-    y=accel.readY();
-    z=accel.readZ();
-    
-    /*
-    * Update wartosci w charakterystykach x y z i all
-    */    
-    ServicePtr->updateXState(x);
-    ServicePtr->updateYState(y);
-    ServicePtr->updateZState(z);
-    ServicePtr->updateALLState(x,y,z);
-}
+enum {                                                  //Different states of the reed switch
+    RELEASED = 0,
+    PRESSED,
+    IDLE
+};
+static uint8_t reedSwitchState = IDLE;                  //Reed switch is initially idle
+static volatile bool  triggerSensorPolling = false;
 
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+void reedSwitchPressedCallback(void)            //Change the Reed switch state to Pressed
 {
-    BLE::Instance().gap().startAdvertising();
+    reedSwitchState = PRESSED;  
 }
 
-/**
- * This function is called when the ble initialization process has failled
- */
-void onBleInitError(BLE &ble, ble_error_t error)
+void reedSwitchReleasedCallback(void)           //Change the Reed switch state to Released
 {
-    /* Initialization error handling should go here */
+    reedSwitchState = RELEASED;
 }
 
-/**
- * Callback triggered when the ble initialization process has finished
- */
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+void periodicCallback(void)
 {
-    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;
-    }
-
-    ble.gap().onDisconnection(disconnectionCallback);
-
-    /* Setup primary service */
-    ServicePtr = new Service(ble);
-
-    /* setup advertising */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(100); /* 1000ms. */
-    ble.gap().startAdvertising();
-
+    triggerSensorPolling = true;
+    
 }
 
 int main(void)
 {
+    button.fall(reedSwitchPressedCallback);
+    button.rise(reedSwitchReleasedCallback);
+    
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
     
     accel.init();
-    ticker.attach(f,1);
+    ticker.attach(periodicCallback,0.2);
     
     /* SpinWait for initialization to complete. This is necessary because the
      * BLE object is used in the main loop below. */
     while (ble.hasInitialized()  == false) { /* spin loop */ }
     
     while (true) {
+        if (reedSwitchState != IDLE) {                                      //When the Reed Switch changes states
+            reedSwitchServicePtr->updateReedSwitchState(reedSwitchState);   //update the value over ble
+            reedSwitchState = IDLE;
+        }
+        if (triggerSensorPolling && ble.getGapState().connected)
+        {
+            triggerSensorPolling = false;
+            if(accel.available())
+            {
+                x=accel.readX(); 
+                y=accel.readY();
+                z=accel.readZ();
+                   
+                accelerationServicePtr->updateXData(x);
+                accelerationServicePtr->updateYData(y);
+                accelerationServicePtr->updateZData(z);
+                accelerationServicePtr->updateALLState(x,y,z);
+            }
+        }
         ble.waitForEvent();
     }
 }