Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. `
  2. using namespace std;
  3. using namespace cv;
  4.  
  5. struct button {
  6. uint8_t btn;
  7. int btn_state;
  8. };
  9. typedef struct button BUTTON;
  10. struct seaid_project {
  11. uint8_t ledGreen = RPI_V2_GPIO_P1_37;
  12. uint8_t ledRed = RPI_V2_GPIO_P1_31;
  13. uint8_t ledYellow = RPI_V2_GPIO_P1_35;
  14. uint8_t ledReader = RPI_V2_GPIO_P1_33;
  15.  
  16. BUTTON btnShutdown;
  17. BUTTON btnReboot;
  18.  
  19. BUTTON btnShutdown;
  20. BUTTON btnReboot;
  21.  
  22. SEAID *seaid;
  23. seaid->btnShutdown.btn = RPI_V2_GPIO_P1_16;
  24. seaid->btnReboot.btn = RPI_V2_GPIO_P1_18;
  25. pthread_t t_btnShutdown, t_btnReboot, t_captureImage;
  26. int ret;
  27.  
  28. bcm2835_gpio_write(seaid->ledGreen, HIGH);
  29. bcm2835_gpio_write(seaid->ledYellow, LOW);
  30. bcm2835_gpio_write(seaid->ledRed, LOW);
  31. bcm2835_gpio_write(seaid->ledReader, LOW);
  32.  
  33. // Inicializa mutex
  34. if (pthread_mutex_init(&lockMutex, NULL) != 0) {
  35. error(seaid->ledRed, "Erro ao inicializar mutex.n");
  36. }
  37. // Iniciliza biblioteca bcm2835
  38. if (!bcm2835_init()) {
  39. error(seaid->ledRed, "Erro ao inicializar biblioteca bcm2835.n");
  40. }
  41. // Seleciona pinos de saída para LEDs
  42. bcm2835_gpio_fsel(seaid->ledGreen, BCM2835_GPIO_FSEL_OUTP);
  43. bcm2835_gpio_fsel(seaid->ledRed, BCM2835_GPIO_FSEL_OUTP);
  44. bcm2835_gpio_fsel(seaid->ledYellow, BCM2835_GPIO_FSEL_OUTP);
  45. bcm2835_gpio_fsel(seaid->ledReader, BCM2835_GPIO_FSEL_OUTP);
  46.  
  47. // Seleciona pinos de entrada para push buttons
  48. // Desabilita resistores de pull up/down nesses pinos
  49. bcm2835_gpio_fsel(seaid->btnShutdown.btn, BCM2835_GPIO_FSEL_INPT);
  50. bcm2835_gpio_fsel(seaid->btnShutdown.btn, BCM2835_GPIO_PUD_OFF);
  51. bcm2835_gpio_fsel(seaid->btnReboot.btn, BCM2835_GPIO_FSEL_INPT);
  52. bcm2835_gpio_fsel(seaid->btnReboot.btn, BCM2835_GPIO_PUD_OFF);
  53.  
  54.  
  55. //Cria thread para OpenCV
  56. ret = pthread_create(&t_captureImage, NULL, captureImage, &seaid);
  57. if (ret != 0) {
  58. error(seaid->ledRed, "Erro thread "t_captureImage"");
  59. }
  60.  
  61. // Cria thread para ler estado dos botões
  62. ret = pthread_create(&t_btnShutdown, NULL, inputRead, &seaid->btnShutdown);
  63. if (ret != 0) {
  64. error(seaid->ledRed, "Erro thread "t_btnShutdown"");
  65. }
  66. ret = pthread_create(&t_btnReboot, NULL, inputRead, &seaid->btnReboot);
  67. if (ret != 0) {
  68. error(seaid->ledRed, "Erro thread "t_btnReboot"");
  69. }
  70. // Aguarda as threads terminarem
  71. pthread_join(t_captureImage, NULL);
  72. pthread_join(t_btnShutdown, NULL);
  73. pthread_join(t_btnReboot, NULL);
  74.  
  75. // Finaliza biblioteca bcm2835
  76. bcm2835_close();
  77. //Destrói o mutex
  78. pthread_mutex_destroy(&lockMutex);
  79.  
  80. return 0;
  81.  
  82. //Loop infinito
  83. while (1) {
  84. // Trava o mutex antes de acessar variável led_state
  85. pthread_mutex_lock(&lockMutex);
  86. // Atualiza estado do LED - liga ou desliga
  87. if (seaid->btnShutdown.btn_state)
  88. bcm2835_gpio_write(seaid->ledRed, HIGH);
  89. else
  90. bcm2835_gpio_write(seaid->ledRed, LOW);
  91. // Libera o mutex
  92. pthread_mutex_unlock(&lockMutex);
  93. // Pequeno delay
  94. bcm2835_delay(50);
  95. }
  96. pthread_exit(NULL);
  97.  
  98. //Loop infinito
  99. while (1) {
  100. // Trava o mutex antes de acessar variável led_state
  101. pthread_mutex_lock(&lockMutex);
  102. // Atualiza estado do botão na variável
  103. button->btn_state = bcm2835_gpio_lev(button->btn);
  104. // Libera o mutex
  105. pthread_mutex_unlock(&lockMutex);
  106. // Pequeno delay
  107. bcm2835_delay(50);
  108. }
  109. pthread_exit(NULL);
  110.  
  111. SEAID *seaid = (SEAID *)p;
  112. std::vector<Rect> digitais;
  113. bcm2835_gpio_write(seaid->ledReader, HIGH);
  114. VideoCapture cap(0);
  115. Mat frame;
  116.  
  117. cap >> frame;
  118. if (frame.empty()) {
  119. error(seaid->ledRed, "Erro ao capturar a imagem");
  120. }
  121. try {
  122. if (hasFingerprint(frame)) {
  123. try {
  124. startCapture(*seaid);
  125. }
  126. catch (...) {
  127. error(seaid->ledRed, "Erro captura das digitais do voluntário");
  128. }
  129. pthread_mutex_unlock(&lockMutex);
  130. bcm2835_gpio_write(seaid->ledReader, LOW);
  131.  
  132. }
  133. else {
  134. pthread_mutex_unlock(&lockMutex);
  135. bcm2835_gpio_write(seaid->ledReader, LOW);
  136.  
  137. }
  138. }catch(...) {
  139. error(seaid->ledRed, "Erro na detecção da imagem");
  140. }
  141. pthread_exit(NULL);
  142.  
  143. CascadeClassifier fingerPrintCascade;
  144. fingerPrintCascade.load("Training/data/cascade.xml");
  145.  
  146. try {
  147. fingerPrintCascade.detectMultiScale(frame, fingerprints, 1.1, 0, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
  148. }
  149. catch (...) {
  150.  
  151. }
  152. if (!fingerprints.empty()) {
  153. hasImage = true;
  154. }
  155. return hasImage;
Add Comment
Please, Sign In to add comment