Let me know what you think

Dependencies:   BLE_API mbed nRF51822

Fork of SDP_Version3_Abdul by Michael Galis

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Senior Project Bluetooth bicycle speedometer
00002 Author: Michael Galis
00003 This is the main file, it implements both Acceleration Service and Reed Switch
00004 Service to send all the necessary data over bluetooth in organized packages. 
00005 Also there is code to reed from the accelerometer over an I2C connection.
00006 */
00007 
00008 #include "mbed.h"
00009 #include "ble/BLE.h"
00010 #include "MMA8452Q.h"
00011 #include "AcclerationService.h"
00012 #include "ReedSwitchService.h"
00013 #include "BLE_Init.h"
00014 //#include <PortInOut.h>
00015 #include <DigitalIn.h>
00016 
00017 #define REED_SWITCH     P0_5
00018 
00019 MMA8452Q accel(P0_10, P0_8, 0x1D);  //Declare accel object at I2C pins(P0_0 -> SDA, P0_1 -> SCL, 0x1D -> Accelerometer Address )
00020 Ticker ticker;
00021 Ticker reedTicker;
00022 float x,y,z;                        //variables assigned values to read x, y, and z accelerometer numbers
00023 //InterruptIn button(REED_SWITCH);
00024 DigitalIn reedPin(REED_SWITCH); // or use DigitalIn readPin(REED_SWITCH, MODE), mode is the initial state of the pin
00025                                 // Modes: PullUp, PullDown, PullNone, or OpenDrain 
00026 
00027 enum {                                                  //Different states of the reed switch
00028     RELEASED = 0,
00029     PRESSED,
00030     IDLE
00031 };
00032 static uint8_t reedSwitchState = IDLE;                  //Reed switch is initially idle
00033 static volatile bool  triggerSensorPolling = false;
00034 
00035 void reedSwitchPressedCallback(void)            //Change the Reed switch state to Pressed
00036 {
00037     reedSwitchState = PRESSED;  
00038 }
00039 
00040 void reedSwitchReleasedCallback(void)           //Change the Reed switch state to Released
00041 {
00042     reedSwitchState = RELEASED;
00043 }
00044 
00045 void periodicCallback(void)
00046 {
00047     triggerSensorPolling = true;
00048     
00049 }
00050 
00051 bool reedBit_is_clear(){
00052     if(!reedPin.read()){
00053         return true;
00054         }
00055     else{
00056         return false;   
00057     }
00058 }
00059 
00060 void checkForRev(){ //looks the falling edge
00061   static uint16_t state = 0; //do need to change the type? the status you have is the same, "static uint16_t"
00062   state = (state << 1) | (! reedBit_is_clear()) | 0xE000;
00063   if (state == 0xF000) {
00064        // in this if statement we want to tell the MCU/App
00065        // "hey this is an actual revolution not the switch bouncing"
00066        // i.e. something like " switchCount++ "
00067        
00068        //something like
00069        reedSwitchServicePtr->updateReedSwitchState(PRESSED);
00070       // ??
00071   }
00072 }
00073 
00074 int main(void)
00075 {
00076 //    button.fall(reedSwitchPressedCallback);
00077 //    button.rise(reedSwitchReleasedCallback);
00078     
00079     BLE &ble = BLE::Instance();
00080     ble.init(bleInitComplete);
00081     
00082     accel.init();
00083     ticker.attach(periodicCallback,0.2);
00084     
00085     reedTicker.attach_us(checkForRev, 10000); // calls checkForRev every 10ms
00086     
00087     /* SpinWait for initialization to complete. This is necessary because the
00088      * BLE object is used in the main loop below. */
00089     while (ble.hasInitialized()  == false) { /* spin loop */ }
00090     
00091     while (true) {
00092         if (reedSwitchState != IDLE) {                                      //When the Reed Switch changes states
00093 //           reedSwitchServicePtr->updateReedSwitchState(reedSwitchState);   //update the value over ble
00094             reedSwitchState = IDLE;
00095         }
00096         if (triggerSensorPolling && ble.getGapState().connected)
00097         {
00098             triggerSensorPolling = false;
00099             if(accel.available())
00100             {
00101                 x=accel.readX(); 
00102                 y=accel.readY();
00103                 z=accel.readZ();
00104                    
00105                 accelerationServicePtr->updateXData(x);
00106                 accelerationServicePtr->updateYData(y);
00107                 accelerationServicePtr->updateZData(z);
00108                 accelerationServicePtr->updateALLState(x,y,z);
00109             }
00110         }
00111         ble.waitForEvent();
00112     }
00113 }