The receiver's code for Home Security with RF

Dependencies:   Camera_LS_Y201 SDFileSystem Servo hmac_md5 mbed

Files at this revision

API Documentation at this revision

Comitter:
jsobchuk3
Date:
Mon Dec 08 22:42:21 2014 +0000
Commit message:
ECE 4180 Final

Changed in this revision

Camera_LS_Y201.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show annotated file Show diff for this revision Revisions of this file
camera.cpp Show annotated file Show diff for this revision Revisions of this file
hmac_md5.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r bbfc7c781872 Camera_LS_Y201.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Camera_LS_Y201.lib	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/shintamainjp/code/Camera_LS_Y201/#43358d40f879
diff -r 000000000000 -r bbfc7c781872 SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
diff -r 000000000000 -r bbfc7c781872 Servo.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r bbfc7c781872 Speaker.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,52 @@
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+// precompute 32 sample points on one sine wave cycle
+// used for continuous sine wave output later
+        for(int k=0; k<32; k++) {
+            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
+            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
+        }
+
+    }
+// class method to play a note based on AnalogOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        // scale samples using current volume level arg
+        for(int k=0; k<32; k++) {
+            Analog_scaled_data[k] = Analog_out_data[k] * volume;
+        }
+        // reset to start of sample array
+        i=0;
+        // turn on timer interrupts to start sine wave output
+        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
+        // play note for specified time
+        wait(duration);
+        // turns off timer interrupts
+        Sample_Period.detach();
+        // sets output to mid range - analog zero
+        this->_pin.write_u16(32768);
+
+    }
+private:
+// sets up specified pin for analog using AnalogOut class
+    AnalogOut _pin;
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;
+
+    //variables used by interrupt routine and PlayNote
+    volatile int i;
+    short unsigned Analog_out_data[32];
+    short unsigned Analog_scaled_data[32];
+
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+    void Sample_timer_interrupt(void) {
+        // send next analog sample out to D to A
+        this->_pin.write_u16(Analog_scaled_data[i]);
+        // increment pointer and wrap around back to 0 at 32
+        i = (i+1) & 0x01F;
+    }
+};
+
diff -r 000000000000 -r bbfc7c781872 camera.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/camera.cpp	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,125 @@
+#include "mbed.h"
+#include "Camera_LS_Y201.h"
+#include "SDFileSystem.h"
+ 
+#define DEBMSG      printf
+#define NEWLINE()   printf("\r\n")
+ 
+#define USE_SDCARD 1
+ 
+#if USE_SDCARD
+#define FILENAME    "/sd/IMG_%04d.jpg"
+SDFileSystem fs(p5, p6, p7, p8, "sd");
+#else
+#define FILENAME    "/local/IMG_%04d.jpg"
+LocalFileSystem fs("local");
+#endif
+Camera_LS_Y201 cam1(p13, p14);
+ 
+typedef struct work {
+    FILE *fp;
+} work_t;
+ 
+work_t work;
+ 
+/**
+ * Callback function for readJpegFileContent.
+ *
+ * @param buf A pointer to a buffer.
+ * @param siz A size of the buffer.
+ */
+void callback_func(int done, int total, uint8_t *buf, size_t siz) {
+    fwrite(buf, siz, 1, work.fp);
+ 
+    static int n = 0;
+    int tmp = done * 100 / total;
+    if (n != tmp) {
+        n = tmp;
+        //DEBMSG("Writing...: %3d%%", n);
+        //NEWLINE();
+    }
+}
+ 
+/**
+ * Capture.
+ *
+ * @param cam A pointer to a camera object.
+ * @param filename The file name.
+ *
+ * @return Return 0 if it succeed.
+ */
+int capture(Camera_LS_Y201 *cam, char *filename) {
+    /*
+     * Take a picture.
+     */
+    if (cam->takePicture() != 0) {
+        return -1;
+    }
+    DEBMSG("Captured.");
+    NEWLINE();
+ 
+    /*
+     * Open file.
+     */
+    work.fp = fopen(filename, "wb");
+    if (work.fp == NULL) {
+        return -2;
+    }
+ 
+    /*
+     * Read the content.
+     */
+    DEBMSG("%s", filename);
+    NEWLINE();
+    if (cam->readJpegFileContent(callback_func) != 0) {
+        fclose(work.fp);
+        return -3;
+    }
+    fclose(work.fp);
+ 
+    /*
+     * Stop taking pictures.
+     */
+    cam->stopTakingPictures();
+ 
+    return 0;
+}
+ 
+/**
+ * Entry point.
+ */
+int camInit(void) {
+    DEBMSG("Camera module");
+    NEWLINE();
+    DEBMSG("Resetting...");
+    NEWLINE();
+    wait(1);
+ 
+    if (cam1.reset() == 0) {
+        DEBMSG("Reset OK.");
+        NEWLINE();
+    } else {
+        DEBMSG("Reset fail.");
+        NEWLINE();
+        error("Reset fail.");
+    }
+    wait(1);
+}
+ int takepic(int cnt) {
+    //int cnt = 0;
+    //while (1) {
+        char fname[64];
+        snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
+        int r = capture(&cam1, fname);
+        if (r == 0) {
+            DEBMSG("[%04d]:OK.", cnt);
+            NEWLINE();
+        } else {
+            DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
+            NEWLINE();
+            error("Failure.");
+        }
+        //cnt++;
+    //}
+    return 1;
+}
\ No newline at end of file
diff -r 000000000000 -r bbfc7c781872 hmac_md5.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hmac_md5.lib	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/igrokhotkov/code/hmac_md5/#83f3dcfa5c8f
diff -r 000000000000 -r bbfc7c781872 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,158 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "hmac_md5.h"
+#include "Camera_LS_Y201.h"
+#include "Speaker.h"
+AnalogIn ir(p20);
+DigitalOut myled2(LED2);
+Serial pc(USBTX,USBRX);
+Serial rf(p9,p10);
+Servo myservo(p23);
+Speaker mySpeaker(p18);
+Timer t;
+int takepic(int);
+int camInit(void);
+
+int main() {
+    pc.baud(1200);
+    rf.baud(1200);
+    char code = 0;
+    char codebuf[32];
+    int cnt = 0;
+    int unlocked = 0;
+    int matching = 0;
+    
+    int i =0;
+     char key[4] = "Dhr";
+    for (i = 0; i < 4; i++) {
+        key[i] = (key[i] + 5); 
+    }
+    char key2[4] = {0};
+    key2[0] = key[0];
+    key2[1] = key[1];
+    key2[2] = key[2];
+    for (i = 0; i < 4; i++) {
+        key2[i] = (key2[i] + 5); 
+    }
+    char key3[4] = {0};
+    key3[0] = key2[0];
+    key3[1] = key2[1];
+    key3[2] = key2[2];
+    for (i = 0; i < 4; i++) {
+        key3[i] = (key3[i] + 5); 
+    }
+
+    char received[3] = {0};
+    
+    camInit(); 
+    
+    while(1) {
+        if (rf.readable()) {
+            myled2 = 1;
+            matching = 0;
+            
+            //Ignore Sync pattern and do not pass on to PC
+                int i = 0;
+                
+                //Clear the buffer
+                for(i = 0; i < 32; i++) {
+                    codebuf[i] = 0;
+                }
+                
+                for(i = 0; i < 32; i++) {
+                    code = rf.getc(); 
+                    if(code != 0xAA) {
+                        codebuf[i] = code;
+                        pc.printf("codebuf[%d] = %c\r\n", i, codebuf[i]);
+                    }
+                }
+                
+                for(i = 0; i < 30; i++) {
+                    if(codebuf[i] == key[0]) {
+                        if(codebuf[i+1] == key[1]) {
+                            if(codebuf[i+2] == key[2]) {
+                                received[0] = codebuf[i];
+                                received[1] = codebuf[i+1];
+                                received[2] = codebuf[i+2];
+                                matching = 1;
+                                break;
+                            }
+                        }
+                    }
+                    if(codebuf[i] == key2[0]) {
+                        if(codebuf[i+1] == key2[1]) {
+                            if(codebuf[i+2] == key2[2]) {
+                                received[0] = codebuf[i];
+                                received[1] = codebuf[i+1];
+                                received[2] = codebuf[i+2];
+                                matching = 2;
+                                break;
+                            }
+                        }
+                    }
+                    if(codebuf[i] == key3[0]) {
+                        if(codebuf[i+1] == key3[1]) {
+                            if(codebuf[i+2] == key3[2]) {
+                                received[0] = codebuf[i];
+                                received[1] = codebuf[i+1];
+                                received[2] = codebuf[i+2];
+                                matching = 3;
+                                break;
+                            }
+                        }
+                    }
+                }
+             
+                myled2 = 0;
+            if(matching) {
+
+                pc.printf("Unlocking door\r\n");
+                mySpeaker.PlayNote(800.0, 0.5, 1.0);
+                // Turn servo
+                myservo = unlocked;
+                wait(1);
+                unlocked = !unlocked;
+                
+                if (matching  == 1) {
+                    for (i = 0; i < 4; i++) {
+                        key[i] = (key[i] + 5); 
+                    }
+                }
+                if (matching  == 2) {
+                    for (i = 0; i < 4; i++) {
+                        key[i] = (key2[i] + 5); 
+                    }
+                }
+                if (matching  == 3) {
+                    for (i = 0; i < 4; i++) {
+                        key[i] = (key3[i] + 5); 
+                    }
+                }
+                
+                    key2[0] = key[0];
+                    key2[1] = key[1];
+                    key2[2] = key[2];
+                    
+                    for (i = 0; i < 4; i++) {
+                        key2[i] = (key2[i] + 5); 
+                    }
+                    
+                    
+                    key3[0] = key2[0];
+                    key3[1] = key2[1];
+                    key3[2] = key2[2];
+                    for (i = 0; i < 4; i++) {
+                        key3[i] = (key3[i] + 5); 
+                    }
+
+                takepic(cnt);
+
+                cnt++;
+            }
+            
+            
+            
+          
+        }
+    } 
+}
diff -r 000000000000 -r bbfc7c781872 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Dec 08 22:42:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file