A security system that is activated after the object is removed from the box.

Dependencies:   4DGL-uLCD-SE Camera_LS_Y201 FatFileSystem SDFileSystem mbed wave_player

Committer:
tdouglas6
Date:
Wed Dec 10 02:42:58 2014 +0000
Revision:
0:92cf9c40d7eb
A program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tdouglas6 0:92cf9c40d7eb 1 #include "mbed.h"
tdouglas6 0:92cf9c40d7eb 2 #include "Camera_LS_Y201.h"
tdouglas6 0:92cf9c40d7eb 3 #include "SDFileSystem.h"
tdouglas6 0:92cf9c40d7eb 4 #include "wave_player.h"
tdouglas6 0:92cf9c40d7eb 5 #include "uLCD_4DGL.h"
tdouglas6 0:92cf9c40d7eb 6 #include <stdio.h>
tdouglas6 0:92cf9c40d7eb 7
tdouglas6 0:92cf9c40d7eb 8 #define DEBMSG printf
tdouglas6 0:92cf9c40d7eb 9 #define DEBUG printf("Bug me")
tdouglas6 0:92cf9c40d7eb 10 #define NEWLINE() printf("\r\n")
tdouglas6 0:92cf9c40d7eb 11
tdouglas6 0:92cf9c40d7eb 12 #define USE_SDCARD 0
tdouglas6 0:92cf9c40d7eb 13
tdouglas6 0:92cf9c40d7eb 14
tdouglas6 0:92cf9c40d7eb 15 #define FILENAME "/sd/IMG_%04d.jpg"
tdouglas6 0:92cf9c40d7eb 16 SDFileSystem fs(p5, p6, p7, p8, "sd");
tdouglas6 0:92cf9c40d7eb 17
tdouglas6 0:92cf9c40d7eb 18 //#define FILENAME "/local/IMG_%04d.jpg"
tdouglas6 0:92cf9c40d7eb 19 //LocalFileSystem fs("local");
tdouglas6 0:92cf9c40d7eb 20
tdouglas6 0:92cf9c40d7eb 21 Camera_LS_Y201 cam1(p13, p14);
tdouglas6 0:92cf9c40d7eb 22
tdouglas6 0:92cf9c40d7eb 23 AnalogIn sensor(p19);
tdouglas6 0:92cf9c40d7eb 24 DigitalOut led(LED1);
tdouglas6 0:92cf9c40d7eb 25 uLCD_4DGL uLCD(p28,p27,p11); // serial tx, serial rx, reset pin;
tdouglas6 0:92cf9c40d7eb 26 AnalogOut DACout(p18);
tdouglas6 0:92cf9c40d7eb 27 wave_player waver(&DACout);
tdouglas6 0:92cf9c40d7eb 28 Serial pc(USBTX, USBRX);
tdouglas6 0:92cf9c40d7eb 29 Serial device(p9,p10);
tdouglas6 0:92cf9c40d7eb 30
tdouglas6 0:92cf9c40d7eb 31 typedef struct work {
tdouglas6 0:92cf9c40d7eb 32 FILE *fp;
tdouglas6 0:92cf9c40d7eb 33 } work_t;
tdouglas6 0:92cf9c40d7eb 34
tdouglas6 0:92cf9c40d7eb 35 work_t work;
tdouglas6 0:92cf9c40d7eb 36
tdouglas6 0:92cf9c40d7eb 37 void regularSub() {
tdouglas6 0:92cf9c40d7eb 38
tdouglas6 0:92cf9c40d7eb 39 }
tdouglas6 0:92cf9c40d7eb 40 void microInterrupt() {
tdouglas6 0:92cf9c40d7eb 41 int strikes = 0;
tdouglas6 0:92cf9c40d7eb 42 char rchar=0;
tdouglas6 0:92cf9c40d7eb 43 //wake up device - needs more work and a timeout
tdouglas6 0:92cf9c40d7eb 44 device.putc('b');
tdouglas6 0:92cf9c40d7eb 45 while (device.getc()!='o') {
tdouglas6 0:92cf9c40d7eb 46 DEBMSG("First While loop.");
tdouglas6 0:92cf9c40d7eb 47 NEWLINE();
tdouglas6 0:92cf9c40d7eb 48 device.putc('b');
tdouglas6 0:92cf9c40d7eb 49 wait(0.2);
tdouglas6 0:92cf9c40d7eb 50 }
tdouglas6 0:92cf9c40d7eb 51 while (strikes<=3) {
tdouglas6 0:92cf9c40d7eb 52 DEBMSG("Second While loop.");
tdouglas6 0:92cf9c40d7eb 53 NEWLINE();
tdouglas6 0:92cf9c40d7eb 54 device.putc('i'); //Start Recognition
tdouglas6 0:92cf9c40d7eb 55 device.putc('B'); //Use Wordset 3 - the numbers 1..10
tdouglas6 0:92cf9c40d7eb 56 //Use built-in speaker independent numbers (0..10) and listen for a number
tdouglas6 0:92cf9c40d7eb 57 while (device.readable()!=0) {DEBMSG("Third While loop.");
tdouglas6 0:92cf9c40d7eb 58 NEWLINE();}
tdouglas6 0:92cf9c40d7eb 59 if (device.getc()=='s') {
tdouglas6 0:92cf9c40d7eb 60 device.putc(' ');
tdouglas6 0:92cf9c40d7eb 61 rchar=device.getc();
tdouglas6 0:92cf9c40d7eb 62 if (rchar=='G') {DEBMSG("Correct Command.");
tdouglas6 0:92cf9c40d7eb 63 //strikes--;
tdouglas6 0:92cf9c40d7eb 64 NEWLINE();
tdouglas6 0:92cf9c40d7eb 65 uLCD.text_width(2); //4X size text
tdouglas6 0:92cf9c40d7eb 66 uLCD.text_height(2);
tdouglas6 0:92cf9c40d7eb 67 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 68 uLCD.color(GREEN);
tdouglas6 0:92cf9c40d7eb 69 uLCD.printf("Correct Command");
tdouglas6 0:92cf9c40d7eb 70 wait(5);
tdouglas6 0:92cf9c40d7eb 71 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 72 wait(2);
tdouglas6 0:92cf9c40d7eb 73 uLCD.color(RED);
tdouglas6 0:92cf9c40d7eb 74 uLCD.text_width(3); //4X size text
tdouglas6 0:92cf9c40d7eb 75 uLCD.text_height(3);
tdouglas6 0:92cf9c40d7eb 76 uLCD.printf("Alarm is turn off"); //Default Green on black text
tdouglas6 0:92cf9c40d7eb 77 strikes = 4;
tdouglas6 0:92cf9c40d7eb 78 }
tdouglas6 0:92cf9c40d7eb 79 else {
tdouglas6 0:92cf9c40d7eb 80 strikes++;
tdouglas6 0:92cf9c40d7eb 81 DEBMSG("Wrong Command.");
tdouglas6 0:92cf9c40d7eb 82 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 83 uLCD.text_width(2); //4X size text
tdouglas6 0:92cf9c40d7eb 84 uLCD.text_height(2);
tdouglas6 0:92cf9c40d7eb 85 uLCD.color(RED);
tdouglas6 0:92cf9c40d7eb 86 uLCD.printf("Wrong Command");
tdouglas6 0:92cf9c40d7eb 87 wait(5);
tdouglas6 0:92cf9c40d7eb 88 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 89 NEWLINE();
tdouglas6 0:92cf9c40d7eb 90 while(strikes==4){
tdouglas6 0:92cf9c40d7eb 91 DEBMSG("buzzing While loop.");
tdouglas6 0:92cf9c40d7eb 92 NEWLINE();
tdouglas6 0:92cf9c40d7eb 93 FILE *wave_file;
tdouglas6 0:92cf9c40d7eb 94 wave_file=fopen("/sd/buzzer2.wav","r");
tdouglas6 0:92cf9c40d7eb 95 waver.play(wave_file);
tdouglas6 0:92cf9c40d7eb 96 fclose(wave_file);
tdouglas6 0:92cf9c40d7eb 97 }
tdouglas6 0:92cf9c40d7eb 98 }
tdouglas6 0:92cf9c40d7eb 99
tdouglas6 0:92cf9c40d7eb 100 }
tdouglas6 0:92cf9c40d7eb 101 }
tdouglas6 0:92cf9c40d7eb 102 }
tdouglas6 0:92cf9c40d7eb 103 /**
tdouglas6 0:92cf9c40d7eb 104 * Callback function for readJpegFileContent.
tdouglas6 0:92cf9c40d7eb 105 *
tdouglas6 0:92cf9c40d7eb 106 * @param buf A pointer to a buffer.
tdouglas6 0:92cf9c40d7eb 107 * @param siz A size of the buffer.
tdouglas6 0:92cf9c40d7eb 108 */
tdouglas6 0:92cf9c40d7eb 109 void callback_func(int done, int total, uint8_t *buf, size_t siz) {
tdouglas6 0:92cf9c40d7eb 110 fwrite(buf, siz, 1, work.fp);
tdouglas6 0:92cf9c40d7eb 111
tdouglas6 0:92cf9c40d7eb 112 static int n = 0;
tdouglas6 0:92cf9c40d7eb 113 int tmp = done * 100 / total;
tdouglas6 0:92cf9c40d7eb 114 if (n != tmp) {
tdouglas6 0:92cf9c40d7eb 115 n = tmp;
tdouglas6 0:92cf9c40d7eb 116 DEBMSG("Writing...: %3d%%", n);
tdouglas6 0:92cf9c40d7eb 117 NEWLINE();
tdouglas6 0:92cf9c40d7eb 118 }
tdouglas6 0:92cf9c40d7eb 119 }
tdouglas6 0:92cf9c40d7eb 120
tdouglas6 0:92cf9c40d7eb 121 /**
tdouglas6 0:92cf9c40d7eb 122 * Capture.
tdouglas6 0:92cf9c40d7eb 123 *
tdouglas6 0:92cf9c40d7eb 124 * @param cam A pointer to a camera object.
tdouglas6 0:92cf9c40d7eb 125 * @param filename The file name.
tdouglas6 0:92cf9c40d7eb 126 *
tdouglas6 0:92cf9c40d7eb 127 * @return Return 0 if it succeed.
tdouglas6 0:92cf9c40d7eb 128 */
tdouglas6 0:92cf9c40d7eb 129 int capture(Camera_LS_Y201 *cam, char *filename) {
tdouglas6 0:92cf9c40d7eb 130 /*
tdouglas6 0:92cf9c40d7eb 131 * Take a picture.
tdouglas6 0:92cf9c40d7eb 132 */
tdouglas6 0:92cf9c40d7eb 133 if (cam->takePicture() != 0) {
tdouglas6 0:92cf9c40d7eb 134 return -1;
tdouglas6 0:92cf9c40d7eb 135 }
tdouglas6 0:92cf9c40d7eb 136 DEBMSG("Captured.");
tdouglas6 0:92cf9c40d7eb 137 NEWLINE();
tdouglas6 0:92cf9c40d7eb 138
tdouglas6 0:92cf9c40d7eb 139 /*
tdouglas6 0:92cf9c40d7eb 140 * Open file.
tdouglas6 0:92cf9c40d7eb 141 */
tdouglas6 0:92cf9c40d7eb 142 work.fp = fopen(filename, "wb");
tdouglas6 0:92cf9c40d7eb 143 if (work.fp == NULL) {
tdouglas6 0:92cf9c40d7eb 144 return -2;
tdouglas6 0:92cf9c40d7eb 145 }
tdouglas6 0:92cf9c40d7eb 146
tdouglas6 0:92cf9c40d7eb 147 /*
tdouglas6 0:92cf9c40d7eb 148 * Read the content.
tdouglas6 0:92cf9c40d7eb 149 */
tdouglas6 0:92cf9c40d7eb 150 DEBMSG("%s", filename);
tdouglas6 0:92cf9c40d7eb 151 NEWLINE();
tdouglas6 0:92cf9c40d7eb 152 if (cam->readJpegFileContent(callback_func) != 0) {
tdouglas6 0:92cf9c40d7eb 153 fclose(work.fp);
tdouglas6 0:92cf9c40d7eb 154 return -3;
tdouglas6 0:92cf9c40d7eb 155 }
tdouglas6 0:92cf9c40d7eb 156 fclose(work.fp);
tdouglas6 0:92cf9c40d7eb 157
tdouglas6 0:92cf9c40d7eb 158 /*
tdouglas6 0:92cf9c40d7eb 159 * Stop taking pictures.
tdouglas6 0:92cf9c40d7eb 160 */
tdouglas6 0:92cf9c40d7eb 161 cam->stopTakingPictures();
tdouglas6 0:92cf9c40d7eb 162
tdouglas6 0:92cf9c40d7eb 163 return 0;
tdouglas6 0:92cf9c40d7eb 164 }
tdouglas6 0:92cf9c40d7eb 165
tdouglas6 0:92cf9c40d7eb 166 /**
tdouglas6 0:92cf9c40d7eb 167 * Entry point.
tdouglas6 0:92cf9c40d7eb 168 */
tdouglas6 0:92cf9c40d7eb 169 int main(void) {
tdouglas6 0:92cf9c40d7eb 170 while(1){
tdouglas6 0:92cf9c40d7eb 171 DEBMSG("Checking again");
tdouglas6 0:92cf9c40d7eb 172 NEWLINE();
tdouglas6 0:92cf9c40d7eb 173 if(sensor > 0.3) {
tdouglas6 0:92cf9c40d7eb 174 //pc.printf("The distance is %d\n",sensor);
tdouglas6 0:92cf9c40d7eb 175 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 176 wait(2);
tdouglas6 0:92cf9c40d7eb 177 uLCD.color(RED);
tdouglas6 0:92cf9c40d7eb 178 uLCD.text_width(3); //4X size text
tdouglas6 0:92cf9c40d7eb 179 uLCD.text_height(3);
tdouglas6 0:92cf9c40d7eb 180 uLCD.printf("Alarm is turn off"); //Default Green on black text
tdouglas6 0:92cf9c40d7eb 181 } else {
tdouglas6 0:92cf9c40d7eb 182 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 183 uLCD.color(GREEN);
tdouglas6 0:92cf9c40d7eb 184 uLCD.text_width(3); //4X size text
tdouglas6 0:92cf9c40d7eb 185 uLCD.text_height(3);
tdouglas6 0:92cf9c40d7eb 186 uLCD.printf("Alarm is turn on");
tdouglas6 0:92cf9c40d7eb 187 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 188 uLCD.printf("A picture is taken now");
tdouglas6 0:92cf9c40d7eb 189 wait(3);
tdouglas6 0:92cf9c40d7eb 190 uLCD.cls();
tdouglas6 0:92cf9c40d7eb 191 uLCD.printf("Alarm is turn on");
tdouglas6 0:92cf9c40d7eb 192 DEBMSG("Camera module");
tdouglas6 0:92cf9c40d7eb 193 NEWLINE();
tdouglas6 0:92cf9c40d7eb 194 DEBMSG("Resetting...");
tdouglas6 0:92cf9c40d7eb 195 NEWLINE();
tdouglas6 0:92cf9c40d7eb 196 wait(1);
tdouglas6 0:92cf9c40d7eb 197
tdouglas6 0:92cf9c40d7eb 198 if (cam1.reset() == 0) {
tdouglas6 0:92cf9c40d7eb 199 DEBMSG("Reset OK.");
tdouglas6 0:92cf9c40d7eb 200 NEWLINE();
tdouglas6 0:92cf9c40d7eb 201 } else {
tdouglas6 0:92cf9c40d7eb 202 DEBMSG("Reset fail.");
tdouglas6 0:92cf9c40d7eb 203 NEWLINE();
tdouglas6 0:92cf9c40d7eb 204 error("Reset fail.");
tdouglas6 0:92cf9c40d7eb 205 }
tdouglas6 0:92cf9c40d7eb 206 wait(1);
tdouglas6 0:92cf9c40d7eb 207
tdouglas6 0:92cf9c40d7eb 208 int cnt = 0;
tdouglas6 0:92cf9c40d7eb 209 char fname[64];
tdouglas6 0:92cf9c40d7eb 210 snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
tdouglas6 0:92cf9c40d7eb 211 int r = capture(&cam1, fname);
tdouglas6 0:92cf9c40d7eb 212 if (r == 0) {
tdouglas6 0:92cf9c40d7eb 213 DEBMSG("[%04d]:OK.", cnt);
tdouglas6 0:92cf9c40d7eb 214 NEWLINE();
tdouglas6 0:92cf9c40d7eb 215 } else {
tdouglas6 0:92cf9c40d7eb 216 DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
tdouglas6 0:92cf9c40d7eb 217 NEWLINE();
tdouglas6 0:92cf9c40d7eb 218 error("Failure.");
tdouglas6 0:92cf9c40d7eb 219 }
tdouglas6 0:92cf9c40d7eb 220 cnt++;
tdouglas6 0:92cf9c40d7eb 221
tdouglas6 0:92cf9c40d7eb 222 microInterrupt();
tdouglas6 0:92cf9c40d7eb 223 }
tdouglas6 0:92cf9c40d7eb 224 }
tdouglas6 0:92cf9c40d7eb 225
tdouglas6 0:92cf9c40d7eb 226 }