Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SDP_Version3 by
Revision 4:caab577334f0, committed 2017-02-26
- Comitter:
- galism
- Date:
- Sun Feb 26 02:33:32 2017 +0000
- Parent:
- 3:7dc284221369
- Commit message:
- No changes
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AcclerationService.h Sun Feb 26 02:33:32 2017 +0000
@@ -0,0 +1,102 @@
+/* Senior Project Bluetooth bicycle speedometer
+Author: Michael Galis
+This header file describes the Acceleration Service that I created to be able
+to send the x, y, and z accelerations as the bicycle is moving. These
+accelerations will be used by the phone to calculate the speed of the bicycle.
+*/
+
+#ifndef __BLE_SERVICE_H__
+#define __BLE_SERVICE_H__
+
+class AccelerationService {
+public:
+ const static uint16_t ACCELERATION_SERVICE_UUID = 0xA010;
+ const static uint16_t X_CHARACTERISTIC_UUID = 0xA011;
+ const static uint16_t Y_CHARACTERISTIC_UUID = 0xA012;
+ const static uint16_t Z_CHARACTERISTIC_UUID = 0xA013;
+ const static uint16_t ALL_CHARACTERISTIC_UUID = 0x0014;
+
+ AccelerationService(BLE &_ble) :
+ ble(_ble),
+ xData(X_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+ yData(Y_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+ zData(Z_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+ allState(ALL_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+ {
+ GattCharacteristic *charTable[] = {&xData,&yData, &zData, &allState};
+ GattService accelerationService(AccelerationService::ACCELERATION_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.gattServer().addService(accelerationService);
+ }
+
+ void updateXData(float newData)
+ {
+ int length = sizeof(float);
+ uint8_t bytes[sizeof(float)];
+ for(int i = 0; i < length; i++)
+ {
+ bytes[i] = ((uint8_t*)&newData)[i];
+ }
+ int n = sizeof(bytes);
+ ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&bytes, n);
+ //int n = sizeof(uint8_t);
+ //ble.gattServer().write(xData.getValueHandle(), (uint8_t *)&newData, n);
+ }
+
+ void updateYData(float newData)
+ {
+ int length = sizeof(float);
+ uint8_t bytes[sizeof(float)];
+ for(int i = 0; i < length; i++)
+ {
+ bytes[i] = ((uint8_t*)&newData)[i];
+ }
+ int n = sizeof(bytes);
+ ble.gattServer().write(yData.getValueHandle(), (uint8_t *)&bytes, n);
+ }
+
+ void updateZData(float newData)
+ {
+ int length = sizeof(float);
+ uint8_t bytes[sizeof(float)];
+ for(int i = 0; i < length; i++)
+ {
+ bytes[i] = ((uint8_t*)&newData)[i];
+ }
+ int n = sizeof(bytes);
+ ble.gattServer().write(zData.getValueHandle(), (uint8_t *)&bytes, n);
+ }
+
+ void updateALLState(float newState,float newStatey,float newStatez)
+ {
+
+ int length = 14;
+ uint8_t bytes[length];
+
+ bytes[0] = ((uint8_t*)&newState)[0];
+ bytes[1] = ((uint8_t*)&newState)[1];
+ bytes[2] = ((uint8_t*)&newState)[2];
+ bytes[3] = ((uint8_t*)&newState)[3];
+ bytes[4] = 0xff;
+ bytes[5] = ((uint8_t*)&newStatey)[0];
+ bytes[6] = ((uint8_t*)&newStatey)[1];
+ bytes[7] = ((uint8_t*)&newStatey)[2];
+ bytes[8] = ((uint8_t*)&newStatey)[3];
+ bytes[9] = 0xff;
+ bytes[10] = ((uint8_t*)&newStatez)[0];
+ bytes[11] = ((uint8_t*)&newStatez)[1];
+ bytes[12] = ((uint8_t*)&newStatez)[2];
+ bytes[13] = ((uint8_t*)&newStatez)[3];
+
+ uint16_t n = sizeof(bytes) / sizeof(bytes[0]);
+ ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
+ }
+
+private:
+ BLE &ble;
+ ReadOnlyGattCharacteristic<float> xData;
+ ReadOnlyGattCharacteristic<float> yData;
+ ReadOnlyGattCharacteristic<float> zData;
+ ReadOnlyArrayGattCharacteristic<uint8_t, 14> allState;
+};
+
+#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_Init.h Sun Feb 26 02:33:32 2017 +0000
@@ -0,0 +1,60 @@
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "MMA8452Q.h"
+#include "AcclerationService.h"
+#include "ReedSwitchService.h"
+
+
+const static char DEVICE_NAME[] = "BLE_Bike"; //Name of BLE Device
+static const uint16_t uuid16_list[] = {ReedSwitchService::REED_SWITCH_SERVICE_UUID, //UUID's of Services
+ AccelerationService::ACCELERATION_SERVICE_UUID};
+
+static ReedSwitchService *reedSwitchServicePtr;
+static AccelerationService *accelerationServicePtr;
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+ BLE::Instance().gap().startAdvertising();
+}
+
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+}
+
+/**
+ * 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;
+ }
+
+ ble.gap().onDisconnection(disconnectionCallback);
+
+ /* Setup primary service */
+ reedSwitchServicePtr = new ReedSwitchService(ble, false /* initial value for button pressed */);
+ accelerationServicePtr = new AccelerationService(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();
+
+}
\ No newline at end of file
--- a/MMA8452Q.lib Sun Apr 10 19:05:36 2016 +0000 +++ b/MMA8452Q.lib Sun Feb 26 02:33:32 2017 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Praktyki/code/MMA8452Q/#1165b1539d4f +https://developer.mbed.org/users/galism/code/SeniorDesignProject_Version3/#6ba7c2c48ce3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ReedSwitchService.h Sun Feb 26 02:33:32 2017 +0000
@@ -0,0 +1,34 @@
+/* Senior Project Bluetooth bicycle speedometer
+Author: Michael Galis
+This header file describes the Reed Switch Service that I created to be able
+to send the state of the reed switch as a magnet passes by.
+*/
+
+#ifndef __BLE_BUTTON_SERVICE_H__
+#define __BLE_BUTTON_SERVICE_H__
+
+class ReedSwitchService {
+public:
+ const static uint16_t REED_SWITCH_SERVICE_UUID = 0xA006;
+ const static uint16_t REED_SWITCH_STATE_CHARACTERISTIC_UUID = 0xA007;
+
+ ReedSwitchService(BLE &_ble, uint8_t reedSwitchPressedInitial) :
+ ble(_ble),
+ reedSwitchState(REED_SWITCH_STATE_CHARACTERISTIC_UUID, &reedSwitchPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+ {
+ GattCharacteristic *charTable[] = {&reedSwitchState};
+ GattService reedSwitchService(ReedSwitchService::REED_SWITCH_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.gattServer().addService(reedSwitchService);
+ }
+
+ void updateReedSwitchState(uint8_t newState)
+ {
+ ble.gattServer().write(reedSwitchState.getValueHandle(), (uint8_t *)&newState, sizeof(uint8_t));
+ }
+
+private:
+ BLE &ble;
+ ReadOnlyGattCharacteristic<uint8_t> reedSwitchState;
+};
+
+#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
--- a/Service.h Sun Apr 10 19:05:36 2016 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +0,0 @@
-#ifndef __BLE_SERVICE_H__
-#define __BLE_SERVICE_H__
-
-class Service {
-public:
- const static uint16_t SERVICE_UUID = 0x0010;
- const static uint16_t X_CHARACTERISTIC_UUID = 0x0011;
- const static uint16_t Y_CHARACTERISTIC_UUID = 0x0012;
- const static uint16_t Z_CHARACTERISTIC_UUID = 0x0013;
- const static uint16_t ALL_CHARACTERISTIC_UUID = 0x0014;
-
- Service(BLE &_ble) :
- ble(_ble), xState(X_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),yState(Y_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),zState(Z_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),allState(ALL_CHARACTERISTIC_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
- {
- GattCharacteristic *charTable[] = {&xState,&yState, &zState, &allState};
- GattService Service(Service::SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
- ble.gattServer().addService(Service);
- }
-
- /*
- * Update wartosci charakterystyk
- */
-
- void updateXState(float newState) {
- int length = sizeof(float);
-
- uint8_t bytes[sizeof(float)]; //tablica podzielonego float'a
-
- for(int i = 0; i < length; i++){
- bytes[i] = ((uint8_t*)&newState)[i];
- }
-
- int n = sizeof(bytes) / sizeof(bytes[0]);
- ble.gattServer().write(xState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
- }
-
- void updateYState(float newState) {
- int length = sizeof(float);
-
- uint8_t bytes[sizeof(float)];//tablica podzielonego float'a
-
- for(int i = 0; i < length; i++){
- bytes[i] = ((uint8_t*)&newState)[i];
- }
-
- int n = sizeof(bytes) / sizeof(bytes[0]);
- ble.gattServer().write(yState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
- }
-
- void updateZState(float newState) {
- int length = sizeof(float);
-
- uint8_t bytes[sizeof(float)];//tablica podzielonego float'a
-
- for(int i = 0; i < length; i++){
- bytes[i] = ((uint8_t*)&newState)[i];
- }
-
- int n = sizeof(bytes) / sizeof(bytes[0]);
- ble.gattServer().write(zState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
- }
-
- void updateALLState(float newState,float newStatey,float newStatez) {
-
- int length = 14;
-
- uint8_t bytes[14]; //tablica podzielonych float'ow
-
- /*
- * dzielenie float'ow x y z do tablicy
- * mona bylo zrobic forem ;)
- */
-
- bytes[0] = ((uint8_t*)&newState)[0];
- bytes[1] = ((uint8_t*)&newState)[1];
- bytes[2] = ((uint8_t*)&newState)[2];
- bytes[3] = ((uint8_t*)&newState)[3];
- bytes[4] = 0xff;
- bytes[5] = ((uint8_t*)&newStatey)[0];
- bytes[6] = ((uint8_t*)&newStatey)[1];
- bytes[7] = ((uint8_t*)&newStatey)[2];
- bytes[8] = ((uint8_t*)&newStatey)[3];
- bytes[9] = 0xff;
- bytes[10] = ((uint8_t*)&newStatez)[0];
- bytes[11] = ((uint8_t*)&newStatez)[1];
- bytes[12] = ((uint8_t*)&newStatez)[2];
- bytes[13] = ((uint8_t*)&newStatez)[3];
-
-
- uint16_t n = sizeof(bytes) / sizeof(bytes[0]);
-
- ble.gattServer().write(allState.getValueHandle(), (uint8_t *)&bytes, n); //zapisanie danych do charakterystyki
- }
-
-private:
- BLE &ble;
- ReadOnlyGattCharacteristic<float> xState;
- ReadOnlyGattCharacteristic<float> yState;
- ReadOnlyGattCharacteristic<float> zState;
- ReadOnlyArrayGattCharacteristic<uint8_t, 14> allState;
-};
-
-#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
--- 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();
}
}
--- a/nRF51822.lib Sun Apr 10 19:05:36 2016 +0000 +++ b/nRF51822.lib Sun Feb 26 02:33:32 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#1751e2e2637a +https://developer.mbed.org/users/galism/code/nRF51822/#b53266ddfa1f
