-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing missing GpioController and ShouldDispose #1016
Changes from all commits
2f4d54b
bcf9769
a4972d2
a8d9352
e80bd1a
871318c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ namespace Iot.Device.DCMotor | |
public abstract class DCMotor : IDisposable | ||
{ | ||
private const int DefaultPwmFrequency = 50; | ||
private bool _shouldDispose; | ||
|
||
/// <summary> | ||
/// Constructs generic <see cref="DCMotor"/> instance | ||
/// </summary> | ||
/// <param name="controller"><see cref="GpioController"/> related with operations on pins</param> | ||
protected DCMotor(GpioController controller) | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
protected DCMotor(GpioController controller, bool shouldDispose) | ||
{ | ||
_shouldDispose = shouldDispose; | ||
Controller = controller; | ||
} | ||
|
||
|
@@ -57,8 +60,11 @@ protected virtual void Dispose(bool disposing) | |
{ | ||
if (disposing) | ||
{ | ||
Controller?.Dispose(); | ||
Controller = null; | ||
if (_shouldDispose) | ||
{ | ||
Controller?.Dispose(); | ||
Controller = null; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -79,21 +85,22 @@ public static DCMotor Create(PwmChannel speedControlChannel) | |
throw new ArgumentNullException(nameof(speedControlChannel)); | ||
} | ||
|
||
return new DCMotor2PinNoEnable(speedControlChannel, -1, null); | ||
return new DCMotor2PinNoEnable(speedControlChannel, -1, null, true); | ||
} | ||
|
||
/// <summary> | ||
/// Creates <see cref="DCMotor"/> instance which allows to control speed in one direction. | ||
/// </summary> | ||
/// <param name="speedControlPin">Pin used to control the speed of the motor with software PWM (frequency will default to 50Hz)</param> | ||
/// <param name="controller"><see cref="GpioController"/> related to the <paramref name="speedControlPin"/></param> | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
/// <returns><see cref="DCMotor"/> instance</returns> | ||
/// <remarks> | ||
/// <paramref name="speedControlPin"/> can be connected to either enable pin of the H-bridge. | ||
/// or directly to the input related with the motor (if H-bridge allows inputs to change frequently). | ||
/// Connecting motor directly to GPIO pin is not recommended and may damage your board. | ||
/// </remarks> | ||
public static DCMotor Create(int speedControlPin, GpioController controller = null) | ||
public static DCMotor Create(int speedControlPin, GpioController controller = null, bool shouldDispose = true) | ||
{ | ||
if (speedControlPin == -1) | ||
{ | ||
|
@@ -104,7 +111,8 @@ public static DCMotor Create(int speedControlPin, GpioController controller = nu | |
return new DCMotor2PinNoEnable( | ||
new SoftwarePwmChannel(speedControlPin, DefaultPwmFrequency, 0.0, controller: controller), | ||
-1, | ||
controller); | ||
controller, | ||
shouldDispose); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -113,14 +121,15 @@ public static DCMotor Create(int speedControlPin, GpioController controller = nu | |
/// <param name="speedControlChannel"><see cref="PwmChannel"/> used to control the speed of the motor</param> | ||
/// <param name="directionPin">Pin used to control the direction of the motor</param> | ||
/// <param name="controller"><see cref="GpioController"/> related to the <paramref name="directionPin"/></param> | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
/// <returns><see cref="DCMotor"/> instance</returns> | ||
/// <remarks> | ||
/// <paramref name="speedControlChannel"/> can be connected to either enable pin of the H-bridge. | ||
/// or directly to the input related with the motor (if H-bridge allows inputs to change frequently). | ||
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs. | ||
/// Connecting motor directly to GPIO pin is not recommended and may damage your board. | ||
/// </remarks> | ||
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, GpioController controller = null) | ||
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, GpioController controller = null, bool shouldDispose = true) | ||
{ | ||
if (speedControlChannel == null) | ||
{ | ||
|
@@ -132,7 +141,7 @@ public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, G | |
throw new ArgumentOutOfRangeException(nameof(directionPin)); | ||
} | ||
|
||
return new DCMotor2PinNoEnable(speedControlChannel, directionPin, controller); | ||
return new DCMotor2PinNoEnable(speedControlChannel, directionPin, controller, shouldDispose); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -141,14 +150,15 @@ public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, G | |
/// <param name="speedControlPin">Pin used to control the speed of the motor with software PWM (frequency will default to 50Hz)</param> | ||
/// <param name="directionPin">Pin used to control the direction of the motor</param> | ||
/// <param name="controller">GPIO controller related to <paramref name="speedControlPin"/> and <paramref name="directionPin"/></param> | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
/// <returns><see cref="DCMotor"/> instance</returns> | ||
/// <remarks> | ||
/// PWM pin <paramref name="speedControlPin"/> can be connected to either enable pin of the H-bridge. | ||
/// or directly to the input related with the motor (if H-bridge allows inputs to change frequently). | ||
/// <paramref name="directionPin"/> should be connected to H-bridge input corresponding to one of the motor inputs. | ||
/// Connecting motor directly to GPIO pin is not recommended and may damage your board. | ||
/// </remarks> | ||
public static DCMotor Create(int speedControlPin, int directionPin, GpioController controller = null) | ||
public static DCMotor Create(int speedControlPin, int directionPin, GpioController controller = null, bool shouldDispose = true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as the CharacterLCd here, we should probably have a Create Overload that doesn't take a shouldDispose, and set it to false in case controller was passed in. Another way to do this is to default it to false instead, and only set it to true if the controller that was passed in was null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment then for the previous one, it's more about consistency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I'm fine with keeping it as is for consistency |
||
{ | ||
if (speedControlPin == -1) | ||
{ | ||
|
@@ -164,7 +174,8 @@ public static DCMotor Create(int speedControlPin, int directionPin, GpioControll | |
return new DCMotor2PinNoEnable( | ||
new SoftwarePwmChannel(speedControlPin, DefaultPwmFrequency, 0.0, controller: controller), | ||
directionPin, | ||
controller); | ||
controller, | ||
shouldDispose); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -174,6 +185,7 @@ public static DCMotor Create(int speedControlPin, int directionPin, GpioControll | |
/// <param name="directionPin">First pin used to control the direction of the motor</param> | ||
/// <param name="otherDirectionPin">Second pin used to control the direction of the motor</param> | ||
/// <param name="controller"><see cref="GpioController"/> related to <paramref name="directionPin"/> and <paramref name="otherDirectionPin"/></param> | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
/// <returns><see cref="DCMotor"/> instance</returns> | ||
/// <remarks> | ||
/// When speed is non-zero the value of <paramref name="otherDirectionPin"/> will always be opposite to that of <paramref name="directionPin"/>. | ||
|
@@ -182,7 +194,7 @@ public static DCMotor Create(int speedControlPin, int directionPin, GpioControll | |
/// <paramref name="otherDirectionPin"/> should be connected to H-bridge input corresponding to the remaining motor input. | ||
/// Connecting motor directly to GPIO pin is not recommended and may damage your board. | ||
/// </remarks> | ||
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, int otherDirectionPin, GpioController controller = null) | ||
public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, int otherDirectionPin, GpioController controller = null, bool shouldDispose = true) | ||
{ | ||
if (speedControlChannel == null) | ||
{ | ||
|
@@ -203,7 +215,8 @@ public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, i | |
speedControlChannel, | ||
directionPin, | ||
otherDirectionPin, | ||
controller); | ||
controller, | ||
shouldDispose); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -213,6 +226,7 @@ public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, i | |
/// <param name="directionPin">First pin used to control the direction of the motor</param> | ||
/// <param name="otherDirectionPin">Second pin used to control the direction of the motor</param> | ||
/// <param name="controller"><see cref="GpioController"/> related to <paramref name="speedControlPin"/>, <paramref name="directionPin"/> and <paramref name="otherDirectionPin"/></param> | ||
/// <param name="shouldDispose">True to dispose the Gpio Controller</param> | ||
/// <returns><see cref="DCMotor"/> instance</returns> | ||
/// <remarks> | ||
/// When speed is non-zero the value of <paramref name="otherDirectionPin"/> will always be opposite to that of <paramref name="directionPin"/> | ||
|
@@ -221,7 +235,7 @@ public static DCMotor Create(PwmChannel speedControlChannel, int directionPin, i | |
/// <paramref name="otherDirectionPin"/> should be connected to H-bridge input corresponding to the remaining motor input. | ||
/// Connecting motor directly to GPIO pin is not recommended and may damage your board. | ||
/// </remarks> | ||
public static DCMotor Create(int speedControlPin, int directionPin, int otherDirectionPin, GpioController controller = null) | ||
public static DCMotor Create(int speedControlPin, int directionPin, int otherDirectionPin, GpioController controller = null, bool shouldDispose = true) | ||
{ | ||
if (speedControlPin == -1) | ||
{ | ||
|
@@ -243,7 +257,8 @@ public static DCMotor Create(int speedControlPin, int directionPin, int otherDir | |
new SoftwarePwmChannel(speedControlPin, DefaultPwmFrequency, 0.0, controller: controller), | ||
directionPin, | ||
otherDirectionPin, | ||
controller); | ||
controller, | ||
shouldDispose); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait with your latest changes you are not really disposing anything right? It looks like you are only taking in the extra parameter but not really assigning to anything, and also not checking it before disposing.