No changes

Dependencies:   BLE_API mbed

Fork of SDP_Version3 by Michael Galis

Committer:
galism
Date:
Sun Feb 26 02:33:32 2017 +0000
Revision:
4:caab577334f0
Parent:
3:7dc284221369
No changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
galism 4:caab577334f0 1 /* Senior Project Bluetooth bicycle speedometer
galism 4:caab577334f0 2 Author: Michael Galis
galism 4:caab577334f0 3 This is the main file, it implements both Acceleration Service and Reed Switch
galism 4:caab577334f0 4 Service to send all the necessary data over bluetooth in organized packages.
galism 4:caab577334f0 5 Also there is code to reed from the accelerometer over an I2C connection.
galism 4:caab577334f0 6 */
galism 4:caab577334f0 7
Radoj 0:6bd1a61571d0 8 #include "mbed.h"
Radoj 0:6bd1a61571d0 9 #include "ble/BLE.h"
Radoj 0:6bd1a61571d0 10 #include "MMA8452Q.h"
galism 4:caab577334f0 11 #include "AcclerationService.h"
galism 4:caab577334f0 12 #include "ReedSwitchService.h"
galism 4:caab577334f0 13 #include "BLE_Init.h"
Radoj 0:6bd1a61571d0 14
galism 4:caab577334f0 15 #define REED_SWITCH P0_5
Radoj 0:6bd1a61571d0 16
galism 4:caab577334f0 17 MMA8452Q accel(P0_10, P0_8, 0x1D); //Declare accel object at I2C pins(P0_0 -> SDA, P0_1 -> SCL, 0x1D -> Accelerometer Address )
galism 4:caab577334f0 18 Ticker ticker;
galism 4:caab577334f0 19 float x,y,z; //variables assigned values to read x, y, and z accelerometer numbers
galism 4:caab577334f0 20 InterruptIn button(REED_SWITCH);
Radoj 0:6bd1a61571d0 21
galism 4:caab577334f0 22 enum { //Different states of the reed switch
galism 4:caab577334f0 23 RELEASED = 0,
galism 4:caab577334f0 24 PRESSED,
galism 4:caab577334f0 25 IDLE
galism 4:caab577334f0 26 };
galism 4:caab577334f0 27 static uint8_t reedSwitchState = IDLE; //Reed switch is initially idle
galism 4:caab577334f0 28 static volatile bool triggerSensorPolling = false;
Radoj 0:6bd1a61571d0 29
galism 4:caab577334f0 30 void reedSwitchPressedCallback(void) //Change the Reed switch state to Pressed
Radoj 0:6bd1a61571d0 31 {
galism 4:caab577334f0 32 reedSwitchState = PRESSED;
Radoj 0:6bd1a61571d0 33 }
Radoj 0:6bd1a61571d0 34
galism 4:caab577334f0 35 void reedSwitchReleasedCallback(void) //Change the Reed switch state to Released
Radoj 0:6bd1a61571d0 36 {
galism 4:caab577334f0 37 reedSwitchState = RELEASED;
Radoj 0:6bd1a61571d0 38 }
Radoj 0:6bd1a61571d0 39
galism 4:caab577334f0 40 void periodicCallback(void)
Radoj 0:6bd1a61571d0 41 {
galism 4:caab577334f0 42 triggerSensorPolling = true;
galism 4:caab577334f0 43
Radoj 0:6bd1a61571d0 44 }
Radoj 0:6bd1a61571d0 45
Radoj 0:6bd1a61571d0 46 int main(void)
Radoj 0:6bd1a61571d0 47 {
galism 4:caab577334f0 48 button.fall(reedSwitchPressedCallback);
galism 4:caab577334f0 49 button.rise(reedSwitchReleasedCallback);
galism 4:caab577334f0 50
Radoj 0:6bd1a61571d0 51 BLE &ble = BLE::Instance();
Radoj 0:6bd1a61571d0 52 ble.init(bleInitComplete);
Radoj 0:6bd1a61571d0 53
Radoj 0:6bd1a61571d0 54 accel.init();
galism 4:caab577334f0 55 ticker.attach(periodicCallback,0.2);
Radoj 0:6bd1a61571d0 56
Radoj 0:6bd1a61571d0 57 /* SpinWait for initialization to complete. This is necessary because the
Radoj 0:6bd1a61571d0 58 * BLE object is used in the main loop below. */
Radoj 0:6bd1a61571d0 59 while (ble.hasInitialized() == false) { /* spin loop */ }
Radoj 0:6bd1a61571d0 60
Radoj 0:6bd1a61571d0 61 while (true) {
galism 4:caab577334f0 62 if (reedSwitchState != IDLE) { //When the Reed Switch changes states
galism 4:caab577334f0 63 reedSwitchServicePtr->updateReedSwitchState(reedSwitchState); //update the value over ble
galism 4:caab577334f0 64 reedSwitchState = IDLE;
galism 4:caab577334f0 65 }
galism 4:caab577334f0 66 if (triggerSensorPolling && ble.getGapState().connected)
galism 4:caab577334f0 67 {
galism 4:caab577334f0 68 triggerSensorPolling = false;
galism 4:caab577334f0 69 if(accel.available())
galism 4:caab577334f0 70 {
galism 4:caab577334f0 71 x=accel.readX();
galism 4:caab577334f0 72 y=accel.readY();
galism 4:caab577334f0 73 z=accel.readZ();
galism 4:caab577334f0 74
galism 4:caab577334f0 75 accelerationServicePtr->updateXData(x);
galism 4:caab577334f0 76 accelerationServicePtr->updateYData(y);
galism 4:caab577334f0 77 accelerationServicePtr->updateZData(z);
galism 4:caab577334f0 78 accelerationServicePtr->updateALLState(x,y,z);
galism 4:caab577334f0 79 }
galism 4:caab577334f0 80 }
Radoj 0:6bd1a61571d0 81 ble.waitForEvent();
Radoj 0:6bd1a61571d0 82 }
Radoj 0:6bd1a61571d0 83 }