Need help making module

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Lennyz1988
Posts: 6
Joined: Thu Aug 02, 2018 6:22 am

Need help making module

Post by Lennyz1988 » Sun Jun 09, 2019 8:56 pm

Hi,

I am trying to add functions to an existing module. I am not an experienced coder so please be gentle. I am trying to learn.

The module is about the ESP32-CAM support. I want to add a flip function. The module is already working. I just want to add functions to it. I manage to call the function that I added using:

import camera
camera.flip()

But it always returns "Flipping Failed". What am I doing wrong? I tried to look at the Arduino CameraWebServer example because all the other functions used in the camera module look similair to the Arduino example.

The files used:
https://github.com/Lennyz1988/micropyth ... odcamera.c
https://github.com/Lennyz1988/esp32-cam ... e/sensor.h
https://github.com/Lennyz1988/esp32-cam ... p_camera.h

Arduino example:

Code: Select all

 sensor_t * s = esp_camera_sensor_get();
  //initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);//flip it back
    s->set_brightness(s, 1);//up the blightness just a bit
    s->set_saturation(s, -2);//lower the saturation
  }
  //drop down frame size for higher initial frame rate
  s->set_framesize(s, FRAMESIZE_QVGA);

In the file sensors.h is the function I want to add.

sensors.h

Code: Select all

typedef struct _sensor sensor_t;
typedef struct _sensor {
    sensor_id_t id;             // Sensor ID.
    uint8_t  slv_addr;          // Sensor I2C slave address.
    pixformat_t pixformat;
    camera_status_t status;
    int xclk_freq_hz;

    // Sensor function pointers
    int  (*init_status)         (sensor_t *sensor);
    int  (*reset)               (sensor_t *sensor);
    int  (*set_pixformat)       (sensor_t *sensor, pixformat_t pixformat);
    int  (*set_framesize)       (sensor_t *sensor, framesize_t framesize);
    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_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_aec2)            (sensor_t *sensor, int enable);
    int  (*set_awb_gain)        (sensor_t *sensor, int enable);
    int  (*set_agc_gain)        (sensor_t *sensor, int gain);
    int  (*set_aec_value)       (sensor_t *sensor, int gain);

    int  (*set_special_effect)  (sensor_t *sensor, int effect);
    int  (*set_wb_mode)         (sensor_t *sensor, int mode);
    int  (*set_ae_level)        (sensor_t *sensor, int level);

    int  (*set_dcw)             (sensor_t *sensor, int enable);
    int  (*set_bpc)             (sensor_t *sensor, int enable);
    int  (*set_wpc)             (sensor_t *sensor, int enable);

    int  (*set_raw_gma)         (sensor_t *sensor, int enable);
    int  (*set_lenc)            (sensor_t *sensor, int enable);
} sensor_t;
In the file esp_camera.h is the pointer:

Code: Select all

/**
 * @brief Get a pointer to the image sensor control structure
 *
 * @return pointer to the sensor
 */
sensor_t * esp_camera_sensor_get();
In the file modcamera.c (this is the module) I added the following parts:

Code: Select all

STATIC mp_obj_t camera_flip(){
   
    sensor_t * s = esp_camera_sensor_get();
    if (!s) {
        ESP_LOGE(TAG, "Flipping Failed");
        return mp_const_false;
    }

    s->set_vflip(s, 1);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(camera_flip_obj, camera_flip);

STATIC const mp_rom_map_elem_t camera_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_camera) },
    { MP_ROM_QSTR(MP_QSTR_flip), MP_ROM_PTR(&camera_flip_obj) },
};


stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Need help making module

Post by stijn » Mon Jun 10, 2019 7:42 am

Looks like esp_camera_sensor_get is returning NULL. Which, looking at https://github.com/espressif/esp32-came ... p_camera.h, is because you're not initalizing the camera using esp_camera_init.
But it always returns "Flipping Failed".
Just for information: you're using incorrect terminology here which can be confusing. It alwyas returns False, it logs "Flipping Failed".

Lennyz1988
Posts: 6
Joined: Thu Aug 02, 2018 6:22 am

Re: Need help making module

Post by Lennyz1988 » Mon Jun 10, 2019 8:01 am

stijn wrote:
Mon Jun 10, 2019 7:42 am
Looks like esp_camera_sensor_get is returning NULL. Which, looking at https://github.com/espressif/esp32-came ... p_camera.h, is because you're not initalizing the camera using esp_camera_init.
But it always returns "Flipping Failed".
Just for information: you're using incorrect terminology here which can be confusing. It alwyas returns False, it logs "Flipping Failed".
Aah you are right! Thank you! I'ts working as it should.

And thank you for the tip about using the correct terminology. I will try to use correct terminology.

Post Reply