Skip to content
Merged
Changes from 3 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
49 changes: 49 additions & 0 deletions src/devices/MotorHat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,55 @@ 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.
- **Disposing a motor as well is safe.** Disposing a `DCMotor` created by `CreateDCMotor` only stops its PWM channels; it does not dispose them. Combined with the point above, disposing both the motor and the `MotorHat` will not throw or leave the board in a random state.
- **Order does not matter.** You can dispose the motors before or after the `MotorHat`; the result is the same.
Comment thread
Copilot marked this conversation as resolved.
Outdated

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()
{
// Disposing the MotorHat stops and releases every motor/channel it created.
// Order is not important and disposing the motors as well would be safe.
_motorHat.Dispose();
Comment thread
Copilot marked this conversation as resolved.
Outdated
}
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