/// <summary> /// Switch event has occurred /// </summary> /// <param name="pressed">true iff switch is pressed (on)</param> public void SwitchEvent(bool pressed) { #if DebugViaPrint //Debug.Print("Switch. _debounceState: " + DebounceStateDef + ", pressed: " + pressed); #endif switch (_debounceState) { case DebounceStates.WaitPress: if (!pressed) { return; } _debounceState = DebounceStates.DebouncePress; _debounceTimer.Start(); #if DebugViaPrint Debug.Print("Switch. Switch to " + DebounceStateDef); #endif break; case DebounceStates.DebouncePress: break; case DebounceStates.WaitRelease: if (pressed) { return; } var now = DateTime.Now; var pressDuration = now - _pressedTime; var shortPress = pressDuration < _longPressWaitSpan; #if DebugViaPrint Debug.Print("Switch. now: " + now + "." + now.Millisecond + ", _pressedTime: " + _pressedTime + "." + _pressedTime.Millisecond + ", \npressDuration: " + pressDuration + ", shortPress: " + shortPress); #endif if (ClickResult != null) { ClickResult(now, shortPress); } _debounceState = DebounceStates.DebounceRelease; _debounceTimer.Start(); #if DebugViaPrint Debug.Print("Switch. Switch to " + DebounceStateDef); #endif break; case DebounceStates.DebounceRelease: break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Debounce timer /// </summary> /// <param name="obj"></param> private void DebounceTimer_Tick(object obj) { #if DebugViaPrint //Debug.Print("Timer. _debounceState: " + DebounceStateDef); #endif switch (_debounceState) { case DebounceStates.WaitPress: // Ignore break; case DebounceStates.DebouncePress: var portValue = _port.Read() ? 1 : 0; if (portValue != _onVal) { _debounceState = DebounceStates.WaitPress; #if DebugViaPrint Debug.Print("Timer. Brief click. Switch to " + DebounceStateDef); #endif break; } _debounceState = DebounceStates.WaitRelease; _pressedTime = DateTime.Now; #if DebugViaPrint Debug.Print("Timer. Switch to " + DebounceStateDef); #endif break; case DebounceStates.WaitRelease: // Ignore break; case DebounceStates.DebounceRelease: _debounceState = DebounceStates.WaitPress; #if DebugViaPrint Debug.Print("Timer. Switch to " + DebounceStateDef); #endif break; default: throw new ArgumentOutOfRangeException(); } }