From 425f8e70f8d029fd69504cfaea071ffe5ccb31c0 Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Mon, 19 Feb 2024 11:42:13 +0100 Subject: [PATCH] initial version for Xiao ESP32S3 cam (unstable) --- common/CamXiao.cpp | 59 ++++ common/CamXiao.h | 19 ++ common/xiao_pins.h | 299 ++++++++++++++++++ examples/freenove-tank/freenove-tank.cpp | 1 + .../xiao-espnow-sender/xiao-espnow-sender.cpp | 50 +++ platformio.ini | 12 + 6 files changed, 440 insertions(+) create mode 100644 common/CamXiao.cpp create mode 100644 common/CamXiao.h create mode 100644 common/xiao_pins.h create mode 100644 examples/xiao-espnow-sender/xiao-espnow-sender.cpp diff --git a/common/CamXiao.cpp b/common/CamXiao.cpp new file mode 100644 index 0000000..1046d47 --- /dev/null +++ b/common/CamXiao.cpp @@ -0,0 +1,59 @@ +#include "CamXiao.h" + +static camera_config_t camera_config = { + + .pin_pwdn = PWDN_GPIO_NUM, + .pin_reset = RESET_GPIO_NUM, + .pin_xclk = XCLK_GPIO_NUM, + .pin_sccb_sda = SIOD_GPIO_NUM, + .pin_sccb_scl = SIOC_GPIO_NUM, + .pin_d7 = Y9_GPIO_NUM, + .pin_d6 = Y8_GPIO_NUM, + .pin_d5 = Y7_GPIO_NUM, + .pin_d4 = Y6_GPIO_NUM, + .pin_d3 = Y5_GPIO_NUM, + .pin_d2 = Y4_GPIO_NUM, + .pin_d1 = Y3_GPIO_NUM, + .pin_d0 = Y2_GPIO_NUM, + .pin_vsync = VSYNC_GPIO_NUM, + .pin_href = HREF_GPIO_NUM, + .pin_pclk = PCLK_GPIO_NUM, + + .xclk_freq_hz = 20000000, + .ledc_timer = LEDC_TIMER_0, + .ledc_channel = LEDC_CHANNEL_0, + + .pixel_format = PIXFORMAT_RGB565, + .frame_size = FRAMESIZE_HVGA, + .jpeg_quality = 0, + .fb_count = 2, + .fb_location = CAMERA_FB_IN_PSRAM, + .grab_mode = CAMERA_GRAB_WHEN_EMPTY, + .sccb_i2c_port = 0 +}; + +bool CamXiao::begin() { + + esp_err_t err = esp_camera_init(&camera_config); + if (err != ESP_OK) { + return false; + } + sensor = esp_camera_sensor_get(); + return true; +} + +bool CamXiao::get() { + fb = esp_camera_fb_get(); + if (!fb) { + return false; + } + return true; +} + +bool CamXiao::free() { + if (fb) { + esp_camera_fb_return(fb); + return true; + } + return false; +} diff --git a/common/CamXiao.h b/common/CamXiao.h new file mode 100644 index 0000000..a0371e5 --- /dev/null +++ b/common/CamXiao.h @@ -0,0 +1,19 @@ +#ifndef CAMXIAO_H +#define CAMXIAO_H + +#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM +#include "esp_camera.h" +#include "xiao_pins.h" + +class CamXiao { + private: + public: + camera_fb_t* fb; + sensor_t* sensor; + camera_config_t* config; + bool begin(); + bool get(); + bool free(); +}; + +#endif \ No newline at end of file diff --git a/common/xiao_pins.h b/common/xiao_pins.h new file mode 100644 index 0000000..51fe178 --- /dev/null +++ b/common/xiao_pins.h @@ -0,0 +1,299 @@ + +#if defined(CAMERA_MODEL_WROVER_KIT) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 21 +#define SIOD_GPIO_NUM 26 +#define SIOC_GPIO_NUM 27 + +#define Y9_GPIO_NUM 35 +#define Y8_GPIO_NUM 34 +#define Y7_GPIO_NUM 39 +#define Y6_GPIO_NUM 36 +#define Y5_GPIO_NUM 19 +#define Y4_GPIO_NUM 18 +#define Y3_GPIO_NUM 5 +#define Y2_GPIO_NUM 4 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 23 +#define PCLK_GPIO_NUM 22 + +#elif defined(CAMERA_MODEL_ESP_EYE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define LED_GPIO_NUM 22 + +#elif defined(CAMERA_MODEL_M5STACK_PSRAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_V2_PSRAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 22 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_WIDE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 22 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#define LED_GPIO_NUM 2 + +#elif defined(CAMERA_MODEL_M5STACK_ESP32CAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_UNITCAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_AI_THINKER) +#define PWDN_GPIO_NUM 32 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 0 +#define SIOD_GPIO_NUM 26 +#define SIOC_GPIO_NUM 27 + +#define Y9_GPIO_NUM 35 +#define Y8_GPIO_NUM 34 +#define Y7_GPIO_NUM 39 +#define Y6_GPIO_NUM 36 +#define Y5_GPIO_NUM 21 +#define Y4_GPIO_NUM 19 +#define Y3_GPIO_NUM 18 +#define Y2_GPIO_NUM 5 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 23 +#define PCLK_GPIO_NUM 22 + +#define LED_GPIO_NUM 33 + +#elif defined(CAMERA_MODEL_TTGO_T_JOURNAL) +#define PWDN_GPIO_NUM 0 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_XIAO_ESP32S3) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 10 +#define SIOD_GPIO_NUM 40 +#define SIOC_GPIO_NUM 39 + +#define Y9_GPIO_NUM 48 +#define Y8_GPIO_NUM 11 +#define Y7_GPIO_NUM 12 +#define Y6_GPIO_NUM 14 +#define Y5_GPIO_NUM 16 +#define Y4_GPIO_NUM 18 +#define Y3_GPIO_NUM 17 +#define Y2_GPIO_NUM 15 +#define VSYNC_GPIO_NUM 38 +#define HREF_GPIO_NUM 47 +#define PCLK_GPIO_NUM 13 + +#define LED_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_ESP32_CAM_BOARD) +// The 18 pin header on the board has Y5 and Y3 swapped +#define USE_BOARD_HEADER 0 +#define PWDN_GPIO_NUM 32 +#define RESET_GPIO_NUM 33 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 19 +#define Y7_GPIO_NUM 21 +#define Y6_GPIO_NUM 39 +#if USE_BOARD_HEADER +#define Y5_GPIO_NUM 13 +#else +#define Y5_GPIO_NUM 35 +#endif +#define Y4_GPIO_NUM 14 +#if USE_BOARD_HEADER +#define Y3_GPIO_NUM 35 +#else +#define Y3_GPIO_NUM 13 +#endif +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#elif defined(CAMERA_MODEL_ESP32S3_CAM_LCD) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 40 +#define SIOD_GPIO_NUM 17 +#define SIOC_GPIO_NUM 18 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 41 +#define Y7_GPIO_NUM 42 +#define Y6_GPIO_NUM 12 +#define Y5_GPIO_NUM 3 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 47 +#define Y2_GPIO_NUM 13 +#define VSYNC_GPIO_NUM 21 +#define HREF_GPIO_NUM 38 +#define PCLK_GPIO_NUM 11 + +#elif defined(CAMERA_MODEL_ESP32S2_CAM_BOARD) +// The 18 pin header on the board has Y5 and Y3 swapped +#define USE_BOARD_HEADER 0 +#define PWDN_GPIO_NUM 1 +#define RESET_GPIO_NUM 2 +#define XCLK_GPIO_NUM 42 +#define SIOD_GPIO_NUM 41 +#define SIOC_GPIO_NUM 18 + +#define Y9_GPIO_NUM 16 +#define Y8_GPIO_NUM 39 +#define Y7_GPIO_NUM 40 +#define Y6_GPIO_NUM 15 +#if USE_BOARD_HEADER +#define Y5_GPIO_NUM 12 +#else +#define Y5_GPIO_NUM 13 +#endif +#define Y4_GPIO_NUM 5 +#if USE_BOARD_HEADER +#define Y3_GPIO_NUM 13 +#else +#define Y3_GPIO_NUM 12 +#endif +#define Y2_GPIO_NUM 14 +#define VSYNC_GPIO_NUM 38 +#define HREF_GPIO_NUM 4 +#define PCLK_GPIO_NUM 3 + +#elif defined(CAMERA_MODEL_ESP32S3_EYE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 15 +#define SIOD_GPIO_NUM 4 +#define SIOC_GPIO_NUM 5 + +#define Y2_GPIO_NUM 11 +#define Y3_GPIO_NUM 9 +#define Y4_GPIO_NUM 8 +#define Y5_GPIO_NUM 10 +#define Y6_GPIO_NUM 12 +#define Y7_GPIO_NUM 18 +#define Y8_GPIO_NUM 17 +#define Y9_GPIO_NUM 16 + +#define VSYNC_GPIO_NUM 6 +#define HREF_GPIO_NUM 7 +#define PCLK_GPIO_NUM 13 + +#else +#error "Camera model not selected" +#endif diff --git a/examples/freenove-tank/freenove-tank.cpp b/examples/freenove-tank/freenove-tank.cpp index 147ef7e..6696f70 100644 --- a/examples/freenove-tank/freenove-tank.cpp +++ b/examples/freenove-tank/freenove-tank.cpp @@ -6,6 +6,7 @@ **************************************************/ #include +#include #include #include "CamFreenove.h" #include "ESPNowCam.h" diff --git a/examples/xiao-espnow-sender/xiao-espnow-sender.cpp b/examples/xiao-espnow-sender/xiao-espnow-sender.cpp new file mode 100644 index 0000000..fe7d94c --- /dev/null +++ b/examples/xiao-espnow-sender/xiao-espnow-sender.cpp @@ -0,0 +1,50 @@ +/************************************************** + * ESPNowCam video Transmitter + * by @hpsaturn Copyright (C) 2024 + * This file is part ESP32S3 camera tests project: + * https://github.com/hpsaturn/esp32s3-cam +**************************************************/ + +#include +#include +#include "ESPNowCam.h" +#include "CamXiao.h" +#include "Utils.h" + +CamXiao Camera; +ESPNowCam radio; + +void processFrame() { + if (Camera.get()) { + uint8_t *out_jpg = NULL; + size_t out_jpg_len = 0; + frame2jpg(Camera.fb, 18, &out_jpg, &out_jpg_len); + // Serial.printf("JPG len %i\r\n",out_jpg_len); + radio.sendData(out_jpg, out_jpg_len); + // printFPS("CAM:"); + // drawFPS(); + free(out_jpg); + Camera.free(); + } +} + +void setup() { + Serial.begin(115200); + + delay(5000); + + if(psramFound()){ + size_t psram_size = esp_spiram_get_size() / 1048576; + Serial.printf("PSRAM size: %dMb\r\n", psram_size); + } + + radio.init(); + if (!Camera.begin()) { + Serial.println("Camera Init Fail"); + } + delay(500); +} + +void loop() { + processFrame(); +} diff --git a/platformio.ini b/platformio.ini index 9482150..15136b9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,6 +19,8 @@ build_flags = ; -Werror -D CORE_DEBUG_LEVEL=0 -D BOARD_HAS_PSRAM=1 + -D MAIN_HW_EN_PIN=3 + [esp32common] extends = env @@ -26,6 +28,15 @@ board = esp32-s3-devkitc-1 board_build.flash_size = 16MB board_build.partitions = ./config/partitions.csv +[env:xiao-espnow-sender] +extends = esp32common +board_build.arduino.memory_type = dio_opi ; +build_src_filter = -<*> + +lib_deps = + nanopb/Nanopb@0.4.8 + https://github.com/hpsaturn/SerialTerminal.git + hpsaturn/ESP32 Wifi CLI @^0.2.1 + [env:freenove-tank] extends = esp32common board_build.arduino.memory_type = dio_opi ; @@ -34,6 +45,7 @@ lib_deps = nanopb/Nanopb@0.4.8 madhephaestus/ESP32Servo@1.1.2 https://github.com/hpsaturn/SerialTerminal.git + hpsaturn/CanAirIO Air Quality Sensors Library@^0.7.4 hpsaturn/ESP32 Wifi CLI @^0.2.1 [m5cores3_common]