Skip to content

Commit 43baef4

Browse files
committed
Enable motion detection.
1 parent 780323e commit 43baef4

File tree

5 files changed

+182
-3
lines changed

5 files changed

+182
-3
lines changed

libraries/Himax_HM01B0/himax.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,49 @@ int HIMAX_SetFramerate(uint32_t framerate)
312312
return HIMAX_RegWrite(OSC_CLK_DIV, 0x08 | osc_div);
313313
}
314314

315+
int HIMAX_EnableMD(bool enable)
316+
{
317+
int ret = HIMAX_ClearMD();
318+
if (enable) {
319+
ret |= HIMAX_RegWrite(MD_CTRL, 0x03);
320+
} else {
321+
ret |= HIMAX_RegWrite(MD_CTRL, 0x30);
322+
}
323+
return ret;
324+
}
325+
326+
int HIMAX_SetMDThreshold(uint32_t low, uint32_t high)
327+
{
328+
int ret = 0;
329+
ret |= HIMAX_RegWrite(MD_THL, low & 0xff);
330+
ret |= HIMAX_RegWrite(MD_THH, high & 0xff);
331+
return ret;
332+
}
333+
334+
int HIMAX_SetLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
335+
{
336+
int ret = 0;
337+
ret |= HIMAX_RegWrite(MD_LROI_X_START_H, (x1>>8));
338+
ret |= HIMAX_RegWrite(MD_LROI_X_START_L, (x1&0xff));
339+
ret |= HIMAX_RegWrite(MD_LROI_Y_START_H, (y1>>8));
340+
ret |= HIMAX_RegWrite(MD_LROI_Y_START_L, (y1&0xff));
341+
ret |= HIMAX_RegWrite(MD_LROI_X_END_H, (x2>>8));
342+
ret |= HIMAX_RegWrite(MD_LROI_X_END_L, (x2&0xff));
343+
ret |= HIMAX_RegWrite(MD_LROI_Y_END_H, (y2>>8));
344+
ret |= HIMAX_RegWrite(MD_LROI_Y_END_L, (y2&0xff));
345+
return ret;
346+
}
347+
348+
int HIMAX_PollMD()
349+
{
350+
return HIMAX_RegRead(MD_INTERRUPT);
351+
}
352+
353+
int HIMAX_ClearMD()
354+
{
355+
return HIMAX_RegWrite(I2C_CLEAR, 0x01);
356+
}
357+
315358
/**
316359
* @brief This function writes HIMAX camera registers.
317360
* @retval None

libraries/Himax_HM01B0/himax.h

+15
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@
104104
#define MD_THM1 0x2159
105105
#define MD_THM2 0x215A
106106
#define MD_THL 0x215B
107+
#define STATISTIC_CTRL 0x2000
108+
#define MD_LROI_X_START_H 0x2011
109+
#define MD_LROI_X_START_L 0x2012
110+
#define MD_LROI_Y_START_H 0x2013
111+
#define MD_LROI_Y_START_L 0x2014
112+
#define MD_LROI_X_END_H 0x2015
113+
#define MD_LROI_X_END_L 0x2016
114+
#define MD_LROI_Y_END_H 0x2017
115+
#define MD_LROI_Y_END_L 0x2018
116+
#define MD_INTERRUPT 0x2160
107117
// Sensor timing control
108118
#define QVGA_WIN_EN 0x3010
109119
#define SIX_BIT_MODE_EN 0x3011
@@ -147,6 +157,11 @@ uint8_t HIMAX_Open(void);
147157
int HIMAX_Mode(uint8_t mode);
148158
int HIMAX_SetResolution(uint32_t resolution);
149159
int HIMAX_SetFramerate(uint32_t framerate);
160+
int HIMAX_EnableMD(bool enable);
161+
int HIMAX_SetMDThreshold(uint32_t low, uint32_t high);
162+
int HIMAX_SetLROI(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
163+
int HIMAX_PollMD();
164+
int HIMAX_ClearMD();
150165
void HIMAX_TestPattern(bool enable = true, bool walking = true);
151166

152167

libraries/Portenta_Camera/camera.cpp

+77-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static const int CamRes[][2] = {
1212
{320, 240},
1313
};
1414
static __IO uint32_t camera_frame_ready = 0;
15+
static md_callback_t user_md_callback = NULL;
1516

1617
/* DCMI DMA Stream definitions */
1718
#define CAMERA_DCMI_DMAx_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE
@@ -349,8 +350,6 @@ static int extclk_config(int frequency)
349350
*/
350351
void BSP_CAMERA_PwrUp(void)
351352
{
352-
GPIO_InitTypeDef gpio_init_structure;
353-
354353
mbed::DigitalOut* powerup = new mbed::DigitalOut(PC_13);
355354
*powerup = 0;
356355
delay(50);
@@ -452,6 +451,13 @@ void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
452451

453452
}
454453

454+
void CameraClass::HIMAXIrqHandler()
455+
{
456+
if (user_md_callback) {
457+
user_md_callback();
458+
}
459+
}
460+
455461
int CameraClass::begin(uint32_t resolution, uint32_t framerate)
456462
{
457463
if (resolution >= CAMERA_RMAX) {
@@ -477,6 +483,7 @@ int CameraClass::begin(uint32_t resolution, uint32_t framerate)
477483
return -1;
478484
}
479485

486+
user_md_callback = NULL;
480487
this->initialized = true;
481488
this->resolution = resolution;
482489
return 0;
@@ -536,6 +543,74 @@ int CameraClass::standby(bool enable)
536543
}
537544
}
538545

546+
int CameraClass::setMDThreshold(uint32_t low, uint32_t high)
547+
{
548+
if (this->initialized == false) {
549+
return -1;
550+
}
551+
return HIMAX_SetMDThreshold(low, high);
552+
}
553+
554+
int CameraClass::setMDWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
555+
{
556+
uint32_t width, height;
557+
558+
if (this->initialized == false) {
559+
return -1;
560+
}
561+
562+
width = CamRes[this->resolution][0];
563+
height= CamRes[this->resolution][1];
564+
565+
if (((x+w) > width) || ((y+h) > height)) {
566+
return -1;
567+
}
568+
return HIMAX_SetLROI(x, y, x+w, y+h);
569+
}
570+
571+
int CameraClass::enableMD(bool enable, md_callback_t callback)
572+
{
573+
if (this->initialized == false) {
574+
return -1;
575+
}
576+
577+
this->md_irq.rise(0);
578+
579+
if (enable == false) {
580+
user_md_callback = NULL;
581+
} else {
582+
user_md_callback = callback;
583+
this->md_irq.rise(mbed::Callback<void()>(this, &CameraClass::HIMAXIrqHandler));
584+
this->md_irq.enable_irq();
585+
}
586+
587+
return HIMAX_EnableMD(enable);
588+
}
589+
590+
int CameraClass::pollMD()
591+
{
592+
int ret = 0;
593+
594+
if (this->initialized == false) {
595+
return -1;
596+
}
597+
598+
ret = HIMAX_PollMD();
599+
if (ret == 1) {
600+
HIMAX_ClearMD();
601+
}
602+
return ret;
603+
}
604+
605+
int CameraClass::clearMDFlag()
606+
{
607+
if (this->initialized == false) {
608+
return -1;
609+
}
610+
611+
return HIMAX_ClearMD();
612+
}
613+
539614
int CameraClass::testPattern(bool walking)
540615
{
541616
if (this->initialized == false) {

libraries/Portenta_Camera/camera.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ enum {
44
CAMERA_RMAX
55
};
66

7+
typedef void (*md_callback_t)();
8+
79
class CameraClass {
810
private:
911
uint32_t resolution;
1012
bool initialized;
13+
mbed::InterruptIn md_irq;
14+
void HIMAXIrqHandler();
1115
public:
12-
CameraClass(): initialized(false) {}
16+
CameraClass(): initialized(false), md_irq(PC_15){}
1317
int begin(uint32_t resolution = CAMERA_R320x240, uint32_t framerate = 30);
1418
int framerate(uint32_t framerate);
1519
int grab(uint8_t *buffer, uint32_t timeout=5000);
1620
int standby(bool enable);
21+
int enableMD(bool enable, md_callback_t callback=NULL);
22+
int setMDWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
23+
int setMDThreshold(uint32_t low, uint32_t high);
24+
int pollMD();
25+
int clearMDFlag();
1726
int testPattern(bool walking);
1827
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "camera.h"
2+
3+
CameraClass cam;
4+
uint8_t fb[320*240];
5+
bool motion_detected = false;
6+
7+
void on_motion()
8+
{
9+
motion_detected = true;
10+
}
11+
12+
void setup() {
13+
Serial.begin(115200);
14+
15+
pinMode(LEDB, OUTPUT);
16+
digitalWrite(LEDB, HIGH);
17+
18+
// Init the cam QVGA, 30FPS
19+
cam.begin(CAMERA_R320x240, 60);
20+
21+
cam.setMDThreshold(100, 200);
22+
cam.setMDWindow(0, 0, 320, 240);
23+
cam.enableMD(true, on_motion);
24+
}
25+
26+
void loop() {
27+
// put your main code here, to run repeatedly:
28+
if (motion_detected) {
29+
Serial.printf("Motion Detected!\n");
30+
digitalWrite(LEDB, LOW);
31+
delay(500);
32+
cam.clearMDFlag();
33+
motion_detected =false;
34+
digitalWrite(LEDB, HIGH);
35+
}
36+
delay(10);
37+
}

0 commit comments

Comments
 (0)