Skip to content

Commit

Permalink
Enable motion detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
iabdalkader committed Jan 23, 2021
1 parent 780323e commit 43baef4
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 3 deletions.
43 changes: 43 additions & 0 deletions libraries/Himax_HM01B0/himax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,49 @@ int HIMAX_SetFramerate(uint32_t framerate)
return HIMAX_RegWrite(OSC_CLK_DIV, 0x08 | osc_div);
}

int HIMAX_EnableMD(bool enable)
{
int ret = HIMAX_ClearMD();
if (enable) {
ret |= HIMAX_RegWrite(MD_CTRL, 0x03);
} else {
ret |= HIMAX_RegWrite(MD_CTRL, 0x30);
}
return ret;
}

int HIMAX_SetMDThreshold(uint32_t low, uint32_t high)
{
int ret = 0;
ret |= HIMAX_RegWrite(MD_THL, low & 0xff);
ret |= HIMAX_RegWrite(MD_THH, high & 0xff);
return ret;
}

int HIMAX_SetLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
{
int ret = 0;
ret |= HIMAX_RegWrite(MD_LROI_X_START_H, (x1>>8));
ret |= HIMAX_RegWrite(MD_LROI_X_START_L, (x1&0xff));
ret |= HIMAX_RegWrite(MD_LROI_Y_START_H, (y1>>8));
ret |= HIMAX_RegWrite(MD_LROI_Y_START_L, (y1&0xff));
ret |= HIMAX_RegWrite(MD_LROI_X_END_H, (x2>>8));
ret |= HIMAX_RegWrite(MD_LROI_X_END_L, (x2&0xff));
ret |= HIMAX_RegWrite(MD_LROI_Y_END_H, (y2>>8));
ret |= HIMAX_RegWrite(MD_LROI_Y_END_L, (y2&0xff));
return ret;
}

int HIMAX_PollMD()
{
return HIMAX_RegRead(MD_INTERRUPT);
}

int HIMAX_ClearMD()
{
return HIMAX_RegWrite(I2C_CLEAR, 0x01);
}

/**
* @brief This function writes HIMAX camera registers.
* @retval None
Expand Down
15 changes: 15 additions & 0 deletions libraries/Himax_HM01B0/himax.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
#define MD_THM1 0x2159
#define MD_THM2 0x215A
#define MD_THL 0x215B
#define STATISTIC_CTRL 0x2000
#define MD_LROI_X_START_H 0x2011
#define MD_LROI_X_START_L 0x2012
#define MD_LROI_Y_START_H 0x2013
#define MD_LROI_Y_START_L 0x2014
#define MD_LROI_X_END_H 0x2015
#define MD_LROI_X_END_L 0x2016
#define MD_LROI_Y_END_H 0x2017
#define MD_LROI_Y_END_L 0x2018
#define MD_INTERRUPT 0x2160
// Sensor timing control
#define QVGA_WIN_EN 0x3010
#define SIX_BIT_MODE_EN 0x3011
Expand Down Expand Up @@ -147,6 +157,11 @@ uint8_t HIMAX_Open(void);
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_SetLROI(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
int HIMAX_PollMD();
int HIMAX_ClearMD();
void HIMAX_TestPattern(bool enable = true, bool walking = true);


Expand Down
79 changes: 77 additions & 2 deletions libraries/Portenta_Camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static const int CamRes[][2] = {
{320, 240},
};
static __IO uint32_t camera_frame_ready = 0;
static md_callback_t user_md_callback = NULL;

/* DCMI DMA Stream definitions */
#define CAMERA_DCMI_DMAx_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE
Expand Down Expand Up @@ -349,8 +350,6 @@ static int extclk_config(int frequency)
*/
void BSP_CAMERA_PwrUp(void)
{
GPIO_InitTypeDef gpio_init_structure;

mbed::DigitalOut* powerup = new mbed::DigitalOut(PC_13);
*powerup = 0;
delay(50);
Expand Down Expand Up @@ -452,6 +451,13 @@ void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)

}

void CameraClass::HIMAXIrqHandler()
{
if (user_md_callback) {
user_md_callback();
}
}

int CameraClass::begin(uint32_t resolution, uint32_t framerate)
{
if (resolution >= CAMERA_RMAX) {
Expand All @@ -477,6 +483,7 @@ int CameraClass::begin(uint32_t resolution, uint32_t framerate)
return -1;
}

user_md_callback = NULL;
this->initialized = true;
this->resolution = resolution;
return 0;
Expand Down Expand Up @@ -536,6 +543,74 @@ int CameraClass::standby(bool enable)
}
}

int CameraClass::setMDThreshold(uint32_t low, uint32_t high)
{
if (this->initialized == false) {
return -1;
}
return HIMAX_SetMDThreshold(low, high);
}

int CameraClass::setMDWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{
uint32_t width, height;

if (this->initialized == false) {
return -1;
}

width = CamRes[this->resolution][0];
height= CamRes[this->resolution][1];

if (((x+w) > width) || ((y+h) > height)) {
return -1;
}
return HIMAX_SetLROI(x, y, x+w, y+h);
}

int CameraClass::enableMD(bool enable, md_callback_t callback)
{
if (this->initialized == false) {
return -1;
}

this->md_irq.rise(0);

if (enable == false) {
user_md_callback = NULL;
} else {
user_md_callback = callback;
this->md_irq.rise(mbed::Callback<void()>(this, &CameraClass::HIMAXIrqHandler));
this->md_irq.enable_irq();
}

return HIMAX_EnableMD(enable);
}

int CameraClass::pollMD()
{
int ret = 0;

if (this->initialized == false) {
return -1;
}

ret = HIMAX_PollMD();
if (ret == 1) {
HIMAX_ClearMD();
}
return ret;
}

int CameraClass::clearMDFlag()
{
if (this->initialized == false) {
return -1;
}

return HIMAX_ClearMD();
}

int CameraClass::testPattern(bool walking)
{
if (this->initialized == false) {
Expand Down
11 changes: 10 additions & 1 deletion libraries/Portenta_Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ enum {
CAMERA_RMAX
};

typedef void (*md_callback_t)();

class CameraClass {
private:
uint32_t resolution;
bool initialized;
mbed::InterruptIn md_irq;
void HIMAXIrqHandler();
public:
CameraClass(): initialized(false) {}
CameraClass(): initialized(false), md_irq(PC_15){}
int begin(uint32_t resolution = CAMERA_R320x240, uint32_t framerate = 30);
int framerate(uint32_t framerate);
int grab(uint8_t *buffer, uint32_t timeout=5000);
int standby(bool enable);
int enableMD(bool enable, md_callback_t callback=NULL);
int setMDWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
int setMDThreshold(uint32_t low, uint32_t high);
int pollMD();
int clearMDFlag();
int testPattern(bool walking);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "camera.h"

CameraClass cam;
uint8_t fb[320*240];
bool motion_detected = false;

void on_motion()
{
motion_detected = true;
}

void setup() {
Serial.begin(115200);

pinMode(LEDB, OUTPUT);
digitalWrite(LEDB, HIGH);

// Init the cam QVGA, 30FPS
cam.begin(CAMERA_R320x240, 60);

cam.setMDThreshold(100, 200);
cam.setMDWindow(0, 0, 320, 240);
cam.enableMD(true, on_motion);
}

void loop() {
// put your main code here, to run repeatedly:
if (motion_detected) {
Serial.printf("Motion Detected!\n");
digitalWrite(LEDB, LOW);
delay(500);
cam.clearMDFlag();
motion_detected =false;
digitalWrite(LEDB, HIGH);
}
delay(10);
}

0 comments on commit 43baef4

Please sign in to comment.