MiddleWare GUS

Dependents:   MainTaskGus

Files at this revision

API Documentation at this revision

Comitter:
RoHe
Date:
Thu Dec 10 05:03:13 2015 +0000
Parent:
2:a56763667f20
Child:
4:a7da6ca6aef2
Commit message:
Update Program to count people.; Sensor.h and Sensors.cpp

Changed in this revision

Initial.h Show annotated file Show diff for this revision Revisions of this file
Sensors.cpp Show annotated file Show diff for this revision Revisions of this file
Sensors.h Show annotated file Show diff for this revision Revisions of this file
StringManager.cpp Show annotated file Show diff for this revision Revisions of this file
StringManager.h Show annotated file Show diff for this revision Revisions of this file
--- a/Initial.h	Wed Dec 09 04:50:13 2015 +0000
+++ b/Initial.h	Thu Dec 10 05:03:13 2015 +0000
@@ -37,24 +37,5 @@
 static char *s_end="\n\r";
 static bool bo_somethingWrong=false;
 
-/*Extern Variables*/
-//START PIR SENSOR INPUT INIT
-static DigitalIn PIR1(D0);
-static DigitalIn PIR2(D1);
-//END PIR SENSOR INPUT INIT
-//START WINDOW SENSOR INPUT
-static DigitalIn W1(D2);
-static DigitalIn W2(D3);
-static DigitalIn W3(D4);
-//END WINDOW SENSOR INPUT INIT
-//START DOOR SENSOR INPUT
-static DigitalIn DOORSensor(D5);
-//END DOOR SENSOR INPUT INIT
-//START LIGHT CONTROL OUTPUT
-static DigitalOut LightControl(D6);
-//END LIGHT CONTROL OUTPUT
-
-
-
 
 #endif
\ No newline at end of file
--- a/Sensors.cpp	Wed Dec 09 04:50:13 2015 +0000
+++ b/Sensors.cpp	Thu Dec 10 05:03:13 2015 +0000
@@ -1,6 +1,4 @@
 #include "Sensors.h"
-#include "Initial.h"
-
 
 /*Functions*/
 
@@ -87,14 +85,104 @@
     return LightStatus;
 }
 
-//PERSON COUNTER FUNCTION
-unsigned int GetPersonStatus(){
-    static unsigned int PersonNumber = 0;   
+//PERSON COUNTER FUNCTIONS
+static void dist(int distance)
+{
+    /*Void*/
+}
+static void dist2(int distance2)
+{
+    /*Void*/
+}
+
+//START ULTRASONIC INIT
+ultrasonic mu(D10, D11, .1, 1, &dist);    //Set the trigger pin to D10 and the echo pin to D11
+                                        //have updates every .1 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes
+ultrasonic mu2(D12, D13, .1, 1, &dist2);  //Set the trigger pin to D12 and the echo pin to D13
+                                        //have updates every .1 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes 
+//END ULTRASONIC INIT
+
+unsigned int GetPersonStatus()
+{
+    static int CDistance = 0;
+    static int CDistance2 = 0;
+    int PersonNumber = 0;
+    static int flag=0;
+    mu.startUpdates();//start mesuring the distance
+    mu2.startUpdates();//start mesuring the distance
     
-    //TODO Insert code here
-    /***      TODO      ***/
-    /***      TODO      ***/
-    /***      TODO      ***/
+    printf("--------------------------------------------- \r\n");
+        //Do something else here
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
+        mu2.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
+        CDistance = mu.getCurrentDistance();
+        CDistance2 = mu2.getCurrentDistance();
+        
+        if(CDistance <= RANGE && flag == 0)
+        {
+            flag = 1;
+            printf("Entrando %d\r\n", flag);
+        }
+        if(CDistance2 <= RANGE && flag == 0)
+        {
+           flag = 2;
+           printf("Saliendo %d\r\n", flag);
+        }
+
+        switch(flag)
+            {
+              case 1:
+                    wait(0.3);
+                    CDistance2 = mu2.getCurrentDistance();
+                    if(CDistance2 <= RANGE)
+                    {
+                       PersonNumber++;
+                       printf("Entro una persona \r\n");
+                    }
+                    else
+                    {
+                        printf("No entro nadie \r\n");
+                    }
+                    break;
+                    
+                case 2:
+                    wait(0.3);
+                    CDistance = mu.getCurrentDistance();
+                    if(CDistance <= RANGE)
+                    {
+                       if(PersonNumber > 0)PersonNumber--;
+                       printf("Salio una persona \r\n");
+                    }
+                        else
+                    {
+                        printf("No salio nadie \r\n");
+                    }
+                    break;
+                    
+                default:
+
+                    break;
+            }
+            printf("Personas adentro %d\r\n", PersonNumber);      
+            printf("+++++++++++++++++++ \r\n");
+            wait(0.5);
+            flag = 0;
     
     return PersonNumber;
-}
\ No newline at end of file
+}
+
+//LIGHT FEEDBACK FUNCION
+bool GetLightStatus(){
+    static bool LightFeedback = OFF;   
+
+    if(LIGHTSTATUS == ON)
+        LightFeedback = ON;
+    else
+        LightFeedback = OFF;
+    
+    return LightFeedback;
+}
--- a/Sensors.h	Wed Dec 09 04:50:13 2015 +0000
+++ b/Sensors.h	Thu Dec 10 05:03:13 2015 +0000
@@ -1,4 +1,9 @@
+#ifndef SENSORS_H_
+#define SENSORS_H_
+
+
 #include "mbed.h"
+#include "ultrasonic.h"
 
 /*Macros*/
 #define BAD_PARAMETER (-1)
@@ -8,8 +13,30 @@
 #define TRUE 1
 #define CLOSED 0
 #define OPEN 1
+#define RANGE 500
 
 
+/*Function Initialization*/
+//START PIR SENSOR INPUT INIT
+static DigitalIn PIR1(D0);
+static DigitalIn PIR2(D1);
+//END PIR SENSOR INPUT INIT
+//START WINDOW SENSOR INPUT
+static DigitalIn W1(D2);
+static DigitalIn W2(D3);
+static DigitalIn W3(D4);
+//END WINDOW SENSOR INPUT INIT
+//START DOOR SENSOR INPUT
+static DigitalIn DOORSensor(D5);
+//END DOOR SENSOR INPUT INIT
+//START LIGHT CONTROL OUTPUT
+static DigitalOut LightControl(D6);
+//END LIGHT CONTROL OUTPUT
+//START LIGHT FEEDBACK INPUT
+static DigitalIn LIGHTSTATUS(D8);
+//END LIGHT FEEDBACK INPUT
+
+ static int PersonNumber;
 
 
 /*Extern Functions*/
@@ -55,4 +82,15 @@
 Input parameters  -> void
 
 Output parameters -> unsigned int PersonNumber; 0 - 4294967296.
-*/
\ No newline at end of file
+*/
+
+////////////////////////////////////////////////////
+extern bool GetLightStatus();
+/* GetLightStatus:
+Input parameters  -> void
+
+Output parameters -> bool LightFeedback;    0-> Light is OFF
+                                            1-> Light is ON
+*/
+
+#endif
\ No newline at end of file
--- a/StringManager.cpp	Wed Dec 09 04:50:13 2015 +0000
+++ b/StringManager.cpp	Thu Dec 10 05:03:13 2015 +0000
@@ -11,6 +11,8 @@
 */
 
 #include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include "StringManager.h"
 
 StringManager::StringManager()
@@ -48,6 +50,29 @@
                 // aparición de caracter dentro de cadena
     posicion = strstr(cadena,subcadena) // Devuelve la posición en memoria de la primer
                 // aparición de subcadena dentro de cadena
+
+    #include<stdio.h>
+    #include<string.h>
+    #include<stdlib.h>
+    char *convertIC(int val, int base){
+    static char buf[32] = {0};
+    int i = 30;
+    for(; val && i ; --i, val /= base)
+    buf[i] = "0123456789abcdef"[val % base];
+    return &buf[i+1];
+    }
+    //Prueba
+    main()
+    {
+    int n;
+    char *cad;
+    cad=(char *)malloc(sizeof(char)*30);
+    printf("inserta un numero\n");
+    scanf("%d",&n);
+    cad=convertIC(n,10);
+    printf("%s",cad);
+    }
+
     */
 
     //Case: "set:light:1" -> light ON
@@ -55,12 +80,12 @@
     if(strcmp(cadena,"set:light:1\r\n")==0) {
         int set_light = SetLight(true);
         //bool isOKLight = getLight();
-        return "reply:set:light:1:192.168.1.82:success";
+        return "reply:set:light:success";
         //Case: "set:light:1" -> light ON
     } else if(strcmp(cadena,"set:light:0\r\n")==0) {
         int set_light = SetLight(false);
         //bool isOKLight = readLight();
-        return "reply:set:light:0:192.168.1.82:succes";
+        return "reply:set:light:fail";
     }
     //Case: "get:people"
     else if(strcmp(cadena,"get:people\r\n")==0) {
@@ -71,27 +96,27 @@
         return stringMakerGetPresence(ipServer);
     }
     //Case: "get:door" -> 1 = open , 0 = close
-    else if(strcmp(cadena,"get:door\r\n")==0) {
+    else if(strcmp(cadena,"get:status:door\r\n")==0) {
         return stringMakerGetDoor(ipServer);
     }
     //Case: "get:light" -> 1 = ON , 0 = OFF
-    else if(strcmp(cadena,"get:light\r\n")==0) {
+    else if(strcmp(cadena,"get:status:light\r\n")==0) {
         return stringMakerGetLight(ipServer);
     }
     //Case: "get:window:1"
-    else if(strcmp(cadena,"get:window:1\r\n")==0) {
+    else if(strcmp(cadena,"get:status:window:1\r\n")==0) {
         return stringMakerGetWindow1(ipServer);
     }
     //Case: "get:window:2"
-    else if(strcmp(cadena,"get:window:2\r\n")==0) {
+    else if(strcmp(cadena,"get:status:window:2\r\n")==0) {
         return stringMakerGetWindow2(ipServer);
     }
     //Case: "get:window:3"
-    else if(strcmp(cadena,"get:window:3\r\n")==0) {
+    else if(strcmp(cadena,"get:status:window:3\r\n")==0) {
         return stringMakerGetWindow3(ipServer);
     }
     //Case: "get:room:<IPbroker>"
-    else if(strcmp(cadena,"get:room\r\n")==0) {
+    else if(strcmp(cadena,"get:status:room\r\n")==0) {
         return stringMakerGetRoom(ipServer);
     }
     return "Error,it does NOT in the correct format...";
@@ -102,7 +127,7 @@
     bool get_light /*= getLight()*/;
     get_light=true; //HARDCODED please remove
     strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    //strcat(initialString,ipServer);
     if(get_light) {
         strcat(initialString,":light:1");
     } else {
@@ -115,7 +140,7 @@
 {
     bool get_door = GetDoorStatus();
     strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    //strcat(initialString,ipServer);
     if(get_door) {
         strcat(initialString,":door:1");
     } else {
@@ -127,8 +152,8 @@
 char* StringManager::stringMakerGetPresence(char* ipServer)
 {
     bool get_presence = GetPrescenceStatus();
-    strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    strcpy(initialString,"reply:get");
+    //strcat(initialString,ipServer);
     if(get_presence) {
         strcat(initialString,":presence:1");
     } else {
@@ -140,7 +165,7 @@
 {
     int get_window1 = GetWindowStatus(1);
     strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    //strcat(initialString,ipServer);
     if(get_window1==1) {
         strcat(initialString,":window:1:1");
     } else {
@@ -152,7 +177,7 @@
 {
     int get_window2 = GetWindowStatus(2);
     strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    //strcat(initialString,ipServer);
     if(get_window2==1) {
         strcat(initialString,":window:2:1");
     } else {
@@ -164,7 +189,7 @@
 {
     int get_window3 = GetWindowStatus(3);
     strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    // strcat(initialString,ipServer);
     if(get_window3==1) {
         strcat(initialString,":window:3:1");
     } else {
@@ -175,13 +200,23 @@
 
 char* StringManager::stringMakerGetPeople(char* ipServer)
 {
-    strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
-    if(true) {
-        strcat(initialString,":people:12");
-    } else {
-        strcat(initialString,":people:0");
-    }
+    strcpy(initialString,"reply:get");
+    /*
+    Other option to convert integer to Strin, but it seems that "sprintf()" 
+    works better that "convertIC"
+     //int num_people = PersonNumber;
+     int num_people = 123456;
+     char* cad=convertIC(num_people,10);
+     strcat(initialString,":people:");
+     strcat(initialString,cad);
+     */
+
+    int num_people = PersonNumber;
+    //int num_people = 123456;
+    static char aux_buffer [50];
+    sprintf (aux_buffer,"%d",num_people);
+    strcat(initialString,":people:");
+    strcat(initialString,aux_buffer);
     return initialString;
 }
 
@@ -194,8 +229,8 @@
     int get_window1 = GetWindowStatus(1);
     int get_window2 = GetWindowStatus(2);
     int get_window3 = GetWindowStatus(3);
-    strcpy(initialString,"reply:get:status:");
-    strcat(initialString,ipServer);
+    strcpy(initialString,"reply:get:status:room");
+    //strcat(initialString,ipServer);
     //GET DOOR
     if(get_door) {
         strcat(initialString,":door:1");
@@ -227,12 +262,15 @@
     } else {
         strcat(initialString,":3:0");
     }
+    
     //GET PEOPLE
-    if(true) {
-        strcat(initialString,":people:12");
-    } else {
-        strcat(initialString,":people:0");
-    }
+     int num_people = PersonNumber;
+    //int num_people = 123456;//HARDCODED PLEASE REMOVE
+    static char aux_buffer [50];
+    sprintf (aux_buffer,"%d",num_people);
+    strcat(initialString,":people:");
+    strcat(initialString,aux_buffer);
+    
     //GET PRESENCE
     if(get_presence) {
         strcat(initialString,":presence:1");
@@ -243,3 +281,13 @@
     return initialString;
 
 }
+
+
+char* StringManager::convertIC(int val, int base)
+{
+    static char buf[32] = {0};
+    int i = 30;
+    for(; val && i ; --i, val /= base)
+        buf[i] = "0123456789abcdef"[val % base];
+    return &buf[i+1];
+}
--- a/StringManager.h	Wed Dec 09 04:50:13 2015 +0000
+++ b/StringManager.h	Thu Dec 10 05:03:13 2015 +0000
@@ -39,6 +39,7 @@
      char* stringMakerGetWindow2(char* ipClient);
      char* stringMakerGetWindow3(char* ipClient);
      char* stringMakerGetRoom(char* ipClient);
+     char* convertIC(int val, int base);
 };
 
 #endif
\ No newline at end of file