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.
Dependencies: BLE_API mbed nRF51822
Fork of SDP_Version3_Abdul by
main.cpp@6:60f89e533491, 2017-02-26 (annotated)
- Committer:
- alfayeza
- Date:
- Sun Feb 26 06:22:32 2017 +0000
- Revision:
- 6:60f89e533491
- Parent:
- 4:caab577334f0
- Child:
- 7:79e6de3b27d4
Let me know what you think
Who changed what in which revision?
User | Revision | Line number | New 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" |
alfayeza | 6:60f89e533491 | 14 | //#include <PortInOut.h> |
alfayeza | 6:60f89e533491 | 15 | #include <DigitalIn.h> |
Radoj | 0:6bd1a61571d0 | 16 | |
galism | 4:caab577334f0 | 17 | #define REED_SWITCH P0_5 |
Radoj | 0:6bd1a61571d0 | 18 | |
galism | 4:caab577334f0 | 19 | 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 | 20 | Ticker ticker; |
alfayeza | 6:60f89e533491 | 21 | Ticker reedTicker; |
galism | 4:caab577334f0 | 22 | float x,y,z; //variables assigned values to read x, y, and z accelerometer numbers |
alfayeza | 6:60f89e533491 | 23 | //InterruptIn button(REED_SWITCH); |
alfayeza | 6:60f89e533491 | 24 | DigitalIn reedPin(REED_SWITCH); // or use DigitalIn readPin(REED_SWITCH, MODE), mode is the initial state of the pin |
alfayeza | 6:60f89e533491 | 25 | // Modes: PullUp, PullDown, PullNone, or OpenDrain |
Radoj | 0:6bd1a61571d0 | 26 | |
galism | 4:caab577334f0 | 27 | enum { //Different states of the reed switch |
galism | 4:caab577334f0 | 28 | RELEASED = 0, |
galism | 4:caab577334f0 | 29 | PRESSED, |
galism | 4:caab577334f0 | 30 | IDLE |
galism | 4:caab577334f0 | 31 | }; |
galism | 4:caab577334f0 | 32 | static uint8_t reedSwitchState = IDLE; //Reed switch is initially idle |
galism | 4:caab577334f0 | 33 | static volatile bool triggerSensorPolling = false; |
Radoj | 0:6bd1a61571d0 | 34 | |
galism | 4:caab577334f0 | 35 | void reedSwitchPressedCallback(void) //Change the Reed switch state to Pressed |
Radoj | 0:6bd1a61571d0 | 36 | { |
galism | 4:caab577334f0 | 37 | reedSwitchState = PRESSED; |
Radoj | 0:6bd1a61571d0 | 38 | } |
Radoj | 0:6bd1a61571d0 | 39 | |
galism | 4:caab577334f0 | 40 | void reedSwitchReleasedCallback(void) //Change the Reed switch state to Released |
Radoj | 0:6bd1a61571d0 | 41 | { |
galism | 4:caab577334f0 | 42 | reedSwitchState = RELEASED; |
Radoj | 0:6bd1a61571d0 | 43 | } |
Radoj | 0:6bd1a61571d0 | 44 | |
galism | 4:caab577334f0 | 45 | void periodicCallback(void) |
Radoj | 0:6bd1a61571d0 | 46 | { |
galism | 4:caab577334f0 | 47 | triggerSensorPolling = true; |
galism | 4:caab577334f0 | 48 | |
Radoj | 0:6bd1a61571d0 | 49 | } |
Radoj | 0:6bd1a61571d0 | 50 | |
alfayeza | 6:60f89e533491 | 51 | bool reedBit_is_clear(){ |
alfayeza | 6:60f89e533491 | 52 | if(!reedPin.read()){ |
alfayeza | 6:60f89e533491 | 53 | return true; |
alfayeza | 6:60f89e533491 | 54 | } |
alfayeza | 6:60f89e533491 | 55 | else{ |
alfayeza | 6:60f89e533491 | 56 | return false; |
alfayeza | 6:60f89e533491 | 57 | } |
alfayeza | 6:60f89e533491 | 58 | } |
alfayeza | 6:60f89e533491 | 59 | |
alfayeza | 6:60f89e533491 | 60 | void checkForRev(){ //looks the falling edge |
alfayeza | 6:60f89e533491 | 61 | static uint16_t state = 0; //do need to change the type? the status you have is the same, "static uint16_t" |
alfayeza | 6:60f89e533491 | 62 | state = (state << 1) | (! reedBit_is_clear()) | 0xE000; |
alfayeza | 6:60f89e533491 | 63 | if (state == 0xF000) { |
alfayeza | 6:60f89e533491 | 64 | // in this if statement we want to tell the MCU/App |
alfayeza | 6:60f89e533491 | 65 | // "hey this is an actual revolution not the switch bouncing" |
alfayeza | 6:60f89e533491 | 66 | // i.e. something like " switchCount++ " |
alfayeza | 6:60f89e533491 | 67 | |
alfayeza | 6:60f89e533491 | 68 | //something like |
alfayeza | 6:60f89e533491 | 69 | //reedSwitchServicePtr->updateReedSwitchState(PRESSED); |
alfayeza | 6:60f89e533491 | 70 | // ?? |
alfayeza | 6:60f89e533491 | 71 | } |
alfayeza | 6:60f89e533491 | 72 | } |
alfayeza | 6:60f89e533491 | 73 | |
Radoj | 0:6bd1a61571d0 | 74 | int main(void) |
Radoj | 0:6bd1a61571d0 | 75 | { |
alfayeza | 6:60f89e533491 | 76 | // button.fall(reedSwitchPressedCallback); |
alfayeza | 6:60f89e533491 | 77 | // button.rise(reedSwitchReleasedCallback); |
galism | 4:caab577334f0 | 78 | |
Radoj | 0:6bd1a61571d0 | 79 | BLE &ble = BLE::Instance(); |
Radoj | 0:6bd1a61571d0 | 80 | ble.init(bleInitComplete); |
Radoj | 0:6bd1a61571d0 | 81 | |
Radoj | 0:6bd1a61571d0 | 82 | accel.init(); |
galism | 4:caab577334f0 | 83 | ticker.attach(periodicCallback,0.2); |
Radoj | 0:6bd1a61571d0 | 84 | |
alfayeza | 6:60f89e533491 | 85 | reedTicker.attach_us(checkForRev, 10000); // calls checkForRev every 10ms |
alfayeza | 6:60f89e533491 | 86 | |
Radoj | 0:6bd1a61571d0 | 87 | /* SpinWait for initialization to complete. This is necessary because the |
Radoj | 0:6bd1a61571d0 | 88 | * BLE object is used in the main loop below. */ |
Radoj | 0:6bd1a61571d0 | 89 | while (ble.hasInitialized() == false) { /* spin loop */ } |
Radoj | 0:6bd1a61571d0 | 90 | |
Radoj | 0:6bd1a61571d0 | 91 | while (true) { |
galism | 4:caab577334f0 | 92 | if (reedSwitchState != IDLE) { //When the Reed Switch changes states |
alfayeza | 6:60f89e533491 | 93 | // reedSwitchServicePtr->updateReedSwitchState(reedSwitchState); //update the value over ble |
galism | 4:caab577334f0 | 94 | reedSwitchState = IDLE; |
galism | 4:caab577334f0 | 95 | } |
galism | 4:caab577334f0 | 96 | if (triggerSensorPolling && ble.getGapState().connected) |
galism | 4:caab577334f0 | 97 | { |
galism | 4:caab577334f0 | 98 | triggerSensorPolling = false; |
galism | 4:caab577334f0 | 99 | if(accel.available()) |
galism | 4:caab577334f0 | 100 | { |
galism | 4:caab577334f0 | 101 | x=accel.readX(); |
galism | 4:caab577334f0 | 102 | y=accel.readY(); |
galism | 4:caab577334f0 | 103 | z=accel.readZ(); |
galism | 4:caab577334f0 | 104 | |
galism | 4:caab577334f0 | 105 | accelerationServicePtr->updateXData(x); |
galism | 4:caab577334f0 | 106 | accelerationServicePtr->updateYData(y); |
galism | 4:caab577334f0 | 107 | accelerationServicePtr->updateZData(z); |
galism | 4:caab577334f0 | 108 | accelerationServicePtr->updateALLState(x,y,z); |
galism | 4:caab577334f0 | 109 | } |
galism | 4:caab577334f0 | 110 | } |
Radoj | 0:6bd1a61571d0 | 111 | ble.waitForEvent(); |
Radoj | 0:6bd1a61571d0 | 112 | } |
Radoj | 0:6bd1a61571d0 | 113 | } |