Add missing cam functionality

Added the ability to turn AGC off. Kinda will need the ability to restore
AGC settings back to user specified ones in the future... but, this will
do for now.

Added the ability to turn AEC off. Objectively this function probably
won't be used. But, in low light situations it can help.

Added get_fb() to allow you to get the last image snapshot returned.

There was some old exposure function in the code that was getting
optimized out. So, I deleted the used methods that didn't have any code
in them and commented out the only method that did.
This commit is contained in:
Kwabena W. Agyeman 2016-04-27 21:53:06 -04:00
parent 794bb367ae
commit 80cb701d3b
9 changed files with 200 additions and 41 deletions

View File

@ -560,11 +560,6 @@ static int set_saturation(sensor_t *sensor, int level)
return ret;
}
static int set_exposure(sensor_t *sensor, int exposure)
{
return 0;
}
static int set_gainceiling(sensor_t *sensor, gainceiling_t gainceiling)
{
int ret =0;
@ -620,7 +615,7 @@ static int set_whitebal(sensor_t *sensor, int enable)
/* Switch to SENSOR register bank */
ret |= SCCB_Write(sensor->slv_addr, BANK_SEL, BANK_SEL_DSP);
/* Update COM7 */
/* Update CTRL1 */
reg = SCCB_Read(sensor->slv_addr, CTRL1);
if (enable) {
@ -633,6 +628,48 @@ static int set_whitebal(sensor_t *sensor, int enable)
return ret;
}
static int set_gain_ctrl(sensor_t *sensor, int enable)
{
int ret=0;
uint8_t reg;
/* Switch to SENSOR register bank */
ret |= SCCB_Write(sensor->slv_addr, BANK_SEL, BANK_SEL_SENSOR);
/* Update COM8 */
reg = SCCB_Read(sensor->slv_addr, COM8);
if (enable) {
reg |= COM8_AGC_EN;
} else {
reg &= ~COM8_AGC_EN;
}
ret |= SCCB_Write(sensor->slv_addr, COM8, reg);
return ret;
}
static int set_exposure_ctrl(sensor_t *sensor, int enable)
{
int ret=0;
uint8_t reg;
/* Switch to SENSOR register bank */
ret |= SCCB_Write(sensor->slv_addr, BANK_SEL, BANK_SEL_SENSOR);
/* Update COM8 */
reg = SCCB_Read(sensor->slv_addr, COM8);
if (enable) {
reg |= COM8_AEC_EN;
} else {
reg &= ~COM8_AEC_EN;
}
ret |= SCCB_Write(sensor->slv_addr, COM8, reg);
return ret;
}
static int set_hmirror(sensor_t *sensor, int enable)
{
int ret=0;
@ -685,10 +722,11 @@ int ov2640_init(sensor_t *sensor)
sensor->set_contrast = set_contrast;
sensor->set_brightness= set_brightness;
sensor->set_saturation= set_saturation;
sensor->set_exposure = set_exposure;
sensor->set_gainceiling = set_gainceiling;
sensor->set_quality = set_quality;
sensor->set_colorbar = set_colorbar;
sensor->set_gain_ctrl = set_gain_ctrl;
sensor->set_exposure_ctrl = set_exposure_ctrl;
sensor->set_whitebal = set_whitebal;
sensor->set_hmirror = set_hmirror;
sensor->set_vflip = set_vflip;

View File

@ -262,11 +262,6 @@ static int set_saturation(sensor_t *sensor, int level)
return ret;
}
static int set_exposure(sensor_t *sensor, int exposure)
{
return 0;
}
static int set_gainceiling(sensor_t *sensor, gainceiling_t gainceiling)
{
// Read register COM9
@ -306,6 +301,30 @@ static int set_whitebal(sensor_t *sensor, int enable)
return SCCB_Write(sensor->slv_addr, COM8, reg);
}
static int set_gain_ctrl(sensor_t *sensor, int enable)
{
// Read register COM8
uint8_t reg = SCCB_Read(sensor->slv_addr, COM8);
// Set white bal on/off
reg = COM8_SET_AGC(reg, enable);
// Write back register COM8
return SCCB_Write(sensor->slv_addr, COM8, reg);
}
static int set_exposure_ctrl(sensor_t *sensor, int enable)
{
// Read register COM8
uint8_t reg = SCCB_Read(sensor->slv_addr, COM8);
// Set white bal on/off
reg = COM8_SET_AEC(reg, enable);
// Write back register COM8
return SCCB_Write(sensor->slv_addr, COM8, reg);
}
static int set_hmirror(sensor_t *sensor, int enable)
{
// Read register COM3
@ -360,10 +379,11 @@ int ov7725_init(sensor_t *sensor)
sensor->set_contrast = set_contrast;
sensor->set_brightness= set_brightness;
sensor->set_saturation= set_saturation;
sensor->set_exposure = set_exposure;
sensor->set_gainceiling = set_gainceiling;
sensor->set_colorbar = set_colorbar;
sensor->set_whitebal = set_whitebal;
sensor->set_gain_ctrl = set_gain_ctrl;
sensor->set_exposure_ctrl = set_exposure_ctrl;
sensor->set_hmirror = set_hmirror;
sensor->set_vflip = set_vflip;
sensor->set_special_effect = set_special_effect;

View File

@ -93,7 +93,9 @@
#define COM8_AGC_EN 0x04 /* AGC Enable */
#define COM8_AWB_EN 0x02 /* AWB Enable */
#define COM8_AEC_EN 0x01 /* AEC Enable */
#define COM8_SET_AGC(r, x) ((r&0xFB)|((x&0x1)<<2))
#define COM8_SET_AWB(r, x) ((r&0xFD)|((x&0x1)<<1))
#define COM8_SET_AEC(r, x) ((r&0xFE)|((x&0x1)<<0))
#define COM9 0x14 /* Common Control 9 */
#define COM9_HISTO_AVG 0x80 /* Histogram or average based AEC/AGC selection */

View File

@ -319,22 +319,22 @@ static int set_brightness(sensor_t *sensor, int level)
return 0;
}
static int set_exposure(sensor_t *sensor, int exposure)
{
uint8_t val;
val = SCCB_Read(sensor->slv_addr, REG_COM1);
//static int set_exposure(sensor_t *sensor, int exposure)
//{
// uint8_t val;
// val = SCCB_Read(sensor->slv_addr, REG_COM1);
/* exposure [1:0] */
SCCB_Write(sensor->slv_addr, REG_COM1, val | (exposure&0x03));
// /* exposure [1:0] */
// SCCB_Write(sensor->slv_addr, REG_COM1, val | (exposure&0x03));
/* exposure [9:2] */
SCCB_Write(sensor->slv_addr, REG_AECH, ((exposure>>2)&0xFF));
// /* exposure [9:2] */
// SCCB_Write(sensor->slv_addr, REG_AECH, ((exposure>>2)&0xFF));
/* exposure [15:10] */
SCCB_Write(sensor->slv_addr, REG_AECHM, ((exposure>>10)&0x3F));
// /* exposure [15:10] */
// SCCB_Write(sensor->slv_addr, REG_AECHM, ((exposure>>10)&0x3F));
return 0;
}
// return 0;
//}
static int set_gainceiling(sensor_t *sensor, gainceiling_t gainceiling)
{
@ -354,6 +354,28 @@ static int set_whitebal(sensor_t *sensor, int enable)
return 0;
}
static int set_gain_ctrl(sensor_t *sensor, int enable)
{
uint8_t val;
val = SCCB_Read(sensor->slv_addr, REG_COM8);
SCCB_Write(sensor->slv_addr, REG_COM8,
enable ? (val | REG_COM8_AGC) : (val & ~REG_COM8_AGC));
return 0;
}
static int set_exposure_ctrl(sensor_t *sensor, int enable)
{
uint8_t val;
val = SCCB_Read(sensor->slv_addr, REG_COM8);
SCCB_Write(sensor->slv_addr, REG_COM8,
enable ? (val | REG_COM8_AEC) : (val & ~REG_COM8_AEC));
return 0;
}
static int set_hmirror(sensor_t *sensor, int enable)
{
uint8_t val;
@ -384,9 +406,10 @@ int ov9650_init(sensor_t *sensor)
sensor->set_framesize = set_framesize;
sensor->set_framerate = set_framerate;
sensor->set_brightness= set_brightness;
sensor->set_exposure = set_exposure;
sensor->set_gainceiling = set_gainceiling;
sensor->set_whitebal = set_whitebal;
sensor->set_gain_ctrl = set_gain_ctrl;
sensor->set_exposure_ctrl = set_exposure_ctrl;
sensor->set_hmirror = set_hmirror;
sensor->set_vflip = set_vflip;

View File

@ -120,7 +120,9 @@
#define REG_COM7_QVGA (1<<4)
#define REG_COM7_CIF (1<<5)
#define REG_COM7_VGA (1<<6)
#define REG_COM8_AGC (1<<2)
#define REG_COM8_AWB (1<<1)
#define REG_COM8_AEC (1<<0)
#define REG_MVFP_HMIRROR (1<<5)
#define REG_MVFP_VFLIP (1<<4)
#endif //__REG_REGS_H__

View File

@ -24,10 +24,6 @@ static mp_obj_t py_sensor_reset() {
return mp_const_none;
}
static mp_obj_t py_sensor_get_id() {
return mp_obj_new_int(sensor_get_id());
}
static mp_obj_t py_sensor_snapshot() {
mp_obj_t image = py_image(0, 0, 0, 0);
@ -52,6 +48,18 @@ static mp_obj_t py_sensor_skip_frames(uint n_args, const mp_obj_t *args) {
return mp_const_none;
}
static mp_obj_t py_sensor_get_fb() {
mp_obj_t image = py_image(0, 0, 0, 0);
if (sensor_get_fb(py_image_cobj(image))) {
return mp_const_none;
}
return image;
}
static mp_obj_t py_sensor_get_id() {
return mp_obj_new_int(sensor_get_id());
}
static mp_obj_t py_sensor_set_pixformat(mp_obj_t pixformat) {
if (sensor_set_pixformat(mp_obj_get_int(pixformat)) != 0) {
PY_ASSERT_TRUE_MSG(0, "Pixel format is not supported!");
@ -177,6 +185,20 @@ static mp_obj_t py_sensor_set_whitebal(mp_obj_t enable) {
return mp_const_true;
}
static mp_obj_t py_sensor_set_gain_ctrl(mp_obj_t enable) {
if (sensor_set_gain_ctrl(mp_obj_is_true(enable)) != 0) {
return mp_const_false;
}
return mp_const_true;
}
static mp_obj_t py_sensor_set_exposure_ctrl(mp_obj_t enable) {
if (sensor_set_exposure_ctrl(mp_obj_is_true(enable)) != 0) {
return mp_const_false;
}
return mp_const_true;
}
static mp_obj_t py_sensor_set_hmirror(mp_obj_t enable) {
if (sensor_set_hmirror(mp_obj_is_true(enable)) != 0) {
return mp_const_false;
@ -251,9 +273,10 @@ static mp_obj_t py_sensor_read_reg(mp_obj_t addr) {
//}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_sensor_reset_obj, py_sensor_reset);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_sensor_get_id_obj, py_sensor_get_id);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_sensor_snapshot_obj, py_sensor_snapshot);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(py_sensor_skip_frames_obj, 0, 1, py_sensor_skip_frames);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_sensor_get_fb_obj, py_sensor_get_fb);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_sensor_get_id_obj, py_sensor_get_id);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_pixformat_obj, py_sensor_set_pixformat);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_framerate_obj, py_sensor_set_framerate);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_framesize_obj, py_sensor_set_framesize);
@ -264,6 +287,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_saturation_obj, py_sensor_se
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_quality_obj, py_sensor_set_quality);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_colorbar_obj, py_sensor_set_colorbar);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_whitebal_obj, py_sensor_set_whitebal);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_gain_ctrl_obj, py_sensor_set_gain_ctrl);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_exposure_ctrl_obj, py_sensor_set_exposure_ctrl);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_hmirror_obj, py_sensor_set_hmirror);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_vflip_obj, py_sensor_set_vflip);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_sensor_set_special_effect_obj, py_sensor_set_special_effect);
@ -307,6 +332,7 @@ STATIC const mp_map_elem_t globals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&py_sensor_reset_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_snapshot), (mp_obj_t)&py_sensor_snapshot_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_skip_frames), (mp_obj_t)&py_sensor_skip_frames_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_fb), (mp_obj_t)&py_sensor_get_fb_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_id), (mp_obj_t)&py_sensor_get_id_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_pixformat), (mp_obj_t)&py_sensor_set_pixformat_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_framerate), (mp_obj_t)&py_sensor_set_framerate_obj },
@ -318,6 +344,8 @@ STATIC const mp_map_elem_t globals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_quality), (mp_obj_t)&py_sensor_set_quality_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_colorbar), (mp_obj_t)&py_sensor_set_colorbar_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_whitebal), (mp_obj_t)&py_sensor_set_whitebal_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_gain_ctrl), (mp_obj_t)&py_sensor_set_gain_ctrl_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_exposure_ctrl), (mp_obj_t)&py_sensor_set_exposure_ctrl_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_hmirror), (mp_obj_t)&py_sensor_set_hmirror_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_vflip), (mp_obj_t)&py_sensor_set_vflip_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_special_effect), (mp_obj_t)&py_sensor_set_special_effect_obj },

View File

@ -163,6 +163,7 @@ Q(NEGATIVE)
Q(reset)
Q(snapshot)
Q(skip_frames)
Q(get_fb)
Q(get_id)
Q(set_pixformat)
Q(set_framerate)
@ -174,6 +175,8 @@ Q(set_saturation)
Q(set_quality)
Q(set_colorbar)
Q(set_whitebal)
Q(set_gain_ctrl)
Q(set_exposure_ctrl)
Q(set_hmirror)
Q(set_vflip)
Q(set_special_effect)

View File

@ -410,11 +410,6 @@ int sensor_set_saturation(int level)
return -1;
}
int sensor_set_exposure(int exposure)
{
return 0;
}
int sensor_set_gainceiling(gainceiling_t gainceiling)
{
if (sensor.gainceiling == gainceiling) {
@ -466,6 +461,28 @@ int sensor_set_whitebal(int enable)
return 0;
}
int sensor_set_gain_ctrl(int enable)
{
/* call the sensor specific function */
if (sensor.set_gain_ctrl == NULL
|| sensor.set_gain_ctrl(&sensor, enable) != 0) {
/* operation not supported */
return -1;
}
return 0;
}
int sensor_set_exposure_ctrl(int enable)
{
/* call the sensor specific function */
if (sensor.set_exposure_ctrl == NULL
|| sensor.set_exposure_ctrl(&sensor, enable) != 0) {
/* operation not supported */
return -1;
}
return 0;
}
int sensor_set_hmirror(int enable)
{
/* call the sensor specific function */
@ -666,3 +683,23 @@ int sensor_snapshot(image_t *image)
return 0;
}
int sensor_get_fb(image_t *img)
{
if (!fb->bpp) {
return -1;
}
if (img != NULL) {
img->w = fb->w;
img->h = fb->h;
img->bpp = fb->bpp;
img->pixels = fb->pixels;
if (sensor.pixformat != PIXFORMAT_JPEG &&
SENSOR_HW_FLAGS_GET(&sensor, SENSOR_HW_FLAGS_SW_JPEG)) {
img->pixels += FB_JPEG_OFFS_SIZE;
}
}
return 0;
}

View File

@ -122,11 +122,12 @@ typedef struct _sensor {
int (*set_contrast) (sensor_t *sensor, int level);
int (*set_brightness) (sensor_t *sensor, int level);
int (*set_saturation) (sensor_t *sensor, int level);
int (*set_exposure) (sensor_t *sensor, int exposure);
int (*set_gainceiling) (sensor_t *sensor, gainceiling_t gainceiling);
int (*set_quality) (sensor_t *sensor, int quality);
int (*set_colorbar) (sensor_t *sensor, int enable);
int (*set_whitebal) (sensor_t *sensor, int enable);
int (*set_gain_ctrl) (sensor_t *sensor, int enable);
int (*set_exposure_ctrl) (sensor_t *sensor, int enable);
int (*set_hmirror) (sensor_t *sensor, int enable);
int (*set_vflip) (sensor_t *sensor, int enable);
int (*set_special_effect) (sensor_t *sensor, sde_t sde);
@ -160,6 +161,9 @@ int sensor_enable_jpeg(bool enable);
// Capture a Snapshot.
int sensor_snapshot(image_t *image);
// Capture the frame buffer.
int sensor_get_fb(image_t *img);
// Set the sensor pixel format.
int sensor_set_pixformat(pixformat_t pixformat);
@ -178,10 +182,6 @@ int sensor_set_brightness(int level);
// Set the sensor saturation level (from -3 to +3).
int sensor_set_saturation(int level);
// Set the sensor exposure level.
// Note: This function has no effect when AEC (Automatic Exposure Control) is enabled.
int sensor_set_exposure(int exposure);
// Set the sensor AGC gain ceiling.
// Note: This function has no effect when AGC (Automatic Gain Control) is disabled.
int sensor_set_gainceiling(gainceiling_t gainceiling);
@ -195,6 +195,12 @@ int sensor_set_colorbar(int enable);
// Enable/disable the whitebal mode.
int sensor_set_whitebal(int enable);
// Enable/disable the agc mode.
int sensor_set_gain_ctrl(int enable);
// Enable/disable the aec mode.
int sensor_set_exposure_ctrl(int enable);
// Enable/disable the hmirror mode.
int sensor_set_hmirror(int enable);