Esempio n. 1
0
void TTBOUNCE::update(){
  uint8_t pinState = digitalRead(_pin);
  //debounce
  if (pinState != _currentPinUnstableState){
    _timestamp = millis();
  }
  else if(millis() - _timestamp >= _debounceInterval){
    if (pinState != _currentPinState){
      _currentPinState = pinState;
      if(read()){
        _previousHighStateTime = millis();
      }
    }
  }
  _currentPinUnstableState = pinState;
  
  //states
  if (_state == 0){
    if (read() == HIGH){
      _state = 1;
      _timestamp = millis(); // remember starting time
    }
  } 
  else if (_state == 1){
    if (read() == LOW){
      _state = 2;
    } 
    else if ((read() == HIGH) && (millis() > _timestamp + _pressInterval)){
      if (_pressFunction) _pressFunction();
      _state = 4;
    }  
  } 
  else if (_state == 2){
    if (millis() > _timestamp + _clickInterval || (read() == LOW && !_doubleClickFunction)){
      if (_clickFunction) _clickFunction();
      _state = 0;
    } 
    else if (read() == HIGH){
      _state = 3;
    }
  } 
  else if (_state == 3){
    if (read() == LOW){
      if (_doubleClickFunction) _doubleClickFunction();
      _state = 0;
    }
  } 
  else if (_state == 4){
    if (read() == LOW){
      _state = 0;
    }
  }
}
Esempio n. 2
0
void ButtonOne::buttonEvaluate(boolean buttonLevel) {
  switch ( _state_idx ) {
    case 0: // read btn - waiting for press
        if (buttonLevel == _active_state) {
          _btnEngagedState = true;
          _btn_millis = millis(); // ready for debounce and Click and LongPress detection
          if ( _pressFunction) _pressFunction();
          _state_idx++;
        }  // if
        break;
    case 1: // debounce btn
        if (millis() - _btn_millis > _pressDebounceTime)  { 
          _state_idx++; 
        } // if
        break;
    case 2: // read btn - waiting for release
		if( _toggleAsPush && ( millis() - _btn_millis  > _toggleRestTime )  ) {
          _active_state = !_active_state; // toggel to new state as the active state
		 }
        // check to see if the button has been released
        if (buttonLevel == !_active_state) { // the button has been released
          _btnEngagedState = false; 
          _btn_millis = millis();  // ready for debounce
          _state_idx++;   // ready for debounce
          if (_releaseFunction) _releaseFunction(); {
          }// if
        }
        break;
    case 3: // debounce btn
        if (millis() - _btn_millis > _releaseDebounceTime)   {
          _state_idx = 0;  // ready to register next push action
        } // if
        break;
    default: 
        _state_idx = 0; 
        break;
  } // switch
  return;
} // check
Esempio n. 3
0
void TTBOUNCE::update() {
  uint8_t pinState = digitalRead(_pin);
  // debounce
  if (pinState != _currentPinUnstableState) {
    _timestamp = millis();
  } else if (millis() - _timestamp >= _debounceInterval) {
    if (pinState != _currentPinState) {
      _currentPinState = pinState;
      if (read()) {
        _previousHighStateTime = millis();
      }
    }
  }
  _currentPinUnstableState = pinState;

  // states
  switch (_state) {
  case 0:
    if (read() == HIGH) {
      _state     = 1;
      _timestamp = millis();    // remember starting time
    }
    break;
  case 1:
    if (read() == LOW) {
      _state = 2;
    } else if ((read() == HIGH) && (millis() > _timestamp + _pressInterval)) {
      if (_pressFunction)
        _pressFunction();
      _state = 4;
    }
    break;
  case 2:
    if (millis() > _timestamp + _clickInterval ||
        (read() == LOW && !_doubleClickFunction)) {
      if (_clickFunction)
        _clickFunction();
      _state = 0;
    } else if (read() == HIGH) {
      _state = 3;
    }
    break;
  case 3:
    if (read() == LOW) {
      if (_doubleClickFunction)
        _doubleClickFunction();
      _state = 0;
    }
    break;
  case 4:
    if (read() == LOW) {
	  if (_releaseFunction)
        _releaseFunction();
      _state = 0;
    } else {
      if (_reTickFunction) {
        if (millis() - _previousReTickTime > _reTickInterval) {
          _reTickFunction();
          _previousReTickTime = millis();
        }
      }
    }
    break;
  }
}