From c2370c4ce864ce22845a0357601ffe7e863358e9 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 1 Dec 2023 17:20:21 +0800 Subject: [PATCH] Modify the low power mode to adapt to the new version of GUI software --- .../ArduinoDue/full_featured/ArducamLink.cpp | 352 +++++++++--------- .../ArduinoDue/full_featured/ArducamLink.h | 68 ++-- .../ArduinoDue/full_featured/ArducamUart.h | 12 + .../full_featured/full_featured.ino | 189 ++++------ .../ArduinoDue/lowPowerMode/ArducamLink.cpp | 337 +++++++++-------- .../ArduinoDue/lowPowerMode/ArducamLink.h | 67 ++-- .../ArduinoDue/lowPowerMode/ArducamUart.h | 12 + .../ArduinoDue/lowPowerMode/lowPowerMode.ino | 4 +- .../full_featured/ArducamLink.cpp | 141 +++---- .../full_featured/ArducamLink.h | 63 ++-- .../full_featured/ArducamUart.h | 12 + .../full_featured/ArducamLink.cpp | 351 ++++++++--------- .../full_featured/ArducamLink.h | 68 ++-- .../full_featured/ArducamUart.h | 12 + .../full_featured/full_featured.ino | 189 ++++------ .../ArduinoUNO/lowPowerMode/ArducamLink.cpp | 337 +++++++++-------- .../ArduinoUNO/lowPowerMode/ArducamLink.h | 67 ++-- .../ArduinoUNO/lowPowerMode/ArducamUart.h | 12 + .../ArduinoUNO/lowPowerMode/lowPowerMode.ino | 4 +- .../lowPowerMode/ArducamLink.cpp | 337 +++++++++-------- .../lowPowerMode/ArducamLink.h | 67 ++-- .../lowPowerMode/ArducamUart.h | 12 + .../lowPowerMode/lowPowerMode.ino | 4 +- library.properties | 2 +- src/Arducam/ArducamCamera.c | 4 +- 25 files changed, 1432 insertions(+), 1291 deletions(-) create mode 100644 examples/ArduinoDue/full_featured/ArducamUart.h create mode 100644 examples/ArduinoDue/lowPowerMode/ArducamUart.h create mode 100644 examples/ArduinoESP32-C3/full_featured/ArducamUart.h create mode 100644 examples/ArduinoMicrobit/full_featured/ArducamUart.h create mode 100644 examples/ArduinoUNO/lowPowerMode/ArducamUart.h create mode 100644 examples/ArdunioMega2560/lowPowerMode/ArducamUart.h diff --git a/examples/ArduinoDue/full_featured/ArducamLink.cpp b/examples/ArduinoDue/full_featured/ArducamLink.cpp index afbd3be..4f10648 100644 --- a/examples/ArduinoDue/full_featured/ArducamLink.cpp +++ b/examples/ArduinoDue/full_featured/ArducamLink.cpp @@ -7,216 +7,230 @@ * */ #include "ArducamLink.h" -#include "Arduino.h" +#include "ArducamUart.h" -ArducamLink::ArducamLink() -{ -} - -ArducamLink::~ArducamLink() -{ -} +ArducamLink::ArducamLink() {} +ArducamLink::~ArducamLink() {} void ArducamLink::arducamUartBegin(uint32_t baudRate) { - Serial.begin(baudRate); + SerialBegin(baudRate); } - void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) { - ArducamCamera* cameraInstance = myCamera->getCameraInstance(); - Serial.write(cameraInstance->verDate,3); - Serial.println(); + 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(); - - Serial.write((cameraInstance->currentSDK->sdkVersion>>24)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion>>16)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion>>8)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion)&0xFF); - Serial.println(); + 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(); - Serial.println("ReportCameraInfo"); - Serial.print("Camera Type:"); - Serial.println(cameraInstance->myCameraInfo.cameraId); - Serial.print("Camera Support Resolution:"); - Serial.println(cameraInstance->myCameraInfo.supportResolution); - Serial.print("Camera Support specialeffects:"); - Serial.println(cameraInstance->myCameraInfo.supportSpecialEffects); - Serial.print("Camera Support Focus:"); - Serial.println(cameraInstance->myCameraInfo.supportFocus); - Serial.print("Camera Exposure Value Max:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMax); - Serial.print("Camera Exposure Value Min:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMin); - Serial.print("Camera Gain Value Max:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMax); - Serial.print("Camera Gain Value Min:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMin); - Serial.print("Camera Support Sharpness:"); - Serial.println(cameraInstance->myCameraInfo.supportSharpness); + 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); } -uint8_t buff[READ_IMAGE_LENGTH] = {0}; - void ArducamLink::cameraGetPicture(Arducam_Mega* myCamera) { - ArducamCamera* cameraInstance = myCamera->getCameraInstance(); - uint8_t rtLength = 0; - arducamUartWrite(0x55); - arducamUartWrite(0xAA); - arducamUartWrite(cameraInstance->cameraDataFormat); - arducamUartWrite((uint8_t)(myCamera->getTotalLength()&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>8)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>16)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>24)&0xff)); - while (myCamera->getReceivedLength()) - { - rtLength=readBuff(cameraInstance,buff,READ_IMAGE_LENGTH); - for (uint8_t i = 0; i < rtLength; i++) - { - arducamUartWrite(buff[i]); - } - } - arducamUartWrite(0x55); - arducamUartWrite(0xBB); + 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; +} -uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM,uint8_t* commandBuff) +void ArducamLink::arducamUartWrite(uint8_t data) { - 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) - { - Serial.println("callback function is not registered"); - } - 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_EXPOSURE_CONTROL: //exposure control - myCAM->setAutoExposure(commandBuff[1]&0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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 GET_FRM_VER_INFO: //Get Firmware version info - reportVerInfo(myCAM); - break; - case GET_SDK_VER_INFO: //Get sdk version info - reportSdkVerInfo(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; - - default: - break; - } - return CAM_ERR_SUCCESS; + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWriteBuff(uint8_t* buff,uint8_t length) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(buff,length); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::printf(char* buff) { - Serial.write(data); - delayMicroseconds(12); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) { - while(arducamUartAvailable()) - { - arducamUartRead(); - } -} \ No newline at end of file + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoDue/full_featured/ArducamLink.h b/examples/ArduinoDue/full_featured/ArducamLink.h index cf4332d..ba1f3b1 100644 --- a/examples/ArduinoDue/full_featured/ArducamLink.h +++ b/examples/ArduinoDue/full_featured/ArducamLink.h @@ -11,54 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); }; - - - - - - #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoDue/full_featured/ArducamUart.h b/examples/ArduinoDue/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoDue/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/ArduinoDue/full_featured/full_featured.ino b/examples/ArduinoDue/full_featured/full_featured.ino index 1c8f603..a330c29 100644 --- a/examples/ArduinoDue/full_featured/full_featured.ino +++ b/examples/ArduinoDue/full_featured/full_featured.ino @@ -6,142 +6,89 @@ // 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 "Arducam_Mega.h" -// #include "Platform.h" #include "ArducamLink.h" - +#include "Arducam_Mega.h" const int CS = 7; Arducam_Mega myCAM(CS); ArducamLink myUart; -uint8_t temp = 0xff; -uint8_t sendFlag = TRUE; -uint8_t commandBuff[20] = { 0 }; -uint8_t commandLength = 0; +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) { - CamStatus state; - uint16_t gainValue = 0; - uint32_t exposureValue = 0; - uint32_t exposureLen1 = 0; - uint32_t exposureLen2 = 0; - uint32_t exposureLen3 = 0; - if (myUart.arducamUartAvailable()) { - temp = myUart.arducamUartRead(); - arducamDelayMs(1); - if (temp == 0x55) { - while (myUart.arducamUartAvailable()) { - commandBuff[commandLength] = myUart.arducamUartRead(); - if (commandBuff[commandLength] == 0xAA) { - break; - } - commandLength++; - } - if (commandBuff[0] == 0x21) { +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(0x55); + myUart.arducamUartWrite(0xff); myUart.arducamUartWrite(0xBB); - return FALSE; - } - switch (commandBuff[0]) { - 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_EXPOSURE_CONTROL: //exposure control - myCAM.setAutoExposure(commandBuff[1] & 0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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; - } - commandLength = 0; } - } - if (imagebuf[0] == 0xff && imagebuf[1] == 0xd8) { - jpegHeadFlag = 1; - myUart.arducamUartWrite(0x55); - myUart.arducamUartWrite(0xAA); - myUart.arducamUartWrite(CAM_IMAGE_PIX_FMT_JPG); - 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)); - } - if (jpegHeadFlag == 1) { - readImageLength += length; - for (uint8_t i = 0; i < length; i++) { - myUart.arducamUartWrite(imagebuf[i]); - } - } - if (readImageLength == myCAM.getTotalLength()) { + return sendFlag; +} + +void stop_preivew() +{ readImageLength = 0; - jpegHeadFlag = 0; - myUart.arducamUartWrite(0x55); + 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); - } - return sendFlag; } -void setup() { - myUart.arducamUartBegin(115200); - myCAM.begin(); - myCAM.registerCallBack(readBuffer, 200); +void setup() +{ + myUart.arducamUartBegin(115200); + myUart.send_data_pack(7, "Hello Arduino Due!"); + myCAM.begin(); + myUart.send_data_pack(8, "Mega start!"); + myCAM.registerCallBack(readBuffer, 200, stop_preivew); } -void loop() { - if (myUart.arducamUartAvailable()) { - temp = myUart.arducamUartRead(); - arducamDelayMs(5); - if (temp == 0x55) { - while (myUart.arducamUartAvailable()) { - commandBuff[commandLength] = myUart.arducamUartRead(); - if (commandBuff[commandLength] == 0xAA) { - break; +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; } - commandLength++; - } - myUart.arducamFlush(); - myUart.uartCommandProcessing(&myCAM, commandBuff); - commandLength = 0; } - } + myCAM.captureThread(); } \ No newline at end of file diff --git a/examples/ArduinoDue/lowPowerMode/ArducamLink.cpp b/examples/ArduinoDue/lowPowerMode/ArducamLink.cpp index 99a9bcd..b6524ce 100644 --- a/examples/ArduinoDue/lowPowerMode/ArducamLink.cpp +++ b/examples/ArduinoDue/lowPowerMode/ArducamLink.cpp @@ -7,190 +7,231 @@ * */ #include "ArducamLink.h" -#include "Arduino.h" +#include "ArducamUart.h" -ArducamLink::ArducamLink() +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) { + SerialBegin(baudRate); } -ArducamLink::~ArducamLink() +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::arducamUartBegin(uint32_t baudRate) +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) { - Serial.begin(baudRate); -} + 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(); - Serial.println("ReportCameraInfo"); - Serial.print("Camera Type:"); - Serial.println(cameraInstance->myCameraInfo.cameraId); - Serial.print("Camera Support Resolution:"); - Serial.println(cameraInstance->myCameraInfo.supportResolution); - Serial.print("Camera Support specialeffects:"); - Serial.println(cameraInstance->myCameraInfo.supportSpecialEffects); - Serial.print("Camera Support Focus:"); - Serial.println(cameraInstance->myCameraInfo.supportFocus); - Serial.print("Camera Exposure Value Max:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMax); - Serial.print("Camera Exposure Value Min:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMin); - Serial.print("Camera Gain Value Max:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMax); - Serial.print("Camera Gain Value Min:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMin); - Serial.print("Camera Support Sharpness:"); - Serial.println(cameraInstance->myCameraInfo.supportSharpness); + 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 buff[READ_IMAGE_LENGTH] = {0}; - uint8_t rtLength = 0; - arducamUartWrite(0x55); - arducamUartWrite(0xAA); - arducamUartWrite(cameraInstance->cameraDataFormat); - arducamUartWrite((uint8_t)(myCamera->getTotalLength()&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>8)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>16)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>24)&0xff)); - while (myCamera->getReceivedLength()) - { - rtLength=readBuff(cameraInstance,buff,READ_IMAGE_LENGTH); - for (uint8_t i = 0; i < rtLength; i++) - { - arducamUartWrite(buff[i]); - } - } - arducamUartWrite(0x55); - arducamUartWrite(0xBB); + 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 ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM,uint8_t* commandBuff) + 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: + delay(1000); + 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) { - 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) - { - Serial.println("callback function is not registered"); - } - 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_EXPOSURE_CONTROL: //exposure control - myCAM->setAutoExposure(commandBuff[1]&0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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; - default: - break; - } - return CAM_ERR_SUCCESS; + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWriteBuff(uint8_t* buff,uint8_t length) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(buff,length); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::printf(char* buff) { - Serial.write(data); - delayMicroseconds(10); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) { - while(arducamUartAvailable()) - { - arducamUartRead(); - } -} \ No newline at end of file + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoDue/lowPowerMode/ArducamLink.h b/examples/ArduinoDue/lowPowerMode/ArducamLink.h index 2774e16..ba1f3b1 100644 --- a/examples/ArduinoDue/lowPowerMode/ArducamLink.h +++ b/examples/ArduinoDue/lowPowerMode/ArducamLink.h @@ -11,49 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); }; - - - - - - #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoDue/lowPowerMode/ArducamUart.h b/examples/ArduinoDue/lowPowerMode/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoDue/lowPowerMode/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/ArduinoDue/lowPowerMode/lowPowerMode.ino b/examples/ArduinoDue/lowPowerMode/lowPowerMode.ino index d5bfe04..1f1dd98 100644 --- a/examples/ArduinoDue/lowPowerMode/lowPowerMode.ino +++ b/examples/ArduinoDue/lowPowerMode/lowPowerMode.ino @@ -32,7 +32,7 @@ void setup() { if (myUart.arducamUartAvailable()) { temp = myUart.arducamUartRead(); - arducamDelayMs(5); + delay(5); if (temp == 0x55) { while (myUart.arducamUartAvailable()) @@ -46,7 +46,7 @@ void setup() { } myUart.arducamFlush(); myCAM.lowPowerOff(); - arducamDelayMs(300); //If the camera is 3MP, you need to lengthen the delay + delay(500); //If the camera is 3MP, you need to lengthen the delay myUart.uartCommandProcessing(&myCAM,commandBuff); myCAM.lowPowerOn(); commandLength = 0; diff --git a/examples/ArduinoESP32-C3/full_featured/ArducamLink.cpp b/examples/ArduinoESP32-C3/full_featured/ArducamLink.cpp index d877b68..4f10648 100644 --- a/examples/ArduinoESP32-C3/full_featured/ArducamLink.cpp +++ b/examples/ArduinoESP32-C3/full_featured/ArducamLink.cpp @@ -7,7 +7,7 @@ * */ #include "ArducamLink.h" -#include +#include "ArducamUart.h" ArducamLink::ArducamLink() {} @@ -15,51 +15,43 @@ ArducamLink::~ArducamLink() {} void ArducamLink::arducamUartBegin(uint32_t baudRate) { - Serial.begin(baudRate); + SerialBegin(baudRate); } void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) { ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x03, 0xff, 0xbb}; - uint32_t len = 5; - Serial.write(0xff); - Serial.write(0xAA); - Serial.write(0x03); - Serial.write((uint8_t*)&len, 4); - Serial.write(cameraInstance->verDate, 3); - Serial.println(); - Serial.write(0xff); - Serial.write(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; - Serial.write(0xff); - Serial.write(0xAA); - Serial.write(0x05); - Serial.write((uint8_t*)&len, 4); - Serial.write((cameraInstance->currentSDK->sdkVersion >> 24) & 0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion >> 16) & 0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion >> 8) & 0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion) & 0xFF); - Serial.println(); - Serial.write(0xff); - Serial.write(0xBB); + 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]; - Serial.write(0xff); - Serial.write(0xAA); - Serial.write(0x02); + 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 " @@ -70,60 +62,64 @@ void ArducamLink::reportCameraInfo(Arducam_Mega* myCamera) cameraInstance->myCameraInfo.gainValueMax, cameraInstance->myCameraInfo.gainValueMin, cameraInstance->myCameraInfo.supportSharpness); len = strlen(buff); - Serial.write((uint8_t*)&len, 4); - Serial.write(buff, len); - Serial.write(0xff); - Serial.write(0xBB); + arducamUartWriteBuff((uint8_t*)&len, 4); + printf(buff); + arducamUartWriteBuff(&headAndtail[3], 2); } void ArducamLink::cameraGetPicture(Arducam_Mega* myCamera) { - ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + ArducamCamera* cameraInstance = myCamera->getCameraInstance(); + uint8_t headAndtail[] = {0xff, 0xaa, 0x01, 0xff, 0xbb}; uint8_t buff[READ_IMAGE_LENGTH] = {0}; uint8_t rtLength = 0; - arducamUartWrite(0xff); - arducamUartWrite(0xAA); - arducamUartWrite(0x01); - arducamUartWrite((uint8_t)(myCamera->getTotalLength() & 0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength() >> 8) & 0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength() >> 16) & 0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength() >> 24) & 0xff)); - arducamUartWrite(((cameraInstance->cameraDataFormat & 0x0f) << 4) | 0x01); + 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); - for (uint8_t i = 0; i < rtLength; i++) { - arducamUartWrite(buff[i]); - } + arducamUartWriteBuff(buff, rtLength); } - arducamUartWrite(0xff); - arducamUartWrite(0xBB); + 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; + uint16_t gainValue = 0; uint32_t exposureValue = 0; - uint32_t exposureLen1 = 0; - uint32_t exposureLen2 = 0; - uint32_t exposureLen3 = 0; + uint32_t exposureLen1 = 0; + uint32_t exposureLen2 = 0; + uint32_t exposureLen3 = 0; uint8_t cameraResolution = cameraInstance->currentPictureMode; - uint8_t cameraFarmat = cameraInstance->currentPixelFormat; + uint8_t cameraFarmat = cameraInstance->currentPixelFormat; switch (commandBuff[0]) { - case SET_PICTURE_RESOLUTION: // Set Camera Resolution cameraResolution = commandBuff[1] & 0x0f; - cameraFarmat = (commandBuff[1] & 0x70) >> 4; + 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); + state = myCAM->startPreview((CAM_VIDEO_MODE)cameraResolution); if (state == CAM_ERR_NO_CALLBACK) { - Serial.println("callback function is not registered"); + printf("callback function is not registered\n"); } break; case SET_BRIGHTNESS: // Set brightness @@ -150,10 +146,8 @@ uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* command myCAM->setAutoFocus(0x02); } break; - case SET_EXPOSURE_CONTROL: // exposure control + case SET_EXPOSUREANDGAIN_CONTROL: // exposure and Gain control myCAM->setAutoExposure(commandBuff[1] & 0x01); - break; - case SET_GAIN_CONTROL: // Gain control myCAM->setAutoISOSensitive(commandBuff[1] & 0x01); break; case SET_WHILEBALANCE_CONTROL: // while balance control @@ -167,9 +161,9 @@ uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* command myCAM->setISOSensitivity(gainValue); break; case SET_MANUAL_EXPOSURE: // manual exposure control - exposureLen1 = commandBuff[1]; - exposureLen2 = commandBuff[2]; - exposureLen3 = commandBuff[3]; + exposureLen1 = commandBuff[1]; + exposureLen2 = commandBuff[2]; + exposureLen3 = commandBuff[3]; exposureValue = (exposureLen1 << 16) | (exposureLen2 << 8) | exposureLen3; myCAM->setAbsoluteExposure(exposureValue); break; @@ -192,37 +186,46 @@ uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM, uint8_t* command 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::arducamUartWriteBuff(uint8_t* buff, uint8_t length) +void ArducamLink::arducamUartWrite(uint8_t data) { - Serial.write(buff, length); + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(data); - delayMicroseconds(12); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::printf(char *buff) +void ArducamLink::printf(char* buff) { - Serial.print(buff); - delayMicroseconds(12); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) @@ -230,4 +233,4 @@ void ArducamLink::arducamFlush(void) while (arducamUartAvailable()) { arducamUartRead(); } -} \ No newline at end of file +} diff --git a/examples/ArduinoESP32-C3/full_featured/ArducamLink.h b/examples/ArduinoESP32-C3/full_featured/ArducamLink.h index 5b4b83e..ba1f3b1 100644 --- a/examples/ArduinoESP32-C3/full_featured/ArducamLink.h +++ b/examples/ArduinoESP32-C3/full_featured/ArducamLink.h @@ -11,49 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); - void printf(char *buff); }; #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoESP32-C3/full_featured/ArducamUart.h b/examples/ArduinoESP32-C3/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoESP32-C3/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/ArduinoMicrobit/full_featured/ArducamLink.cpp b/examples/ArduinoMicrobit/full_featured/ArducamLink.cpp index a4d3e07..4f10648 100644 --- a/examples/ArduinoMicrobit/full_featured/ArducamLink.cpp +++ b/examples/ArduinoMicrobit/full_featured/ArducamLink.cpp @@ -7,215 +7,230 @@ * */ #include "ArducamLink.h" -#include "Arduino.h" +#include "ArducamUart.h" -ArducamLink::ArducamLink() -{ -} - -ArducamLink::~ArducamLink() -{ -} +ArducamLink::ArducamLink() {} +ArducamLink::~ArducamLink() {} void ArducamLink::arducamUartBegin(uint32_t baudRate) { - Serial.begin(baudRate); + SerialBegin(baudRate); } - void ArducamLink::reportVerInfo(Arducam_Mega* myCamera) { - ArducamCamera* cameraInstance = myCamera->getCameraInstance(); - Serial.write(cameraInstance->verDate,3); - Serial.println(); + 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(); - - Serial.write((cameraInstance->currentSDK->sdkVersion>>24)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion>>16)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion>>8)&0xFF); - Serial.write((cameraInstance->currentSDK->sdkVersion)&0xFF); - Serial.println(); + 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(); - Serial.println("ReportCameraInfo"); - Serial.print("Camera Type:"); - Serial.println(cameraInstance->myCameraInfo.cameraId); - Serial.print("Camera Support Resolution:"); - Serial.println(cameraInstance->myCameraInfo.supportResolution); - Serial.print("Camera Support specialeffects:"); - Serial.println(cameraInstance->myCameraInfo.supportSpecialEffects); - Serial.print("Camera Support Focus:"); - Serial.println(cameraInstance->myCameraInfo.supportFocus); - Serial.print("Camera Exposure Value Max:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMax); - Serial.print("Camera Exposure Value Min:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMin); - Serial.print("Camera Gain Value Max:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMax); - Serial.print("Camera Gain Value Min:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMin); - Serial.print("Camera Support Sharpness:"); - Serial.println(cameraInstance->myCameraInfo.supportSharpness); + 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 buff[READ_IMAGE_LENGTH] = {0}; - uint8_t rtLength = 0; - arducamUartWrite(0x55); - arducamUartWrite(0xAA); - arducamUartWrite(cameraInstance->cameraDataFormat); - arducamUartWrite((uint8_t)(myCamera->getTotalLength()&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>8)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>16)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>24)&0xff)); - while (myCamera->getReceivedLength()) - { - rtLength=readBuff(cameraInstance,buff,READ_IMAGE_LENGTH); - for (uint8_t i = 0; i < rtLength; i++) - { - arducamUartWrite(buff[i]); - } - } - arducamUartWrite(0x55); - arducamUartWrite(0xBB); + 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; +} -uint8_t ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM,uint8_t* commandBuff) +void ArducamLink::arducamUartWrite(uint8_t data) { - 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) - { - Serial.println("callback function is not registered"); - } - 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_EXPOSURE_CONTROL: //exposure control - myCAM->setAutoExposure(commandBuff[1]&0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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 GET_FRM_VER_INFO: //Get Firmware version info - reportVerInfo(myCAM); - break; - case GET_SDK_VER_INFO: //Get sdk version info - reportSdkVerInfo(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; - - default: - break; - } - return CAM_ERR_SUCCESS; + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWriteBuff(uint8_t* buff,uint8_t length) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(buff,length); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::printf(char* buff) { - Serial.write(data); - delayMicroseconds(12); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) { - while(arducamUartAvailable()) - { - arducamUartRead(); - } -} \ No newline at end of file + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoMicrobit/full_featured/ArducamLink.h b/examples/ArduinoMicrobit/full_featured/ArducamLink.h index cf4332d..ba1f3b1 100644 --- a/examples/ArduinoMicrobit/full_featured/ArducamLink.h +++ b/examples/ArduinoMicrobit/full_featured/ArducamLink.h @@ -11,54 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); }; - - - - - - #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoMicrobit/full_featured/ArducamUart.h b/examples/ArduinoMicrobit/full_featured/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoMicrobit/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/ArduinoMicrobit/full_featured/full_featured.ino b/examples/ArduinoMicrobit/full_featured/full_featured.ino index 1c8f603..e563fb9 100644 --- a/examples/ArduinoMicrobit/full_featured/full_featured.ino +++ b/examples/ArduinoMicrobit/full_featured/full_featured.ino @@ -6,142 +6,89 @@ // 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 "Arducam_Mega.h" -// #include "Platform.h" #include "ArducamLink.h" - +#include "Arducam_Mega.h" const int CS = 7; Arducam_Mega myCAM(CS); ArducamLink myUart; -uint8_t temp = 0xff; -uint8_t sendFlag = TRUE; -uint8_t commandBuff[20] = { 0 }; -uint8_t commandLength = 0; +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) { - CamStatus state; - uint16_t gainValue = 0; - uint32_t exposureValue = 0; - uint32_t exposureLen1 = 0; - uint32_t exposureLen2 = 0; - uint32_t exposureLen3 = 0; - if (myUart.arducamUartAvailable()) { - temp = myUart.arducamUartRead(); - arducamDelayMs(1); - if (temp == 0x55) { - while (myUart.arducamUartAvailable()) { - commandBuff[commandLength] = myUart.arducamUartRead(); - if (commandBuff[commandLength] == 0xAA) { - break; - } - commandLength++; - } - if (commandBuff[0] == 0x21) { +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(0x55); + myUart.arducamUartWrite(0xff); myUart.arducamUartWrite(0xBB); - return FALSE; - } - switch (commandBuff[0]) { - 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_EXPOSURE_CONTROL: //exposure control - myCAM.setAutoExposure(commandBuff[1] & 0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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; - } - commandLength = 0; } - } - if (imagebuf[0] == 0xff && imagebuf[1] == 0xd8) { - jpegHeadFlag = 1; - myUart.arducamUartWrite(0x55); - myUart.arducamUartWrite(0xAA); - myUart.arducamUartWrite(CAM_IMAGE_PIX_FMT_JPG); - 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)); - } - if (jpegHeadFlag == 1) { - readImageLength += length; - for (uint8_t i = 0; i < length; i++) { - myUart.arducamUartWrite(imagebuf[i]); - } - } - if (readImageLength == myCAM.getTotalLength()) { + return sendFlag; +} + +void stop_preivew() +{ readImageLength = 0; - jpegHeadFlag = 0; - myUart.arducamUartWrite(0x55); + 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); - } - return sendFlag; } -void setup() { - myUart.arducamUartBegin(115200); - myCAM.begin(); - myCAM.registerCallBack(readBuffer, 200); +void setup() +{ + myUart.arducamUartBegin(115200); + myUart.send_data_pack(7, "Hello Arduino Microbit!"); + myCAM.begin(); + myUart.send_data_pack(8, "Mega start!"); + myCAM.registerCallBack(readBuffer, 200, stop_preivew); } -void loop() { - if (myUart.arducamUartAvailable()) { - temp = myUart.arducamUartRead(); - arducamDelayMs(5); - if (temp == 0x55) { - while (myUart.arducamUartAvailable()) { - commandBuff[commandLength] = myUart.arducamUartRead(); - if (commandBuff[commandLength] == 0xAA) { - break; +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; } - commandLength++; - } - myUart.arducamFlush(); - myUart.uartCommandProcessing(&myCAM, commandBuff); - commandLength = 0; } - } + myCAM.captureThread(); } \ No newline at end of file diff --git a/examples/ArduinoUNO/lowPowerMode/ArducamLink.cpp b/examples/ArduinoUNO/lowPowerMode/ArducamLink.cpp index 99a9bcd..b6524ce 100644 --- a/examples/ArduinoUNO/lowPowerMode/ArducamLink.cpp +++ b/examples/ArduinoUNO/lowPowerMode/ArducamLink.cpp @@ -7,190 +7,231 @@ * */ #include "ArducamLink.h" -#include "Arduino.h" +#include "ArducamUart.h" -ArducamLink::ArducamLink() +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) { + SerialBegin(baudRate); } -ArducamLink::~ArducamLink() +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::arducamUartBegin(uint32_t baudRate) +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) { - Serial.begin(baudRate); -} + 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(); - Serial.println("ReportCameraInfo"); - Serial.print("Camera Type:"); - Serial.println(cameraInstance->myCameraInfo.cameraId); - Serial.print("Camera Support Resolution:"); - Serial.println(cameraInstance->myCameraInfo.supportResolution); - Serial.print("Camera Support specialeffects:"); - Serial.println(cameraInstance->myCameraInfo.supportSpecialEffects); - Serial.print("Camera Support Focus:"); - Serial.println(cameraInstance->myCameraInfo.supportFocus); - Serial.print("Camera Exposure Value Max:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMax); - Serial.print("Camera Exposure Value Min:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMin); - Serial.print("Camera Gain Value Max:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMax); - Serial.print("Camera Gain Value Min:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMin); - Serial.print("Camera Support Sharpness:"); - Serial.println(cameraInstance->myCameraInfo.supportSharpness); + 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 buff[READ_IMAGE_LENGTH] = {0}; - uint8_t rtLength = 0; - arducamUartWrite(0x55); - arducamUartWrite(0xAA); - arducamUartWrite(cameraInstance->cameraDataFormat); - arducamUartWrite((uint8_t)(myCamera->getTotalLength()&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>8)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>16)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>24)&0xff)); - while (myCamera->getReceivedLength()) - { - rtLength=readBuff(cameraInstance,buff,READ_IMAGE_LENGTH); - for (uint8_t i = 0; i < rtLength; i++) - { - arducamUartWrite(buff[i]); - } - } - arducamUartWrite(0x55); - arducamUartWrite(0xBB); + 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 ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM,uint8_t* commandBuff) + 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: + delay(1000); + 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) { - 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) - { - Serial.println("callback function is not registered"); - } - 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_EXPOSURE_CONTROL: //exposure control - myCAM->setAutoExposure(commandBuff[1]&0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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; - default: - break; - } - return CAM_ERR_SUCCESS; + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWriteBuff(uint8_t* buff,uint8_t length) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(buff,length); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::printf(char* buff) { - Serial.write(data); - delayMicroseconds(10); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) { - while(arducamUartAvailable()) - { - arducamUartRead(); - } -} \ No newline at end of file + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArduinoUNO/lowPowerMode/ArducamLink.h b/examples/ArduinoUNO/lowPowerMode/ArducamLink.h index 2774e16..ba1f3b1 100644 --- a/examples/ArduinoUNO/lowPowerMode/ArducamLink.h +++ b/examples/ArduinoUNO/lowPowerMode/ArducamLink.h @@ -11,49 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); }; - - - - - - #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArduinoUNO/lowPowerMode/ArducamUart.h b/examples/ArduinoUNO/lowPowerMode/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArduinoUNO/lowPowerMode/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/ArduinoUNO/lowPowerMode/lowPowerMode.ino b/examples/ArduinoUNO/lowPowerMode/lowPowerMode.ino index 19593f5..72896e0 100644 --- a/examples/ArduinoUNO/lowPowerMode/lowPowerMode.ino +++ b/examples/ArduinoUNO/lowPowerMode/lowPowerMode.ino @@ -32,7 +32,7 @@ void setup() { if (myUart.arducamUartAvailable()) { temp = myUart.arducamUartRead(); - arducamDelayMs(5); + delay(5); if (temp == 0x55) { while (myUart.arducamUartAvailable()) @@ -46,7 +46,7 @@ void setup() { } myUart.arducamFlush(); myCAM.lowPowerOff(); - arducamDelayMs(300); //If the camera is 3MP, you need to lengthen the delay + delay(300); //If the camera is 3MP, you need to lengthen the delay myUart.uartCommandProcessing(&myCAM,commandBuff); myCAM.lowPowerOn(); commandLength = 0; diff --git a/examples/ArdunioMega2560/lowPowerMode/ArducamLink.cpp b/examples/ArdunioMega2560/lowPowerMode/ArducamLink.cpp index 99a9bcd..b6524ce 100644 --- a/examples/ArdunioMega2560/lowPowerMode/ArducamLink.cpp +++ b/examples/ArdunioMega2560/lowPowerMode/ArducamLink.cpp @@ -7,190 +7,231 @@ * */ #include "ArducamLink.h" -#include "Arduino.h" +#include "ArducamUart.h" -ArducamLink::ArducamLink() +ArducamLink::ArducamLink() {} + +ArducamLink::~ArducamLink() {} + +void ArducamLink::arducamUartBegin(uint32_t baudRate) { + SerialBegin(baudRate); } -ArducamLink::~ArducamLink() +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::arducamUartBegin(uint32_t baudRate) +void ArducamLink::reportSdkVerInfo(Arducam_Mega* myCamera) { - Serial.begin(baudRate); -} + 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(); - Serial.println("ReportCameraInfo"); - Serial.print("Camera Type:"); - Serial.println(cameraInstance->myCameraInfo.cameraId); - Serial.print("Camera Support Resolution:"); - Serial.println(cameraInstance->myCameraInfo.supportResolution); - Serial.print("Camera Support specialeffects:"); - Serial.println(cameraInstance->myCameraInfo.supportSpecialEffects); - Serial.print("Camera Support Focus:"); - Serial.println(cameraInstance->myCameraInfo.supportFocus); - Serial.print("Camera Exposure Value Max:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMax); - Serial.print("Camera Exposure Value Min:"); - Serial.println(cameraInstance->myCameraInfo.exposureValueMin); - Serial.print("Camera Gain Value Max:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMax); - Serial.print("Camera Gain Value Min:"); - Serial.println(cameraInstance->myCameraInfo.gainValueMin); - Serial.print("Camera Support Sharpness:"); - Serial.println(cameraInstance->myCameraInfo.supportSharpness); + 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 buff[READ_IMAGE_LENGTH] = {0}; - uint8_t rtLength = 0; - arducamUartWrite(0x55); - arducamUartWrite(0xAA); - arducamUartWrite(cameraInstance->cameraDataFormat); - arducamUartWrite((uint8_t)(myCamera->getTotalLength()&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>8)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>16)&0xff)); - arducamUartWrite((uint8_t)((myCamera->getTotalLength()>>24)&0xff)); - while (myCamera->getReceivedLength()) - { - rtLength=readBuff(cameraInstance,buff,READ_IMAGE_LENGTH); - for (uint8_t i = 0; i < rtLength; i++) - { - arducamUartWrite(buff[i]); - } - } - arducamUartWrite(0x55); - arducamUartWrite(0xBB); + 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 ArducamLink::uartCommandProcessing(Arducam_Mega* myCAM,uint8_t* commandBuff) + 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: + delay(1000); + 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) { - 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) - { - Serial.println("callback function is not registered"); - } - 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_EXPOSURE_CONTROL: //exposure control - myCAM->setAutoExposure(commandBuff[1]&0x01); - break; - case SET_GAIN_CONTROL: //Gain control - 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; - default: - break; - } - return CAM_ERR_SUCCESS; + SerialWrite(data); + delayUs(12); } -void ArducamLink::arducamUartWriteBuff(uint8_t* buff,uint8_t length) +void ArducamLink::arducamUartWriteBuff(uint8_t* buff, uint16_t length) { - Serial.write(buff,length); + // SerialWriteBuff(buff, length); + // delayUs(12); + for (uint16_t i = 0; i < length; i++) + arducamUartWrite(buff[i]); } -void ArducamLink::arducamUartWrite(uint8_t data) +void ArducamLink::printf(char* buff) { - Serial.write(data); - delayMicroseconds(10); + uint16_t len = strlen(buff); + // SerialPrintf(buff); + // delayUs(12); + arducamUartWriteBuff((uint8_t*)buff, len); } uint32_t ArducamLink::arducamUartAvailable(void) { - return Serial.available(); + return SerialAvailable(); } uint8_t ArducamLink::arducamUartRead(void) { - return Serial.read(); + return SerialRead(); } void ArducamLink::arducamFlush(void) { - while(arducamUartAvailable()) - { - arducamUartRead(); - } -} \ No newline at end of file + while (arducamUartAvailable()) { + arducamUartRead(); + } +} diff --git a/examples/ArdunioMega2560/lowPowerMode/ArducamLink.h b/examples/ArdunioMega2560/lowPowerMode/ArducamLink.h index 2774e16..ba1f3b1 100644 --- a/examples/ArdunioMega2560/lowPowerMode/ArducamLink.h +++ b/examples/ArdunioMega2560/lowPowerMode/ArducamLink.h @@ -11,49 +11,54 @@ #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 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_EXPOSURE_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 READ_IMAGE_LENGTH 255 +#define READ_IMAGE_LENGTH 255 class ArducamLink { -private: - /* data */ -public: + public: + uint8_t uartCommandProcessing(Arducam_Mega*, uint8_t*); + + public: ArducamLink(); ~ArducamLink(); void arducamUartBegin(uint32_t); - uint8_t uartCommandProcessing(Arducam_Mega*,uint8_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*,uint8_t); + void arducamUartWriteBuff(uint8_t*, uint16_t); uint32_t arducamUartAvailable(void); uint8_t arducamUartRead(void); - void arducamFlush(void); }; - - - - - - #endif /*__ARDUCAMLINK_H*/ \ No newline at end of file diff --git a/examples/ArdunioMega2560/lowPowerMode/ArducamUart.h b/examples/ArdunioMega2560/lowPowerMode/ArducamUart.h new file mode 100644 index 0000000..44db6f6 --- /dev/null +++ b/examples/ArdunioMega2560/lowPowerMode/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/ArdunioMega2560/lowPowerMode/lowPowerMode.ino b/examples/ArdunioMega2560/lowPowerMode/lowPowerMode.ino index 674fc01..3372e81 100644 --- a/examples/ArdunioMega2560/lowPowerMode/lowPowerMode.ino +++ b/examples/ArdunioMega2560/lowPowerMode/lowPowerMode.ino @@ -32,7 +32,7 @@ void setup() { if (myUart.arducamUartAvailable()) { temp = myUart.arducamUartRead(); - arducamDelayMs(5); + delay(5); if (temp == 0x55) { while (myUart.arducamUartAvailable()) @@ -46,7 +46,7 @@ void setup() { } myUart.arducamFlush(); myCAM.lowPowerOff(); - arducamDelayMs(300); //If the camera is 3MP, you need to lengthen the delay + delay(300); //If the camera is 3MP, you need to lengthen the delay myUart.uartCommandProcessing(&myCAM,commandBuff); myCAM.lowPowerOn(); commandLength = 0; diff --git a/library.properties b/library.properties index 61ad8e3..f6fe4f9 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arducam_Mega -version=2.0.8 +version=2.0.9 author=Arducam maintainer=Arducam sentence=Examples of spi cameras used on different hardware. diff --git a/src/Arducam/ArducamCamera.c b/src/Arducam/ArducamCamera.c index 3f9be77..4463506 100644 --- a/src/Arducam/ArducamCamera.c +++ b/src/Arducam/ArducamCamera.c @@ -291,13 +291,13 @@ CamStatus cameraTakePicture(ArducamCamera* camera, CAM_IMAGE_MODE mode, CAM_IMAG CamStatus cameratakeMultiPictures(ArducamCamera* camera, CAM_IMAGE_MODE mode, CAM_IMAGE_PIX_FMT pixel_format, uint8_t num) { - if (camera->currentPixelFormat != pixel_format) { + if (camera->currentPixelFormat != pixel_format) { camera->currentPixelFormat = pixel_format; writeReg(camera, CAM_REG_FORMAT, pixel_format); // set the data format waitI2cIdle(camera); // Wait I2c Idle } - if (camera->currentPictureMode != mode) { + if (camera->currentPictureMode != mode) { camera->currentPictureMode = mode; writeReg(camera, CAM_REG_CAPTURE_RESOLUTION, CAM_SET_CAPTURE_MODE | mode); waitI2cIdle(camera); // Wait I2c Idle