|
| 1 | +# Copilot Instructions for RotaryEncoder |
| 2 | + |
| 3 | +## Project Expectations |
| 4 | + |
| 5 | +RotaryEncoder is a lightweight Arduino library for handling rotary encoder input across |
| 6 | +multiple microcontroller platforms (Arduino, ESP8266, ESP32). The library prioritizes |
| 7 | +reliability, minimal resource usage, and compatibility with embedded systems. |
| 8 | + |
| 9 | +- **Target Platforms**: Arduino (AVR), ESP8266, ESP32, and compatible boards |
| 10 | +- **Primary Use Case**: Non-blocking rotary encoder event detection in applications with tight timing constraints |
| 11 | +- **Target Users**: Embedded systems developers, IoT projects, Arduino hobbyists |
| 12 | +- **Quality Bar**: Production-ready code suitable for battery-powered and resource-constrained devices |
| 13 | + |
| 14 | +## Coding Style |
| 15 | + |
| 16 | +- **Language**: C++ with compatible C patterns |
| 17 | +- **Compatibility**: Must compile with Arduino IDE and PlatformIO toolchains |
| 18 | +- **Indentation**: 2-space indentation for consistency with Arduino conventions |
| 19 | +- **Naming Conventions**: |
| 20 | + - Classes: `PascalCase` (e.g., `RotaryEncoder`) |
| 21 | + - Methods/Functions: `camelCase` (e.g., `begin()`, `getPosition()`) |
| 22 | + - Private/Protected Members: prefix with `_` (e.g., `_pinA`, `_pinB`) |
| 23 | + - Constants: `UPPER_CASE` (e.g., `ENCODER_STEPS`) |
| 24 | +- **Code Clarity**: Prioritize readable, self-documenting code over complexity |
| 25 | +- **Comments**: Include comments for non-obvious logic; explain the "why," not the "what" |
| 26 | + |
| 27 | +## Library-Specific Rules |
| 28 | + |
| 29 | +- **No Dynamic Allocation**: All data structures must be statically allocated. No `new`, `malloc()`, or dynamic arrays. Use fixed-size arrays instead. |
| 30 | +- **API Stability**: Maintain backward compatibility. Only add methods; never remove or change existing public signatures. |
| 31 | +- **Non-Blocking Patterns**: All library functions must be non-blocking. No `delay()`, `millis()` waits, or blocking I/O in the main library code. |
| 32 | +- **Interrupt Safety**: Code must be safe to call from both interrupt handlers and main loop. Use volatile variables appropriately. |
| 33 | +- **Minimal Dependencies**: No external libraries or cloud dependencies. Only use Arduino core APIs. |
| 34 | +- **Memory Efficiency**: Minimize RAM and flash usage. Suitable for devices with as little as 16KB RAM. |
| 35 | +- **Platform Abstraction**: Use Arduino core abstractions (e.g., `digitalWrite()`, `attachInterrupt()`) for compatibility across boards. |
| 36 | + |
| 37 | +## Examples and Documentation |
| 38 | + |
| 39 | +- **Location**: See `/examples/` directory for working examples: |
| 40 | + - `SimplePollRotator.ino` — Basic polling-based usage |
| 41 | + - `InterruptRotator.ino` — Interrupt-driven implementation |
| 42 | + - `AcceleratedRotator.ino` — Acceleration detection |
| 43 | + - `LimitedRotator.ino` — Position limiting with boundary checks |
| 44 | +- **Example Standards**: All examples must: |
| 45 | + - Be minimal and focused (< 100 lines when possible) |
| 46 | + - Include clear comments explaining each step |
| 47 | + - Work without modification on Arduino boards |
| 48 | + - Demonstrate a single feature/use case |
| 49 | + |
| 50 | +## How Copilot Should Respond |
| 51 | + |
| 52 | +- **Code Suggestions**: Provide implementations that follow Arduino conventions and embedded C++ best practices |
| 53 | +- **API Design**: Suggest additions that maintain backward compatibility; mark breaking changes clearly |
| 54 | +- **Documentation**: Include JavaDoc-style comments for public methods (/** ... */) |
| 55 | +- **Testing Suggestions**: Recommend test scenarios that cover interrupt safety and different platform behavior |
| 56 | +- **Examples**: When suggesting new features, provide minimal, runnable examples |
| 57 | +- **Performance**: Highlight memory usage and timing implications of suggestions |
| 58 | +- **Problem-Solving**: Ask clarifying questions about platform targets and hardware constraints |
| 59 | +- **Refactoring**: Suggest improvements that maintain the non-blocking guarantee and static allocation requirement |
| 60 | + |
| 61 | +## Do Not |
| 62 | + |
| 63 | +- ❌ Use `malloc()`, `new`, `delete`, or `free()`. Use stack and static allocation only. |
| 64 | +- ❌ Add `delay()` or other blocking calls to library code. |
| 65 | +- ❌ Introduce external library dependencies (beyond Arduino core). |
| 66 | +- ❌ Change existing public method signatures without deprecation warnings. |
| 67 | +- ❌ Use c++17 or later features; maintain c++11/14 compatibility. |
| 68 | +- ❌ Implement cloud connectivity or network features. |
| 69 | +- ❌ Add threads or RTOS features that aren't part of Arduino core. |
| 70 | +- ❌ Make assumptions about global state; support multiple encoder instances independently. |
| 71 | +- ❌ Use platform-specific code without appropriate `#ifdef` guards (e.g., `#ifdef ESP8266`). |
| 72 | +- ❌ Perform heavy computation in interrupt handlers; defer processing to `loop()`. |
| 73 | +- ❌ Commit build artifacts (see `.gitignore`); only commit source files. |
| 74 | +- ❌ Remove backward compatibility without explicit version bump and migration guide. |
| 75 | + |
| 76 | +## Additional Context |
| 77 | + |
| 78 | +- **Build System**: Arduino IDE standard; also compatible with PlatformIO |
| 79 | +- **License**: LGPL-2.1 (see LICENSE file) |
| 80 | +- **Changelog**: Document significant changes in `CHANGELOG.md` |
| 81 | +- **Testing**: Manual testing on target boards is preferred; automated tests must not depend on hardware |
0 commit comments