private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args) { lock (_lock) { if (!_evaluatingRows) { IGpioPinMapping mapping = this.PinMappings.Where(t => t.PinNumber == sender.PinNumber).SingleOrDefault(); if (mapping != null) { PinChangedStatus status = mapping.GetChangedStatus(args.Edge); if (status != PinChangedStatus.None) { this.OnPinValueChanged(mapping, status); } } } } }
public PinChangedStatus GetChangedStatus(GpioPinEdge edge) { PinChangedStatus returnValue = PinChangedStatus.None; GpioPinValue currentValue = this.GpioPin.Read(); try { if (edge == GpioPinEdge.RisingEdge && this.PreviousValue == GpioPinValue.Low && currentValue != this.PreviousValue) { returnValue = PinChangedStatus.WentHigh; } else if (edge == GpioPinEdge.FallingEdge && this.PreviousValue == GpioPinValue.High && currentValue != this.PreviousValue) { returnValue = PinChangedStatus.WentLow; } } finally { this.PreviousValue = currentValue; } return(returnValue); }
protected virtual void OnPinValueChanged(IGpioPinMapping mapping, PinChangedStatus status) { try { // *** // *** Need to ignore other keypad changes until this is complete // *** _evaluatingRows = true; if (status == PinChangedStatus.WentHigh) { // *** // *** Once the column and row is known we can determine the key // *** ICharacterGridMapping characterGridMapping = null; // *** // *** Need to change each row to LOW until the pin we have changes again. This helps determine which // *** row is selected. // *** foreach (var rowMapping in this.RowMappings) { try { rowMapping.GpioPin.Write(GpioPinValue.Low); GpioPinValue newValue = mapping.GpioPin.Read(); if (newValue == GpioPinValue.Low) { // *** // *** Select the character mapping // *** characterGridMapping = this.CharacterGridMappings.Where(t => t.Row == rowMapping.MatrixValue && t.Column == mapping.MatrixValue).SingleOrDefault(); if (characterGridMapping != null) { if (this.EnableButtonUpEvent) { _lastCharacterDown = characterGridMapping; } break; } } } finally { // *** // *** Need the pin to be on high in it's default state // *** rowMapping.GpioPin.Write(GpioPinValue.High); } } if (characterGridMapping != null) { this.OnKeyEvent(new KeyPadEventArgs(characterGridMapping.Value, KeyDownEventType.ButtonDown)); } } else { if (_lastCharacterDown != null) { char key = _lastCharacterDown.Value; _lastCharacterDown = null; this.OnKeyEvent(new KeyPadEventArgs(key, KeyDownEventType.ButtonUp)); } } } finally { _evaluatingRows = false; } }