ESE project: Portable Camera Safe Box

Dependencies:   Freetronics_16x2_LCD X_NUCLEO_IKS01A1 mbed

Fork of HelloWorld_IKS01A1 by ST

Revision:
9:0098e603178d
Parent:
4:b1526d074d83
--- a/main.cpp	Thu Dec 17 11:03:49 2015 +0000
+++ b/main.cpp	Thu Sep 15 10:36:59 2016 +0000
@@ -1,44 +1,11 @@
-/**
- ******************************************************************************
- * @file    main.cpp
- * @author  AST / EST
- * @version V0.0.1
- * @date    14-August-2015
- * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
- *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
- ******************************************************************************
- * @attention
- *
- * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *   1. Redistributions of source code must retain the above copyright notice,
- *      this list of conditions and the following disclaimer.
- *   2. Redistributions in binary form must reproduce the above copyright notice,
- *      this list of conditions and the following disclaimer in the documentation
- *      and/or other materials provided with the distribution.
- *   3. Neither the name of STMicroelectronics nor the names of its contributors
- *      may be used to endorse or promote products derived from this software
- *      without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
-*/ 
-
 /* Includes */
 #include "mbed.h"
 #include "x_nucleo_iks01a1.h"
+#include "freetronicsLCDShield.h"
+
+freetronicsLCDShield lcd(D12, D11, D10, D9, D8, D7, D3, A0);
+
+
 
 /* Instantiate the expansion board */
 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
@@ -46,9 +13,9 @@
 /* Retrieve the composing elements of the expansion board */
 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
-static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
+//static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
-static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
+//static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
 
@@ -93,42 +60,181 @@
   float value1, value2;
   char buffer1[32], buffer2[32];
   int32_t axes[3];
+  int32_t m[3];
+  int32_t n[3];
+  int32_t r;
+  int32_t i;
+  int32_t lock_unlock=0;
+  int temp=0;
   
-  printf("\r\n--- Starting new run ---\r\n");
+   // turn on the back light (it's off by default)
+    lcd.setBackLight(true);
+    
+    // print the first line and wait 3 sec
+    lcd.printf("Starting ESEproc");
+    wait(2);
+  
+  
+  printf("\r\n--- Starting ESEproc ---\r\n");
+  
+  
+ 
+    
+    // print the counter prefix; the number will be printed in the while loop
+    lcd.setCursorPosition(1, 0);
+    lcd.printf("Initializing...");
 
   humidity_sensor->ReadID(&id);
   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
-  pressure_sensor->ReadID(&id);
-  printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
-  magnetometer->ReadID(&id);
-  printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
+  //pressure_sensor->ReadID(&id);
+ // printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
+  //magnetometer->ReadID(&id);
+  //printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
   gyroscope->ReadID(&id);
   printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
   
-  wait(3);
+  DigitalOut myled(LED1);
+  DigitalIn mybutton(USER_BUTTON);
+  
+  wait(2);
+  
+      // turn off the back light 
+    lcd.setBackLight(false);
  
   while(1) {
+    wait(0.001);
     printf("\r\n");
+    
+    temp_sensor1->GetTemperature(&value1); 
+    humidity_sensor->GetHumidity(&value2);
+    
+    // print the Temperature and Humidity 
+    lcd.cls();
+    lcd.setBackLight(false);
+    lcd.printf("T:%4s C", printDouble(buffer1, value1));
+
+    
+    lcd.setCursorPosition(1, 0);
+    lcd.printf("H:%s %%", printDouble(buffer2, value2));
+    if ((value1>37) && (value2>80))
+        {
+           myled=1;
 
-    temp_sensor1->GetTemperature(&value1);
-    humidity_sensor->GetHumidity(&value2);
+           printf("Excessive Temperature & Excessive Humidity\r\n");
+           lcd.setBackLight(true);
+           lcd.setCursorPosition(0, 12);
+           lcd.printf("T");
+           lcd.setCursorPosition(1, 12);
+           lcd.printf("H");        
+           wait(0.1);
+           myled=0;
+        }
+    if ((value1>37) && (value2<=80))
+        {
+            myled=1;
+
+            printf("Excessive Temperature\r\n");
+            lcd.setBackLight(true);
+            lcd.setCursorPosition(0, 12);
+            lcd.printf("T");
+            wait(0.4);
+            myled=0;
+        }
+    if ((value1<=37) && (value2>80))
+        {
+            myled=1;
+
+            printf("Excessive Humidity\r\n");
+            lcd.setBackLight(true);
+            lcd.setCursorPosition(1, 12);
+            lcd.printf("H");
+            wait(0.8);
+            myled=0;
+        }
+    
+    
     printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
     
-    temp_sensor2->GetFahrenheit(&value1);
-    pressure_sensor->GetPressure(&value2);
-    printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
 
-    printf("---\r\n");
+    
+  //  temp_sensor2->GetFahrenheit(&value1);
+    //pressure_sensor->GetPressure(&value2);
+    //printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
 
-    magnetometer->Get_M_Axes(axes);
-    printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+   
+ //           if (temp>=100){
+ //               lock_unlock =! lock_unlock;
+  //              myled=1;
+ //               wait(0.2);
+ //               myled=0;
+ //               wait(0.2);
+ //               myled=1;
+  //              wait(0.2);
+ //               myled=0;
+ //               }
+     
+    if (lock_unlock == 0)
+        {
+        printf("SecuritySystemON\r\n");
+        lcd.setCursorPosition(0, 15);
+        lcd.printf("S");
+        accelerometer->Get_X_Axes(axes);
+        for (i=0;i<3;i++)
+            {
+                m[i]=axes[i];
+            }
+        wait(0.2);
+        accelerometer->Get_X_Axes(axes);
+        for (i=0;i<3;i++)
+            {
+                n[i]=axes[i];
+            }
+        r=(m[0]-n[0])*(m[0]-n[0])+(m[1]-n[1])*(m[1]-n[1])+(m[2]-n[2])*(m[2]-n[2]);
+        if(r>1500)
+            {
+                myled=1;
+                printf("Security Alert\r\n");
+                lcd.cls();
+                lcd.setBackLight(true);
+                lcd.printf("Security Alert");
+                lcd.setCursorPosition(1, 0);
+                lcd.printf("BUZZING...");
+                wait(5);
 
-    accelerometer->Get_X_Axes(axes);
-    printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
-    gyroscope->Get_G_Axes(axes);
-    printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
-    wait(1.5);
+            }
+        printf("LSM6DS0 [acc/mg]:%6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+    }
+    if (lock_unlock != 0)
+        {
+            printf("UNLOCKED\r\n");
+            lcd.setCursorPosition(0, 15);
+            lcd.printf(" ");//means unlock by removing the s on LCD
+            printf("SecuritySystemOFF\r\n");
+            }
+     if(mybutton == 0)
+    {
+            temp=0;
+            lcd.setBackLight(true);
+            while(mybutton==0 and temp<500){
+                wait(0.01);
+                temp++;
+            }
+            if (temp==500){
+                    lock_unlock =! lock_unlock;
+                    if (lock_unlock==0){
+                        lcd.setCursorPosition(0, 15);
+                        lcd.printf("S");//means lock by adding a s on LCD             
+                    }
+                    if (lock_unlock==1){
+                        lcd.setCursorPosition(0, 15);
+                        lcd.printf(" ");//means unlock by removing the s on LCD             
+                    }
+                }
+            
+            wait(0.2);
+            lcd.setBackLight(false);
+    }
+    lcd.setBackLight(false);
+    wait(0.3);
   }
 }