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