![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
No changes
Dependencies: BLE_API MMA8452Q mbed nRF51822
Fork of BLE_Accelerometer_final by
Revision 4:1c2d208ce418, committed 2017-02-26
- Comitter:
- galism
- Date:
- Sun Feb 26 03:16:13 2017 +0000
- Parent:
- 3:7dc284221369
- Commit message:
- No changes
Changed in this revision
diff -r 7dc284221369 -r 1c2d208ce418 AcclerationService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AcclerationService.h Sun Feb 26 03:16:13 2017 +0000 @@ -0,0 +1,101 @@ +/* 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__ */
diff -r 7dc284221369 -r 1c2d208ce418 ReedSwitchService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ReedSwitchService.h Sun Feb 26 03:16:13 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, bool 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(bool newState) + { + ble.gattServer().write(reedSwitchState.getValueHandle(), (uint8_t *)&newState, sizeof(bool)); + } + +private: + BLE &ble; + ReadOnlyGattCharacteristic<bool> reedSwitchState; +}; + +#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
diff -r 7dc284221369 -r 1c2d208ce418 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__ */
diff -r 7dc284221369 -r 1c2d208ce418 main.cpp --- a/main.cpp Sun Apr 10 19:05:36 2016 +0000 +++ b/main.cpp Sun Feb 26 03:16:13 2017 +0000 @@ -1,35 +1,48 @@ +/* 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" +#include "AcclerationService.h" +#include "ReedSwitchService.h" -MMA8452Q accel(P0_0, P0_1, 0x1D); //deklaracja obiektu akcelerometru (P0_0 -> SDA, P0_1 -> SCL, 0x1D -> ID urzadzenia) +#define REED_SWITCH P0_5 + +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; //zmienne do których przypisywane są wartosci odczytu x y z z akcelerometru +float x,y,z; //variables assigned values to read x, y, and z accelerometer numbers +//uint16_t x,y,z; +InterruptIn button(REED_SWITCH); + +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}; -const static char DEVICE_NAME[] = "BLE_Accel"; -static const uint16_t uuid16_list[] = {Service::SERVICE_UUID}; +enum { //Different states of the reed switch + RELEASED = 0, + PRESSED, + IDLE +}; +static uint8_t reedSwitchState = IDLE; //Reed switch is initially idle +static ReedSwitchService *reedSwitchServicePtr; +static AccelerationService *accelerationServicePtr; +static volatile bool triggerSensorPolling = false; -static Service *ServicePtr; - -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); +void reedSwitchPressedCallback(void) //Change the Reed switch state to Pressed +{ + reedSwitchState = PRESSED; } +void reedSwitchReleasedCallback(void) //Change the Reed switch state to Released +{ + reedSwitchState = RELEASED; +} void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { @@ -37,11 +50,10 @@ } /** - * This function is called when the ble initialization process has failled + * 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 */ } /** @@ -66,7 +78,8 @@ ble.gap().onDisconnection(disconnectionCallback); /* Setup primary service */ - ServicePtr = new Service(ble); + 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); @@ -78,19 +91,48 @@ } +void periodicCallback(void) +{ + + /* Note that the periodicCallback() executes in interrupt context, so it is safer to do + * heavy-weight sensor polling from the main thread. */ + 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; + 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(); } }
diff -r 7dc284221369 -r 1c2d208ce418 nRF51822.lib --- a/nRF51822.lib Sun Apr 10 19:05:36 2016 +0000 +++ b/nRF51822.lib Sun Feb 26 03:16:13 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#1751e2e2637a +https://developer.mbed.org/users/galism/code/nRF51822/#abe58b2a2b5d