未验证 提交 a1a16e94 编写于 作者: E efraimnewman 提交者: GitHub

Added support for internal pull-down resistors (#1847)

* Added support for internal pull-down resistors

* Added ability to set PinMode in constructer

* Fix Breaking chage to constructor

* Fix error in calls

* Fix typos
Co-authored-by: NKrzysztof Wicher <mordotymoja@gmail.com>
上级 a42a670d
......@@ -44,6 +44,7 @@ namespace Iot.Device.KeyMatrix
private int[] _outputPins;
private int[] _inputPins;
private GpioController? _gpioController;
private PinMode _inputPinMode;
private PinValue[] _buttonValues;
private bool _pinsOpened;
private int _currentOutput = 0;
......@@ -72,15 +73,35 @@ namespace Iot.Device.KeyMatrix
/// <param name="gpioController">GPIO controller</param>
/// <param name="shouldDispose">True to dispose the GpioController</param>
public KeyMatrix(IEnumerable<int> outputPins, IEnumerable<int> inputPins, TimeSpan scanInterval, GpioController? gpioController = null, bool shouldDispose = true)
: this(outputPins, inputPins, scanInterval, PinMode.Input, gpioController, shouldDispose)
{
}
/// <summary>
/// Initialize key matrix
/// </summary>
/// <param name="outputPins">Output pins</param>
/// <param name="inputPins">Input pins</param>
/// <param name="scanInterval">Scanning interval in milliseconds</param>
/// <param name="gpioController">GPIO controller</param>
/// <param name="inputPinMode">Mode for input pins - Input / InputPullDown</param>
/// <param name="shouldDispose">True to dispose the GpioController</param>
public KeyMatrix(IEnumerable<int> outputPins, IEnumerable<int> inputPins, TimeSpan scanInterval, PinMode inputPinMode, GpioController? gpioController = null, bool shouldDispose = true)
{
_shouldDispose = shouldDispose || gpioController == null;
_gpioController = gpioController ?? new();
_inputPinMode = inputPinMode;
if (outputPins == null)
{
throw new ArgumentNullException(nameof(outputPins));
}
if (inputPinMode != PinMode.Input && inputPinMode != PinMode.InputPullDown)
{
throw new ArgumentException("Input pins can only be set to Input or InputPullDown.");
}
if (!outputPins.Any())
{
throw new ArgumentOutOfRangeException(nameof(outputPins), "The number of outputs must be at least 1");
......@@ -96,6 +117,12 @@ namespace Iot.Device.KeyMatrix
throw new ArgumentOutOfRangeException(nameof(inputPins), "The number of inputs must be at least 1");
}
if (inputPinMode == PinMode.InputPullDown &&
!inputPins.All(p => _gpioController.IsPinModeSupported(p, _inputPinMode)))
{
throw new ArgumentException("Not all input pins support InputPullDown");
}
_outputPins = outputPins.ToArray();
_inputPins = inputPins.ToArray();
_buttonValues = new PinValue[_outputPins.Length * _inputPins.Length];
......@@ -209,7 +236,7 @@ namespace Iot.Device.KeyMatrix
for (int i = 0; i < _inputPins.Length; i++)
{
_gpioController!.OpenPin(_inputPins[i], PinMode.Input);
_gpioController!.OpenPin(_inputPins[i], _inputPinMode);
}
_pinsOpened = true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册