Skip to content

Builder Pattern for MpuConfig #20

@tchamelot

Description

@tchamelot

Currently, MpuConfig<MODE> tries to follow the Builder Pattern. However, to fully match the pattern, MpuConfig<MODE> is missing a function like

impl MpuConfig<MODE> {
    pub fn consume(&self) -> Self {
        self
    }
}

The problem is that currently MpuConfig<MODE> has only default builders which return Self or setters which return &mut Self. That makes it really difficult to use. Would a refactor of the configuration be possible. My current idea would be to add:

impl MpuConfig<MODE> {
    pub fn build<Device>(&self,  dev: Device) -> Mpu9250<D, MODE> {
        Mpu9250 {
            dev,
            raw_mag_sensitivity_adjustments: [0; 3],
            mag_sensitivity_adjustments: [0.0; 3],
            gyro_scale: self.gyro_scale.unwrap_or_default(),
            accel_scale: self.accel_scale.unwrap_or_default(),
            mag_scale: self.mag_scale.unwrap_or_defaut(),
            accel_data_rate: self.accel_data_rate
                .unwrap_or_default(),
            gyro_temp_data_rate: self.gyro_temp_data_rate
                .unwrap_or_default(),
            sample_rate_divisor: self.sample_rate_divisor,
            _mode: PhantomData,
        }
    }
}

Actually, just that would pose a problem as the Device creation is now to be handled by the final user. But we could imagine having two build for I2C and SPI which create the Device.

Like for my previous issue, I am up for it.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions