1708浏览
查看: 1708|回复: 0

[ESP8266/ESP32] FireBeetle 2 ESP32-S3 相机

[复制链接]
本帖最后由 云天 于 2023-8-27 10:19 编辑

FireBeetle 2 ESP32-S3 相机图1


本次测试的是将摄像头与屏幕结合起来,实现监控相机功能。
一、测试屏幕显示
使用DFRobot_GDL库自带的“bitmap”示例。
引脚定义:
#define TFT_DC  3
#define TFT_CS  18
#define TFT_RST 38
#define TFT_BL  47
屏幕定义:
DFRobot_ILI9341_240x320_HW_SPI  screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);

示例主程序:
  1. /*!
  2. * @file bitmap.ino
  3. * @brief Draw monochrome, colorful, and grayscale images based on the image array generated by the bitmap software.
  4. * [url=home.php?mod=space&uid=821650]@N[/url] The demo requires to be run on a main-controller with 40000 bytes of dynamic memory, like FireBeetle-ESP8266 and FireBeetle-ESP32 since there are 380,000 bytes in this project.
  5. * [url=home.php?mod=space&uid=843077]@n[/url] This demo supports mainboard FireBeetle-ESP32, FireBeetle-ESP8266, and FireBeetle-M0.
  6. * @copyright Copyright (c) 2010 DFRobot Co. Ltd (http://www.dfrobot.com)
  7. * @license The MIT License (MIT)
  8. * @author [YeHangYu] (hangyu.ye@dfrobot.com)
  9. * @version V0.1
  10. * @date 2020-01-07
  11. * @url https://github.com/DFRobot/DFRobot_GDL
  12. */
  13. #include "DFRobot_GDL.h"
  14. #include "Bitmap.h"
  15. #include "XBitmap.h"
  16. #include "GrayscaleBitmap.h"
  17. #include "mask.h"
  18. #include "RGBBitmap.h"
  19. //Custom communication pins
  20. /*M0*/
  21. #if defined Arduino_SAM_ZERO
  22. #define TFT_DC  7
  23. #define TFT_CS  5
  24. #define TFT_RST 6
  25. #define TFT_BL  9
  26. /*ESP32 and ESP8266*/
  27. #elif defined(ESP32) || defined(ESP8266)
  28. #define TFT_DC  3
  29. #define TFT_CS  18
  30. #define TFT_RST 38
  31. #define TFT_BL  47
  32. /* AVR series mainboard */
  33. #else
  34. #define TFT_DC  2
  35. #define TFT_CS  3
  36. #define TFT_RST 4
  37. #define TFT_BL  5
  38. #endif
  39. /**
  40. * @brief Constructor Constructor of hardware SPI communication
  41. * @param dc Command/data line pin for SPI communication
  42. * @param cs Chip select pin for SPI communication
  43. * @param rst reset pin of the screen
  44. */
  45. //DFRobot_ST7789_240x204_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST,/*bl=*/TFT_BL);
  46. //DFRobot_ST7789_240x240_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  47. //DFRobot_ST7789_240x320_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  48. DFRobot_ILI9341_240x320_HW_SPI  screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  49. //DFRobot_ILI9488_320x480_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  50. /* M0 mainboard DMA transfer */
  51. //DFRobot_ST7789_240x204_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST,/*bl=*/TFT_BL);
  52. //DFRobot_ST7789_240x240_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  53. //DFRobot_ST7789_240x320_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  54. //DFRobot_ILI9341_240x320_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  55. //DFRobot_ILI9488_320x480_DMA_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  56. void setup() {
  57.   Serial.begin(115200);
  58.   screen.begin();
  59. }
  60. void loop() {
  61.   /**
  62.    * @brief Paint the screen white
  63.    * @param 0xFFFF white parameter
  64.    */
  65.   screen.fillScreen(COLOR_RGB565_WHITE );
  66.   /**
  67.    * @brief Draw monochrome pictures with different drawing orders
  68.    * @param x 0 Set the starting point to be at the upper left of the screen, near the left side
  69.    * @param y 0 Near the upper side
  70.    * @param bitmap gImage_XBitmap The array in the header file XBitmap, the array elements are single bytes
  71.    * @param w 240 Picture width
  72.    * @param h 240 Picture height
  73.    * @param color 0x0000 The black picture part is set to black
  74.    */
  75.   screen.drawXBitmap(/*x=*/(screen.width()-240)/2,/*y=*/(screen.height()-240)/2,/*bitmap gImage_Bitmap=*/gImage_XBitmap,/*w=*/240,/*h=*/240,/*color=*/0x0000);
  76.   screen.fillScreen(COLOR_RGB565_WHITE);
  77.   /**
  78.    * @brief Draw colorful picture
  79.    * @param x 0 Set the starting point to be at the upper left of the screen, near the left side
  80.    * @param y 0 Near the upper side
  81.    * @param bitmap gImage_RGBBitmap The array of RGBBitmap header files, the array elements are single bytes, here forced to convert to 2 bytes
  82.    * @param w 240 picture width
  83.    * @param h 240 Picture height
  84.    */
  85.   screen.drawRGBBitmap(/*x=*/(screen.width()-240)/2,/*y=*/(screen.height()-240)/2,/*bitmap gImage_Bitmap=*/(const unsigned uint16_t*)gImage_RGBBitmap,/*w=*/240,/*h=*/240);
  86.   /**
  87.    * @brief Use the mask as a monochrome picture to draw a monochrome picture
  88.    * @param x 0 Set the starting point to be at the upper left of the screen, near the left side
  89.    * @param y 0 Near the upper side
  90.    * @param bitmap gImage_mask The array in the header mask, the array elements are single bytes
  91.    * @param w 240 Picture width
  92.    * @param h 240 Picture height
  93.    * @param color 0x0000 The black picture part is set to black
  94.    */
  95.   screen.drawBitmap(/*x=*/(screen.width()-240)/2,/*y=*/(screen.height()-240)/2,/*bitmap gImage_Bitmap=*/gImage_mask,/*w=*/240,/*h=*/240,/*color=*/0x0000);
  96.   /**
  97.     * @brief Draw a colorful picture, the white part of the mask is not painted
  98.     * @param x 0 Set the starting point to be at the upper left of the screen, near the left side
  99.     * @param y 0 Near the upper side
  100.     * @param bitmap gImage_RGBBitmap The array of RGBBitmap header files, the array elements are single bytes, here forced to convert to 2 bytes
  101.     * @param mask gImage_mask The array in the header mask, the array elements are single bytes
  102.     * @param w 240 Picture width
  103.     * @param h 240 Picture height
  104.     */
  105.   screen.drawRGBBitmap(/*x=*/(screen.width()-240)/2,/*y=*/(screen.height()-240)/2,/*bitmap gImage_Bitmap=*/(const unsigned uint16_t*)gImage_RGBBitmap,/*mask=*/gImage_mask,/*w=*/240,/*h=*/240);
  106.   screen.fillScreen(COLOR_RGB565_BLACK);
  107.   /**
  108.    * @brief Draw a monochrome picture without background circularly, and the color is changing constantly.
  109.    * @param x 0 Set the starting point to be at the upper left of the screen, near the left side
  110.    * @param y 0 Near the upper side
  111.    * @param bitmap gImage_Bitmap The array in the header file Bitmap, the array elements are single bytes
  112.    * @param w 240 Picture width
  113.    * @param h 240 Picture height
  114.    * @param color i Set the black image part to bluish color
  115.    */
  116.   for (int16_t i = 0x00ff; ; i+=0x3300) {
  117.     screen.drawBitmap(/*x=*/(screen.width()-240)/2,/*y=*/(screen.height()-240)/2,/*bitmap gImage_Bitmap=*/gImage_Bitmap,/*w=*/240,/*h=*/240,/*color=*/i);
  118.   }
  119.   //screen.drawGrayscaleBitmap(0,0,gImage_GrayscaleBitmap,240,240);//Draw grayscale image, not supported by this screen
  120.   //screen.drawGrayscaleBitmap(0,0,gImage_GrayscaleBitmap,gImage_mask,240,240);//Draw grayscale image with mask, not supported by this screen
  121. }
复制代码


二、摄像头与屏幕结合
1、需要Arduino IDE安装GFX_Library for Arduino、TJpg_Decoder、DFRobot_AXP313A库。
2、引脚定义
  1. #define TFT_CS     18
  2. #define TFT_RST  38
  3. #define TFT_DC     3
  4. #define TFT_MOSI   15
  5. #define TFT_SCK    17
  6. #define TFT_MISO   16
  7. #define PIN_BTN 47
复制代码

3、要设置摄像头供电
  1. while (axp.begin() != 0)
  2.   {
  3.     Serial.println("init error");
  4.     delay(1000);
  5.   }
  6.   axp.enableCameraPower(axp.eOV2640);
复制代码

4、图像质量设置config.jpeg_quality = 12;
值越小越质量越好,但易出现重启。经测试小于6会出问题。
FireBeetle 2 ESP32-S3 相机图2


5、程序代码
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Arduino_GFX_Library.h>
  6. #include <TJpg_Decoder.h>
  7. #include "esp_camera.h"
  8. #include "DFRobot_AXP313A.h"
  9. DFRobot_AXP313A axp;
  10. #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
  11. #define PWDN_GPIO_NUM     -1
  12. #define RESET_GPIO_NUM    -1
  13. #define XCLK_GPIO_NUM     45
  14. #define SIOD_GPIO_NUM     1
  15. #define SIOC_GPIO_NUM     2
  16. #define Y9_GPIO_NUM       48
  17. #define Y8_GPIO_NUM       46
  18. #define Y7_GPIO_NUM       8
  19. #define Y6_GPIO_NUM       7
  20. #define Y5_GPIO_NUM       4
  21. #define Y4_GPIO_NUM       41
  22. #define Y3_GPIO_NUM       40
  23. #define Y2_GPIO_NUM       39
  24. #define VSYNC_GPIO_NUM    6
  25. #define HREF_GPIO_NUM     42
  26. #define PCLK_GPIO_NUM     5
  27. //以下引脚都可以成对配置,根据你的显示屏引脚位置自行选择,注意部分引脚不能混用.320*240
  28. #define TFT_CS     18
  29. #define TFT_RST  38
  30. #define TFT_DC     3
  31. #define TFT_MOSI   15
  32. #define TFT_SCK    17
  33. #define TFT_MISO   16
  34. #define PIN_BTN 47
  35. Arduino_ESP32SPI bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
  36. Arduino_ILI9341 tft = Arduino_ILI9341(&bus, TFT_RST);
  37. bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
  38. {
  39.   // Stop further decoding as image is running off bottom of screen
  40.   if ( y >= tft.height() ) return 0;
  41.   tft.draw16bitRGBBitmap(x, y, bitmap, w, h);
  42.   // Return 1 to decode next block
  43.   return 1;
  44. }
  45. void init_camera() {
  46.   camera_config_t config;
  47.   config.ledc_channel = LEDC_CHANNEL_0;
  48.   config.ledc_timer = LEDC_TIMER_0;
  49.   config.pin_d0 = Y2_GPIO_NUM;
  50.   config.pin_d1 = Y3_GPIO_NUM;
  51.   config.pin_d2 = Y4_GPIO_NUM;
  52.   config.pin_d3 = Y5_GPIO_NUM;
  53.   config.pin_d4 = Y6_GPIO_NUM;
  54.   config.pin_d5 = Y7_GPIO_NUM;
  55.   config.pin_d6 = Y8_GPIO_NUM;
  56.   config.pin_d7 = Y9_GPIO_NUM;
  57.   config.pin_xclk = XCLK_GPIO_NUM;
  58.   config.pin_pclk = PCLK_GPIO_NUM;
  59.   config.pin_vsync = VSYNC_GPIO_NUM;
  60.   config.pin_href = HREF_GPIO_NUM;
  61.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  62.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  63.   config.pin_pwdn = PWDN_GPIO_NUM;
  64.   config.pin_reset = RESET_GPIO_NUM;
  65.   config.xclk_freq_hz = 20000000;
  66.   config.frame_size = FRAMESIZE_UXGA;
  67.   config.pixel_format = PIXFORMAT_JPEG; // for streaming
  68.   //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  69.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  70.   config.fb_location = CAMERA_FB_IN_PSRAM;
  71.   config.jpeg_quality = 12;
  72.   config.fb_count = 1;
  73.   
  74.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  75.   //                      for larger pre-allocated frame buffer.
  76.   if (psramFound()) {
  77.     config.frame_size = FRAMESIZE_QVGA;
  78.     config.jpeg_quality = 6;
  79.     config.fb_count = 1;
  80.     Serial.println("PSRAM");
  81.   } else {
  82.     config.frame_size = FRAMESIZE_QVGA;
  83.     config.jpeg_quality = 12;
  84.     config.fb_count = 1;
  85.   }
  86.   // camera init
  87.   while (axp.begin() != 0)
  88.   {
  89.     Serial.println("init error");
  90.     delay(1000);
  91.   }
  92.   axp.enableCameraPower(axp.eOV2640); // 设置摄像头供电
  93.   esp_err_t err = esp_camera_init(&config);
  94.   if (err != ESP_OK) {
  95.     Serial.printf("Camera init failed with error 0x%x", err);
  96.     return;
  97.   }
  98.   sensor_t * s = esp_camera_sensor_get();
  99.   // initial sensors are flipped vertically and colors are a bit saturated
  100.   if (s->id.PID == OV2640_PID) {
  101.     Serial.println("PID");
  102.     s->set_vflip(s, 1); // flip it back
  103.     s->set_brightness(s, 2); // up the brightness just a bit
  104.     s->set_saturation(s, 0);
  105.   }
  106. }
  107. void setup() {
  108.   Serial.begin(9600);
  109.   Serial.println("ESP32-CAM Picture");
  110.   //tft.Init(INITR_BLACKTAB);
  111.   tft.begin();
  112.   tft.setRotation(1);
  113.   tft.fillScreen(MAGENTA);
  114.   init_camera();
  115.   TJpgDec.setJpgScale(1);
  116.     // The decoder must be given the exact name of the rendering function above
  117.   TJpgDec.setCallback(tft_output);
  118. }
  119. void take_picture() {
  120.   Serial.println("Taking picture..");
  121.   camera_fb_t * fb = NULL;
  122.   fb = esp_camera_fb_get();
  123.   if (!fb) {
  124.    Serial.println("Camera capture failed");
  125.   }
  126.   uint16_t w = 0, h = 0;
  127.   TJpgDec.getJpgSize(&w, &h, fb->buf, fb->len);
  128.   Serial.print("- Width = "); Serial.print(fb->width); Serial.print(", height = "); Serial.println(fb->height);
  129.   Serial.print("Width = "); Serial.print(w); Serial.print(", height = "); Serial.println(h);
  130.   // Draw the image, top left at 0,0
  131.   TJpgDec.drawJpg(0, 0, fb->buf, fb->len);
  132.   esp_camera_fb_return(fb);
  133. }
  134. void loop() {
  135. Serial.println("start");
  136.   while (true) {
  137.       take_picture();
  138.   }
  139. }
复制代码
三、演示视频


、使用DF的“DFRobot_GDL”图形库

  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include "esp_timer.h"
  5. #include "esp_camera.h"
  6. #include "DFRobot_AXP313A.h"
  7. #include "DFRobot_GDL.h"
  8. DFRobot_AXP313A axp;
  9. #define TFT_DC  3
  10. #define TFT_CS  18
  11. #define TFT_RST 38
  12. #define TFT_BL  21
  13. DFRobot_ILI9341_240x320_HW_SPI  screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
  14. #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
  15. #define PWDN_GPIO_NUM     -1
  16. #define RESET_GPIO_NUM    -1
  17. #define XCLK_GPIO_NUM     45
  18. #define SIOD_GPIO_NUM     1
  19. #define SIOC_GPIO_NUM     2
  20. #define Y9_GPIO_NUM       48
  21. #define Y8_GPIO_NUM       46
  22. #define Y7_GPIO_NUM       8
  23. #define Y6_GPIO_NUM       7
  24. #define Y5_GPIO_NUM       4
  25. #define Y4_GPIO_NUM       41
  26. #define Y3_GPIO_NUM       40
  27. #define Y2_GPIO_NUM       39
  28. #define VSYNC_GPIO_NUM    6
  29. #define HREF_GPIO_NUM     42
  30. #define PCLK_GPIO_NUM     5
  31. void init_camera() {
  32.   camera_config_t config;
  33.   config.ledc_channel = LEDC_CHANNEL_0;
  34.   config.ledc_timer = LEDC_TIMER_0;
  35.   config.pin_d0 = Y2_GPIO_NUM;
  36.   config.pin_d1 = Y3_GPIO_NUM;
  37.   config.pin_d2 = Y4_GPIO_NUM;
  38.   config.pin_d3 = Y5_GPIO_NUM;
  39.   config.pin_d4 = Y6_GPIO_NUM;
  40.   config.pin_d5 = Y7_GPIO_NUM;
  41.   config.pin_d6 = Y8_GPIO_NUM;
  42.   config.pin_d7 = Y9_GPIO_NUM;
  43.   config.pin_xclk = XCLK_GPIO_NUM;
  44.   config.pin_pclk = PCLK_GPIO_NUM;
  45.   config.pin_vsync = VSYNC_GPIO_NUM;
  46.   config.pin_href = HREF_GPIO_NUM;
  47.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  48.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  49.   config.pin_pwdn = PWDN_GPIO_NUM;
  50.   config.pin_reset = RESET_GPIO_NUM;
  51.   config.xclk_freq_hz = 20000000;
  52.   config.frame_size = FRAMESIZE_UXGA;
  53.   //config.pixel_format = PIXFORMAT_JPEG; // for streaming
  54.   config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  55.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  56.   config.fb_location = CAMERA_FB_IN_PSRAM;
  57.   config.jpeg_quality = 12;
  58.   config.fb_count = 1;
  59.   
  60.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  61.   //                      for larger pre-allocated frame buffer.
  62.   if (psramFound()) {
  63.     config.frame_size = FRAMESIZE_QVGA;
  64.     config.jpeg_quality = 20;
  65.     config.fb_count = 1;
  66.     Serial.println("PSRAM");
  67.   } else {
  68.     config.frame_size = FRAMESIZE_QVGA;
  69.     config.jpeg_quality = 12;
  70.     config.fb_count = 1;
  71.   }
  72.   // camera init
  73.   while (axp.begin() != 0)
  74.   {
  75.     Serial.println("init error");
  76.     delay(1000);
  77.   }
  78.   axp.enableCameraPower(axp.eOV2640); // 设置摄像头供电
  79.   esp_err_t err = esp_camera_init(&config);
  80.   if (err != ESP_OK) {
  81.     Serial.printf("Camera init failed with error 0x%x", err);
  82.     return;
  83.   }
  84.   sensor_t * s = esp_camera_sensor_get();
  85.   // initial sensors are flipped vertically and colors are a bit saturated
  86.   if (s->id.PID == OV2640_PID) {
  87.     Serial.println("PID");
  88.     s->set_vflip(s, 1); // flip it back
  89.     s->set_brightness(s, 2); // up the brightness just a bit
  90.     s->set_saturation(s, 0);
  91.   }
  92. }
  93. int64_t last_frame = 0;
  94. void setup() {
  95.   Serial.begin(9600);
  96.   Serial.println("ESP32-CAM Picture");
  97.   //tft.Init(INITR_BLACKTAB);
  98.   init_camera();
  99.   screen.begin();
  100.   screen.fillScreen(COLOR_RGB565_BLACK);
  101.   screen.setRotation(1);
  102.     if(!last_frame) {
  103.         last_frame = esp_timer_get_time();
  104.     }
  105.     screen.setCursor(0, 50);
  106. }
  107. void loop() {
  108. Serial.println("start");
  109.   while (true) {
  110.     //int64_t fr_end = esp_timer_get_time();
  111.     //int64_t frame_time = fr_end - last_frame;
  112.     //int16_t color = 0x00FF;
  113.     //last_frame = fr_end;
  114.     //frame_time /= 1000;
  115.     //bool s;
  116.     camera_fb_t * fb = NULL;
  117.     fb = esp_camera_fb_get();
  118.     //size_t _jpg_len = 0;
  119.     //uint8_t * _jpg_buf = NULL;
  120.     //s=fmt2jpg(fb->buf,fb->len,fb->width,fb->height,fb->format,80,&_jpg_buf,&_jpg_len);
  121.     //s=frame2jpg(fb,80,&_jpg_buf,&_jpg_len);
  122.     //s=frame2bmp(fb,&_jpg_buf,&_jpg_len);
  123.     screen.drawPIC(0,0,fb->width,fb->height,fb->buf);
  124.     //screen.setCursor(20, 50);
  125.     //screen.setTextColor(color+=0x3000);
  126.     //screen.setTextSize(4);
  127.     //screen.println(String(frame_time));
  128.     esp_camera_fb_return(fb);
  129.     delay(10);
  130.   }
  131. }
复制代码


FireBeetle 2 ESP32-S3 相机图3

六、演示视频

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail