Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
6:d14e3df498f4
Parent:
5:0f42dcae4cdf
Child:
8:570eb66d50b5
--- a/main.cpp	Wed Jul 19 11:22:03 2017 +0000
+++ b/main.cpp	Wed Jul 19 12:07:23 2017 +0000
@@ -2,7 +2,7 @@
  *
  *  Made by Jurica Resetar @ aconno
  *  aconno.de
- *  All rights reserved__
+ *  All rights reserved
  *
  */
 
@@ -10,6 +10,7 @@
 #include "ble/BLE.h"
 #include "GapAdvertisingData.h"
 #include "acd52832_bsp.h"
+#include "mma8452.h"
 
 #define SLEEP_TIME      (0.85)      // Sleep time in seconds WAS 0.85
 #define AWAKE_TIME      (0.15)      // Was 0.15
@@ -21,11 +22,19 @@
 #define MSD_ID              (0xFF)
 #define BUZZ_TIME           (1.0)   // Buzz time in s
 
+/* Static constants for the accelerometer */
+#define WHO_AM_I            0x0D           // Type 'read' : This should return the device id of 0x2A
+#define OUT_Z_MSB           0x05           // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample
+#define I2C_DATA            (p29)
+#define I2C_CLK             (p2)
+#define INT2_PIN            (p4)
 
-bool SLEEP = true;
+
+//uint8_t SLEEP = true;
 int8_t txPower = 4;
 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
-uint8_t my_mac_address[6] = {};
+uint8_t my_mac_address[6] = {};          
+uint8_t buzzer_flag = 0;
 
 void turnBuzzOff(void);
 void goToSleep();
@@ -33,6 +42,10 @@
 Ticker WakeSleepT;
 Ticker turnBuzzOffT;
 PwmOut buzzer(BUZZER);
+PwmOut gyro_power(p7);
+PwmOut i2c_power(p5);     // I2C Pull-ups power pin
+InterruptIn gyro_pulse(INT2_PIN);
+Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
 BLE &ble = BLE::Instance();
 
 /* Restart Advertising on disconnection*/
@@ -159,6 +172,21 @@
     SLEEP = true;
 }
 
+void buzz(void){
+        buzzer.write(0.5f);
+        wait_ms(100);
+        buzzer.write(0.0f);
+        buzzer_flag = 0;
+}
+
+void pulse_handler(void){
+        #if DEBUG
+            int_led = !int_led;
+        #endif
+        i2c_power.write(1.0F);
+        buzzer_flag = 1;
+}
+
 int main(void){
     WakeSleepT.attach(goToSleep, AWAKE_TIME);
     ble.init(bleInitComplete);
@@ -170,6 +198,28 @@
     
     buzzer.period(0.001F);
     buzzer.write(0.0F);
+    gyro_power.period(0.01F);
+    gyro_power.write(1.0F);
+    i2c_power.period(0.01F);
+    i2c_power.write(1.0F);
+    wait_ms(1000);
+    
+    /* Pulse interrupt detection */
+    acc.set_register((char)CTRL_REG_4, (char) 0x04);        //  INT_EN_FF_MT Freefall/motion interrupt enabled
+    wait_ms(1);
+    acc.set_register((char)FF_MT_CFG, (char) 0b01011000);   //ELE, Motion Flag ON, YEFE, X Event Flag Enable
+    wait_ms(1);        
+    acc.set_register((char)CTRL_REG_5, (char) 0x00);        // INT_EN_FF_MT interrupt is router t0 INT2
+    wait_ms(1);
+    acc.set_register((char)FF_COUNT, (char) 0x08);          // Set Counter degister value (10ms)
+    wait_ms(1);
+    acc.set_register((char)FF_MT_THS, (char) 0x90);         // Set TH value for motion detection on 1 G (1/0.063) and DBCNTM = 1 (Increments or clears counter)
+    wait_ms(1);
+            
+    /* Setup for the interrupt handler */
+    gyro_pulse.rise(&pulse_handler);
+    acc.set_register((char)CTRL_REG_1, (char) 0x01);        // Flow data rate and Active mode           
+    wait(1);
     
     __enable_irq();
     
@@ -181,6 +231,7 @@
     
     while(true){
         if(SLEEP) __WFI();
+        else if(buzzer_flag) buzz();
         else ble.waitForEvent();
     }
 }