Smart Miniature House / Mbed OS Xbee-Smart-Home-Outside

Dependencies:   4DGL-uLCD-SE PinDetect

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 #include <stdio.h>
00004 #include "uLCD_4DGL.h"
00005 #include "rtos.h"
00006 #include "PinDetect.h"
00007 
00008 Serial pc(USBTX, USBRX);
00009 uLCD_4DGL lcd(p28,p27,p24);
00010 Serial xbee(p9, p10);
00011 DigitalOut reset(p8);
00012 DigitalOut led1(LED1);
00013 DigitalOut led2(LED2);
00014 PinDetect getStatus(p16);
00015 PinDetect toggleLight(p17);
00016 PinDetect camPic(p18);
00017 
00018 volatile bool statusReq = false;
00019 volatile bool flipLed = false;
00020 volatile bool cameraReq = false;
00021 
00022 Thread t1;
00023 
00024 char buffer[50];
00025 
00026 void statusRequest()
00027 {
00028     statusReq = true;
00029 }
00030 
00031 void flipLight()
00032 {
00033     flipLed = true;
00034 }
00035 
00036 void cameraTakePic()
00037 {
00038     cameraReq = true;
00039 }
00040 
00041 void sendcommand(char out)
00042 {
00043         if(xbee.writeable()) {
00044             led2 = 1;
00045             char outbuf = out;
00046             xbee.putc(outbuf);
00047             pc.putc(outbuf);
00048             outbuf = ' ';
00049             led2 = 0;
00050         }
00051 }
00052 
00053 void getcommand()
00054 {
00055     while(1) {
00056         if(xbee.readable()) {
00057             led1 = 1;
00058             int x = 0;
00059             while(xbee.readable()) {
00060                 buffer[x] = xbee.getc();
00061                 x++;
00062             }
00063             led1 = 0;
00064             bool statusmsg = true;
00065             // pc.printf(buffer);
00066             if(buffer[0] == 'p') {
00067                 lcd.locate(0, 6);
00068                 char msg[] = "Pic Taken,See Mbed";
00069                 lcd.puts(msg);
00070                 statusmsg = false;
00071             }
00072             else if (buffer[0] == 'i'){
00073                 lcd.locate(0,6);
00074                 char msg[] = "Intrusion Detected";
00075                 lcd.puts(msg);
00076                 lcd.media_init(); // initialize uSD card
00077                 lcd.set_sector_address(0,0);  // address of font file
00078                 lcd.media_init();
00079                 lcd.display_image(0,60);
00080                 Thread::wait(4000);
00081                 statusmsg = false;
00082                 lcd.filled_rectangle(0, 48, 128, 128, BLACK);
00083             } else if(buffer[0] == 'u'){
00084                 lcd.locate(0,6);
00085                 char msg[] = "Door Unlocked";
00086                 lcd.puts(msg);
00087                 Thread::wait(4000);
00088                 lcd.locate(0,6);
00089                 for(int i = 0; i < 18; ++i){
00090                     msg[i] = ' ';
00091                 }
00092                 lcd.puts(msg);
00093                 statusmsg = false;
00094             }
00095             if(statusmsg == true) {
00096                 const char s[2] = "|"; // s[2]
00097                 char *token;
00098                 token = strtok(buffer, s); // get the first token
00099                 int j = 2;
00100                 while( token != NULL ) { // walk through other tokens
00101                     lcd.locate(11,j);   // Print in correct place of LCD
00102                     lcd.puts(token );
00103                     token = strtok(NULL, s);
00104                     j++;
00105                 }
00106             }
00107         }
00108     }
00109 }
00110 
00111 int main()
00112 {
00113     reset = 0;
00114     wait_ms(1);
00115     reset = 1;
00116     wait_ms(1);
00117 
00118     getStatus.mode(PullDown);
00119     getStatus.attach_asserted( &statusRequest );
00120     getStatus.setSampleFrequency();
00121 
00122     toggleLight.mode(PullDown);
00123     toggleLight.attach_asserted( &flipLight );
00124     toggleLight.setSampleFrequency();
00125 
00126     camPic.mode(PullDown);
00127     camPic.attach_asserted( &cameraTakePic );
00128     camPic.setSampleFrequency();
00129 
00130     lcd.cls();
00131     lcd.locate(0,0);
00132     lcd.line(0, 5, 128, 5, 0xFF0000);
00133     lcd.printf("\r\nStatus:");
00134     lcd.printf("\r\n");
00135     lcd.printf("Temp degC:\r\n");
00136     lcd.printf("Humidity %:\r\n");
00137     lcd.printf("Lights: \r\n");
00138     lcd.line(0, 45, 128, 45, 0xFF0000);
00139     t1.start(getcommand);
00140     statusReq = true;
00141 
00142     while(1) {
00143         if (statusReq == true) {
00144             sendcommand('s');
00145             statusReq = false;
00146         } else if (flipLed == true) {
00147             sendcommand('l');
00148             flipLed = false;
00149         } else if(cameraReq == true) {
00150             sendcommand('c');
00151             cameraReq = false;
00152         }
00153         Thread::wait(2000);
00154     }
00155 }