Smart-Home-TX Test

Dependencies:   4DGL-uLCD-SE PinDetect

Fork of Xbee-Smart-Home-Outside by prana koirala

Revision:
4:f87a36946c09
Parent:
3:80ca319bd924
Child:
5:65fd90a3352d
diff -r 80ca319bd924 -r f87a36946c09 main.cpp
--- a/main.cpp	Wed Apr 26 14:52:11 2017 +0000
+++ b/main.cpp	Tue May 02 20:37:18 2017 +0000
@@ -4,7 +4,6 @@
 #include "uLCD_4DGL.h"
 #include "rtos.h"
 #include "PinDetect.h"
-// #include "jpegutil.h"
 
 Serial pc(USBTX, USBRX);
 uLCD_4DGL lcd(p28,p27,p24);
@@ -14,38 +13,42 @@
 DigitalOut led2(LED2);
 PinDetect getStatus(p16);
 PinDetect toggleLight(p17);
-
-// LocalFileSystem local("local");  /
+PinDetect camPic(p18);
+PwmOut speaker(p21);
 
 volatile bool statusReq = false;
 volatile bool flipLed = false;
+volatile bool cameraReq = false;
 
 Thread t1;
-// Mutex serialMutex;
 
 char buffer[50];
 
 void statusRequest()
 {
-    led1 = 1;
     statusReq = true;
 }
 
 void flipLight()
 {
-    led2 = 1;
     flipLed = true;
 }
 
+void cameraTakePic()
+{
+    cameraReq = true;
+}
+
 void sendcommand(char out)
 {
-    if(xbee.writeable()) {
-        led2 = 1;
-        char outbuf = out;
-        xbee.putc(outbuf);
-        pc.putc(outbuf);
-        led2 = 0;
-    }
+        if(xbee.writeable()) {
+            led2 = 1;
+            char outbuf = out;
+            xbee.putc(outbuf);
+            pc.putc(outbuf);
+            outbuf = ' ';
+            led2 = 0;
+        }
 }
 
 void getcommand()
@@ -59,18 +62,50 @@
                 x++;
             }
             led1 = 0;
-            const char s[2] = "|";
-            char *token;
-            token = strtok(buffer, s); // get the first token 
-            int j = 2;
-            while( token != NULL ) { // walk through other tokens 
-                lcd.locate(11,j);   // Print in correct place of LCD
-                lcd.printf( "%s", token );
-                token = strtok(NULL, s);
-                j++;
+            bool statusmsg = true;
+            // pc.printf(buffer);
+            if(buffer[0] == 'p') {
+                lcd.locate(0, 6);
+                char msg[] = "Pic Taken,See Mbed";
+                lcd.puts(msg);
+                statusmsg = false;
+            }
+            else if (buffer[0] == 'i'){
+                lcd.locate(0,6);
+                char msg[] = "Intrusion Detected";
+                lcd.puts(msg);
+                lcd.media_init(); // initialize uSD card
+                lcd.set_sector_address(0,0);  // address of font file
+                lcd.media_init();
+                lcd.display_image(0,60);
+                Thread::wait(4000);
+                statusmsg = false;
+                lcd.filled_rectangle(0, 48, 128, 128, BLACK);
+            } else if(buffer[0] == 'u'){
+                lcd.locate(0,6);
+                char msg[] = "Door Unlocked";
+                lcd.puts(msg);
+                Thread::wait(4000);
+                lcd.locate(0,6);
+                for(int i = 0; i < 18; ++i){
+                    msg[i] = ' ';
+                }
+                lcd.puts(msg);
+                statusmsg = false;
+            }
+            if(statusmsg == true) {
+                const char s[2] = "|"; // s[2]
+                char *token;
+                token = strtok(buffer, s); // get the first token
+                int j = 2;
+                while( token != NULL ) { // walk through other tokens
+                    lcd.locate(11,j);   // Print in correct place of LCD
+                    lcd.puts(token );
+                    token = strtok(NULL, s);
+                    j++;
+                }
             }
         }
-        Thread::wait(1000);
     }
 }
 
@@ -80,16 +115,19 @@
     wait_ms(1);
     reset = 1;
     wait_ms(1);
-    
+
     getStatus.mode(PullDown);
     getStatus.attach_asserted( &statusRequest );
-    getStatus.setSampleFrequency(); // Defaults to 20ms.
-    
+    getStatus.setSampleFrequency();
+
     toggleLight.mode(PullDown);
     toggleLight.attach_asserted( &flipLight );
-    toggleLight.setSampleFrequency(); // Defaults to 20ms.
+    toggleLight.setSampleFrequency();
 
-    // lcd.baudrate(300000);
+    camPic.mode(PullDown);
+    camPic.attach_asserted( &cameraTakePic );
+    camPic.setSampleFrequency();
+
     lcd.cls();
     lcd.locate(0,0);
     lcd.line(0, 5, 128, 5, 0xFF0000);
@@ -99,10 +137,8 @@
     lcd.printf("Humidity %:\r\n");
     lcd.printf("Lights: \r\n");
     lcd.line(0, 45, 128, 45, 0xFF0000);
-    // xbee.baud(115200); // May need to latter
     t1.start(getcommand);
-    wait(0.5);
-    sendcommand('s');
+    statusReq = true;
 
     while(1) {
         if (statusReq == true) {
@@ -111,24 +147,10 @@
         } else if (flipLed == true) {
             sendcommand('l');
             flipLed = false;
+        } else if(cameraReq == true) {
+            sendcommand('c');
+            cameraReq = false;
         }
         Thread::wait(2000);
-
     }
-}
-
-/*
-        string file = "/local/PICT003.jpg";
-        lcd.locate(0, 6);
-        ReadJPEGFromFile(file.c_str());
-        lcd.printf("Image Displaying:\r\n");
-        lcd.BLIT(0, 60, 60, 80, &color[0]);
-        led1 = 1;
-
-        BLIT(int x, int y, int w, int h, int *colors)
-        x   is the left-edge of the region.
-        y   is the top-edge of the region.
-        w   specifies the width of the region.
-        h   specifies the height of the region.
-        colour  is a pointer to the array with size = w * h.
-*/
\ No newline at end of file
+}
\ No newline at end of file