Smart coffee machine with facial recognition and remote control
Dependencies: Camera_LS_Y201 EthernetInterface EthernetNetIf HTTPClient SRF05 TextLCD mbed-rtos mbed-src
camera.cpp@0:43669f623d43, 2014-01-15 (annotated)
- Committer:
- projetmacintel
- Date:
- Wed Jan 15 11:09:52 2014 +0000
- Revision:
- 0:43669f623d43
dep?t final PAO Macintel
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
projetmacintel | 0:43669f623d43 | 1 | #include "camera.h" |
projetmacintel | 0:43669f623d43 | 2 | |
projetmacintel | 0:43669f623d43 | 3 | LocalFileSystem local("local"); |
projetmacintel | 0:43669f623d43 | 4 | Camera_LS_Y201 camera(p13, p14); |
projetmacintel | 0:43669f623d43 | 5 | |
projetmacintel | 0:43669f623d43 | 6 | typedef struct work |
projetmacintel | 0:43669f623d43 | 7 | { |
projetmacintel | 0:43669f623d43 | 8 | FILE *fp; |
projetmacintel | 0:43669f623d43 | 9 | } work_t; |
projetmacintel | 0:43669f623d43 | 10 | |
projetmacintel | 0:43669f623d43 | 11 | work_t work; |
projetmacintel | 0:43669f623d43 | 12 | |
projetmacintel | 0:43669f623d43 | 13 | void acallback_func(int done, int total, uint8_t *buf, size_t siz, char *reponse) |
projetmacintel | 0:43669f623d43 | 14 | { |
projetmacintel | 0:43669f623d43 | 15 | // Fonction de callback pour écriture de l'image |
projetmacintel | 0:43669f623d43 | 16 | // buf : pointeur sur un buffer |
projetmacintel | 0:43669f623d43 | 17 | // siz : taille tu buffer |
projetmacintel | 0:43669f623d43 | 18 | //printf("callback : buffer de taille %d\r\n", siz); |
projetmacintel | 0:43669f623d43 | 19 | //fwrite(buf, siz, 1, work.fp); |
projetmacintel | 0:43669f623d43 | 20 | |
projetmacintel | 0:43669f623d43 | 21 | |
projetmacintel | 0:43669f623d43 | 22 | envoyerChaineSocket((char*)buf, siz, reponse, 30); |
projetmacintel | 0:43669f623d43 | 23 | //printf("callback response : %s\r\n", reponse); |
projetmacintel | 0:43669f623d43 | 24 | |
projetmacintel | 0:43669f623d43 | 25 | |
projetmacintel | 0:43669f623d43 | 26 | //static int n = 0; |
projetmacintel | 0:43669f623d43 | 27 | //int tmp = done * 100 / total; |
projetmacintel | 0:43669f623d43 | 28 | |
projetmacintel | 0:43669f623d43 | 29 | //if(n != tmp) |
projetmacintel | 0:43669f623d43 | 30 | //{ |
projetmacintel | 0:43669f623d43 | 31 | // n = tmp; |
projetmacintel | 0:43669f623d43 | 32 | // printf("Writing...: %3d%%\r\n", n); |
projetmacintel | 0:43669f623d43 | 33 | //} |
projetmacintel | 0:43669f623d43 | 34 | } |
projetmacintel | 0:43669f623d43 | 35 | |
projetmacintel | 0:43669f623d43 | 36 | int acapture(Camera_LS_Y201 *cam, char *reponse) |
projetmacintel | 0:43669f623d43 | 37 | { |
projetmacintel | 0:43669f623d43 | 38 | // Prise de l'image |
projetmacintel | 0:43669f623d43 | 39 | if(cam->takePicture() != 0) |
projetmacintel | 0:43669f623d43 | 40 | return -1; |
projetmacintel | 0:43669f623d43 | 41 | |
projetmacintel | 0:43669f623d43 | 42 | printf("\tPhoto prise\r\n"); |
projetmacintel | 0:43669f623d43 | 43 | //afficherAuCentreDeLEcran("Photo prise", "wesh"); |
projetmacintel | 0:43669f623d43 | 44 | |
projetmacintel | 0:43669f623d43 | 45 | // Ouverture du fichier |
projetmacintel | 0:43669f623d43 | 46 | //work.fp = fopen(nom_fichier, "wb"); |
projetmacintel | 0:43669f623d43 | 47 | //if(work.fp == NULL) |
projetmacintel | 0:43669f623d43 | 48 | // return -2; |
projetmacintel | 0:43669f623d43 | 49 | |
projetmacintel | 0:43669f623d43 | 50 | // Lecture du contenu |
projetmacintel | 0:43669f623d43 | 51 | //printf("%s\r\n", nom_fichier); |
projetmacintel | 0:43669f623d43 | 52 | |
projetmacintel | 0:43669f623d43 | 53 | if(cam->readJpegFileContent(acallback_func, envoyerChaineSocket, reponse) != 0) |
projetmacintel | 0:43669f623d43 | 54 | { |
projetmacintel | 0:43669f623d43 | 55 | //fclose(work.fp); |
projetmacintel | 0:43669f623d43 | 56 | return -3; |
projetmacintel | 0:43669f623d43 | 57 | } |
projetmacintel | 0:43669f623d43 | 58 | |
projetmacintel | 0:43669f623d43 | 59 | //fclose(work.fp); |
projetmacintel | 0:43669f623d43 | 60 | wait(1); |
projetmacintel | 0:43669f623d43 | 61 | |
projetmacintel | 0:43669f623d43 | 62 | // Fin de la prise d'image |
projetmacintel | 0:43669f623d43 | 63 | cam->stopTakingPictures(); |
projetmacintel | 0:43669f623d43 | 64 | |
projetmacintel | 0:43669f623d43 | 65 | return 0; // Retourne donc 0 en cas de succès |
projetmacintel | 0:43669f623d43 | 66 | } |
projetmacintel | 0:43669f623d43 | 67 | |
projetmacintel | 0:43669f623d43 | 68 | bool initialiserCamera() |
projetmacintel | 0:43669f623d43 | 69 | { |
projetmacintel | 0:43669f623d43 | 70 | int initCam; |
projetmacintel | 0:43669f623d43 | 71 | |
projetmacintel | 0:43669f623d43 | 72 | do |
projetmacintel | 0:43669f623d43 | 73 | { |
projetmacintel | 0:43669f623d43 | 74 | printf("Reset CAMERA\r\n"); |
projetmacintel | 0:43669f623d43 | 75 | initCam = camera.reset(); |
projetmacintel | 0:43669f623d43 | 76 | } while(initCam != 0); |
projetmacintel | 0:43669f623d43 | 77 | |
projetmacintel | 0:43669f623d43 | 78 | if(initCam != 0) |
projetmacintel | 0:43669f623d43 | 79 | { |
projetmacintel | 0:43669f623d43 | 80 | printf("Echec de l'initialisation caméra %d\r\n", initCam); |
projetmacintel | 0:43669f623d43 | 81 | afficherAuCentreDeLEcran("Echec init", "camera"); |
projetmacintel | 0:43669f623d43 | 82 | return false; |
projetmacintel | 0:43669f623d43 | 83 | } |
projetmacintel | 0:43669f623d43 | 84 | |
projetmacintel | 0:43669f623d43 | 85 | else |
projetmacintel | 0:43669f623d43 | 86 | { |
projetmacintel | 0:43669f623d43 | 87 | printf("Initialisation caméra OK.\r\n"); |
projetmacintel | 0:43669f623d43 | 88 | afficherAuCentreDeLEcran("Initialisation", "camera : ok"); |
projetmacintel | 0:43669f623d43 | 89 | return true; |
projetmacintel | 0:43669f623d43 | 90 | } |
projetmacintel | 0:43669f623d43 | 91 | } |
projetmacintel | 0:43669f623d43 | 92 | |
projetmacintel | 0:43669f623d43 | 93 | bool detecterUtilisateur(bool *utilisateur_reconnu, char* nom_utilisateur, int* preference_intensite, int *preference_longueur) |
projetmacintel | 0:43669f623d43 | 94 | { |
projetmacintel | 0:43669f623d43 | 95 | printf("\tDébut détection utilisateur\r\n"); |
projetmacintel | 0:43669f623d43 | 96 | char reponse[30], requete[10]; |
projetmacintel | 0:43669f623d43 | 97 | int taille_reponse = 0; |
projetmacintel | 0:43669f623d43 | 98 | |
projetmacintel | 0:43669f623d43 | 99 | if(connexionSocket()) |
projetmacintel | 0:43669f623d43 | 100 | { |
projetmacintel | 0:43669f623d43 | 101 | sprintf(requete,"PHOTO"); |
projetmacintel | 0:43669f623d43 | 102 | |
projetmacintel | 0:43669f623d43 | 103 | // Envoi de "RECO". Réception de "RECO OK" |
projetmacintel | 0:43669f623d43 | 104 | envoyerChaineSocket(requete, sizeof(requete), reponse, 30); |
projetmacintel | 0:43669f623d43 | 105 | //printf("Reponse %s : %s\r\n\r", requete, reponse); |
projetmacintel | 0:43669f623d43 | 106 | |
projetmacintel | 0:43669f623d43 | 107 | int r = acapture(&camera, reponse); |
projetmacintel | 0:43669f623d43 | 108 | |
projetmacintel | 0:43669f623d43 | 109 | if(r != 0) |
projetmacintel | 0:43669f623d43 | 110 | printf("IMG_TEST:NG. (code=%d)\r\n", r); |
projetmacintel | 0:43669f623d43 | 111 | |
projetmacintel | 0:43669f623d43 | 112 | /*else |
projetmacintel | 0:43669f623d43 | 113 | printf("IMG_TEST:OK.\r\n");*/ |
projetmacintel | 0:43669f623d43 | 114 | |
projetmacintel | 0:43669f623d43 | 115 | // Parsing des parametres depuis la réponse |
projetmacintel | 0:43669f623d43 | 116 | |
projetmacintel | 0:43669f623d43 | 117 | /* CHAINES A RECEVOIR : |
projetmacintel | 0:43669f623d43 | 118 | USER=Leo 2 3 // cas Léo reconnu |
projetmacintel | 0:43669f623d43 | 119 | USER=Simon 3 4 // cas Simon reconnu |
projetmacintel | 0:43669f623d43 | 120 | USER- // cas aucun visage |
projetmacintel | 0:43669f623d43 | 121 | USER?userTest // cas visage non reconnu |
projetmacintel | 0:43669f623d43 | 122 | */ |
projetmacintel | 0:43669f623d43 | 123 | |
projetmacintel | 0:43669f623d43 | 124 | //sprintf(reponse, "USER?userTest"); |
projetmacintel | 0:43669f623d43 | 125 | //sprintf(reponse, "USER=Simon 3 4"); |
projetmacintel | 0:43669f623d43 | 126 | printf("Chaine d'identification : [%s]\n\r", reponse); |
projetmacintel | 0:43669f623d43 | 127 | |
projetmacintel | 0:43669f623d43 | 128 | if(reponse[4] == '-') |
projetmacintel | 0:43669f623d43 | 129 | { |
projetmacintel | 0:43669f623d43 | 130 | deconnexionSocket(); |
projetmacintel | 0:43669f623d43 | 131 | return false; |
projetmacintel | 0:43669f623d43 | 132 | } |
projetmacintel | 0:43669f623d43 | 133 | |
projetmacintel | 0:43669f623d43 | 134 | *utilisateur_reconnu = (reponse[4] != '?'); |
projetmacintel | 0:43669f623d43 | 135 | taille_reponse = strlen(reponse); |
projetmacintel | 0:43669f623d43 | 136 | |
projetmacintel | 0:43669f623d43 | 137 | int j; |
projetmacintel | 0:43669f623d43 | 138 | for(j = 5 ; j < taille_reponse && reponse[j] != ' ' ; j++) |
projetmacintel | 0:43669f623d43 | 139 | nom_utilisateur[j - 5] = reponse[j]; |
projetmacintel | 0:43669f623d43 | 140 | nom_utilisateur[j - 5] = '\0'; |
projetmacintel | 0:43669f623d43 | 141 | |
projetmacintel | 0:43669f623d43 | 142 | *preference_intensite = reponse[taille_reponse - 3] - '0'; |
projetmacintel | 0:43669f623d43 | 143 | *preference_longueur = reponse[taille_reponse - 1] - '0'; |
projetmacintel | 0:43669f623d43 | 144 | |
projetmacintel | 0:43669f623d43 | 145 | deconnexionSocket(); |
projetmacintel | 0:43669f623d43 | 146 | return true; |
projetmacintel | 0:43669f623d43 | 147 | } |
projetmacintel | 0:43669f623d43 | 148 | |
projetmacintel | 0:43669f623d43 | 149 | else |
projetmacintel | 0:43669f623d43 | 150 | { |
projetmacintel | 0:43669f623d43 | 151 | printf("\tErreur d'ouverture du socket\r\n\r"); |
projetmacintel | 0:43669f623d43 | 152 | return false; |
projetmacintel | 0:43669f623d43 | 153 | } |
projetmacintel | 0:43669f623d43 | 154 | } |