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 18:19:54 2014 +0000
Parent:
0:88dc49222b35
Child:
2:07c9334e3088
Commit message:
Updated comments in main.cpp.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Mar 28 01:21:17 2014 +0000
+++ b/main.cpp	Fri Mar 28 18:19:54 2014 +0000
@@ -1,17 +1,66 @@
-// Project: Self-powered USB Health and Security Monitoring Device (HSMD)
-
+// Project: Self-powered USB Health and Security Monitoring Device 
+// Erick Wan
+// March 2014
+// 
+// Note: 
+// The codes are written in a rush that they are missing descriptions, not as
+// organized or modularized, resource planning, etc. 
+// More design, write-up and organizing work are required. 
+// If you are learning to write codes in style, this is not a good example 
+// to follow. 
+//
+// Summary: 
+// The self-powered system monitors temperature and detect motion when
+// attached to the USB host. It is first configured as HID device for
+// the sake of driver-free support. Audio alarm (when 
+// connected an external buzzer at P21) and led will get turned on if 
+// the temperature goes out of monitoring range or motion exceeds
+// detecting sensitivity threshold. 
+// 
+// Two modes: Test mode and Operation mode. In operational mode, 
+// user can lock the device enter a passcode through serial debug 
+// terminal. Motion alarm will continue to be on until the passcode
+// is entered. 
+//
+// An external buzzer is used and connected at P21 and ground. 
+// 
+// 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)
+// 
+// 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
+//          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
 
-TO-DO: 
+TO-FIX: 
+   1. To complete USB connection detection. Currently, system stalls 
+      or fails to respond when detached from a USB host. 
    
-   implement USB connection detection
-   try record sound
-   try gps
-   add menu to LCD
+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
    
 */
 
@@ -42,9 +91,9 @@
 #define LOW_SEN 0.7                     // Low sensitivity
 #define MID_SEN 0.5                     // Medium sensitivity
 #define HIGH_SEN 0.3                    // High sensitivity
-#define BUZZ_POWER 0.5                  // Buzz power level
+#define BUZZ_POWER 1                    // Buzz power level
 #define BUZZ_SPERIOD 0.2                // Security alarm buzz period
-#define BUZZ_TPERIOD 1                // Temperature alarm buzz period
+#define BUZZ_TPERIOD 1                  // Temperature alarm buzz period
 
 // System I/O
 C12832_LCD lcd;
@@ -115,17 +164,11 @@
 
 
 float timeElapsed;
-int tol;                        // Tolerance for hysteresis effect
 time_t seconds;
 
 float potBase1; 
 float potBase2; 
 
-#ifdef C
-#define deg 'C'
-#else 
-#define deg 'F'
-#endif 
 
 typedef struct {
     bool active;
@@ -192,19 +235,6 @@
 }
 
 
-/*    
-    now = time(NULL);
-    if (!activated && now > yet) {
-        sAlarmOn = !sAlarmOn;
-        yet = now + 1;
-        activated = true;
-        //first = true;
-        count++;
-        //printf("sAlarmOn: %d\n\r", sAlarmOn);
-    }
-*/
-
-
 // ISR for joystick up direction
 void ISRJoyUp()
 {
@@ -268,7 +298,7 @@
         activated = false;
 }
  
-// Turn on buzz in different patterns
+// Turn on buzzer in different patterns
 void buzzOn(int pattern, float period) 
 {
     switch (pattern) {
@@ -292,14 +322,6 @@
             break;
 
         }
-    /*
-        led = led + 0.01;
-        wait(0.2);
-        if(led == 1.0) {
-            led = 0;
-        }
-        */
-    
 }
 
 // Turn on led in different patterns
@@ -313,36 +335,21 @@
             wait(0.05);
             break;
         case 1:
+            /*
             for(int p=0; p<10; p += 1) {
                 ledSAlarm = 1;
                 wait(0.02);
                 ledSAlarm = 0;
                 wait(0.02);
             }
-            /*
+            */
             ledSAlarm = 1;
             wait(0.1);
             ledSAlarm = 0;
             wait(0.02);
-            */
             break;
 
         }
-            /*
-            for(float p=0; p<1.0; p += 0.1) {
-                leds = statusAlarm;
-                wait(period);
-            }
-            */
-
-    /*
-        led = led + 0.01;
-        wait(0.2);
-        if(led == 1.0) {
-            led = 0;
-        }
-        */
-    
 }
 
 // Turn off alarm 
@@ -351,6 +358,7 @@
     alarm = 0;
 }
 
+// For testing buzzer. 
 void buzzTest()
 {
     sAlarmOn = OFF; 
@@ -459,7 +467,8 @@
 
 }
 
-// Peep if serial data is received and read if so
+// Peep uart buffer if serial data is available for read
+// Return the entry when hit ENTER button
 void peepEntry()
 {
     int i;
@@ -600,6 +609,9 @@
 
 // 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.
+// To-Do: Correct the logic as status should return alternate results as
+//        CAP LOCK KEY is being hit continuously. 
 void USBConnCheck()
 {
     char status = 0;
@@ -609,11 +621,11 @@
     //{
     //    dev.keyCode(KEY_NUM_LOCK);
     
-    if (dev.keyCode(KEY_CAPS_LOCK))
-            printf("\rSend KEY_CAPS_LOCK OK\n\r");
+    dev.keyCode(KEY_CAPS_LOCK);
+    //if (dev.keyCode(KEY_CAPS_LOCK))
+    //        printf("\rSend KEY_CAPS_LOCK OK\n\r");
     
     status = dev.lockStatus();
-    //printf("1: lockStatus = 0x%x statusAlarm = 0x%x\n\r", status, statusAlarm);
     if (!(statusAlarm & 0x80000000) && (status & 0x2))
     {
         statusAlarm |= 0x80000000; 
@@ -793,9 +805,8 @@
     IRQJoyRight.rise(&ISRJoyRight);    
     timer1.attach(&DisplayLCD, 1);
     timer3.attach(&debounce, 1);
-    //timer4.attach(&USBConnCheck, 2);
-    timer4.attach(&AlarmCheck, 1);
-    //timer2.attach(&ReadTemp, 5);
+    //timer4.attach(&USBConnCheck, 2);  // Cannot use with ticker
+    //timer4.attach(&AlarmCheck, 1);    // Cannot use or it won't respond external events
     sHealthFeatureActive = OFF;
     sSecurityFeatureActive = OFF;
     sAlarmOn = OFF;
@@ -824,7 +835,7 @@
 {
     while(1) 
     {
-        //USBConnCheck();
+        USBConnCheck();
         
         // Override temperature read for testing. Use pot to control temperature
         if (!tempOverride)
@@ -854,7 +865,7 @@
  
         MotionMonitor();        
         TempMonitor();
-        //AlarmCheck();
+        AlarmCheck();
         
         /* Live check
         lockLed = 1;