diff --git a/libraries/Himax_HM01B0/himax.cpp b/libraries/Himax_HM01B0/himax.cpp index 781c6190f..90d67e8f4 100644 --- a/libraries/Himax_HM01B0/himax.cpp +++ b/libraries/Himax_HM01B0/himax.cpp @@ -117,7 +117,7 @@ static regval_list_t himax_default_regs[] = { {SINGLE_THR_HOT, 0x90}, // single hot pixel th {SINGLE_THR_COLD, 0x40}, // single cold pixel th {0x1012, 0x00}, // Sync. shift disable - {0x2000, 0x07}, + {STATISTIC_CTRL, 0x07}, // AE stat en | MD LROI stat en | magic {0x2003, 0x00}, {0x2004, 0x1C}, {0x2007, 0x00}, @@ -156,7 +156,7 @@ static regval_list_t himax_default_regs[] = { {FS_50HZ_H, 0x00}, {FS_50HZ_L, 0x32}, - {MD_CTRL, 0x30}, + {MD_CTRL, 0x00}, {FRAME_LEN_LINES_H, HIMAX_FRAME_LENGTH_QVGA>>8}, {FRAME_LEN_LINES_L, HIMAX_FRAME_LENGTH_QVGA&0xFF}, {LINE_LEN_PCK_H, HIMAX_LINE_LEN_PCK_QVGA>>8}, @@ -315,20 +315,21 @@ int HIMAX_SetFramerate(uint32_t framerate) int HIMAX_EnableMD(bool enable) { int ret = HIMAX_ClearMD(); - if (enable) { - ret |= HIMAX_RegWrite(MD_CTRL, 0x03); - } else { - ret |= HIMAX_RegWrite(MD_CTRL, 0x30); - } + ret |= HIMAX_RegWrite(MD_CTRL, enable ? 1:0); return ret; } -int HIMAX_SetMDThreshold(uint32_t low, uint32_t high) +int HIMAX_SetMDThreshold(uint32_t threshold) { - int ret = 0; - ret |= HIMAX_RegWrite(MD_THL, low & 0xff); - ret |= HIMAX_RegWrite(MD_THH, high & 0xff); - return ret; + // Set motion detection threshold/sensitivity. + // The recommended threshold range is 0x03 to 0xF0. + // + // Motion is detected according to the following: + // |MD_LROI_MEAN – MD_LROI_IIR_MEAN| > ( MD_LROI_MEAN * MD_THL / 64) + // + // In other words, motion is detected if the abs difference of the ROI mean and the + // average ROI mean of the last 8 or 16 frames is higher than (ROI mean * threshold / 64). + return HIMAX_RegWrite(MD_THL, (threshold < 3) ? 3 : (threshold > 0xF0) ? 0xF0 : threshold); } int HIMAX_SetLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2) diff --git a/libraries/Himax_HM01B0/himax.h b/libraries/Himax_HM01B0/himax.h index e58bb3a42..a17cee05b 100644 --- a/libraries/Himax_HM01B0/himax.h +++ b/libraries/Himax_HM01B0/himax.h @@ -158,7 +158,7 @@ int HIMAX_Mode(uint8_t mode); int HIMAX_SetResolution(uint32_t resolution); int HIMAX_SetFramerate(uint32_t framerate); int HIMAX_EnableMD(bool enable); -int HIMAX_SetMDThreshold(uint32_t low, uint32_t high); +int HIMAX_SetMDThreshold(uint32_t threshold); int HIMAX_SetLROI(uint32_t x, uint32_t y, uint32_t w, uint32_t h); int HIMAX_PollMD(); int HIMAX_ClearMD(); diff --git a/libraries/Portenta_Camera/camera.cpp b/libraries/Portenta_Camera/camera.cpp index 8027a8847..ca717ce07 100644 --- a/libraries/Portenta_Camera/camera.cpp +++ b/libraries/Portenta_Camera/camera.cpp @@ -543,12 +543,12 @@ int CameraClass::standby(bool enable) } } -int CameraClass::motionDetectionThreshold(uint32_t low, uint32_t high) +int CameraClass::motionDetectionThreshold(uint32_t threshold) { if (this->initialized == false) { return -1; } - return HIMAX_SetMDThreshold(low, high); + return HIMAX_SetMDThreshold(threshold); } int CameraClass::motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h) diff --git a/libraries/Portenta_Camera/camera.h b/libraries/Portenta_Camera/camera.h index a184a2ac2..1bdff899e 100644 --- a/libraries/Portenta_Camera/camera.h +++ b/libraries/Portenta_Camera/camera.h @@ -20,7 +20,7 @@ class CameraClass { int standby(bool enable); int motionDetection(bool enable, md_callback_t callback=NULL); int motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h); - int motionDetectionThreshold(uint32_t low, uint32_t high); + int motionDetectionThreshold(uint32_t threshold); int motionDetected(); int testPattern(bool walking); }; diff --git a/libraries/Portenta_Camera/examples/CameraMotionDetect/CameraMotionDetect.ino b/libraries/Portenta_Camera/examples/CameraMotionDetect/CameraMotionDetect.ino index 6350ee624..cffdf8ebc 100644 --- a/libraries/Portenta_Camera/examples/CameraMotionDetect/CameraMotionDetect.ino +++ b/libraries/Portenta_Camera/examples/CameraMotionDetect/CameraMotionDetect.ino @@ -14,12 +14,17 @@ void setup() { pinMode(LEDB, OUTPUT); digitalWrite(LEDB, HIGH); - + // Init the cam QVGA, 30FPS - cam.begin(CAMERA_R320x240, 60); + cam.begin(CAMERA_R320x240, 30); + + // Set motion detection threshold (0 -> 255). + // The lower the threshold the higher the sensitivity. + cam.motionDetectionThreshold(0); - cam.motionDetectionThreshold(100, 200); + // Set motion detection window/ROI. cam.motionDetectionWindow(0, 0, 320, 240); + // The detection can also be enabled without any callback cam.motionDetection(true, on_motion); }