| 33 | */ |
| 34 | |
| 35 | PushButton::ButtonState PushButton::getState() { |
| 36 | uint32_t newTime; |
| 37 | uint8_t state; |
| 38 | |
| 39 | // read the pin and flip it if this switch reads high when open |
| 40 | |
| 41 | state=_pin.read(); |
| 42 | |
| 43 | if(_pressedState) |
| 44 | state^=true; |
| 45 | |
| 46 | // if state is low then wherever we were then |
| 47 | // we are now back at not pressed |
| 48 | |
| 49 | if(!state) { |
| 50 | _internalState=Idle; |
| 51 | return NotPressed; |
| 52 | } |
| 53 | |
| 54 | // sample the counter |
| 55 | |
| 56 | newTime=MillisecondTimer::millis(); |
| 57 | |
| 58 | // act on the internal state machine |
| 59 | |
| 60 | switch(_internalState) { |
| 61 | case Idle: |
| 62 | _internalState=DebounceDelay; |
| 63 | _lastTime=newTime; |
| 64 | break; |
| 65 | |
| 66 | case DebounceDelay: |
| 67 | if(newTime - _lastTime >= DEBOUNCE_DELAY_MILLIS) { |
| 68 | // been high for at least the debounce time |
| 69 | |
| 70 | return Pressed; |
| 71 | } |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | // nothing happened at this time |
| 76 | |
| 77 | return NotPressed; |
| 78 | } |
| 79 | } |