Documentation: The Use of Templates and Overridden Functions Instead of Initialization Lists

Background

In this library, templates and overridden functions are used to specify pin configurations for the RotEncoder instead of using initialization lists. This design choice has been made to improve both flexibility and efficiency, and to ensure that pin configurations can be optimized at compile-time.

Benefits of Using Templates and Overridden Functions:

1. Compile-Time Evaluation and Optimization

2. Reduced Memory Usage

3. Increased Flexibility

4. Fewer Runtime Errors

Why Not Use Initialization Lists?

1. Runtime Overhead

2. Higher RAM Usage

3. Limits Compile-Time Optimization

Comparison Between the Two Methods

Method Compile-Time Optimization Memory Usage Flexibility Risk of Runtime Errors
Templates Yes Low High Low
Initialization Lists No High Medium High

Examples from the Code

Example 1: Using RotEncoder with Default Pin Configurations

RotEncoder encoder; // Uses default pins (PinA = 2, PinB = 3)

In this example, the encoder uses the default pin values for PinA (2) and PinB (3). These values are hardcoded into the RotEncoder class.

Example 2: Using RotEncoderPins with Custom Pin Configurations

RotEncoderPins<5, 6> customEncoder; // Uses pin 5 and 6

In this example, the pin numbers are passed via the template parameters and resolved at compile-time. This reduces the memory footprint and ensures efficient execution. The virtual functions getPinA() and getPinB() are overridden to return the values from the template.

Conclusion

By using templates and overridden functions instead of initialization lists, the code is optimized for both speed and memory usage. This approach provides a more flexible structure where users can easily change pin configurations via the RotEncoderPins class, without managing runtime initialization or risking runtime errors.