Advertisement
talofer99

DIY_CAM_W_SCREEN

Jun 12th, 2025
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.69 KB | Source Code | 0 0
  1. #include "Arduino.h"
  2. #include "soc/soc.h"           // Disable brownour problems
  3. #include "soc/rtc_cntl_reg.h"  // Disable brownour problems
  4. #include "driver/rtc_io.h"
  5. #include "esp_camera.h"
  6. #include <JPEGDEC.h>
  7. #include <Arduino_GFX_Library.h>
  8. /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
  9. Arduino_DataBus *bus = new Arduino_ESP32SPI(15 /*DF_GFX_DC*/, 2 /*DF_GFX_CS*/ , 14 /*DF_GFX_SCK*/, 13/*DF_GFX_MOSI*/, -1 /* DF_GFX_MISO*/, VSPI /* spi_num */);
  10. /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
  11. Arduino_GFX *gfx = new Arduino_GC9A01(bus, -1 /* RST */, 0 /* rotation */, true /* IPS */);
  12.  
  13.  
  14. // Pin definition for CAMERA_MODEL_AI_THINKER
  15. #define PWDN_GPIO_NUM     32
  16. #define RESET_GPIO_NUM    -1
  17. #define XCLK_GPIO_NUM      0
  18. #define SIOD_GPIO_NUM     26
  19. #define SIOC_GPIO_NUM     27
  20. #define Y9_GPIO_NUM       35
  21. #define Y8_GPIO_NUM       34
  22. #define Y7_GPIO_NUM       39
  23. #define Y6_GPIO_NUM       36
  24. #define Y5_GPIO_NUM       21
  25. #define Y4_GPIO_NUM       19
  26. #define Y3_GPIO_NUM       18
  27. #define Y2_GPIO_NUM        5
  28. #define VSYNC_GPIO_NUM    25
  29. #define HREF_GPIO_NUM     23
  30. #define PCLK_GPIO_NUM     22
  31.  
  32.  
  33. // variables
  34. JPEGDEC jpeg;
  35. byte t = 0;
  36. byte takeNextPhotoFlag = 1;
  37.  
  38.  
  39.  
  40. void setup() {
  41.   // for cam init
  42.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector // NEEDED ????
  43.  
  44.   // start serial
  45.   Serial.begin(115200);
  46.   Serial.println("System startred");
  47.  
  48.   // start gfx
  49.   Serial.print("Starting gfx ..... ");
  50.   gfx->begin();
  51.   gfx->fillScreen(BLACK);
  52.   gfx->setTextColor(RED);
  53.   gfx->setTextSize(2, 2, 1);
  54.   gfx->setCursor(45, 75);
  55.   gfx->println("GFX -> OK");
  56.   Serial.println("Done");
  57.  
  58.   // start camera
  59.   Serial.print("Starting camera init ..... ");
  60.   gfx->setCursor(45, 110);
  61.   gfx->println("Cam ->");
  62.   initCamera();
  63.   gfx->setCursor(95, 110);
  64.   gfx->println("OK");
  65.   Serial.println("Done");
  66.  
  67.  
  68.   //  gfx->setCursor(25, 145);
  69.   //  gfx->println("Watching!");
  70.  
  71.  
  72.  
  73.  
  74.   delay(4000); // 4 seconds
  75. }
  76.  
  77.  
  78.  
  79. void loop() {
  80.  
  81.  
  82.   // if takeNextPhotoFlag is set
  83.   if (takeNextPhotoFlag)
  84.   {
  85.     takePhoto();
  86. //    takeNextPhotoFlag = 0;
  87.   }
  88.  
  89.  
  90.   // we only read serial if we use the uart
  91.   if (Serial.available())
  92.   {
  93.     switch (Serial.read())
  94.     {
  95.       case 'p':
  96.       case 'P':
  97.         takeNextPhotoFlag = 1;
  98.         break;
  99.       default:
  100.         Serial.println("not supported!!!");
  101.         break;
  102.     } //end switch
  103.   } //end if
  104. } //end loop
  105.  
  106.  
  107.  
  108.  
  109. /* ***************************************************************** */
  110. /* jpegDrawCallback                                                      */
  111. /* ***************************************************************** */
  112. // pixel drawing callback
  113. static int jpegDrawCallback(JPEGDRAW *pDraw)
  114. {
  115.   //Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
  116.   gfx->draw16bitBeRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
  117.   return 1;
  118. } //end draw pixwl
  119.  
  120.  
  121.  
  122.  
  123. /* ***************************************************************** */
  124. /* INIT CAMERA                                                       */
  125. /* ***************************************************************** */
  126. void initCamera()
  127. {
  128.   camera_config_t config;
  129.   config.ledc_channel = LEDC_CHANNEL_0;
  130.   config.ledc_timer = LEDC_TIMER_0;
  131.   config.pin_d0 = Y2_GPIO_NUM;
  132.   config.pin_d1 = Y3_GPIO_NUM;
  133.   config.pin_d2 = Y4_GPIO_NUM;
  134.   config.pin_d3 = Y5_GPIO_NUM;
  135.   config.pin_d4 = Y6_GPIO_NUM;
  136.   config.pin_d5 = Y7_GPIO_NUM;
  137.   config.pin_d6 = Y8_GPIO_NUM;
  138.   config.pin_d7 = Y9_GPIO_NUM;
  139.   config.pin_xclk = XCLK_GPIO_NUM;
  140.   config.pin_pclk = PCLK_GPIO_NUM;
  141.   config.pin_vsync = VSYNC_GPIO_NUM;
  142.   config.pin_href = HREF_GPIO_NUM;
  143.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  144.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  145.   config.pin_pwdn = PWDN_GPIO_NUM;
  146.   config.pin_reset = RESET_GPIO_NUM;
  147.   config.xclk_freq_hz = 20000000;
  148.   config.pixel_format = PIXFORMAT_JPEG;
  149.  
  150.   Serial.print("psramFound() = " + String(psramFound()));
  151.   Serial.print("...");
  152.  
  153.   config.frame_size = FRAMESIZE_SVGA; //FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA //FRAMESIZE_QVGA
  154.   config.jpeg_quality = 10;
  155.   config.fb_count = 2;
  156.  
  157.  
  158.   // Init Camera
  159.   esp_err_t err = esp_camera_init(&config);
  160.   if (err != ESP_OK) {
  161.     Serial.printf("Camera init failed with error 0x%x", err);
  162.     return;
  163.   }
  164. }
  165.  
  166.  
  167.  
  168. /* ***************************************************************** */
  169. /* TAKE PHOTO                                                        */
  170. /* ***************************************************************** */
  171. void takePhoto()
  172. {
  173.   delay(50);
  174.   camera_fb_t * fb = NULL;
  175.  
  176.   // Take Picture with Camera
  177.   fb = esp_camera_fb_get();
  178.   if (!fb) {
  179.     Serial.println("Camera capture failed");
  180.     return;
  181.   }
  182.   else
  183.   {
  184.     Serial.println("photo taken ...");
  185.  
  186. //    long lTime;
  187.     int stateOpen = jpeg.openRAM((uint8_t *)fb->buf, fb->len, jpegDrawCallback);
  188.     //jpeg.setMaxOutputSize(1600);
  189.     jpeg.setPixelType(RGB565_BIG_ENDIAN);
  190. //    Serial.println(stateOpen);
  191.     if (stateOpen)
  192.     {
  193. //      lTime = micros();
  194.       if (jpeg.decode(20,45, JPEG_SCALE_QUARTER)) // JPEG_SCALE_HALF, JPEG_SCALE_QUARTER, JPEG_SCALE_EIGHTH
  195.       {
  196. //        lTime = micros() - lTime;
  197. //        Serial.printf("%d x %d image, decode time = %d us\n", jpeg.getWidth(), jpeg.getHeight() , (int)lTime);
  198.       }
  199.       else
  200.       {
  201.         Serial.println("FAIL TO decode ????");
  202.       }
  203.       jpeg.close();
  204.     }
  205.     else
  206.     {
  207.       Serial.println("FAIL TO OPEN RAM ????");
  208.     }
  209.  
  210.  
  211.  
  212.   } //end if not fail capture
  213.  
  214.   // clear
  215.   esp_camera_fb_return(fb);
  216.  
  217. } //endd phototaken
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement