Skip to content

Commit 8a1d016

Browse files
committed
Restore Camera APIs for motion detection
1 parent 2e5ebc3 commit 8a1d016

File tree

6 files changed

+140
-36
lines changed

6 files changed

+140
-36
lines changed

libraries/Camera/examples/CameraMotionDetect/CameraMotionDetect.ino

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#include "camera.h"
2+
#include "himax.h"
3+
HM01B0 himax;
4+
Camera cam(himax);
5+
6+
#ifdef ARDUINO_NICLA_VISION
7+
#error "GalaxyCore camera module does not support Motion Detection"
8+
#endif
29

3-
CameraClass cam;
4-
uint8_t fb[320*240];
510
bool motion_detected = false;
611

12+
void blinkLED(uint32_t count = 0xFFFFFFFF)
13+
{
14+
pinMode(LED_BUILTIN, OUTPUT);
15+
while (count--) {
16+
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
17+
delay(50); // wait for a second
18+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
19+
delay(50); // wait for a second
20+
}
21+
}
22+
723
void on_motion()
824
{
925
motion_detected = true;
@@ -16,17 +32,19 @@ void setup() {
1632
digitalWrite(LEDB, HIGH);
1733

1834
// Init the cam QVGA, 30FPS
19-
cam.begin(CAMERA_R320x240, 30);
35+
if (!cam.begin(CAMERA_R320x240, CAMERA_GRAYSCALE, 30)) {
36+
blinkLED();
37+
}
2038

2139
// Set motion detection threshold (0 -> 255).
2240
// The lower the threshold the higher the sensitivity.
23-
cam.motionDetectionThreshold(0);
41+
cam.setMotionDetectionThreshold(0);
2442

2543
// Set motion detection window/ROI.
26-
cam.motionDetectionWindow(0, 0, 320, 240);
44+
cam.setMotionDetectionWindow(0, 0, 320, 240);
2745

2846
// The detection can also be enabled without any callback
29-
cam.motionDetection(true, on_motion);
47+
cam.enableMotionDetection(on_motion);
3048
}
3149

3250
void loop() {

libraries/Camera/src/camera.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ bool FrameBuffer::isAllocated()
354354
return _isAllocated;
355355
}
356356

357-
358357
Camera::Camera(ImageSensor &sensor) :
359358
pixformat(-1),
360359
resolution(-1),
@@ -612,6 +611,40 @@ int Camera::grabFrame(FrameBuffer &fb, uint32_t timeout)
612611
return 0;
613612
}
614613

614+
int Camera::setMotionDetectionThreshold(uint32_t threshold)
615+
{
616+
return this->sensor->setMotionDetectionThreshold(threshold);
617+
}
618+
619+
int Camera::setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
620+
{
621+
uint32_t width, height;
622+
623+
width = restab[this->resolution][0];
624+
height= restab[this->resolution][1];
625+
626+
if (((x+w) > width) || ((y+h) > height)) {
627+
return -1;
628+
}
629+
return this->sensor->setMotionDetectionWindow(x, y, x+w, y+h);
630+
}
631+
632+
int Camera::enableMotionDetection(md_callback_t callback)
633+
{
634+
return this->sensor->enableMotionDetection(callback);
635+
}
636+
637+
int Camera::disableMotionDetection()
638+
{
639+
return this->sensor->disableMotionDetection();
640+
}
641+
642+
int Camera::motionDetected()
643+
{
644+
return this->sensor->motionDetected();
645+
}
646+
647+
615648
void Camera::debug(Stream &stream)
616649
{
617650
_debug = &stream;

libraries/Camera/src/camera.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class FrameBuffer {
5959
bool isAllocated();
6060
};
6161

62+
typedef void (*md_callback_t)();
63+
6264
class ImageSensor {
6365
public:
6466
virtual ~ImageSensor() { }
@@ -71,16 +73,17 @@ class ImageSensor {
7173
virtual int setPixelFormat(int32_t pixelformat) = 0;
7274
virtual int reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr) = 0;
7375
virtual uint8_t reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr) = 0;
76+
virtual int enableMotionDetection(md_callback_t callback) = 0;
77+
virtual int disableMotionDetection() = 0;
78+
virtual int setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h) = 0;
79+
virtual int setMotionDetectionThreshold(uint32_t threshold) = 0;
80+
virtual int motionDetected() = 0;
7481
virtual void debug(Stream &stream) = 0;
7582

7683
int setStandby(bool enable) {
7784
return -1;
7885
}
7986

80-
int IOCTL(int request, ...) {
81-
return -1;
82-
}
83-
8487
int setTestPattern(bool enable, bool walking) {
8588
return -1;
8689
}
@@ -108,6 +111,11 @@ class Camera {
108111
int setTestPattern(bool enable, bool walking);
109112
int frameSize();
110113
int grabFrame(FrameBuffer &fb, uint32_t timeout=5000);
114+
int enableMotionDetection(md_callback_t callback=NULL);
115+
int disableMotionDetection();
116+
int setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
117+
int setMotionDetectionThreshold(uint32_t threshold);
118+
int motionDetected();
111119
void debug(Stream &stream);
112120
};
113121

libraries/GC2145/gc2145.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class GC2145: public ImageSensor {
3838
int setPixelFormat(int32_t pixformat);
3939
int reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr = false);
4040
uint8_t reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr = false);
41+
int enableMotionDetection(md_callback_t callback) { return 0; };
42+
int disableMotionDetection() { return 0; };
43+
int setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h) { return 0; };
44+
int setMotionDetectionThreshold(uint32_t threshold) { return 0; };
45+
int motionDetected() { return 0; };
4146
void debug(Stream &stream);
4247
};
4348

libraries/Himax_HM01B0/himax.cpp

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,19 @@ static const uint16_t himax_qqvga_regs[][2] = {
294294
};
295295

296296
HM01B0::HM01B0(arduino::MbedI2C &i2c) :
297-
_i2c(&i2c)
297+
_i2c(&i2c),
298+
md_irq(PC_15),
299+
_md_callback(NULL)
298300
{
299301
}
300302

303+
void HM01B0::irqHandler()
304+
{
305+
if (_md_callback) {
306+
_md_callback();
307+
}
308+
}
309+
301310
int HM01B0::init()
302311
{
303312
_i2c->begin();
@@ -412,14 +421,7 @@ int HM01B0::setTestPattern(bool enable, bool walking)
412421
return 0;
413422
}
414423

415-
int HM01B0::enableMD(bool enable)
416-
{
417-
int ret = clearMD();
418-
ret |= reg_write(HM01B0_I2C_ADDR, MD_CTRL, enable ? 1:0, true);
419-
return ret;
420-
}
421-
422-
int HM01B0::setMDThreshold(uint32_t threshold)
424+
int HM01B0::setMotionDetectionThreshold(uint32_t threshold)
423425
{
424426
// Set motion detection threshold/sensitivity.
425427
// The recommended threshold range is 0x03 to 0xF0.
@@ -432,26 +434,57 @@ int HM01B0::setMDThreshold(uint32_t threshold)
432434
return reg_write(HM01B0_I2C_ADDR, MD_THL, (threshold < 3) ? 3 : (threshold > 0xF0) ? 0xF0 : threshold, true);
433435
}
434436

435-
int HM01B0::setLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
437+
int HM01B0::setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
438+
{
439+
int ret = 0;
440+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_START_H, (x>>8), true);
441+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_START_L, (x&0xff), true);
442+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_START_H, (y>>8), true);
443+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_START_L, (y&0xff), true);
444+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_END_H, (w>>8), true);
445+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_END_L, (w&0xff), true);
446+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_END_H, (h>>8), true);
447+
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_END_L, (h&0xff), true);
448+
return ret;
449+
}
450+
451+
int HM01B0::enableMotionDetection(md_callback_t callback)
452+
{
453+
md_irq.rise(0);
454+
_md_callback = callback;
455+
md_irq.rise(mbed::Callback<void()>(this, &HM01B0::irqHandler));
456+
md_irq.enable_irq();
457+
458+
int ret = clearMotionDetection();
459+
ret |= reg_write(HM01B0_I2C_ADDR, MD_CTRL, 1, true);
460+
return ret;
461+
}
462+
463+
int HM01B0::disableMotionDetection()
464+
{
465+
_md_callback = NULL;
466+
int ret = clearMotionDetection();
467+
ret |= reg_write(HM01B0_I2C_ADDR, MD_CTRL, 0, true);
468+
return ret;
469+
}
470+
471+
int HM01B0::motionDetected()
436472
{
437473
int ret = 0;
438-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_START_H, (x1>>8), true);
439-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_START_L, (x1&0xff), true);
440-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_START_H, (y1>>8), true);
441-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_START_L, (y1&0xff), true);
442-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_END_H, (x2>>8), true);
443-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_X_END_L, (x2&0xff), true);
444-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_END_H, (y2>>8), true);
445-
ret |= reg_write(HM01B0_I2C_ADDR, MD_LROI_Y_END_L, (y2&0xff), true);
474+
475+
ret = pollMotionDetection();
476+
if (ret == 1) {
477+
clearMotionDetection();
478+
}
446479
return ret;
447480
}
448481

449-
int HM01B0::pollMD()
482+
int HM01B0::pollMotionDetection()
450483
{
451484
return reg_read(HM01B0_I2C_ADDR, MD_INTERRUPT, true);
452485
}
453486

454-
int HM01B0::clearMD()
487+
int HM01B0::clearMotionDetection()
455488
{
456489
return reg_write(HM01B0_I2C_ADDR, I2C_CLEAR, 0x01, true);
457490
}

libraries/Himax_HM01B0/himax.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
#define __HIMAX_H
2121

2222
#include "camera.h"
23+
#include "drivers/InterruptIn.h"
2324

2425
class HM01B0: public ImageSensor {
2526
private:
2627
Stream *_debug;
2728
arduino::MbedI2C *_i2c;
29+
mbed::InterruptIn md_irq;
30+
md_callback_t _md_callback;
31+
void irqHandler();
2832
public:
2933
HM01B0(arduino::MbedI2C &i2c = CameraWire);
3034
int init();
@@ -37,11 +41,14 @@ class HM01B0: public ImageSensor {
3741
int reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr = false);
3842
uint8_t reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr = false);
3943
int setTestPattern(bool enable, bool walking);
40-
int enableMD(bool enable);
41-
int setMDThreshold(uint32_t threshold);
42-
int setLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
43-
int pollMD();
44-
int clearMD();
44+
int enableMotionDetection(md_callback_t callback=NULL);
45+
int disableMotionDetection();
46+
int setMotionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
47+
int setMotionDetectionThreshold(uint32_t threshold);
48+
int motionDetected();
49+
int pollMotionDetection();
50+
int clearMotionDetection();
51+
4552
uint8_t printRegs();
4653
void debug(Stream &stream);
4754
};

0 commit comments

Comments
 (0)