pub struct ArducamMega<SPI, Delay> { /* private fields */ }
Expand description

The main ArducamMega struct

This struct is the driver’s main entrypoint. By providing it with a configured SPI device and Delay implementation, this driver will be able to configure the Arducam Mega camera, take pictures, and read the picture data back from the camera.

Examples

The following example shows how the camera could be used on an ESP32 using the SPI3 device.

let peripherals = Peripherals::take();
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();
let delay = Delay::new(&clocks);

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio18;
let miso = io.pins.gpio19;
let mosi = io.pins.gpio23;
let cs = io.pins.gpio5;

let spi_controller = SpiBusController::from_spi(Spi::new_no_cs(
    peripherals.SPI3,
    sclk,
    mosi,
    miso,
    8u32.MHz(),
    SpiMode::Mode0,
    &mut system.peripheral_clock_control,
    &clocks,
));

let spi_device_1 = spi_controller.add_device(cs);

let mut cam = ArducamMega::new(spi_device_1, delay);

cam.reset()?
    .set_format(Format::Jpeg)?
    .set_resolution(Resolution::Hd)?
    .set_white_balance_mode(WhiteBalanceMode::Home)?;

let length = cam
    .capture()?
    .read_fifo_length()?;

// assuming buf.len() == length
cam.read_fifo_full(buf)?;

Implementations§

source§

impl<SPI, Delay> ArducamMega<SPI, Delay>where SPI: SpiDevice, SPI::Bus: SpiBus, Delay: DelayUs,

source

pub fn new(spi: SPI, delay: Delay) -> Self

source

pub fn reset(&mut self) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Resets the camera sensor sensor

This command sends a reset command to the camera. The Arducam SDK uses this after initialisation to ensure that the sensor is in a known-good state.

source

pub fn get_camera_type( &mut self ) -> Result<CameraType, Error<SPI::Error, Delay::Error>>

Reads the camera model from the camera

This function uses the SensorId register in the camera to obtain information about the sensor type. See CameraType for more information.

source

pub fn set_auto_focus( &mut self, value: u8 ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Available on crate feature 5mp only.

Sets the auto-focus of the camera

It is not clear how this feature should be used. As of current testing, only sending 0x00 actually produces successful captures. Sending 0x01 makes the camera produce invalid image data. More testing welcome.

source

pub fn set_format( &mut self, format: Format ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the capture format of the camera

This function allows you to control what format the camera captures pictures in. Format::Jpeg provides a good mix between image size and quality, and is the default.

source

pub fn set_resolution( &mut self, resolution: Resolution ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the capture resolution of the camera

This function allows you to control the resolution of the pictures that are captured by the camera. Both the 3MP and 5MP cameras have two different default resolutions. See Resolution for more details.

source

pub fn set_debug_device_address( &mut self, addr: u8 ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s debug device address

The Arducam SDK uses this command as part of the camera initialisation with the address 0x78, however this does not appear to be necessary for the camera to function properly.

source

pub fn capture_finished( &mut self ) -> Result<bool, Error<SPI::Error, Delay::Error>>

Checks whether the sensor has finished capturing an image

This command reads a register in the camera to check if the sensor has finished taking a picture. You should not attempt to read the FIFO buffer length nor read any FIFO buffer data prior to this returning true.

source

pub fn capture(&mut self) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Takes a picture using the currently-configured settings

This command starts by emptying the camera’s FIFO buffer and subsequently tells the sensor to capture an image. The image will be captured using the currently-configured settings (white balance, gain, exposure, colour filters, etc). This command blocks until the sensor has finished capturing the image.

source

pub fn capture_noblock( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Non-blocking version of capture()

source

pub fn read_fifo_length( &mut self ) -> Result<usize, Error<SPI::Error, Delay::Error>>

Reads the size of the camera’s FIFO buffer length

This function reads out the size of the FIFO buffer length. This (roughly) represents the size of the picture. It appears that in some cases, the reported buffer is larger than the actual picture data.

source

pub fn read_fifo_byte(&mut self) -> Result<u8, Error<SPI::Error, Delay::Error>>

Reads a single byte out of the FIFO buffer

Returns the first byte out of the FIFO buffer. After it has been read, the FIFO buffer will advance and the byte will be gone from the camera. In theory, calling this function fifo_length times should provide you with the full picture data.

Reading out the entire image data like this will be relatively slow, as each byte transfer will require an SPI transaction to be setup and ended. For faster transfers, please see read_fifo_full.

source

pub fn read_fifo_full<T>( &mut self, data: &mut T ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>where T: AsMut<[u8]>,

Reads out the entire FIFO buffer

Reads out the camera’s entire FIFO buffer into data. data must be large enough to accomodate the entire data transfer (as indicated by read_fifo_length()), or some data loss may occur. The data is currently read in chunks of 63 bytes, as this appears to be how the camera responds to burst read commands (however, this may be a side-effect of the ESP32’s SPI controller used for testing). If the provided data buffer is longer than the FIFO contents, the final bytes will be 0x00.

Please note: It appears the length reported by the camera through read_fifo_length() for JPEG pictures is bigger than the actual picture. Previous versions of this function made attempts at finding the EOF marker in the JPEG stream, however this is no longer the case. Please see find_jpeg_eof() to help trim the data stream.

This function is not currently tested with other data formats (YUV, RGB).

source

pub fn enable_auto_white_balance( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Enables the camera’s auto white balance

Note: This appears to not work on the ArducamMega 5MP, where pictures are severely green-tinted when using AWB.

source

pub fn disable_auto_white_balance( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Disables the camera’s auto white balance

This function is automatically called by set_white_balance_mode().

source

pub fn enable_auto_iso( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Enables the camera’s automatic gain adjustment

source

pub fn disable_auto_iso( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Disables the camera’s automatic gain adjustment

source

pub fn enable_auto_exposure( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Enables the camera’s automatic exposure control

source

pub fn disable_auto_exposure( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Disables the camera’s automatic exposure control

source

pub fn set_white_balance_mode( &mut self, mode: WhiteBalanceMode ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Fixes the white balance of the camera to mode

This will set the white balance mode to the fixed value described by mode. The auto mode has not been tested yet, and is copied straight from the SDK provided by Arducam. This function ensures that auto white balance is disabled in the sensor by calling disable_auto_white_balance() first.

source

pub fn enable_low_power_mode( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Turns on the camera’s low power mode

source

pub fn disable_low_power_mode( &mut self ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Turns off the camera’s low power mode

source

pub fn set_brightness_bias( &mut self, level: BrightnessLevel ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s brightness bias

source

pub fn set_contrast( &mut self, level: Level ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s contrast

source

pub fn set_saturation( &mut self, level: Level ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s saturation

source

pub fn set_exposure( &mut self, level: Level ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s exposure

source

pub fn set_color_effect( &mut self, effect: ColorEffect ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Sets the camera’s color effect

source

pub fn set_sharpness( &mut self, level: SharpnessLevel ) -> Result<&mut Self, Error<SPI::Error, Delay::Error>>

Available on crate feature 3mp only.

Sets the camera’s sharpness

Auto Trait Implementations§

§

impl<SPI, Delay> RefUnwindSafe for ArducamMega<SPI, Delay>where Delay: RefUnwindSafe, SPI: RefUnwindSafe,

§

impl<SPI, Delay> Send for ArducamMega<SPI, Delay>where Delay: Send, SPI: Send,

§

impl<SPI, Delay> Sync for ArducamMega<SPI, Delay>where Delay: Sync, SPI: Sync,

§

impl<SPI, Delay> Unpin for ArducamMega<SPI, Delay>where Delay: Unpin, SPI: Unpin,

§

impl<SPI, Delay> UnwindSafe for ArducamMega<SPI, Delay>where Delay: UnwindSafe, SPI: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.