Line data Source code
1 : #include "IncrementDecrementButtons.hpp" 2 : 3 : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings 4 : 5 : BEGIN_AH_NAMESPACE 6 : 7 : IncrementDecrementButtons::State 8 38 : IncrementDecrementButtons::updateImplementation() { 9 38 : Button::State incrState = incrementButton.update(); 10 38 : Button::State decrState = decrementButton.update(); 11 : 12 38 : if (decrState == Button::Released && incrState == Button::Released) { 13 : // Both released 14 40 : } else if ((decrState == Button::Rising && incrState == Button::Released) || 15 29 : (incrState == Button::Rising && decrState == Button::Released) || 16 26 : (incrState == Button::Rising && decrState == Button::Rising)) { 17 : // One released, the other rising → nothing 18 : // now both released, so go to initial state 19 8 : longPressState = Initial; 20 33 : } else if (incrState == Button::Falling && decrState == Button::Falling) { 21 : // Both falling → reset 22 : // (rather unlikely, but just in case) 23 2 : longPressState = AfterReset; 24 2 : return Reset; 25 23 : } else if (incrState == Button::Falling) { 26 7 : if (decrState == Button::Pressed) { 27 : // One pressed, the other falling → reset 28 1 : longPressState = AfterReset; 29 1 : return Reset; 30 : } else { 31 : // Increment falling, the other released → increment 32 6 : return IncrementShort; 33 : } 34 16 : } else if (decrState == Button::Falling) { 35 7 : if (incrState == Button::Pressed) { 36 : // One pressed, the other falling → reset 37 1 : longPressState = AfterReset; 38 1 : return Reset; 39 : } else { 40 : // Decrement falling, the other released → decrement 41 6 : return DecrementShort; 42 : } 43 9 : } else if (incrState == Button::Pressed && decrState == Button::Pressed) { 44 : // Both pressed → nothing 45 9 : } else if (longPressState != AfterReset && incrState == Button::Pressed) { 46 : // Not reset and increment pressed → long press? 47 4 : auto now = millis(); 48 4 : if (longPressState == LongPress) { 49 2 : if (now - longPressRepeat >= LONG_PRESS_REPEAT_DELAY) { 50 1 : longPressRepeat += LONG_PRESS_REPEAT_DELAY; 51 1 : return IncrementHold; 52 : } 53 3 : } else if (incrementButton.stableTime() >= LONG_PRESS_DELAY) { 54 1 : longPressState = LongPress; 55 1 : longPressRepeat = now; 56 1 : return IncrementLong; 57 : } 58 9 : } else if (longPressState != AfterReset && decrState == Button::Pressed) { 59 : // Not reset and decrement pressed → long press? 60 4 : auto now = millis(); 61 4 : if (longPressState == LongPress) { 62 2 : if (now - longPressRepeat >= LONG_PRESS_REPEAT_DELAY) { 63 1 : longPressRepeat += LONG_PRESS_REPEAT_DELAY; 64 1 : return DecrementHold; 65 : } 66 3 : } else if (decrementButton.stableTime() >= LONG_PRESS_DELAY) { 67 1 : longPressState = LongPress; 68 1 : longPressRepeat = now; 69 1 : return DecrementLong; 70 : } 71 4 : } 72 18 : return Nothing; 73 38 : } 74 : 75 : END_AH_NAMESPACE 76 : 77 : AH_DIAGNOSTIC_POP()