From 22477c3700645adb762e8c3a11a50ef8b8a9e359 Mon Sep 17 00:00:00 2001 From: lvbin Date: Mon, 7 Aug 2023 09:40:03 +0800 Subject: [PATCH] add support Arduino UNO R4 board --- README.md | 37 +-- .../full_featured/ArducamLink.cpp | 236 ++++++++++++++++++ .../full_featured/ArducamLink.h | 64 +++++ .../full_featured/ArducamUart.h | 12 + .../full_featured/full_featured.ino | 96 +++++++ .../full_featured/ArducamLink.cpp | 236 ++++++++++++++++++ .../full_featured/ArducamLink.h | 64 +++++ .../full_featured/ArducamUart.h | 12 + .../full_featured/full_featured.ino | 96 +++++++ .../full_featured/ArducamLink.cpp | 236 ++++++++++++++++++ .../full_featured/ArducamLink.h | 64 +++++ .../full_featured/ArducamUart.h | 12 + .../full_featured/full_featured.ino | 96 +++++++ library.properties | 6 +- src/Arducam/Platform.h | 2 +- 15 files changed, 1248 insertions(+), 21 deletions(-) create mode 100644 examples/ArduinoProtentaC33/full_featured/ArducamLink.cpp create mode 100644 examples/ArduinoProtentaC33/full_featured/ArducamLink.h create mode 100644 examples/ArduinoProtentaC33/full_featured/ArducamUart.h create mode 100644 examples/ArduinoProtentaC33/full_featured/full_featured.ino create mode 100644 examples/ArduinoUNOR4Minima/full_featured/ArducamLink.cpp create mode 100644 examples/ArduinoUNOR4Minima/full_featured/ArducamLink.h create mode 100644 examples/ArduinoUNOR4Minima/full_featured/ArducamUart.h create mode 100644 examples/ArduinoUNOR4Minima/full_featured/full_featured.ino create mode 100644 examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.cpp create mode 100644 examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.h create mode 100644 examples/ArduinoUNOR4Wifi/full_featured/ArducamUart.h create mode 100644 examples/ArduinoUNOR4Wifi/full_featured/full_featured.ino diff --git a/README.md b/README.md index 498d454..5b48c2c 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,26 @@ Arducam released Mega SDK for mega camera. The specific test is as follows: -| Board | Development Environment | Mega SDK Version | Board Manager URL
/SDK  | Test Status | -| ------------- | ----------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------- | ----------- | -| UNO | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | -| Mega2560 | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | -| DUE | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | -| Nano33Ble | Arduino IDE 2.0.1 | 2.0.4 | / | Testing | -| Microbit | Arduino IDE 2.0.1 | 2.0.4 | / | Testing | -| Esp8266 | Arduino IDE 2.0.1 | 2.0.4 | [esp8266 board url](http://arduino.esp8266.com/stable/package_esp8266com_index.json) | Tested | -| Esp32-c3 | Arduino IDE 2.0.1 | 2.0.4 | [esp32s board url](https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json) | Tested | -| Esp32-c3 | ESP-IDF 4.4.2 | 2.0.4 | [esp-idf](https://dl.espressif.cn/dl/esp-idf/?idf=4.4) | Tested | -| Esp32s | Arduino IDE 2.0.1 | 2.0.4 | [esp32c-c3 board url](https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json) | Tested | -| Esp32s | ESP-IDF 4.4.2 | 2.0.4 | [esp-idf](https://dl.espressif.cn/dl/esp-idf/?idf=4.4) | Tested | -| Pico | pico | 2.0.4 | [pico sdk](https://github.com/raspberrypi/pico-sdk) | Tested | -| Pico | Arduino IDE 2.0.1 | 2.0.4 | Arduino Mbed OS RP2040 Boards | Tested | -| Stm32f103c8t6 | MDK5 | 2.0.4 | / | Tested | -| Rpi | Bullseye | 2.0.2/2.0.4 | / | Tested | -| EK-RA6M4 | [Renesas e² studio 23.1.0](https://github.com/renesas/fsp/releases/download/v4.3.0/setup_fsp_v4_3_0_e2s_v2023-01.exe)| 2.0.4 | RA6M4-EK Board Support Files v4.3.0 | Tested | +| Board | Development Environment | Mega SDK Version | Board Manager URL
/SDK  | Test Status | +| ------------- | ------------------------------------------------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------- | ----------- | +| UNO | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | +| Mega2560 | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | +| DUE | Arduino IDE 2.0.1 | 2.0.2/2.0.4 | / | Tested | +| Nano33Ble | Arduino IDE 2.0.1 | 2.0.4 | / | Testing | +| Microbit | Arduino IDE 2.0.1 | 2.0.4 | / | Testing | +| Esp8266 | Arduino IDE 2.0.1 | 2.0.4 | [esp8266 board url](http://arduino.esp8266.com/stable/package_esp8266com_index.json) | Tested | +| Esp32-c3 | Arduino IDE 2.0.1 | 2.0.4 | [esp32s board url](https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json) | Tested | +| Esp32-c3 | ESP-IDF 4.4.2 | 2.0.4 | [esp-idf](https://dl.espressif.cn/dl/esp-idf/?idf=4.4) | Tested | +| Esp32s | Arduino IDE 2.0.1 | 2.0.4 | [esp32c-c3 board url](https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json) | Tested | +| Esp32s | ESP-IDF 4.4.2 | 2.0.4 | [esp-idf](https://dl.espressif.cn/dl/esp-idf/?idf=4.4) | Tested | +| Pico | pico | 2.0.4 | [pico sdk](https://github.com/raspberrypi/pico-sdk) | Tested | +| Pico | Arduino IDE 2.0.1 | 2.0.4 | Arduino Mbed OS RP2040 Boards | Tested | +| Stm32f103c8t6 | MDK5 | 2.0.4 | / | Tested | +| Rpi | Bullseye | 2.0.2/2.0.4 | / | Tested | +| EK-RA6M4 | [Renesas e² studio 23.1.0](https://github.com/renesas/fsp/releases/download/v4.3.0/setup_fsp_v4_3_0_e2s_v2023-01.exe) | 2.0.4 | RA6M4-EK Board Support Files v4.3.0 | Tested | +| UNOR4Minima | Arduino IDE 2.1.1 | 2.0.5 | / | Tested | +| UNOR4WIFI | Arduino IDE 2.1.1 | 2.0.5 | / | Tested | +| ProtentaC33 | Arduino IDE 2.1.1 | 2.0.5 | / | Tested | ## Specific Disclaimer diff --git a/examples/ArduinoProtentaC33/full_featured/ArducamLink.cpp b/examples/ArduinoProtentaC33/full_featured/ArducamLink.cpp new file mode 100644 index 0000000..4f10648 --- /dev/null +++ b/examples/ArduinoProtentaC33/full_featured/ArducamLink.cpp @@ -0,0 +1,236 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#include "ArducamLink.h" +#include "ArducamUart.h" + +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) +{ + SerialBegin(baudRate); +} + +void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x03, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff(cameraInstance->verDateAndNumber, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x05, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff((uint8_t*)&cameraInstance->currentSDK->sdkVersion, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportCameraInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x02, 0xff, 0xbb}; + + uint32_t len = 0; + char buff[400]; + arducamUartWriteBuff(&headAndtail[0], 3); + sprintf(buff, + "ReportCameraInfo\r\nCamera Type:%s\r\nCamera Support Resolution:%d\r\nCamera Support " + "specialeffects:%d\r\nCamera Support Focus:%d\r\nCamera Exposure Value Max:%ld\r\nCamera Exposure Value " + "Min:%d\r\nCamera Gain Value Max:%d\r\nCamera Gain Value Min:%d\r\nCamera Support Sharpness:%d\r\n", + cameraInstance->myCameraInfo.cameraId, cameraInstance->myCameraInfo.supportResolution, + cameraInstance->myCameraInfo.supportSpecialEffects, cameraInstance->myCameraInfo.supportFocus, + cameraInstance->myCameraInfo.exposureValueMax, cameraInstance->myCameraInfo.exposureValueMin, + cameraInstance->myCameraInfo.gainValueMax, cameraInstance->myCameraInfo.gainValueMin, + cameraInstance->myCameraInfo.supportSharpness); + len = strlen(buff); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(buff); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::cameraGetPicture(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x01, 0xff, 0xbb}; + uint8_t buff[READ_IMAGE_LENGTH] = {0}; + + uint8_t rtLength = 0; + uint32_t len = myCamera->getTotalLength(); + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)(&len), 4); + arducamUartWrite(((cameraInstance->currentPictureMode & 0x0f) << 4) | 0x01); + while (myCamera->getReceivedLength()) { + rtLength = readBuff(cameraInstance, buff, READ_IMAGE_LENGTH); + arducamUartWriteBuff(buff, rtLength); + } + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::send_data_pack(char cmd_type, char* msg) +{ + uint8_t headAndtail[] = {0xff, 0xaa, 0x07, 0xff, 0xbb}; + headAndtail[2] = cmd_type; + uint32_t len = strlen(msg) + 2; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(msg); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* commandBuff) +{ + ArducamCamera* cameraInstance = myCAM->getCameraInstance(); + CamStatus state; + uint16_t gainValue = 0; + uint32_t exposureValue = 0; + uint32_t exposureLen1 = 0; + uint32_t exposureLen2 = 0; + uint32_t exposureLen3 = 0; + + uint8_t cameraResolution = cameraInstance->currentPictureMode; + uint8_t cameraFarmat = cameraInstance->currentPixelFormat; + switch (commandBuff[0]) { + case SET_PICTURE_RESOLUTION: // Set Camera Resolution + cameraResolution = commandBuff[1] & 0x0f; + cameraFarmat = (commandBuff[1] & 0x70) >> 4; + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + break; + case SET_VIDEO_RESOLUTION: // Set Video Resolution + cameraResolution = commandBuff[1] & 0x0f; + state = myCAM->startPreview((CAM_VIDEO_MODE)cameraResolution); + if (state == CAM_ERR_NO_CALLBACK) { + printf("callback function is not registered\n"); + } + break; + case SET_BRIGHTNESS: // Set brightness + myCAM->setBrightness((CAM_BRIGHTNESS_LEVEL)commandBuff[1]); + break; + case SET_CONTRAST: // Set Contrast + myCAM->setContrast((CAM_CONTRAST_LEVEL)commandBuff[1]); + break; + case SET_SATURATION: // Set saturation + myCAM->setSaturation((CAM_STAURATION_LEVEL)commandBuff[1]); + break; + case SET_EV: // Set EV + myCAM->setEV((CAM_EV_LEVEL)commandBuff[1]); + break; + case SET_WHITEBALANCE: // Set White balance + myCAM->setAutoWhiteBalanceMode((CAM_WHITE_BALANCE)commandBuff[1]); + break; + case SET_SPECIAL_EFFECTS: // Set Special effects + myCAM->setColorEffect((CAM_COLOR_FX)commandBuff[1]); + break; + case SET_FOCUS_CONTROL: // Focus Control + myCAM->setAutoFocus(commandBuff[1]); + if (commandBuff[1] == 0) { + myCAM->setAutoFocus(0x02); + } + break; + case SET_EXPOSUREANDGAIN_CONTROL: // exposure and Gain control + myCAM->setAutoExposure(commandBuff[1] & 0x01); + myCAM->setAutoISOSensitive(commandBuff[1] & 0x01); + break; + case SET_WHILEBALANCE_CONTROL: // while balance control + myCAM->setAutoWhiteBalance(commandBuff[1] & 0x01); + break; + case SET_SHARPNESS: + myCAM->setSharpness((CAM_SHARPNESS_LEVEL)commandBuff[1]); + break; + case SET_MANUAL_GAIN: // manual gain control + gainValue = (commandBuff[1] << 8) | commandBuff[2]; + myCAM->setISOSensitivity(gainValue); + break; + case SET_MANUAL_EXPOSURE: // manual exposure control + exposureLen1 = commandBuff[1]; + exposureLen2 = commandBuff[2]; + exposureLen3 = commandBuff[3]; + exposureValue = (exposureLen1 << 16) | (exposureLen2 << 8) | exposureLen3; + myCAM->setAbsoluteExposure(exposureValue); + break; + case GET_CAMERA_INFO: // Get Camera info + reportCameraInfo(myCAM); + break; + case TAKE_PICTURE: + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + cameraGetPicture(myCAM); + break; + case DEBUG_WRITE_REGISTER: + myCAM->debugWriteRegister(commandBuff + 1); + break; + case STOP_STREAM: + myCAM->stopPreview(); + break; + case GET_FRM_VER_INFO: // Get Firmware version info + reportVerInfo(myCAM); + break; + case GET_SDK_VER_INFO: // Get sdk version info + reportSdkVerInfo(myCAM); + break; + case RESET_CAMERA: + myCAM->reset(); + case SET_IMAGE_QUALITY: + myCAM->setImageQuality((IMAGE_QUALITY)commandBuff[1]); + default: + break; + } + return CAM_ERR_SUCCESS; +} + +void ArducamLink::arducamUartWrite(uint8_t data) +{ + SerialWrite(data); + delayUs(12); +} + +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) +{ + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); +} + +void ArducamLink::printf(char* buff) +{ + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); +} + +uint32_t ArducamLink::arducamUartAvailable(void) +{ + return SerialAvailable(); +} + +uint8_t ArducamLink::arducamUartRead(void) +{ + return SerialRead(); +} + +void ArducamLink::arducamFlush(void) +{ + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoProtentaC33/full_featured/ArducamLink.h b/examples/ArduinoProtentaC33/full_featured/ArducamLink.h new file mode 100644 index 0000000..ba1f3b1 --- /dev/null +++ b/examples/ArduinoProtentaC33/full_featured/ArducamLink.h @@ -0,0 +1,64 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#ifndef __ARDUCAMLINK_H +#define __ARDUCAMLINK_H +#include "Arducam_Mega.h" +#include "stdint.h" + +#define RESET_CAMERA 0XFF +#define SET_PICTURE_RESOLUTION 0X01 +#define SET_VIDEO_RESOLUTION 0X02 +#define SET_BRIGHTNESS 0X03 +#define SET_CONTRAST 0X04 +#define SET_SATURATION 0X05 +#define SET_EV 0X06 +#define SET_WHITEBALANCE 0X07 +#define SET_SPECIAL_EFFECTS 0X08 +#define SET_FOCUS_CONTROL 0X09 +#define SET_EXPOSUREANDGAIN_CONTROL 0X0A +// #define SET_GAIN_CONTROL 0X0B +#define SET_WHILEBALANCE_CONTROL 0X0C +#define SET_MANUAL_GAIN 0X0D +#define SET_MANUAL_EXPOSURE 0X0E +#define GET_CAMERA_INFO 0X0F +#define TAKE_PICTURE 0X10 +#define SET_SHARPNESS 0X11 +#define DEBUG_WRITE_REGISTER 0X12 +#define STOP_STREAM 0X21 +#define GET_FRM_VER_INFO 0X30 +#define GET_SDK_VER_INFO 0X40 +#define SET_IMAGE_QUALITY 0X50 + +#define READ_IMAGE_LENGTH 255 + +class ArducamLink +{ + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: + ArducamLink(); + ~ArducamLink(); + void arducamUartBegin(uint32_t); + void reportCameraInfo(Arducam_Mega*); + void reportVerInfo(Arducam_Mega* myCamera); + void reportSdkVerInfo(Arducam_Mega* myCamera); + void cameraGetPicture(Arducam_Mega*); + void arducamFlush(void); + void send_data_pack(char cmd_type, char* msg); + + public: + void printf(char* buff); + void arducamUartWrite(uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); + uint32_t arducamUartAvailable(void); + uint8_t arducamUartRead(void); +}; + +#endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoProtentaC33/full_featured/ArducamUart.h b/examples/ArduinoProtentaC33/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoProtentaC33/full_featured/ArducamUart.h @@ -0,0 +1,12 @@ +#ifndef __ARDUCAM_SLOT_H +#define __ARDUCAM_SLOT_H +#include + +#define SerialBegin(baudRate) Serial.begin(baudRate) +#define SerialWrite(ch) Serial.write(ch) +#define SerialWriteBuff(buf, len) Serial.write(buf, len) +#define SerialPrintf(str) Serial.print(str) +#define SerialAvailable() Serial.available() +#define SerialRead() Serial.read() +#define delayUs(us) delayMicroseconds(us) +#endif \ No newline at end of file diff --git a/examples/ArduinoProtentaC33/full_featured/full_featured.ino b/examples/ArduinoProtentaC33/full_featured/full_featured.ino new file mode 100644 index 0000000..5deb3ab --- /dev/null +++ b/examples/ArduinoProtentaC33/full_featured/full_featured.ino @@ -0,0 +1,96 @@ +// Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. +// License: MIT License (https://en.wikipedia.org/wiki/MIT_License) +// Web: http://www.ArduCAM.com +// This program is a demo of how to use most of the functions +// of the library with ArduCAM Spi camera, and can run on Arduino platform. +// This demo was made for ArduCAM Spi Camera. +// It needs to be used in combination with PC software. +// It can test ArduCAM Spi Camerafunctions +#include "ArducamLink.h" +#include "Arducam_Mega.h" +#include "SPI.h" +const int MEGA_CS = 7; +Arducam_Mega myCAM(MEGA_CS); +ArducamLink myUart; +uint8_t temp = 0xff; +uint8_t sendFlag = TRUE; +uint8_t commandBuff[20] = {0}; +uint8_t commandLength = 0; +uint32_t readImageLength = 0; +uint8_t jpegHeadFlag = 0; +uint8_t readBuffer(uint8_t* imagebuf, uint8_t length) +{ + if (imagebuf[0] == 0xff && imagebuf[1] == 0xd8) { + jpegHeadFlag = 1; + readImageLength = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x01); + + myUart.arducamUartWrite((uint8_t)(myCAM.getTotalLength() & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 8) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 16) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 24) & 0xff)); + myUart.arducamUartWrite(((CAM_IMAGE_PIX_FMT_JPG & 0x0f) << 4) | 0x01); + } + if (jpegHeadFlag == 1) { + readImageLength += length; + for (uint8_t i = 0; i < length; i++) { + myUart.arducamUartWrite(imagebuf[i]); + } + } + if (readImageLength == myCAM.getTotalLength()) { + jpegHeadFlag = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + } + return sendFlag; +} + +void stop_preivew() +{ + readImageLength = 0; + jpegHeadFlag = 0; + uint32_t len = 9; + + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x06); + myUart.arducamUartWriteBuff((uint8_t*)&len, 4); + myUart.printf("streamoff"); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); +} + +void setup() +{ + SPI.begin(); + myUart.arducamUartBegin(115200); + myUart.send_data_pack(7, "Hello Arduino UNO!"); + myCAM.begin(); + myUart.send_data_pack(8, "Mega start!"); + myCAM.registerCallBack(readBuffer, 200, stop_preivew); +} + +void loop() +{ + if (myUart.arducamUartAvailable()) { + temp = myUart.arducamUartRead(); + delay(5); + if (temp == 0x55) { + while (myUart.arducamUartAvailable()) { + commandBuff[commandLength] = myUart.arducamUartRead(); + if (commandBuff[commandLength] == 0xAA) { + break; + } + commandLength++; + } + myUart.arducamFlush(); + myUart.uartCommandProcessing(&myCAM, commandBuff); + commandLength = 0; + } + } + myCAM.captureThread(); +} \ No newline at end of file diff --git a/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.cpp b/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.cpp new file mode 100644 index 0000000..4f10648 --- /dev/null +++ b/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.cpp @@ -0,0 +1,236 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#include "ArducamLink.h" +#include "ArducamUart.h" + +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) +{ + SerialBegin(baudRate); +} + +void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x03, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff(cameraInstance->verDateAndNumber, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x05, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff((uint8_t*)&cameraInstance->currentSDK->sdkVersion, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportCameraInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x02, 0xff, 0xbb}; + + uint32_t len = 0; + char buff[400]; + arducamUartWriteBuff(&headAndtail[0], 3); + sprintf(buff, + "ReportCameraInfo\r\nCamera Type:%s\r\nCamera Support Resolution:%d\r\nCamera Support " + "specialeffects:%d\r\nCamera Support Focus:%d\r\nCamera Exposure Value Max:%ld\r\nCamera Exposure Value " + "Min:%d\r\nCamera Gain Value Max:%d\r\nCamera Gain Value Min:%d\r\nCamera Support Sharpness:%d\r\n", + cameraInstance->myCameraInfo.cameraId, cameraInstance->myCameraInfo.supportResolution, + cameraInstance->myCameraInfo.supportSpecialEffects, cameraInstance->myCameraInfo.supportFocus, + cameraInstance->myCameraInfo.exposureValueMax, cameraInstance->myCameraInfo.exposureValueMin, + cameraInstance->myCameraInfo.gainValueMax, cameraInstance->myCameraInfo.gainValueMin, + cameraInstance->myCameraInfo.supportSharpness); + len = strlen(buff); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(buff); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::cameraGetPicture(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x01, 0xff, 0xbb}; + uint8_t buff[READ_IMAGE_LENGTH] = {0}; + + uint8_t rtLength = 0; + uint32_t len = myCamera->getTotalLength(); + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)(&len), 4); + arducamUartWrite(((cameraInstance->currentPictureMode & 0x0f) << 4) | 0x01); + while (myCamera->getReceivedLength()) { + rtLength = readBuff(cameraInstance, buff, READ_IMAGE_LENGTH); + arducamUartWriteBuff(buff, rtLength); + } + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::send_data_pack(char cmd_type, char* msg) +{ + uint8_t headAndtail[] = {0xff, 0xaa, 0x07, 0xff, 0xbb}; + headAndtail[2] = cmd_type; + uint32_t len = strlen(msg) + 2; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(msg); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* commandBuff) +{ + ArducamCamera* cameraInstance = myCAM->getCameraInstance(); + CamStatus state; + uint16_t gainValue = 0; + uint32_t exposureValue = 0; + uint32_t exposureLen1 = 0; + uint32_t exposureLen2 = 0; + uint32_t exposureLen3 = 0; + + uint8_t cameraResolution = cameraInstance->currentPictureMode; + uint8_t cameraFarmat = cameraInstance->currentPixelFormat; + switch (commandBuff[0]) { + case SET_PICTURE_RESOLUTION: // Set Camera Resolution + cameraResolution = commandBuff[1] & 0x0f; + cameraFarmat = (commandBuff[1] & 0x70) >> 4; + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + break; + case SET_VIDEO_RESOLUTION: // Set Video Resolution + cameraResolution = commandBuff[1] & 0x0f; + state = myCAM->startPreview((CAM_VIDEO_MODE)cameraResolution); + if (state == CAM_ERR_NO_CALLBACK) { + printf("callback function is not registered\n"); + } + break; + case SET_BRIGHTNESS: // Set brightness + myCAM->setBrightness((CAM_BRIGHTNESS_LEVEL)commandBuff[1]); + break; + case SET_CONTRAST: // Set Contrast + myCAM->setContrast((CAM_CONTRAST_LEVEL)commandBuff[1]); + break; + case SET_SATURATION: // Set saturation + myCAM->setSaturation((CAM_STAURATION_LEVEL)commandBuff[1]); + break; + case SET_EV: // Set EV + myCAM->setEV((CAM_EV_LEVEL)commandBuff[1]); + break; + case SET_WHITEBALANCE: // Set White balance + myCAM->setAutoWhiteBalanceMode((CAM_WHITE_BALANCE)commandBuff[1]); + break; + case SET_SPECIAL_EFFECTS: // Set Special effects + myCAM->setColorEffect((CAM_COLOR_FX)commandBuff[1]); + break; + case SET_FOCUS_CONTROL: // Focus Control + myCAM->setAutoFocus(commandBuff[1]); + if (commandBuff[1] == 0) { + myCAM->setAutoFocus(0x02); + } + break; + case SET_EXPOSUREANDGAIN_CONTROL: // exposure and Gain control + myCAM->setAutoExposure(commandBuff[1] & 0x01); + myCAM->setAutoISOSensitive(commandBuff[1] & 0x01); + break; + case SET_WHILEBALANCE_CONTROL: // while balance control + myCAM->setAutoWhiteBalance(commandBuff[1] & 0x01); + break; + case SET_SHARPNESS: + myCAM->setSharpness((CAM_SHARPNESS_LEVEL)commandBuff[1]); + break; + case SET_MANUAL_GAIN: // manual gain control + gainValue = (commandBuff[1] << 8) | commandBuff[2]; + myCAM->setISOSensitivity(gainValue); + break; + case SET_MANUAL_EXPOSURE: // manual exposure control + exposureLen1 = commandBuff[1]; + exposureLen2 = commandBuff[2]; + exposureLen3 = commandBuff[3]; + exposureValue = (exposureLen1 << 16) | (exposureLen2 << 8) | exposureLen3; + myCAM->setAbsoluteExposure(exposureValue); + break; + case GET_CAMERA_INFO: // Get Camera info + reportCameraInfo(myCAM); + break; + case TAKE_PICTURE: + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + cameraGetPicture(myCAM); + break; + case DEBUG_WRITE_REGISTER: + myCAM->debugWriteRegister(commandBuff + 1); + break; + case STOP_STREAM: + myCAM->stopPreview(); + break; + case GET_FRM_VER_INFO: // Get Firmware version info + reportVerInfo(myCAM); + break; + case GET_SDK_VER_INFO: // Get sdk version info + reportSdkVerInfo(myCAM); + break; + case RESET_CAMERA: + myCAM->reset(); + case SET_IMAGE_QUALITY: + myCAM->setImageQuality((IMAGE_QUALITY)commandBuff[1]); + default: + break; + } + return CAM_ERR_SUCCESS; +} + +void ArducamLink::arducamUartWrite(uint8_t data) +{ + SerialWrite(data); + delayUs(12); +} + +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) +{ + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); +} + +void ArducamLink::printf(char* buff) +{ + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); +} + +uint32_t ArducamLink::arducamUartAvailable(void) +{ + return SerialAvailable(); +} + +uint8_t ArducamLink::arducamUartRead(void) +{ + return SerialRead(); +} + +void ArducamLink::arducamFlush(void) +{ + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.h b/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.h new file mode 100644 index 0000000..ba1f3b1 --- /dev/null +++ b/examples/ArduinoUNOR4Minima/full_featured/ArducamLink.h @@ -0,0 +1,64 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#ifndef __ARDUCAMLINK_H +#define __ARDUCAMLINK_H +#include "Arducam_Mega.h" +#include "stdint.h" + +#define RESET_CAMERA 0XFF +#define SET_PICTURE_RESOLUTION 0X01 +#define SET_VIDEO_RESOLUTION 0X02 +#define SET_BRIGHTNESS 0X03 +#define SET_CONTRAST 0X04 +#define SET_SATURATION 0X05 +#define SET_EV 0X06 +#define SET_WHITEBALANCE 0X07 +#define SET_SPECIAL_EFFECTS 0X08 +#define SET_FOCUS_CONTROL 0X09 +#define SET_EXPOSUREANDGAIN_CONTROL 0X0A +// #define SET_GAIN_CONTROL 0X0B +#define SET_WHILEBALANCE_CONTROL 0X0C +#define SET_MANUAL_GAIN 0X0D +#define SET_MANUAL_EXPOSURE 0X0E +#define GET_CAMERA_INFO 0X0F +#define TAKE_PICTURE 0X10 +#define SET_SHARPNESS 0X11 +#define DEBUG_WRITE_REGISTER 0X12 +#define STOP_STREAM 0X21 +#define GET_FRM_VER_INFO 0X30 +#define GET_SDK_VER_INFO 0X40 +#define SET_IMAGE_QUALITY 0X50 + +#define READ_IMAGE_LENGTH 255 + +class ArducamLink +{ + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: + ArducamLink(); + ~ArducamLink(); + void arducamUartBegin(uint32_t); + void reportCameraInfo(Arducam_Mega*); + void reportVerInfo(Arducam_Mega* myCamera); + void reportSdkVerInfo(Arducam_Mega* myCamera); + void cameraGetPicture(Arducam_Mega*); + void arducamFlush(void); + void send_data_pack(char cmd_type, char* msg); + + public: + void printf(char* buff); + void arducamUartWrite(uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); + uint32_t arducamUartAvailable(void); + uint8_t arducamUartRead(void); +}; + +#endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoUNOR4Minima/full_featured/ArducamUart.h b/examples/ArduinoUNOR4Minima/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoUNOR4Minima/full_featured/ArducamUart.h @@ -0,0 +1,12 @@ +#ifndef __ARDUCAM_SLOT_H +#define __ARDUCAM_SLOT_H +#include + +#define SerialBegin(baudRate) Serial.begin(baudRate) +#define SerialWrite(ch) Serial.write(ch) +#define SerialWriteBuff(buf, len) Serial.write(buf, len) +#define SerialPrintf(str) Serial.print(str) +#define SerialAvailable() Serial.available() +#define SerialRead() Serial.read() +#define delayUs(us) delayMicroseconds(us) +#endif \ No newline at end of file diff --git a/examples/ArduinoUNOR4Minima/full_featured/full_featured.ino b/examples/ArduinoUNOR4Minima/full_featured/full_featured.ino new file mode 100644 index 0000000..5deb3ab --- /dev/null +++ b/examples/ArduinoUNOR4Minima/full_featured/full_featured.ino @@ -0,0 +1,96 @@ +// Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. +// License: MIT License (https://en.wikipedia.org/wiki/MIT_License) +// Web: http://www.ArduCAM.com +// This program is a demo of how to use most of the functions +// of the library with ArduCAM Spi camera, and can run on Arduino platform. +// This demo was made for ArduCAM Spi Camera. +// It needs to be used in combination with PC software. +// It can test ArduCAM Spi Camerafunctions +#include "ArducamLink.h" +#include "Arducam_Mega.h" +#include "SPI.h" +const int MEGA_CS = 7; +Arducam_Mega myCAM(MEGA_CS); +ArducamLink myUart; +uint8_t temp = 0xff; +uint8_t sendFlag = TRUE; +uint8_t commandBuff[20] = {0}; +uint8_t commandLength = 0; +uint32_t readImageLength = 0; +uint8_t jpegHeadFlag = 0; +uint8_t readBuffer(uint8_t* imagebuf, uint8_t length) +{ + if (imagebuf[0] == 0xff && imagebuf[1] == 0xd8) { + jpegHeadFlag = 1; + readImageLength = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x01); + + myUart.arducamUartWrite((uint8_t)(myCAM.getTotalLength() & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 8) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 16) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 24) & 0xff)); + myUart.arducamUartWrite(((CAM_IMAGE_PIX_FMT_JPG & 0x0f) << 4) | 0x01); + } + if (jpegHeadFlag == 1) { + readImageLength += length; + for (uint8_t i = 0; i < length; i++) { + myUart.arducamUartWrite(imagebuf[i]); + } + } + if (readImageLength == myCAM.getTotalLength()) { + jpegHeadFlag = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + } + return sendFlag; +} + +void stop_preivew() +{ + readImageLength = 0; + jpegHeadFlag = 0; + uint32_t len = 9; + + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x06); + myUart.arducamUartWriteBuff((uint8_t*)&len, 4); + myUart.printf("streamoff"); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); +} + +void setup() +{ + SPI.begin(); + myUart.arducamUartBegin(115200); + myUart.send_data_pack(7, "Hello Arduino UNO!"); + myCAM.begin(); + myUart.send_data_pack(8, "Mega start!"); + myCAM.registerCallBack(readBuffer, 200, stop_preivew); +} + +void loop() +{ + if (myUart.arducamUartAvailable()) { + temp = myUart.arducamUartRead(); + delay(5); + if (temp == 0x55) { + while (myUart.arducamUartAvailable()) { + commandBuff[commandLength] = myUart.arducamUartRead(); + if (commandBuff[commandLength] == 0xAA) { + break; + } + commandLength++; + } + myUart.arducamFlush(); + myUart.uartCommandProcessing(&myCAM, commandBuff); + commandLength = 0; + } + } + myCAM.captureThread(); +} \ No newline at end of file diff --git a/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.cpp b/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.cpp new file mode 100644 index 0000000..4f10648 --- /dev/null +++ b/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.cpp @@ -0,0 +1,236 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#include "ArducamLink.h" +#include "ArducamUart.h" + +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) +{ + SerialBegin(baudRate); +} + +void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x03, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff(cameraInstance->verDateAndNumber, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x05, 0xff, 0xbb}; + + uint32_t len = 6; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + arducamUartWriteBuff((uint8_t*)&cameraInstance->currentSDK->sdkVersion, 4); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::reportCameraInfo(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x02, 0xff, 0xbb}; + + uint32_t len = 0; + char buff[400]; + arducamUartWriteBuff(&headAndtail[0], 3); + sprintf(buff, + "ReportCameraInfo\r\nCamera Type:%s\r\nCamera Support Resolution:%d\r\nCamera Support " + "specialeffects:%d\r\nCamera Support Focus:%d\r\nCamera Exposure Value Max:%ld\r\nCamera Exposure Value " + "Min:%d\r\nCamera Gain Value Max:%d\r\nCamera Gain Value Min:%d\r\nCamera Support Sharpness:%d\r\n", + cameraInstance->myCameraInfo.cameraId, cameraInstance->myCameraInfo.supportResolution, + cameraInstance->myCameraInfo.supportSpecialEffects, cameraInstance->myCameraInfo.supportFocus, + cameraInstance->myCameraInfo.exposureValueMax, cameraInstance->myCameraInfo.exposureValueMin, + cameraInstance->myCameraInfo.gainValueMax, cameraInstance->myCameraInfo.gainValueMin, + cameraInstance->myCameraInfo.supportSharpness); + len = strlen(buff); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(buff); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::cameraGetPicture(Arducam_Mega* myCamera) +{ + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x01, 0xff, 0xbb}; + uint8_t buff[READ_IMAGE_LENGTH] = {0}; + + uint8_t rtLength = 0; + uint32_t len = myCamera->getTotalLength(); + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)(&len), 4); + arducamUartWrite(((cameraInstance->currentPictureMode & 0x0f) << 4) | 0x01); + while (myCamera->getReceivedLength()) { + rtLength = readBuff(cameraInstance, buff, READ_IMAGE_LENGTH); + arducamUartWriteBuff(buff, rtLength); + } + arducamUartWriteBuff(&headAndtail[3], 2); +} + +void ArducamLink::send_data_pack(char cmd_type, char* msg) +{ + uint8_t headAndtail[] = {0xff, 0xaa, 0x07, 0xff, 0xbb}; + headAndtail[2] = cmd_type; + uint32_t len = strlen(msg) + 2; + arducamUartWriteBuff(&headAndtail[0], 3); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(msg); + printf("\r\n"); + arducamUartWriteBuff(&headAndtail[3], 2); +} + +uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* commandBuff) +{ + ArducamCamera* cameraInstance = myCAM->getCameraInstance(); + CamStatus state; + uint16_t gainValue = 0; + uint32_t exposureValue = 0; + uint32_t exposureLen1 = 0; + uint32_t exposureLen2 = 0; + uint32_t exposureLen3 = 0; + + uint8_t cameraResolution = cameraInstance->currentPictureMode; + uint8_t cameraFarmat = cameraInstance->currentPixelFormat; + switch (commandBuff[0]) { + case SET_PICTURE_RESOLUTION: // Set Camera Resolution + cameraResolution = commandBuff[1] & 0x0f; + cameraFarmat = (commandBuff[1] & 0x70) >> 4; + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + break; + case SET_VIDEO_RESOLUTION: // Set Video Resolution + cameraResolution = commandBuff[1] & 0x0f; + state = myCAM->startPreview((CAM_VIDEO_MODE)cameraResolution); + if (state == CAM_ERR_NO_CALLBACK) { + printf("callback function is not registered\n"); + } + break; + case SET_BRIGHTNESS: // Set brightness + myCAM->setBrightness((CAM_BRIGHTNESS_LEVEL)commandBuff[1]); + break; + case SET_CONTRAST: // Set Contrast + myCAM->setContrast((CAM_CONTRAST_LEVEL)commandBuff[1]); + break; + case SET_SATURATION: // Set saturation + myCAM->setSaturation((CAM_STAURATION_LEVEL)commandBuff[1]); + break; + case SET_EV: // Set EV + myCAM->setEV((CAM_EV_LEVEL)commandBuff[1]); + break; + case SET_WHITEBALANCE: // Set White balance + myCAM->setAutoWhiteBalanceMode((CAM_WHITE_BALANCE)commandBuff[1]); + break; + case SET_SPECIAL_EFFECTS: // Set Special effects + myCAM->setColorEffect((CAM_COLOR_FX)commandBuff[1]); + break; + case SET_FOCUS_CONTROL: // Focus Control + myCAM->setAutoFocus(commandBuff[1]); + if (commandBuff[1] == 0) { + myCAM->setAutoFocus(0x02); + } + break; + case SET_EXPOSUREANDGAIN_CONTROL: // exposure and Gain control + myCAM->setAutoExposure(commandBuff[1] & 0x01); + myCAM->setAutoISOSensitive(commandBuff[1] & 0x01); + break; + case SET_WHILEBALANCE_CONTROL: // while balance control + myCAM->setAutoWhiteBalance(commandBuff[1] & 0x01); + break; + case SET_SHARPNESS: + myCAM->setSharpness((CAM_SHARPNESS_LEVEL)commandBuff[1]); + break; + case SET_MANUAL_GAIN: // manual gain control + gainValue = (commandBuff[1] << 8) | commandBuff[2]; + myCAM->setISOSensitivity(gainValue); + break; + case SET_MANUAL_EXPOSURE: // manual exposure control + exposureLen1 = commandBuff[1]; + exposureLen2 = commandBuff[2]; + exposureLen3 = commandBuff[3]; + exposureValue = (exposureLen1 << 16) | (exposureLen2 << 8) | exposureLen3; + myCAM->setAbsoluteExposure(exposureValue); + break; + case GET_CAMERA_INFO: // Get Camera info + reportCameraInfo(myCAM); + break; + case TAKE_PICTURE: + myCAM->takePicture((CAM_IMAGE_MODE)cameraResolution, (CAM_IMAGE_PIX_FMT)cameraFarmat); + cameraGetPicture(myCAM); + break; + case DEBUG_WRITE_REGISTER: + myCAM->debugWriteRegister(commandBuff + 1); + break; + case STOP_STREAM: + myCAM->stopPreview(); + break; + case GET_FRM_VER_INFO: // Get Firmware version info + reportVerInfo(myCAM); + break; + case GET_SDK_VER_INFO: // Get sdk version info + reportSdkVerInfo(myCAM); + break; + case RESET_CAMERA: + myCAM->reset(); + case SET_IMAGE_QUALITY: + myCAM->setImageQuality((IMAGE_QUALITY)commandBuff[1]); + default: + break; + } + return CAM_ERR_SUCCESS; +} + +void ArducamLink::arducamUartWrite(uint8_t data) +{ + SerialWrite(data); + delayUs(12); +} + +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) +{ + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); +} + +void ArducamLink::printf(char* buff) +{ + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); +} + +uint32_t ArducamLink::arducamUartAvailable(void) +{ + return SerialAvailable(); +} + +uint8_t ArducamLink::arducamUartRead(void) +{ + return SerialRead(); +} + +void ArducamLink::arducamFlush(void) +{ + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.h b/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.h new file mode 100644 index 0000000..ba1f3b1 --- /dev/null +++ b/examples/ArduinoUNOR4Wifi/full_featured/ArducamLink.h @@ -0,0 +1,64 @@ +/* + * This file is part of the Arducam SPI Camera project. + * + * Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. + * + * This work is licensed under the MIT license, see the file LICENSE for details. + * + */ +#ifndef __ARDUCAMLINK_H +#define __ARDUCAMLINK_H +#include "Arducam_Mega.h" +#include "stdint.h" + +#define RESET_CAMERA 0XFF +#define SET_PICTURE_RESOLUTION 0X01 +#define SET_VIDEO_RESOLUTION 0X02 +#define SET_BRIGHTNESS 0X03 +#define SET_CONTRAST 0X04 +#define SET_SATURATION 0X05 +#define SET_EV 0X06 +#define SET_WHITEBALANCE 0X07 +#define SET_SPECIAL_EFFECTS 0X08 +#define SET_FOCUS_CONTROL 0X09 +#define SET_EXPOSUREANDGAIN_CONTROL 0X0A +// #define SET_GAIN_CONTROL 0X0B +#define SET_WHILEBALANCE_CONTROL 0X0C +#define SET_MANUAL_GAIN 0X0D +#define SET_MANUAL_EXPOSURE 0X0E +#define GET_CAMERA_INFO 0X0F +#define TAKE_PICTURE 0X10 +#define SET_SHARPNESS 0X11 +#define DEBUG_WRITE_REGISTER 0X12 +#define STOP_STREAM 0X21 +#define GET_FRM_VER_INFO 0X30 +#define GET_SDK_VER_INFO 0X40 +#define SET_IMAGE_QUALITY 0X50 + +#define READ_IMAGE_LENGTH 255 + +class ArducamLink +{ + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: + ArducamLink(); + ~ArducamLink(); + void arducamUartBegin(uint32_t); + void reportCameraInfo(Arducam_Mega*); + void reportVerInfo(Arducam_Mega* myCamera); + void reportSdkVerInfo(Arducam_Mega* myCamera); + void cameraGetPicture(Arducam_Mega*); + void arducamFlush(void); + void send_data_pack(char cmd_type, char* msg); + + public: + void printf(char* buff); + void arducamUartWrite(uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); + uint32_t arducamUartAvailable(void); + uint8_t arducamUartRead(void); +}; + +#endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoUNOR4Wifi/full_featured/ArducamUart.h b/examples/ArduinoUNOR4Wifi/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoUNOR4Wifi/full_featured/ArducamUart.h @@ -0,0 +1,12 @@ +#ifndef __ARDUCAM_SLOT_H +#define __ARDUCAM_SLOT_H +#include + +#define SerialBegin(baudRate) Serial.begin(baudRate) +#define SerialWrite(ch) Serial.write(ch) +#define SerialWriteBuff(buf, len) Serial.write(buf, len) +#define SerialPrintf(str) Serial.print(str) +#define SerialAvailable() Serial.available() +#define SerialRead() Serial.read() +#define delayUs(us) delayMicroseconds(us) +#endif \ No newline at end of file diff --git a/examples/ArduinoUNOR4Wifi/full_featured/full_featured.ino b/examples/ArduinoUNOR4Wifi/full_featured/full_featured.ino new file mode 100644 index 0000000..5deb3ab --- /dev/null +++ b/examples/ArduinoUNOR4Wifi/full_featured/full_featured.ino @@ -0,0 +1,96 @@ +// Copyright 2021 Arducam Technology co., Ltd. All Rights Reserved. +// License: MIT License (https://en.wikipedia.org/wiki/MIT_License) +// Web: http://www.ArduCAM.com +// This program is a demo of how to use most of the functions +// of the library with ArduCAM Spi camera, and can run on Arduino platform. +// This demo was made for ArduCAM Spi Camera. +// It needs to be used in combination with PC software. +// It can test ArduCAM Spi Camerafunctions +#include "ArducamLink.h" +#include "Arducam_Mega.h" +#include "SPI.h" +const int MEGA_CS = 7; +Arducam_Mega myCAM(MEGA_CS); +ArducamLink myUart; +uint8_t temp = 0xff; +uint8_t sendFlag = TRUE; +uint8_t commandBuff[20] = {0}; +uint8_t commandLength = 0; +uint32_t readImageLength = 0; +uint8_t jpegHeadFlag = 0; +uint8_t readBuffer(uint8_t* imagebuf, uint8_t length) +{ + if (imagebuf[0] == 0xff && imagebuf[1] == 0xd8) { + jpegHeadFlag = 1; + readImageLength = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x01); + + myUart.arducamUartWrite((uint8_t)(myCAM.getTotalLength() & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 8) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 16) & 0xff)); + myUart.arducamUartWrite((uint8_t)((myCAM.getTotalLength() >> 24) & 0xff)); + myUart.arducamUartWrite(((CAM_IMAGE_PIX_FMT_JPG & 0x0f) << 4) | 0x01); + } + if (jpegHeadFlag == 1) { + readImageLength += length; + for (uint8_t i = 0; i < length; i++) { + myUart.arducamUartWrite(imagebuf[i]); + } + } + if (readImageLength == myCAM.getTotalLength()) { + jpegHeadFlag = 0; + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + } + return sendFlag; +} + +void stop_preivew() +{ + readImageLength = 0; + jpegHeadFlag = 0; + uint32_t len = 9; + + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xAA); + myUart.arducamUartWrite(0x06); + myUart.arducamUartWriteBuff((uint8_t*)&len, 4); + myUart.printf("streamoff"); + myUart.arducamUartWrite(0xff); + myUart.arducamUartWrite(0xBB); +} + +void setup() +{ + SPI.begin(); + myUart.arducamUartBegin(115200); + myUart.send_data_pack(7, "Hello Arduino UNO!"); + myCAM.begin(); + myUart.send_data_pack(8, "Mega start!"); + myCAM.registerCallBack(readBuffer, 200, stop_preivew); +} + +void loop() +{ + if (myUart.arducamUartAvailable()) { + temp = myUart.arducamUartRead(); + delay(5); + if (temp == 0x55) { + while (myUart.arducamUartAvailable()) { + commandBuff[commandLength] = myUart.arducamUartRead(); + if (commandBuff[commandLength] == 0xAA) { + break; + } + commandLength++; + } + myUart.arducamFlush(); + myUart.uartCommandProcessing(&myCAM, commandBuff); + commandLength = 0; + } + } + myCAM.captureThread(); +} \ No newline at end of file diff --git a/library.properties b/library.properties index b1a1ea8..a30679c 100644 --- a/library.properties +++ b/library.properties @@ -1,7 +1,7 @@ name=Arducam_Mega -version=2.0.4 -author=Arducam -maintainer=Arducam +version=2.0.5 +author=Arducam +maintainer=Arducam sentence=Examples of spi cameras used on different hardware. paragraph=Supported platforms are ArduinoDue, ArduinoESP32, ArduinoESP32-C3, ArduinoESP8266, ArduinoMicrobit, ArduinoNano33Ble, ArduinoUNO, ArduinoMega2560,etc. category=Device Control diff --git a/src/Arducam/Platform.h b/src/Arducam/Platform.h index ba86dd9..da0e74b 100644 --- a/src/Arducam/Platform.h +++ b/src/Arducam/Platform.h @@ -14,7 +14,7 @@ #if defined(STM32F10X_MD) #include "Stm32Hal.h" #elif defined(__AVR__) || defined(__SAM3X8E__) || defined(ESP32) || defined(ESP8266) || \ - defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_RP2040) || defined(NRF52833_XXAA) + defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_RP2040) || defined(NRF52833_XXAA) || defined(ARDUINO_ARCH_RENESAS) #include "ArduinoHal.h" #elif defined(__MSP430G2553__) #include "Msp430Hal.h"