// ============================================================================ boolean RotaryEncoder::isKeyEvent() { return isBit(_rotary_status, B11000000); }
// ============================================================================ // is the key-mode enabled? boolean RotaryEncoder::isKeyMode() { return isBit(_rotary_status, BIT_KEYMODE); }
// ============================================================================ boolean RotaryEncoder::isFlipped() { return isBit(_rotary_status, BIT_FLIP); }
// ============================================================================ void RotaryEncoder::setStepWidth(float step) { if (isBit(_rotary_status, BIT_FLIP)) _rotary_step = -step; else _rotary_step = step; }
// ============================================================================ // Read input pins and process for events. Call this either from a // loop or an interrupt (eg pin change or timer). boolean RotaryEncoder::process() { // get the current pin state _inter_state = (digitalRead(_pinB) << 1) | digitalRead(_pinA); // any changes in the state? if (_inter_state != _inter_state_old) { // store the old value _inter_state_old = _inter_state; // shift the old states in by 2 // (uses the byte as buffer for the last 4 states) _state <<= 2; // add the new state to B0, B1 _state |= _inter_state; // if the last state is 00 one gray-code cycle is over if (_inter_state == 0) { float step = _rotary_step; if ((isBit(_rotary_status, BIT_DAMP)) && (_rotary_position > _damp_bottom) && (_rotary_position < _damp_top)) { step *= _damp_factor; } // use a bit mask to just the the last 3 states if ((_state & B00111111) == DIR_CW) { // add the step to the rotary position _rotary_position -= step; // check limits if ((_rotary_use_position_limits) && (_rotary_position < _rotary_position_min)) { _rotary_position = _rotary_position_min; } // how fast are we turning? uint32_t sinceLastStep = millis() - _lastStepTime; if (sinceLastStep < VELOCITY_CHANGE_FAST) { _velocity = 100; } else if (sinceLastStep > VELOCITY_CHANGE_SLOW) { _velocity = 1; } else { _velocity = 10; } // remember the time whn we did this _lastStepTime += sinceLastStep; // key mode enabled? if (isBit(_rotary_status, BIT_KEYMODE)) { // new full (integer) step done? if (((int16_t)_rotary_position) != _rotary_position_old) { if (isBit(_rotary_status, BIT_FLIP)) { setBit(_rotary_status, BIT_UP); } else { setBit(_rotary_status, BIT_DOWN); } // store the new value _rotary_position_old = _rotary_position; } } return true; } // use a bit mask to just the the last 3 states if ((_state & B00111111) == DIR_CCW) { // add the step to the rotary position _rotary_position += step; // check limits if ((_rotary_use_position_limits) && (_rotary_position > _rotary_position_max)) { _rotary_position = _rotary_position_max; } // how fast are we turning? uint32_t sinceLastStep = millis() - _lastStepTime; if (sinceLastStep < VELOCITY_CHANGE_FAST) { _velocity = 100; } else if (sinceLastStep > VELOCITY_CHANGE_SLOW) { _velocity = 1; } else { _velocity = 10; } // remember the time whn we did this _lastStepTime += sinceLastStep; // key mode enabled? if (isBit(_rotary_status, BIT_KEYMODE)) { // new full (integer) step done? if (((int16_t)_rotary_position) != _rotary_position_old) { if (isBit(_rotary_status, BIT_FLIP)) { setBit(_rotary_status, BIT_DOWN); } else { setBit(_rotary_status, BIT_UP); } // store the new value _rotary_position_old = _rotary_position; } } return true; } } } return false; }
/* Diese Funktion dient zum Überwachen der seriellen Schnittstelle ** ** liegen Daten an, so ist der Rückgabewert 1, liegen keine an, so ist er 0 */ char isSer(void) { return isBit(UCSR0A, RXC0); }