Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/devices/MotorHat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,60 @@ using (var motorHat = new MotorHat())

Check the [ServoMotor documentation](../ServoMotor/README.md) for examples on how to use the ServoMotor class

## Resource management (disposing)

The `MotorHat` owns all the resources it creates. When you call `CreateDCMotor`, `CreateServoMotor` or `CreatePwmChannel`, the returned object uses PWM channels that belong to the `MotorHat`'s underlying PCA9685 controller.

Because of this ownership model:

- **Disposing the `MotorHat` is enough.** `MotorHat.Dispose()` stops every channel it handed out and then disposes the underlying PCA9685 (and its I2C device). You do not need to dispose the motors, servos or PWM channels separately.
- **If you dispose motors explicitly, do it before disposing the `MotorHat`.** A `DCMotor` created by `CreateDCMotor` stops its PWM channels in `Dispose()`, and stopping a channel after the PCA9685 has been disposed can throw.
- **Dispose order matters when disposing both.** Prefer nested `using` (motor inside the `MotorHat` scope) so the motor is disposed first.

The recommended pattern is to keep the objects you need alive for as long as the `MotorHat` and let a single `using` (or `Dispose`) on the `MotorHat` clean everything up:

```csharp
using (var motorHat = new MotorHat())
{
var motor = motorHat.CreateDCMotor(1);

motor.Speed = 1;

// ... use the motor ...

// No need to dispose 'motor' explicitly; disposing 'motorHat' releases it.
}
```

If you wrap the `MotorHat` in your own class, forward disposal to the `MotorHat`. Disposing the individual motors as well is harmless but not required:

```csharp
public class PumpController : IDisposable
{
private MotorHat _motorHat = new MotorHat();
private List<DCMotor> _motors = new List<DCMotor>();

public void Initialize()
{
_motors.Add(_motorHat.CreateDCMotor(1));
}

// ... use the motors ...

public void Dispose()
{
// Dispose motors first (their Dispose stops the PWM channels),
// then dispose the MotorHat which releases the underlying PCA9685 + I2C device.
foreach (var motor in _motors)
{
motor.Dispose();
}

_motorHat.Dispose();
}
Comment on lines +85 to +94

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
// Disposing the MotorHat stops and releases every motor/channel it created.
// If you dispose motors explicitly, dispose them before disposing the MotorHat.
}
{
// Dispose motors first (their Dispose stops the PWM channels),
// then dispose the MotorHat which releases the underlying PCA9685 + I2C device.
foreach (var motor in _motors)
{
motor.Dispose();
}
_motorHat.Dispose();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix it with correct indent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — updated the Dispose example with the motors-first loop and correct indentation in dbdd54a.

}
```

## Support

- Up to 4 DC Motors
Expand Down
Loading