Project 1 Self-powered Health & Security Monitoring System USB device

Dependencies:   C12832_lcd LM75B MMA7660 USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
wane
Date:
Fri Mar 28 19:11:47 2014 +0000
Parent:
1:930838234048
Commit message:
Updated comments.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Mar 28 18:19:54 2014 +0000
+++ b/main.cpp	Fri Mar 28 19:11:47 2014 +0000
@@ -22,45 +22,63 @@
 // terminal. Motion alarm will continue to be on until the passcode
 // is entered. 
 //
-// An external buzzer is used and connected at P21 and ground. 
+// Set-up:
+// - An external buzzer is used and connected at P21 and ground. 
+// - To be connected to debug serial terminal
+// - Connect to a host as device
 // 
 // Controls: 
-// Joystick DOWN - Switch between Test and Operational mode
-// Joystick LEFT - Turn Temperature monitoring on and off
-// Joystick UP   - Turn Motion monitoring on. Increase sensitivity
-//                level as further push. S1-Low S2-Medium S3-High
-// Joystick RIGHT- Lock and unlock USB device. Enter passcode
-//                at first locking device. 
-// Pot1 - Lower bound temerature (for debugging)
-// Pot2 - Higher bound temperature (for debugging)
-// Fire - Input a simulated air temperature (for debugging)
+//     Joystick DOWN - Switch between Test and Operational mode
+//     Joystick LEFT - Turn Temperature monitoring on and off
+//     Joystick UP   - Turn Motion monitoring on. Increase sensitivity
+//                     level as further push. S1-Low S2-Medium S3-High
+//     Joystick RIGHT- Lock and unlock USB device. Enter passcode
+//                     at first locking device. 
+//     Pot1 - Lower bound temerature (for debugging)
+//     Pot2 - Higher bound temperature (for debugging)
+//     Fire - Input a simulated air temperature (for debugging)
 // 
 // Output: 
-// LED1 - USB Connection 
-// LED2 - Lock Status (On-Lock Off-Unlocked)
-// LED3 - Alarm Status - Temperature
-// LED4 - Alarm Status - Motion
-// Buzzer - Temperature alarm beeps in lower frequency
+//     LED1 - USB Connection 
+//     LED2 - Lock Status (On-Lock Off-Unlocked)
+//     LED3 - Alarm Status - Temperature
+//     LED4 - Alarm Status - Motion
+//     Buzzer - Temperature alarm beeps in lower frequency
 //          Motion alarm beeps in relatively higher frequency
-//
-// History
-/* 241: Added temp alarm
-   251: Added motion alarm, sensitivity level
-                 Validated alarm status handling
-   252: Added lock, passcode control, mode (T-Testing; O-Operational)                 
-   261: Added non-blocking serial entry
+// LCD Key:
+//     Now - Current temperature
+//     L   - Lower bound temperature
+//     U   - Upper bound temperature
+//     Mode: T-Test mode  O-Operational mode
+//           H-Temperature Monitoring
+//           S1 - Security (Low sensitivity)
+//           S2 - Security (Mid sensitivity)
+//           S3 - Security (High sensitivity)
+//     Status: 
+//           L - Device in locked (Unlocked if disappeared)
+//           A - Alarm on (No alarm if disappeared)
+/*
+History:
+    Version 1.0
+    241: Added temp alarm
+    251: Added motion alarm, sensitivity level
+         Validated alarm status handling
+    252: Added lock, passcode control, mode (T-Testing; O-Operational)                 
+    261: Added non-blocking serial entry
 
 TO-FIX: 
-   1. To complete USB connection detection. Currently, system stalls 
-      or fails to respond when detached from a USB host. 
+    1. To set up a standalone device. Currently, device is not in 
+       operation until connected to a host
+    2. To complete USB connection detection. Currently, system stalls 
+       or fails to respond when detached from a USB host. 
    
 TO-DO: 
-   1. Add menu display with LCD
-   2. Add data entry with LCD (without using serial)
-   3. Add file system that there can record events and save into files
-   4. Add capability to report to user remotely 
-   5. Add to record and report sound once security is breached. 
-   6. Add to record GPS data and report back
+    1. Add menu display with LCD
+    2. Add data entry with LCD (without using serial)
+    3. Add file system that there can record events and save into files
+    4. Add capability to report to user remotely 
+    5. Add to record and report sound once security is breached. 
+    6. Add to record GPS data and report back
    
 */
 
@@ -103,7 +121,6 @@
 InterruptIn IRQJoyDown(p12);
 InterruptIn IRQJoyLeft(p13);
 InterruptIn IRQJoyRight(p16);
-//DigitalIn fire(p14);
 InterruptIn IRQFire(p14);
 
 Serial pc(USBTX, USBRX); // tx, rx
@@ -113,35 +130,23 @@
 MMA7660 MMA(p28, p27);
 DigitalOut connectionLed(LED1);
 DigitalOut lockLed(LED2);
-//PwmOut Xaxis_p(LED2);
 //BusOut leds(LED3,LED4);
 //DigitalOut ledTAlarm(LED3);
 //DigitalOut ledSAlarm(LED4);
+PwmOut ledTAlarm(LED3);
 PwmOut ledSAlarm(LED4);
-PwmOut ledTAlarm(LED3);
+
 
-// Gobal parameters
-float setUpperTemp;          // Upper bound of temperature monitoring range
-float setLowerTemp;          // Lower bound of temperature monitoring range
-bool sAlarmOn;               // State of alarm
-int statusAlarm;             // Status indicating whether health or/and security alarm is triggered
+// Global backend variables
+float setUpperTemp;         // Upper bound of temperature monitoring range
+float setLowerTemp;         // Lower bound of temperature monitoring range
+bool sAlarmOn;              // State of alarm
+int statusAlarm;            // Status indicating whether health or/and security alarm is triggered
 bool sHealthFeatureActive;          // State indicating temperature monitoring is activated
 bool sSecurityFeatureActive;        // State indicating motion monitoring is activated
-bool sLockActive;            // State indicating lock is active
-char sState;                 // security setting state
-char gState;                 // Global state
-int buzzPattern; 
-float buzzPeriod;
-Ticker timer1;
-Ticker timer3;
-Ticker timer4;
-Timer timer2;
-Ticker ticAlarm;
-bool first; 
-int count;
-bool activated;             // for debounce fire
-time_t yet;
-time_t now;
+bool sLockActive;           // State indicating lock is active
+char sState;                // security setting state
+char gState;                // Global state
 int alarmType;              // 0 - Health; 1 - Security
 char senLevel;              // Sensitivity level of motion alarm
 int mode;                   // Operation mode: 
@@ -149,26 +154,41 @@
                             // 1 = Lock mode (lock and unlock device)
 int passcode;               // 4-digit passcode for lock and unlock               
 bool sPCOK;                 // state indicating a valid passcode is existing
+float lowerTempBase;        // Simulated lower bound temperature
+float upperTempBase;        // Simulated upper bound temperature
+float temp;                 // Current temperature
 
+
+// Global frontend variables
+int buzzPattern; 
+float buzzPeriod;
+Ticker timer1;              // For LCD display 
+Ticker timer3;              // For debouncer
+Ticker timer4;              // TBU
+Timer timer2;               // For tracking temperature alarm
+bool activated;             // for debounce fire
+time_t yet;                 // For debounce time tracking
+time_t now;                 // For debounce time tracking
 bool updateCurTempDisplay;
 bool updateJoyDisplay;
 bool tempOverride;
-float lowerTempBase;        // For simulated lower bound temperature
-float upperTempBase;        // For simulated upper bound temperature
-float temp;
 
 bool entryReady;            // For tracking if user input is comlete
 unsigned char entryState;   // State tracking input entry
 char inBuf[128];            // Input buffer 
 char pos;                   // Gobal buffer index
-
+int tol;                    // Tolerance
 
 float timeElapsed;
 time_t seconds;
-
 float potBase1; 
 float potBase2; 
 
+#ifdef C
+#define deg 'C'
+#else 
+#define deg 'F'
+#endif 
 
 typedef struct {
     bool active;
@@ -225,9 +245,8 @@
     if (!activated && now > yet) {
         
         tempOverride = !tempOverride;
-        //EnterTemp();
         if (tempOverride)
-            gState = 3;
+            gState = 3;             // EnterTemp
         yet = now + 1;
         activated = true;
     }
@@ -236,6 +255,8 @@
 
 
 // ISR for joystick up direction
+// Turn on motion detection feature. Continue to push to increase motion
+// detection sensitivity. 
 void ISRJoyUp()
 {
     now = time(NULL);
@@ -251,6 +272,7 @@
 }
 
 // ISR for joystick down direction
+// Switch mode between Test and Operational
 void ISRJoyDown()
 {
     now = time(NULL);
@@ -263,6 +285,7 @@
 }
 
 // ISR for joystick left direction
+// Turn temperature monitoring feature on and off
 void ISRJoyLeft()
 {
     now = time(NULL);
@@ -275,6 +298,7 @@
 }
 
 // ISR for joystick right direction
+// Lock and unlock device
 void ISRJoyRight()
 {
     now = time(NULL);
@@ -282,9 +306,9 @@
         if (mode == 1)
         {
             if (!sLockActive)
-                gState = 1; 
+                gState = 1;     // SetPasscode
             else
-                gState = 2; 
+                gState = 2;     // UnlockPasscode
         }
         yet = now + 1;
         activated = true;
@@ -324,7 +348,7 @@
         }
 }
 
-// Turn on led in different patterns
+// Turn on LEDs and operate in different frequency
 void ledOn(int type, float period) 
 {
     switch (type) {
@@ -361,6 +385,7 @@
 // For testing buzzer. 
 void buzzTest()
 {
+    int count = 0;
     sAlarmOn = OFF; 
 
     timer1.attach(&debounce, 1);
@@ -388,7 +413,7 @@
 }   
         
 
-// Set date and time
+// Set date and time at LCD display
 // Input: Year, month, date, hour, minute, second
 void SetTime(int year, int month, int date, int hour, int min, int sec)
 {
@@ -422,50 +447,6 @@
 
 }         
 
-// Non-blockingly innput a simulated temperature
-// Input: None
-bool EnterTemp()
-{
-    int i;
-    bool done = false; 
-    
-    switch (entryState)
-    {
-        case 0:
-            break;
-        case 1: 
-            pc.printf("\n\rEnter a simulated temperature: ");
-            entryReady = false;
-            entryState = 2;
-            break;
-        case 2: 
-            peepEntry();
-            if (!entryReady)
-                break;
-            else
-                entryState = 3;
-        case 3:
-            sscanf(inBuf, "%d", &i);
-            if (i < 0 || i >= 100) {
-                printf("\n\rEnter a temperature between 0F and 100F: ");            
-                entryReady = false;
-                entryState = 2;
-                break;
-            } else {
-                temp = i;
-                printf("Current temperature is now %dF.\n\r", i);
-                updateCurTempDisplay = true;
-                entryState = 1;
-                done = true;
-                calibratePot();
-                break;
-            }
-
-            
-    }
-    return done; 
-
-}
 
 // Peep uart buffer if serial data is available for read
 // Return the entry when hit ENTER button
@@ -607,6 +588,51 @@
     return done;    
 }
 
+// Non-blockingly innput a simulated temperature
+// Input: None
+bool EnterTemp()
+{
+    int i;
+    bool done = false; 
+    
+    switch (entryState)
+    {
+        case 0:
+            break;
+        case 1: 
+            pc.printf("\n\rEnter a simulated temperature: ");
+            entryReady = false;
+            entryState = 2;
+            break;
+        case 2: 
+            peepEntry();
+            if (!entryReady)
+                break;
+            else
+                entryState = 3;
+        case 3:
+            sscanf(inBuf, "%d", &i);
+            if (i < 0 || i >= 100) {
+                printf("\n\rEnter a temperature between 0F and 100F: ");            
+                entryReady = false;
+                entryState = 2;
+                break;
+            } else {
+                temp = i;
+                printf("Current temperature is now %dF.\n\r", i);
+                updateCurTempDisplay = true;
+                entryState = 1;
+                done = true;
+                calibratePot();
+                break;
+            }
+
+            
+    }
+    return done; 
+
+}
+
 // Check if USB is connected   
 // To read lock status: bit0=NUM_LOCK bit1=CAP_LOCK bit2=SCROLL_LOCK
 // To check, either check if keyCode returns OK or status led is signaled.
@@ -615,7 +641,6 @@
 void USBConnCheck()
 {
     char status = 0;
-    char result; 
     
     //if (!(statusAlarm & 0x80000000))
     //{
@@ -629,15 +654,12 @@
     if (!(statusAlarm & 0x80000000) && (status & 0x2))
     {
         statusAlarm |= 0x80000000; 
-        result = true;
     }
     else if ((statusAlarm & 0x80000000) && !(status & 0x2))
     {
         statusAlarm &= 0x7FFFFFFF;
-        result = false;
     }
     //printf("2: lockStatus = 0x%x statusAlarm = 0x%x\n\r", status, statusAlarm);
-    //return result;
     
 }
 
@@ -793,7 +815,7 @@
 }
 
     
-// Initialize settings
+// Initialize settings 
 void Init(void)
 {
     SetTime(YEAR, MONTH, DATE, HOUR, MIN, 0);