CTRL library / Rotary encoders

Rotary encoder methods

Configuration

  • setPinMode(INPUT, PULL_UP): The first parameter can be set to INPUT, INPUT_PULLUP or INPUT_PULLDOWN. If this method is not used, the default mode is INPUT_PULLUP and (if available) the internal pull-up resistor of the pin will be used. If the first parameter is set to INPUT, you will have to implement an external pull-down or pull-up resistor. You should then set the second parameter to PULL_DOWN or PULL_UP.
  • setMultiplexer(&mux): Sets the multiplexer that the rotary encoder subscribes to.
  • setGroup(&group): Register the rotary encoder to a group.

Core

  • process(): Polls the rotary encoder and handles all its functionality (used in the loop method).
  • storePinStates(clkState, dtState): Store pin states from an ISR or interrupt handler. Call this from a hardware interrupt (e.g. attachInterrupt on the CLK pin) to feed the encoder a pair of pin readings without blocking the interrupt context. The next call to process() will consume the stored states and apply state-table decoding as normal.

Event handlers

  • setOnTurnLeft(handler): Sets the onTurnLeft handler. Called when you turn left.
  • setOnTurnRight(handler): Sets the onTurnRight handler. Called when you turn right.

State

  • isTurningLeft(): Returns true if the encoder is currently turning left, false otherwise.
  • isTurningRight(): Returns true if the encoder is currently turning right, false otherwise.
  • isEnabled(): Checks if the rotary encoder is enabled.
  • isDisabled(): Checks if the rotary encoder is disabled.
  • isMuxed(): Checks if the rotary encoder is currently attached to a multiplexer.
  • isGrouped(): Checks if the rotary encoder is currently added to a group.
  • enable(): Enables the rotary encoder.
  • disable(): Disables the rotary encoder.

Custom properties

  • setInteger("id", 0): Define an integer. First parameter is the name, second is the value.
  • setString("name", "Rotary Encoder"): Define a string. First parameter is the name, second is the value.
  • setBoolean("active", true): Define a boolean. First parameter is the name, second is the value.
  • getInteger("id"): Get the value of an integer.
  • getString("name"): Get the value of a string.
  • getBoolean("active"): Get the value of a boolean.
  • setProperties(storage): Optional: use your own CtrlProperties storage instead of allocating it on first use.

An object only reserves memory for its properties the first time you set one (max 8 properties per object, names up to 15 characters, string values up to 20 characters). If your project must avoid the heap, declare the storage yourself: CtrlProperties properties; encoder.setProperties(properties);