camera.ino 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "camera.h"
  2. void initialize_camera() {
  3. // Initialize camera.
  4. esp_err_t err = esp_camera_init(&camera_config);
  5. if (err != ESP_OK) {
  6. return;
  7. }
  8. // Check if the flash is already on, if it is turn it off.
  9. if (camera_model.isFlashEnabled) {
  10. toggle_flash_off();
  11. }
  12. // Get the camera sensor reference.
  13. sensor_t* cam = esp_camera_sensor_get();
  14. cam->set_contrast(cam, 0); // Set initial contrast.
  15. cam->set_vflip(cam, true); // Set initial vertical flip.
  16. // cam->set_hmirror(cam, false); // Set initial horizontal mirror.
  17. // cam->set_brightness(cam, 0); // Set initial brightness.
  18. // cam->set_saturation(cam, 0); // Set initial saturation.
  19. // cam->set_sharpness(cam, 0); // Set initial sharpness.
  20. }
  21. void toggle_flash_off() {
  22. pinMode(FLASH_GPIO_NUM, OUTPUT);
  23. digitalWrite(FLASH_GPIO_NUM, LOW);
  24. camera_model.isFlashEnabled = false;
  25. }
  26. void toggle_flash_on() {
  27. pinMode(FLASH_GPIO_NUM, OUTPUT);
  28. digitalWrite(FLASH_GPIO_NUM, HIGH);
  29. camera_model.isFlashEnabled = true;
  30. }
  31. void handle_flash_state() {
  32. // If the flash state ever gets out of sync with the camera model, fix it.
  33. if (!camera_model.isFlashEnabled) {
  34. int flashState = digitalRead(FLASH_GPIO_NUM);
  35. if (flashState == HIGH) {
  36. toggle_flash_off();
  37. }
  38. }
  39. }