Skip to content

Commit

Permalink
Pass ImageSensor object as Camera parameter + make i2c interface conf…
Browse files Browse the repository at this point in the history
…igurable
  • Loading branch information
giulcioffi committed Mar 7, 2022
1 parent af34f48 commit 77c15d3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 54 deletions.
34 changes: 21 additions & 13 deletions libraries/GC2145/gc2145.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,16 @@ static const uint8_t default_regs[][2] = {
{0x00, 0x00},
};

GC2145::GC2145(arduino::MbedI2C &i2c) :
_i2c(&i2c)
{
}

int GC2145::Init()
{
_i2c->begin();
_i2c->setClock(100000);

int ret = 0;

// Write default regsiters
Expand Down Expand Up @@ -853,32 +861,32 @@ int GC2145::SetPixelFormat(int32_t pixformat)

int GC2145::reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr)
{
Wire2.beginTransmission(dev_addr);
_i2c->beginTransmission(dev_addr);
uint8_t buf[3] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF), reg_data};
if (wide_addr == true) {
Wire2.write(buf, 1);
_i2c->write(buf, 1);
}
Wire2.write(&buf[1], 2);
return Wire2.endTransmission();
_i2c->write(&buf[1], 2);
return _i2c->endTransmission();
}

uint8_t GC2145::reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr)
{
uint8_t reg_data = 0;
uint8_t buf[2] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF)};
Wire2.beginTransmission(dev_addr);
_i2c->beginTransmission(dev_addr);
if (wide_addr) {
Wire2.write(buf, 2);
_i2c->write(buf, 2);
} else {
Wire2.write(&buf[1], 1);
_i2c->write(&buf[1], 1);
}
Wire2.endTransmission(false);
Wire2.requestFrom(dev_addr, 1);
if (Wire2.available()) {
reg_data = Wire2.read();
_i2c->endTransmission(false);
_i2c->requestFrom(dev_addr, 1);
if (_i2c->available()) {
reg_data = _i2c->read();
}
while (Wire2.available()) {
Wire2.read();
while (_i2c->available()) {
_i2c->read();
}
return reg_data;
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/GC2145/gc2145.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class GC2145: public ImageSensor {
private:
int SetWindow(uint16_t reg, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
Stream *_debug;
arduino::MbedI2C *_i2c;

public:
GC2145(arduino::MbedI2C &i2c = CameraWire);
int Init();
int Reset();
int GetID() { return GC2145_I2C_ADDR; };
Expand Down
34 changes: 21 additions & 13 deletions libraries/Himax_HM01B0/himax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,16 @@ static const uint16_t himax_qqvga_regs[][2] = {
{0x0000, 0x00}, // EOF
};

HM01B0::HM01B0(arduino::MbedI2C &i2c) :
_i2c(&i2c)
{
}

int HM01B0::Init()
{
_i2c->begin();
_i2c->setClock(100000);

if (Reset() != 0 ) {
return -1;
}
Expand Down Expand Up @@ -461,32 +469,32 @@ uint8_t HM01B0::PrintRegs()

int HM01B0::reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr)
{
Wire.beginTransmission(dev_addr);
_i2c->beginTransmission(dev_addr);
uint8_t buf[3] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF), reg_data};
if (wide_addr == true) {
Wire.write(buf, 1);
_i2c->write(buf, 1);
}
Wire.write(&buf[1], 2);
return Wire.endTransmission();
_i2c->write(&buf[1], 2);
return _i2c->endTransmission();
}

uint8_t HM01B0::reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr)
{
uint8_t reg_data = 0;
uint8_t buf[2] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF)};
Wire.beginTransmission(dev_addr);
_i2c->beginTransmission(dev_addr);
if (wide_addr) {
Wire.write(buf, 2);
_i2c->write(buf, 2);
} else {
Wire.write(&buf[1], 1);
_i2c->write(&buf[1], 1);
}
Wire.endTransmission(false);
Wire.requestFrom(dev_addr, 1);
if (Wire.available()) {
reg_data = Wire.read();
_i2c->endTransmission(false);
_i2c->requestFrom(dev_addr, 1);
if (_i2c->available()) {
reg_data = _i2c->read();
}
while (Wire.available()) {
Wire.read();
while (_i2c->available()) {
_i2c->read();
}
return reg_data;
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/Himax_HM01B0/himax.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
class HM01B0: public ImageSensor {
private:
Stream *_debug;
arduino::MbedI2C *_i2c;
public:
HM01B0(arduino::MbedI2C &i2c = CameraWire);
int Init();
int Reset();
int GetID() { return HM01B0_I2C_ADDR; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

//REDIRECT_STDOUT_TO(Serial);

// Uncomment for HM01B0
#include "himax.h"
Camera cam(new HM01B0());

// Uncomment to use GC2145 instead
//#include "gc2145.h"
//Camera cam(new GC2145());
Camera cam;

uint8_t *fb = (uint8_t*) SDRAM_START_ADDRESS;

Expand Down
41 changes: 21 additions & 20 deletions libraries/Portenta_Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

// Include all image sensor drivers here.
#ifdef ARDUINO_PORTENTA_H7_M7
#include "himax.h"

#define DCMI_TIM (TIM1)
#define DCMI_TIM_PIN (GPIO_PIN_1)
Expand All @@ -38,10 +37,10 @@
#define DCMI_TIM_CLK_DISABLE() __TIM1_CLK_DISABLE()
#define DCMI_TIM_PCLK_FREQ() HAL_RCC_GetPCLK2Freq()
#define DCMI_TIM_FREQUENCY (6000000)
#endif
arduino::MbedI2C CameraWire(I2C_SDA, I2C_SCL);

#elif defined(ARDUINO_NICLA_VISION)

#ifdef ARDUINO_NICLA_VISION
#include "gc2145.h"
#define DCMI_TIM (TIM3)
#define DCMI_TIM_PIN (GPIO_PIN_7)
#define DCMI_TIM_PORT (GPIOA)
Expand All @@ -51,6 +50,8 @@
#define DCMI_TIM_CLK_DISABLE() __TIM3_CLK_DISABLE()
#define DCMI_TIM_PCLK_FREQ() HAL_RCC_GetPCLK1Freq()
#define DCMI_TIM_FREQUENCY (12000000)
arduino::MbedI2C CameraWire(I2C_SDA2, I2C_SCL2);

#endif

#define DCMI_IRQ_PRI NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 2, 0)
Expand Down Expand Up @@ -299,6 +300,15 @@ void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)

} // extern "C"

Camera::Camera(ImageSensor &sensor) :
pixformat(-1),
resolution(-1),
framerate(-1),
sensor(&sensor),
_debug(NULL)
{
}

int Camera::Reset()
{
#ifdef ARDUINO_PORTENTA_H7_M7
Expand All @@ -311,7 +321,7 @@ int Camera::Reset()
#endif
return 0;
}

/*
int Camera::ProbeSensor()
{
uint8_t addr;
Expand All @@ -337,7 +347,7 @@ int Camera::ProbeSensor()
#endif //ARDUINO_NICLA_VISION
return addr;
}

*/
int Camera::begin(int32_t resolution, int32_t pixformat, int32_t framerate)
{
if (resolution >= CAMERA_RMAX || pixformat >= CAMERA_PMAX) {
Expand All @@ -351,24 +361,15 @@ int Camera::begin(int32_t resolution, int32_t pixformat, int32_t framerate)
// Reset the image sensor.
Reset();

// Start I2C
#ifdef ARDUINO_PORTENTA_H7_M7
Wire.begin();
Wire.setClock(400000);
#endif

#ifdef ARDUINO_NICLA_VISION
Wire2.begin();
Wire2.setClock(100000);
#endif

//if (ProbeSensor() != this->sensor->GetID()) {
/*
if (ProbeSensor() != this->sensor->GetID()) {
if (_debug) {
_debug->print("SensorID: 0x");
_debug->println(this->sensor->GetID(), HEX);
}
//return -1;
//}
return -1;
}
*/

if (sensor->GetClockFrequency() != DCMI_TIM_FREQUENCY) {
// Reconfigure the sensor clock frequency.
Expand Down
7 changes: 6 additions & 1 deletion libraries/Portenta_Camera/src/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern const uint32_t restab[CAMERA_RMAX][2];

class ImageSensor {
public:
virtual ~ImageSensor() { }
virtual int Init() = 0;
virtual int Reset() = 0;
virtual int GetID() = 0;
Expand Down Expand Up @@ -78,8 +79,9 @@ class Camera {
int Reset();
int ProbeSensor();
Stream *_debug;
arduino::MbedI2C *_i2c;
public:
Camera(ImageSensor *sensor): pixformat(-1), resolution(-1), framerate(-1), sensor(sensor) {}
Camera(ImageSensor &sensor);
int begin(int32_t resolution=CAMERA_R320x240, int32_t pixformat=CAMERA_GRAYSCALE, int32_t framerate=30);
int GetID();
int SetFrameRate(int32_t framerate);
Expand All @@ -91,4 +93,7 @@ class Camera {
int GrabFrame(uint8_t *buffer, uint32_t timeout=5000);
void debug(Stream &stream);
};

#endif // __CAMERA_H

extern arduino::MbedI2C CameraWire;

0 comments on commit 77c15d3

Please sign in to comment.