Skip to content

Clarify MotorHat disposal/ownership semantics in README - #2584

Merged
pgrawehr merged 6 commits into
mainfrom
copilot/fix-dcmotor-disposal-documentation
Jul 30, 2026
Merged

Clarify MotorHat disposal/ownership semantics in README#2584
pgrawehr merged 6 commits into
mainfrom
copilot/fix-dcmotor-disposal-documentation

Conversation

Copilot AI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

The MotorHat README showed a DCMotor being created but never disposed, leaving it unclear whether motors need separate disposal, whether dispose order matters, and whether disposing both the MotorHat and its motors causes conflicts.

Confirmed from the source that there is no conflict: MotorHat.Dispose() stops all channels it created and disposes the underlying PCA9685, while DCMotor3Pwm.Dispose() only stops its PWM pins (never disposes them). Disposal is therefore idempotent and order-independent.

Changes

  • Added a "Resource management (disposing)" section to src/devices/MotorHat/README.md covering the ownership model:
    • Disposing the MotorHat alone releases every motor/servo/PWM channel it handed out.
    • Disposing motors as well is safe (stop-only); dispose order is irrelevant.
  • Included a corrected PumpController-style example forwarding disposal to the MotorHat.
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();
}

@dotnet-policy-service dotnet-policy-service Bot added the area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio label Jul 1, 2026
Copilot AI changed the title [WIP] Fix unclear documentation around disposing of DCMotor with MotorHat Clarify MotorHat disposal/ownership semantics in README Jul 1, 2026
Copilot AI requested a review from raffaeler July 1, 2026 14:24
@raffaeler
raffaeler marked this pull request as ready for review July 1, 2026 15:05
Copilot AI review requested due to automatic review settings July 1, 2026 15:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the MotorHat device documentation to clarify resource ownership and disposal behavior when creating motors/servos/PWM channels from MotorHat, aiming to prevent confusion about whether returned objects must be disposed and in what order.

Changes:

  • Adds a new “Resource management (disposing)” section describing MotorHat ownership and cleanup responsibilities.
  • Provides updated usage guidance and a wrapper-class example that forwards disposal to MotorHat.

Comment thread src/devices/MotorHat/README.md Outdated
Comment thread src/devices/MotorHat/README.md Outdated
krwq and others added 2 commits July 30, 2026 17:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment on lines +85 to +88
{
// Disposing the MotorHat stops and releases every motor/channel it created.
// If you dispose motors explicitly, dispose them before disposing the MotorHat.
}

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.

Copilot finished work on behalf of krwq July 30, 2026 15:44
Copilot AI requested a review from krwq July 30, 2026 15:44
@pgrawehr
pgrawehr merged commit 6289c1a into main Jul 30, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unclear documentation around disposing of DCMotor when used with MotorHat

5 participants