From 66a4fa7f0039a6b73f1d19b7ad5558c18ebee406 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 17 Dec 2024 00:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=B5=E6=BA=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/GPIOControl.cpp | 4 +- app/src/main/cpp/GPIOControl.h | 107 +++++++++++++++++++++++++++++ app/src/main/cpp/PhoneDevice.cpp | 114 +++++++++---------------------- app/src/main/cpp/PhoneDevice.h | 4 ++ 4 files changed, 144 insertions(+), 85 deletions(-) diff --git a/app/src/main/cpp/GPIOControl.cpp b/app/src/main/cpp/GPIOControl.cpp index 3ff387a8..e17bd754 100644 --- a/app/src/main/cpp/GPIOControl.cpp +++ b/app/src/main/cpp/GPIOControl.cpp @@ -237,10 +237,10 @@ size_t GpioControl::TurnOff(const std::vector& cmds, uint32_t delayedCloseT ts = time(NULL) + delayedCloseTime; } std::vector::iterator it; - std::vector::const_iterator itCmd; + std::vector::const_reverse_iterator itCmd; m_locker.lock(); // turnOnImpl(param); - for (itCmd = cmds.cbegin(); itCmd != cmds.end(); ++itCmd) + for (itCmd = cmds.crbegin(); itCmd != cmds.crend(); ++itCmd) { for (it = m_items.begin(); it != m_items.end(); ++it) { diff --git a/app/src/main/cpp/GPIOControl.h b/app/src/main/cpp/GPIOControl.h index b23efe06..80257b66 100644 --- a/app/src/main/cpp/GPIOControl.h +++ b/app/src/main/cpp/GPIOControl.h @@ -427,4 +427,111 @@ public: }; + +class PowerControl +{ +public: + PowerControl(int cmd1) : m_delayCloseTime(0) + { + m_cmds.resize(1, cmd1); + } + PowerControl(int cmd1, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) + { + m_cmds.resize(1, cmd1); + } + PowerControl(int cmd1, int cmd2, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) + { + m_cmds.resize(2, cmd1); + m_cmds[1] = cmd2; + } + PowerControl(int cmd1, int cmd2, int cmd3, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) + { + m_cmds.resize(3, cmd1); + m_cmds[1] = cmd2; + m_cmds[2] = cmd3; + } + PowerControl(int cmd1, int cmd2, int cmd3, int cmd4, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) + { + m_cmds.resize(4, cmd1); + m_cmds[1] = cmd2; + m_cmds[2] = cmd3; + m_cmds[3] = cmd3; + } + PowerControl(int cmd1, int cmd2, int cmd3, int cmd4, int cmd5, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) + { + m_cmds.resize(5, cmd1); + m_cmds[1] = cmd2; + m_cmds[2] = cmd3; + m_cmds[3] = cmd3; + m_cmds[4] = cmd4; + } + + void TurnOn() + { + GpioControl::TurnOn(m_cmds); + } + + virtual ~PowerControl() + { + GpioControl::TurnOff(m_cmds, m_delayCloseTime); + } +private: + std::vector m_cmds; + uint32_t m_delayCloseTime; +}; + +class CameraPowerCtrl : public PowerControl +{ +public: + CameraPowerCtrl(uint32_t closeDelayTime) : +#ifdef USING_N938 + PowerControl(0, closeDelayTime) +#else // USING_N938 +#ifdef USING_PLZ + PowerControl(CMD_SET_3V3_PWR_EN, closeDelayTime) +#else // USING_PLZ + PowerControl(CMD_SET_3V3_PWR_EN, closeDelayTime) +#endif // USING_PLZ +#endif // USING_N938 + { + } +}; + +class NetCameraPowerCtrl : public PowerControl +{ +public: + NetCameraPowerCtrl(uint32_t closeDelayTime) : +#ifdef USING_N938 + PowerControl(CMD_SET_OTG_STATE, CMD_SET_NETWORK_POWER_EN, CMD_SET_PIC1_POWER, CMD_SET_485_EN_STATE, closeDelayTime) +#else // USING_N938 +#ifdef USING_PLZ + PowerControl(CMD_SET_PTZ_PWR_ENABLE, CMD_SET_100M_ENABLE, CMD_SET_100M_SWITCH_PWR_ENABLE, CMD_SET_12V_EN_STATE, closeDelayTime) +#else // USING_PLZ + PowerControl(CMD_SET_OTG_STATE, CMD_SET_12V_EN_STATE, closeDelayTime) +#endif // USING_PLZ +#endif // USING_N938 + { + } +}; + +class UsbCameraPowerCtrl : public PowerControl +{ +public: + UsbCameraPowerCtrl(uint32_t closeDelayTime) : +#ifdef USING_N938 + PowerControl(CMD_SET_OTG_STATE, CMD_SET_NETWORK_POWER_EN, CMD_SET_PIC1_POWER, CMD_SET_485_EN_STATE, closeDelayTime) +#else // USING_N938 +#ifdef USING_PLZ + PowerControl(CMD_SET_PTZ_PWR_ENABLE, CMD_SET_100M_ENABLE, CMD_SET_100M_SWITCH_PWR_ENABLE, CMD_SET_12V_EN_STATE, closeDelayTime) +#else // USING_PLZ + PowerControl(CMD_SET_OTG_STATE, CMD_SET_12V_EN_STATE, closeDelayTime) +#endif // USING_PLZ +#endif // USING_N938 + { + } +}; + + + + #endif //MICROPHOTO_GPIOCONTROL_H diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index f595fbba..a0e234ed 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -1530,28 +1530,20 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< bool res = false; - if (photoInfo.cameraType == CAM_TYPE_USB || photoInfo.cameraType == CAM_TYPE_NET) + std::shared_ptr powerCtrlPtr; + + if (photoInfo.cameraType == CAM_TYPE_MIPI) { - GpioControl::setOtgState(true); + powerCtrlPtr = std::shared_ptr(new CameraPowerCtrl(mPhotoInfo.closeDelayTime)); } - if (photoInfo.cameraType == CAM_TYPE_NET) + else if (photoInfo.cameraType == CAM_TYPE_USB) { - GpioControl::set12VEnable(true); -#ifdef USING_N938 - GpioControl::TurnOn(CMD_SET_NETWORK_POWER_EN); - GpioControl::TurnOn(CMD_SET_PIC1_POWER); - GpioControl::TurnOn(CMD_SET_485_EN_STATE); -#else // USING_N938 -#ifdef USING_PLZ - GpioControl::TurnOn(CMD_SET_PTZ_PWR_ENABLE); - GpioControl::TurnOn(CMD_SET_100M_ENABLE); - GpioControl::TurnOn(CMD_SET_100M_SWITCH_PWR_ENABLE); - - -#endif // USING_PLZ -#endif // USING_N938 + powerCtrlPtr = std::shared_ptr(new UsbCameraPowerCtrl(mPhotoInfo.closeDelayTime)); + } + else if (photoInfo.cameraType == CAM_TYPE_NET) + { + powerCtrlPtr = std::shared_ptr(new NetCameraPowerCtrl(mPhotoInfo.closeDelayTime)); } - GpioControl::setCam3V3Enable(true); res = true; if ((mPhotoInfo.mediaType == 0) && ((mPhotoInfo.cameraType == CAM_TYPE_MIPI) || (mPhotoInfo.cameraType == CAM_TYPE_USB))) @@ -1609,6 +1601,7 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< // mCamera = new CJpegCamera(this, photoInfo.width, photoInfo.height, mPath, params); if (mCamera->open(to_string(mPhotoInfo.cameraId)) == 0) { + m_powerCtrlPtr = powerCtrlPtr; XYLOG(XYLOG_SEVERITY_DEBUG, "TP: Succeeded to OpenCamera CH=%u PR=%X PHOTOID=%u", (unsigned int)photoInfo.channel, (unsigned int)photoInfo.preset, photoInfo.photoId); } else @@ -1619,10 +1612,9 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< mCamera = NULL; res = false; - GpioControl::setCam3V3Enable(false); - if (photoInfo.usbCamera) { - GpioControl::setOtgState(false); + std::shared_ptr empty; + empty.swap(powerCtrlPtr); } if (hasFatalError) @@ -1643,7 +1635,7 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< osds.swap(mOsds); IDevice::PHOTO_INFO localPhotoInfo = mPhotoInfo; - std::thread t([localPhotoInfo, path, pThis, osds]() mutable + std::thread t([localPhotoInfo, path, pThis, osds, powerCtrlPtr]() mutable { // AutoEnv autoEnv(pThis->m_vm); uint32_t waitTime = localPhotoInfo.selfTestingTime; @@ -1675,19 +1667,6 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< // timeout XYLOG(XYLOG_SEVERITY_ERROR, "Ethernet not existing CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId); pThis->TakePhotoCb(0, localPhotoInfo, "", 0); - - GpioControl::setOtgState(false); - GpioControl::set12VEnable(false); -#ifdef USING_N938 - GpioControl::TurnOff(CMD_SET_NETWORK_POWER_EN); - GpioControl::TurnOff(CMD_SET_PIC1_POWER); - GpioControl::TurnOff(CMD_SET_485_EN_STATE); -#else // USING_N938 -#ifdef USING_PLZ - GpioControl::TurnOff(CMD_SET_100M_ENABLE); - GpioControl::TurnOff(CMD_SET_100M_SWITCH_PWR_ENABLE); -#endif -#endif // USING_N938 return; } else @@ -1715,19 +1694,6 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< { XYLOG(XYLOG_SEVERITY_ERROR, "Vendor(%u) not Supported CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.vendor, (uint32_t)localPhotoInfo.channel, (unsigned int)localPhotoInfo.preset, localPhotoInfo.photoId); pThis->TakePhotoCb(0, localPhotoInfo, "", 0); - - GpioControl::setOtgState(false); - GpioControl::set12VEnable(false); -#ifdef USING_N938 - GpioControl::TurnOff(CMD_SET_NETWORK_POWER_EN); - GpioControl::TurnOff(CMD_SET_PIC1_POWER); - GpioControl::TurnOff(CMD_SET_485_EN_STATE); -#else // USING_N938 -#ifdef USING_PLZ - GpioControl::TurnOff(CMD_SET_100M_ENABLE); - GpioControl::TurnOff(CMD_SET_100M_SWITCH_PWR_ENABLE); -#endif // USING_PLZ -#endif // USING_N938 return; } @@ -1764,18 +1730,6 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< } } - GpioControl::setOtgState(false); - GpioControl::set12VEnable(false); -#ifdef USING_N938 - GpioControl::TurnOff(CMD_SET_NETWORK_POWER_EN); - GpioControl::TurnOff(CMD_SET_PIC1_POWER); - GpioControl::TurnOff(CMD_SET_485_EN_STATE); -#else // USING_N938 -#ifdef USING_PLZ - GpioControl::TurnOff(CMD_SET_100M_ENABLE); - GpioControl::TurnOff(CMD_SET_100M_SWITCH_PWR_ENABLE); -#endif // USING_PLZ -#endif if (netCaptureResult) { time_t takingTime = time(NULL); @@ -1818,21 +1772,21 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< std::thread t([localPhotoInfo, param, pThis,wid_serial]() mutable { - if (localPhotoInfo.preset != 0 && localPhotoInfo.preset != 0xFF) - { - CameraPhotoCmd(time(NULL), localPhotoInfo.channel, MOVE_PRESETNO, 0, localPhotoInfo.preset, param.serfile, param.baud, param.addr); - std::this_thread::sleep_for(std::chrono::seconds(3)); - } - time_t ts = time(NULL); - if(!pThis->GetPTZSensorsStatus() && !pThis->GetCameraStatus()) - { - pThis->OpenPTZSensors(localPhotoInfo.selfTestingTime); - } - CameraPhotoCmd(ts, localPhotoInfo.channel, 0, localPhotoInfo.resolution, 0, param.serfile, param.baud, param.addr); - XYLOG(XYLOG_SEVERITY_INFO, "Taking photo over"); - pThis->TakePTZPhotoCb(3, localPhotoInfo); - pThis->ClosePTZSensors(localPhotoInfo.closeDelayTime); - GpioControl::setCam3V3Enable(false, localPhotoInfo.closeDelayTime); + if (localPhotoInfo.preset != 0 && localPhotoInfo.preset != 0xFF) + { + CameraPhotoCmd(time(NULL), localPhotoInfo.channel, MOVE_PRESETNO, 0, localPhotoInfo.preset, param.serfile, param.baud, param.addr); + std::this_thread::sleep_for(std::chrono::seconds(3)); + } + time_t ts = time(NULL); + if(!pThis->GetPTZSensorsStatus() && !pThis->GetCameraStatus()) + { + pThis->OpenPTZSensors(localPhotoInfo.selfTestingTime); + } + CameraPhotoCmd(ts, localPhotoInfo.channel, 0, localPhotoInfo.resolution, 0, param.serfile, param.baud, param.addr); + XYLOG(XYLOG_SEVERITY_INFO, "Taking photo over"); + pThis->TakePTZPhotoCb(3, localPhotoInfo); + pThis->ClosePTZSensors(localPhotoInfo.closeDelayTime); + GpioControl::setCam3V3Enable(false, localPhotoInfo.closeDelayTime); pThis->ReleaseWakelock(wid_serial); }); @@ -2037,16 +1991,10 @@ void CPhoneDevice::CloseCamera2(CPhoneDevice::CPhoneCamera* camera, unsigned int } XYLOG(XYLOG_SEVERITY_DEBUG, "TP: Will Turn Off Power PHOTOID=%u", photoId); - if (cameraType == CAM_TYPE_NET) - { - GpioControl::set12VEnable(false); - } - - if (cameraType == CAM_TYPE_USB || cameraType == CAM_TYPE_NET) { - GpioControl::setOtgState(false); + std::shared_ptr empty; + empty.swap(m_powerCtrlPtr); } - GpioControl::setCam3V3Enable(false); XYLOG(XYLOG_SEVERITY_DEBUG, "TP: End Turn Off Power PHOTOID=%u", photoId); XYLOG(XYLOG_SEVERITY_DEBUG, "TP: CloseCamera PHOTOID=%u", photoId); diff --git a/app/src/main/cpp/PhoneDevice.h b/app/src/main/cpp/PhoneDevice.h index 32499d86..ee444c0d 100644 --- a/app/src/main/cpp/PhoneDevice.h +++ b/app/src/main/cpp/PhoneDevice.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -152,6 +153,8 @@ void MatToBitmap(JNIEnv *env, cv::Mat& mat, jobject& bitmap) { } #endif +class PowerControl; + class CPhoneDevice : public IDevice { public: @@ -394,6 +397,7 @@ protected: unsigned int mHeartbeatDuration; std::thread m_threadClose; + std::shared_ptr m_powerCtrlPtr; int m_signalLevel; time_t m_signalLevelUpdateTime;