Treehopper C# API
RotaryEncoder Class Reference

Quadrature rotary encoder More...

Inheritance diagram for RotaryEncoder:


class  PositionChangedEventArgs
 Rotary encoder position changed event arguments More...

Public Member Functions

delegate void PositionChangedDelegate (object sender, PositionChangedEventArgs e)
 Rotary encoder position changed delegate More...
 RotaryEncoder (DigitalIn a, DigitalIn b, int stepsPerTick=1)
 Construct a new RotaryEncoder from two digital inputs More...
void Dispose ()


long Position [get, set]
 Gets or sets the Position counter. More...
long MaxValue = long.MaxValue [get, set]
 The maximum value this encoder should report More...
long MinValue = long.MinValue [get, set]
 The minimum value this encoder should report More...


PositionChangedDelegate PositionChanged
 Fires whenever the position has changed More...
PropertyChangedEventHandler PropertyChanged
 Fires whenever the position has changed More...

Detailed Description

Quadrature rotary encoder

This class is designed to interface with standard, two-channel quadrature-style incremental rotary encoders. These are often used as infinite-turn knobs for human interfaces, motor feedback, measuring wheels, and other rotary/linear motion measurement applications.

If you are using an encoder with an detent, you can set the stepsPerTick parameter to match your encoder so that Position will increment/decrement for each "click" (for many encoders, a value of 4 is correct). For maximum resolution, leave at 1.

This class makes use of DigitalIn's DigitalIn.DigitalValueChanged event. If you're attaching the encoder to an I2c or SPI I/O expander or shift register, make sure your driver is sampling these pins often.

Because the inputs are sampled over USB by the host API, only low to moderate-speed applications are supported; while Treehopper has been tested at input speeds in the 5-10 kHz range, the nondeterministic nature of Treehopper means it is unlikely that this class will work reliably above 500-1000 Hz, and will occasionally miss clicks.

While earlier versions of this library used a 32-signed Position value, this would overflow after less than 25 days with a 1 kHz input signal, which could be problematic for some use cases. As such, this is now stored as a long (64-bit) signed value. With a 1 kHz signal, this will overflow after approximately 292 million years, which should be sufficient for most users.

Constructor & Destructor Documentation

◆ RotaryEncoder()

RotaryEncoder ( DigitalIn  a,
DigitalIn  b,
int  stepsPerTick = 1 

Construct a new RotaryEncoder from two digital inputs

aA channel input
bB channel input
stepsPerTickThe number of steps to count as one "tick"

Member Function Documentation

◆ PositionChangedDelegate()

delegate void PositionChangedDelegate ( object  sender,
PositionChangedEventArgs  e 

Rotary encoder position changed delegate

senderThe RotaryEncoder where this message originated from
eThe EventArgs this RotaryEncoder generated

◆ Dispose()

void Dispose ( )

Property Documentation

◆ Position

long Position

Gets or sets the Position counter.

◆ MaxValue

long MaxValue = long.MaxValue

The maximum value this encoder should report

◆ MinValue

long MinValue = long.MinValue

The minimum value this encoder should report

Event Documentation

◆ PositionChanged

PositionChangedDelegate PositionChanged

Fires whenever the position has changed

◆ PropertyChanged

PropertyChangedEventHandler PropertyChanged

Fires whenever the position has changed

The documentation for this class was generated from the following file: