|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Device.Gpio; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +namespace Iot.Device.KeyMatrix |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// GPIO key matrix Driver |
| 13 | + /// </summary> |
| 14 | + public class KeyMatrix : IDisposable |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Get output pins |
| 18 | + /// </summary> |
| 19 | + public int[] OutputPins => _outputPins; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Get input pins |
| 23 | + /// </summary> |
| 24 | + public int[] InputPins => _inputPins; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Get all buttons' values |
| 28 | + /// </summary> |
| 29 | + public SpanPinValue Values => new SpanPinValue(_buttonValues); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Get or set interval in milliseconds |
| 33 | + /// </summary> |
| 34 | + public TimeSpan ScanInterval { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Get buttons' values by output |
| 38 | + /// </summary> |
| 39 | + /// <param name="output">Output index</param> |
| 40 | + public SpanPinValue this[int output] => new SpanPinValue(_buttonValues, output * _outputPins.Length, _inputPins.Length); |
| 41 | + |
| 42 | + private int[] _outputPins; |
| 43 | + private int[] _inputPins; |
| 44 | + private GpioController? _gpioController; |
| 45 | + private PinValue[] _buttonValues; |
| 46 | + private bool _pinsOpened; |
| 47 | + private int _currentOutput = 0; |
| 48 | + private bool _shouldDispose; |
| 49 | + private bool _isRunning = false; |
| 50 | + private KeyMatrixEvent? _lastKeyEvent; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Fire an event when a key is pressed or released |
| 54 | + /// </summary> |
| 55 | + /// <param name="sender">The sender KeyMatrix</param> |
| 56 | + /// <param name="keyMatrixEvent">The key event</param> |
| 57 | + public delegate void KeyEventHandler(object sender, KeyMatrixEvent keyMatrixEvent); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// The raised event |
| 61 | + /// </summary> |
| 62 | + public event KeyEventHandler? KeyEvent; |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Initialize key matrix |
| 66 | + /// </summary> |
| 67 | + /// <param name="outputPins">Output pins</param> |
| 68 | + /// <param name="inputPins">Input pins</param> |
| 69 | + /// <param name="scanInterval">Scanning interval in milliseconds</param> |
| 70 | + /// <param name="gpioController">GPIO controller</param> |
| 71 | + /// <param name="shouldDispose">True to dispose the GpioController</param> |
| 72 | + public KeyMatrix(int[] outputPins, int[] inputPins, TimeSpan scanInterval, GpioController? gpioController = null, bool shouldDispose = true) |
| 73 | + { |
| 74 | + _shouldDispose = shouldDispose || gpioController == null; |
| 75 | + _gpioController = gpioController ?? new(); |
| 76 | + |
| 77 | + if (outputPins == null) |
| 78 | + { |
| 79 | + throw new ArgumentNullException(nameof(outputPins)); |
| 80 | + } |
| 81 | + |
| 82 | + if (outputPins is null or { Length:0 }) |
| 83 | + { |
| 84 | + throw new ArgumentOutOfRangeException(nameof(outputPins), "The number of outputs must be at least 1"); |
| 85 | + } |
| 86 | + |
| 87 | + if (inputPins == null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException(nameof(inputPins)); |
| 90 | + } |
| 91 | + |
| 92 | + if (inputPins is null or { Length: 0 }) |
| 93 | + { |
| 94 | + throw new ArgumentOutOfRangeException(nameof(inputPins), "The number of inputs must be at least 1"); |
| 95 | + } |
| 96 | + |
| 97 | + _outputPins = outputPins; |
| 98 | + _inputPins = inputPins; |
| 99 | + _buttonValues = new PinValue[_outputPins.Length * _inputPins.Length]; |
| 100 | + for (int i = 0; i < _buttonValues.Length; i++) |
| 101 | + { |
| 102 | + _buttonValues[i] = PinValue.Low; |
| 103 | + } |
| 104 | + |
| 105 | + _pinsOpened = false; |
| 106 | + ScanInterval = scanInterval; |
| 107 | + OpenPins(); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Start listening to key events |
| 112 | + /// </summary> |
| 113 | + public void StartListeningKeyEvent() |
| 114 | + { |
| 115 | + if (_isRunning) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + _isRunning = true; |
| 121 | + new Thread(() => |
| 122 | + { |
| 123 | + LoopReadKey(); |
| 124 | + }).Start(); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Stop listening to key events |
| 129 | + /// </summary> |
| 130 | + public void StopListeningKeyEvent() |
| 131 | + { |
| 132 | + _isRunning = false; |
| 133 | + } |
| 134 | + |
| 135 | + private void LoopReadKey() |
| 136 | + { |
| 137 | + do |
| 138 | + { |
| 139 | + Thread.Sleep(ScanInterval); |
| 140 | + |
| 141 | + _currentOutput = (_currentOutput + 1) % _outputPins.Length; |
| 142 | + _gpioController!.Write(_outputPins[_currentOutput], PinValue.High); |
| 143 | + |
| 144 | + for (var i = 0; i < _inputPins.Length; i++) |
| 145 | + { |
| 146 | + int index = _currentOutput * _inputPins.Length + i; |
| 147 | + |
| 148 | + PinValue oldValue = _buttonValues[index]; |
| 149 | + PinValue newValue = _gpioController.Read(_inputPins[i]); |
| 150 | + _buttonValues[index] = newValue; |
| 151 | + |
| 152 | + if (newValue != oldValue) |
| 153 | + { |
| 154 | + KeyEvent?.Invoke(this, new KeyMatrixEvent(newValue == PinValue.High ? PinEventTypes.Rising : PinEventTypes.Falling, _currentOutput, i)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + _gpioController.Write(_outputPins[_currentOutput], PinValue.Low); |
| 159 | + } |
| 160 | + while (_pinsOpened && _isRunning); |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Blocks execution until a key event is received |
| 165 | + /// </summary> |
| 166 | + public KeyMatrixEvent? ReadKey() |
| 167 | + { |
| 168 | + _currentOutput--; |
| 169 | + KeyEvent += KeyMatrixKeyEvent; |
| 170 | + _isRunning = true; |
| 171 | + LoopReadKey(); |
| 172 | + KeyEvent -= KeyMatrixKeyEvent; |
| 173 | + return _lastKeyEvent; |
| 174 | + } |
| 175 | + |
| 176 | + private void KeyMatrixKeyEvent(object sender, KeyMatrixEvent keyMatrixEvent) |
| 177 | + { |
| 178 | + _isRunning = false; |
| 179 | + _lastKeyEvent = keyMatrixEvent; |
| 180 | + } |
| 181 | + |
| 182 | + /// <inheritdoc/> |
| 183 | + public void Dispose() |
| 184 | + { |
| 185 | + ClosePins(); |
| 186 | + |
| 187 | + if (_shouldDispose) |
| 188 | + { |
| 189 | + _gpioController?.Dispose(); |
| 190 | + _gpioController = null; |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + if (_gpioController is object && _pinsOpened) |
| 195 | + { |
| 196 | + ClosePins(); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private void OpenPins() |
| 202 | + { |
| 203 | + for (int i = 0; i < _outputPins.Length; i++) |
| 204 | + { |
| 205 | + _gpioController!.OpenPin(_outputPins[i], PinMode.Output); |
| 206 | + } |
| 207 | + |
| 208 | + for (int i = 0; i < _inputPins.Length; i++) |
| 209 | + { |
| 210 | + _gpioController!.OpenPin(_inputPins[i], PinMode.Input); |
| 211 | + } |
| 212 | + |
| 213 | + _pinsOpened = true; |
| 214 | + } |
| 215 | + |
| 216 | + private void ClosePins() |
| 217 | + { |
| 218 | + _isRunning = false; |
| 219 | + _pinsOpened = false; |
| 220 | + Thread.Sleep(ScanInterval); // wait for current scan to complete |
| 221 | + |
| 222 | + for (int i = 0; i < _outputPins.Length; i++) |
| 223 | + { |
| 224 | + _gpioController!.ClosePin(_outputPins[i]); |
| 225 | + } |
| 226 | + |
| 227 | + for (int i = 0; i < _inputPins.Length; i++) |
| 228 | + { |
| 229 | + _gpioController!.ClosePin(_inputPins[i]); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments