ov2640_contrast_brightness.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Workaround - bug in cam library - enable bits are set without using bitwise OR logic -> only latest enable setting is used
  2. // Reference: https://esp32.com/viewtopic.php?f=19&t=14376#p93178
  3. /* The memory structure is as follows for
  4. byte_0 = enable_bits
  5. byte_0->bit0 = enable saturation and hue --> OK
  6. byte_0->bit1 = enable saturation --> OK
  7. byte_0->bit2 = enable brightness and contrast --> OK
  8. byte_0->bit3 = enable green -> blue spitial effect (Antique and blunish and greenish and reddish and b&w) enable
  9. byte_0->bit4 = anable gray -> red spitial effect (Antique and blunish and greenish and reddish and b&w) enable
  10. byte_0->bit5 = remove (UV) in YUV color system
  11. byte_0->bit6 = enable negative
  12. byte_0->bit7 = remove (Y) in YUV color system
  13. byte_1 = saturation1 0-255 --> ?
  14. byte_2 = hue 0-255 --> OK
  15. byte_3 = saturation2 0-255 --> OK
  16. byte_4 = reenter saturation2 in documents --> ?
  17. byte_5 = spital effect green -> blue 0-255 --> ?
  18. byte_6 = spital effect gray -> red 0-255 --> ?
  19. byte_7 = contrast lower byte 0-255 --> OK
  20. byte_8 = contrast higher byte 0-255 --> OK
  21. byte_9 = brightness 0-255 --> OK
  22. byte_10 = if byte_10==4 contrast effective --> ?
  23. */
  24. #include <stdint.h>
  25. #include "esp_camera.h"
  26. #include "ov2640_contrast_brightness.h"
  27. static const uint8_t brightness_regs[6][5] = {
  28. {0x7C, 0x7D, 0x7C, 0x7D, 0x7D },
  29. {0x00, 0x04, 0x09, 0x00, 0x00 }, /* -2 */
  30. {0x00, 0x04, 0x09, 0x10, 0x00 }, /* -1 */
  31. {0x00, 0x04, 0x09, 0x20, 0x00 }, /* 0 */
  32. {0x00, 0x04, 0x09, 0x30, 0x00 }, /* +1 */
  33. {0x00, 0x04, 0x09, 0x40, 0x00 }, /* +2 */
  34. };
  35. static const uint8_t contrast_regs[6][7] = {
  36. {0x7C, 0x7D, 0x7C, 0x7D, 0x7D, 0x7D, 0x7D },
  37. {0x00, 0x04, 0x07, 0x20, 0x18, 0x34, 0x06 }, /* -2 */
  38. {0x00, 0x04, 0x07, 0x20, 0x1c, 0x2a, 0x06 }, /* -1 */
  39. {0x00, 0x04, 0x07, 0x20, 0x20, 0x20, 0x06 }, /* 0 */
  40. {0x00, 0x04, 0x07, 0x20, 0x24, 0x16, 0x06 }, /* +1 */
  41. {0x00, 0x04, 0x07, 0x20, 0x28, 0x0c, 0x06 }, /* +2 */
  42. };
  43. int ov2640_set_contrast_brightness(sensor_t *sensor, int _contrast, int _brightness)
  44. {
  45. int ret=0;
  46. _contrast += 3;
  47. if (_contrast <= 0) {
  48. _contrast = 3;
  49. }
  50. else if (_contrast > 5)
  51. {
  52. _contrast = 5;
  53. }
  54. sensor->status.contrast = _contrast-3;
  55. _brightness += 3;
  56. if (_brightness <= 0) {
  57. _brightness = 3;
  58. }
  59. else if (_brightness > 5)
  60. {
  61. _brightness = 5;
  62. }
  63. int brightness = brightness_regs[_brightness][3];
  64. sensor->status.brightness = _brightness-3;
  65. // sensor->set_reg(sensor, int reg, int mask, int value)
  66. sensor->set_reg(sensor, 0xFF, 0x01, 0x00); // Select DSP bank
  67. for (int i=0; i<7; i++)
  68. {
  69. if (i == 5)
  70. {
  71. sensor->set_reg(sensor, contrast_regs[0][i], 0xFF, (brightness | contrast_regs[_contrast][i]));
  72. }
  73. else
  74. {
  75. sensor->set_reg(sensor, contrast_regs[0][i], 0xFF, contrast_regs[_contrast][i]);
  76. }
  77. }
  78. return ret;
  79. }