Skip to content

Commit 35d3912

Browse files
authored
Merge pull request arduino#142 from iabdalkader/himax_md_update
Update Himax motion detection feature.
2 parents 275e2fd + 7b6ac71 commit 35d3912

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

libraries/Himax_HM01B0/himax.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static regval_list_t himax_default_regs[] = {
117117
{SINGLE_THR_HOT, 0x90}, // single hot pixel th
118118
{SINGLE_THR_COLD, 0x40}, // single cold pixel th
119119
{0x1012, 0x00}, // Sync. shift disable
120-
{0x2000, 0x07},
120+
{STATISTIC_CTRL, 0x07}, // AE stat en | MD LROI stat en | magic
121121
{0x2003, 0x00},
122122
{0x2004, 0x1C},
123123
{0x2007, 0x00},
@@ -156,7 +156,7 @@ static regval_list_t himax_default_regs[] = {
156156
{FS_50HZ_H, 0x00},
157157
{FS_50HZ_L, 0x32},
158158

159-
{MD_CTRL, 0x30},
159+
{MD_CTRL, 0x00},
160160
{FRAME_LEN_LINES_H, HIMAX_FRAME_LENGTH_QVGA>>8},
161161
{FRAME_LEN_LINES_L, HIMAX_FRAME_LENGTH_QVGA&0xFF},
162162
{LINE_LEN_PCK_H, HIMAX_LINE_LEN_PCK_QVGA>>8},
@@ -315,20 +315,21 @@ int HIMAX_SetFramerate(uint32_t framerate)
315315
int HIMAX_EnableMD(bool enable)
316316
{
317317
int ret = HIMAX_ClearMD();
318-
if (enable) {
319-
ret |= HIMAX_RegWrite(MD_CTRL, 0x03);
320-
} else {
321-
ret |= HIMAX_RegWrite(MD_CTRL, 0x30);
322-
}
318+
ret |= HIMAX_RegWrite(MD_CTRL, enable ? 1:0);
323319
return ret;
324320
}
325321

326-
int HIMAX_SetMDThreshold(uint32_t low, uint32_t high)
322+
int HIMAX_SetMDThreshold(uint32_t threshold)
327323
{
328-
int ret = 0;
329-
ret |= HIMAX_RegWrite(MD_THL, low & 0xff);
330-
ret |= HIMAX_RegWrite(MD_THH, high & 0xff);
331-
return ret;
324+
// Set motion detection threshold/sensitivity.
325+
// The recommended threshold range is 0x03 to 0xF0.
326+
//
327+
// Motion is detected according to the following:
328+
// |MD_LROI_MEAN – MD_LROI_IIR_MEAN| > ( MD_LROI_MEAN * MD_THL / 64)
329+
//
330+
// In other words, motion is detected if the abs difference of the ROI mean and the
331+
// average ROI mean of the last 8 or 16 frames is higher than (ROI mean * threshold / 64).
332+
return HIMAX_RegWrite(MD_THL, (threshold < 3) ? 3 : (threshold > 0xF0) ? 0xF0 : threshold);
332333
}
333334

334335
int HIMAX_SetLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)

libraries/Himax_HM01B0/himax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int HIMAX_Mode(uint8_t mode);
158158
int HIMAX_SetResolution(uint32_t resolution);
159159
int HIMAX_SetFramerate(uint32_t framerate);
160160
int HIMAX_EnableMD(bool enable);
161-
int HIMAX_SetMDThreshold(uint32_t low, uint32_t high);
161+
int HIMAX_SetMDThreshold(uint32_t threshold);
162162
int HIMAX_SetLROI(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
163163
int HIMAX_PollMD();
164164
int HIMAX_ClearMD();

libraries/Portenta_Camera/camera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ int CameraClass::standby(bool enable)
543543
}
544544
}
545545

546-
int CameraClass::motionDetectionThreshold(uint32_t low, uint32_t high)
546+
int CameraClass::motionDetectionThreshold(uint32_t threshold)
547547
{
548548
if (this->initialized == false) {
549549
return -1;
550550
}
551-
return HIMAX_SetMDThreshold(low, high);
551+
return HIMAX_SetMDThreshold(threshold);
552552
}
553553

554554
int CameraClass::motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)

libraries/Portenta_Camera/camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CameraClass {
2020
int standby(bool enable);
2121
int motionDetection(bool enable, md_callback_t callback=NULL);
2222
int motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
23-
int motionDetectionThreshold(uint32_t low, uint32_t high);
23+
int motionDetectionThreshold(uint32_t threshold);
2424
int motionDetected();
2525
int testPattern(bool walking);
2626
};

libraries/Portenta_Camera/examples/CameraMotionDetect/CameraMotionDetect.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ void setup() {
1414

1515
pinMode(LEDB, OUTPUT);
1616
digitalWrite(LEDB, HIGH);
17-
17+
1818
// Init the cam QVGA, 30FPS
19-
cam.begin(CAMERA_R320x240, 60);
19+
cam.begin(CAMERA_R320x240, 30);
20+
21+
// Set motion detection threshold (0 -> 255).
22+
// The lower the threshold the higher the sensitivity.
23+
cam.motionDetectionThreshold(0);
2024

21-
cam.motionDetectionThreshold(100, 200);
25+
// Set motion detection window/ROI.
2226
cam.motionDetectionWindow(0, 0, 320, 240);
27+
2328
// The detection can also be enabled without any callback
2429
cam.motionDetection(true, on_motion);
2530
}

0 commit comments

Comments
 (0)