|
| 1 | +# Copilot Instructions for OneButton Library |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**OneButton** is an Arduino library for detecting button press patterns on a single digital input pin, supporting: |
| 6 | +- Single clicks |
| 7 | +- Double clicks |
| 8 | +- Long presses with start/stop events |
| 9 | +- Multi-click detection |
| 10 | +- Press events |
| 11 | +- Debouncing with configurable timing |
| 12 | + |
| 13 | +The library comes in two versions: |
| 14 | +- **OneButton**: Full-featured implementation for boards with ample memory |
| 15 | +- **OneButtonTiny**: Lightweight variant for memory-constrained devices (attiny84, etc.) |
| 16 | + |
| 17 | +**License**: BSD-3-Clause |
| 18 | +**Repository**: https://github.com/mathertel/OneButton |
| 19 | + |
| 20 | +## Key Design Patterns |
| 21 | + |
| 22 | +### 1. Deferred Initialization Pattern |
| 23 | +Prefer declaring buttons without initialization and using `setup()` in Arduino's `setup()` function: |
| 24 | + |
| 25 | +```cpp |
| 26 | +// Declaration |
| 27 | +OneButton btn; |
| 28 | + |
| 29 | +// In setup() |
| 30 | +btn.setup(BUTTON_PIN, INPUT_PULLUP, true); // pin, mode, activeLow |
| 31 | +``` |
| 32 | + |
| 33 | +This ensures proper hardware initialization order and maximum compatibility. |
| 34 | + |
| 35 | +### 2. Event-Based Callback Architecture |
| 36 | +OneButton uses callbacks (function pointers) for events. Two callback types are supported: |
| 37 | +- **Simple callbacks**: `void callback(void)` |
| 38 | +- **Parameterized callbacks**: `void callback(void *param)` |
| 39 | + |
| 40 | +### 3. Finite State Machine |
| 41 | +OneButton implements a simple FSM internally for state tracking. When generating code, ensure: |
| 42 | +- `tick()` is called regularly in the main loop |
| 43 | +- Button logic is non-blocking |
| 44 | +- Only one event fires per button state transition |
| 45 | + |
| 46 | +## API Usage Guidelines |
| 47 | + |
| 48 | +### Setup and Configuration |
| 49 | + |
| 50 | +```cpp |
| 51 | +// Configuration in setup() |
| 52 | +btn.setDebounceMs(50); // Debouncing delay (default: 20ms) |
| 53 | +btn.setClickMs(400); // Time to recognize single click (default: 400ms) |
| 54 | +btn.setPressMs(800); // Time to trigger press event (default: 800ms) |
| 55 | +btn.setLongPressIntervalMs(100); // Interval for repeated long-press callbacks (default: 0) |
| 56 | +btn.setIdleMs(3000); // Auto-idle timeout (optional) |
| 57 | +``` |
| 58 | + |
| 59 | +### Event Attachment |
| 60 | + |
| 61 | +When generating code that attaches events: |
| 62 | +```cpp |
| 63 | +// Single click |
| 64 | +btn.attachClick(handleClick); |
| 65 | +btn.attachClick(handleClickWithParam, (void*)myData); |
| 66 | + |
| 67 | +// Double click |
| 68 | +btn.attachDoubleClick(handleDoubleClick); |
| 69 | + |
| 70 | +// Press events |
| 71 | +btn.attachPress(handlePress); |
| 72 | + |
| 73 | +// Long press |
| 74 | +btn.attachLongPressStart(handleLongPressStart); |
| 75 | +btn.attachLongPressStop(handleLongPressStop); |
| 76 | + |
| 77 | +// Multi-click (requires specific configuration) |
| 78 | +btn.attachMultiClick(handleMultiClick); |
| 79 | +``` |
| 80 | + |
| 81 | +### Main Loop Integration |
| 82 | + |
| 83 | +The `tick()` method must be called regularly: |
| 84 | +```cpp |
| 85 | +void loop() { |
| 86 | + btn.tick(); // Usually called on every iteration |
| 87 | + // Other code... |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Getting Button State |
| 92 | + |
| 93 | +```cpp |
| 94 | +// Active state (requires getPressedMs() or related methods) |
| 95 | +if (btn.isPressed()) { } // Button currently held down |
| 96 | +if (btn.isLongPressed()) { } // Long press is active |
| 97 | +``` |
| 98 | +
|
| 99 | +## Code Style and Conventions |
| 100 | +
|
| 101 | +### Naming Conventions |
| 102 | +- **Buttons/pins**: `btn1`, `btn2`, `myButton`, `BUTTON_PIN_1` (constants) |
| 103 | +- **Callbacks**: `handleClick()`, `onButtonPress()`, `button1_click()` |
| 104 | +- **Configuration variables**: Use meaningful names with `Ms` suffix for time values |
| 105 | +
|
| 106 | +### Variable Scope |
| 107 | +- Global OneButton instances are acceptable for simplicity |
| 108 | +- Prefer static arrays for multiple buttons in memory-constrained scenarios |
| 109 | +- Use parameterized callbacks for passing context to handlers |
| 110 | +
|
| 111 | +### Callback Pattern |
| 112 | +```cpp |
| 113 | +// Simple callback (preferred for single-purpose buttons) |
| 114 | +void handleClick() { |
| 115 | + // Handle event |
| 116 | +} |
| 117 | +
|
| 118 | +// Parameterized callback (better for multiple buttons with shared handler) |
| 119 | +void handleClick(void *param) { |
| 120 | + int buttonId = (int)param; |
| 121 | + // Handle event based on buttonId |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Common Implementation Patterns |
| 126 | + |
| 127 | +### Single Button with Multiple Events |
| 128 | +```cpp |
| 129 | +OneButton btn; |
| 130 | + |
| 131 | +void setup() { |
| 132 | + btn.setup(BUTTON_PIN, INPUT_PULLUP, true); |
| 133 | + btn.attachClick(handleClick); |
| 134 | + btn.attachDoubleClick(handleDoubleClick); |
| 135 | + btn.attachLongPressStart(handleLongStart); |
| 136 | + btn.attachLongPressStop(handleLongStop); |
| 137 | +} |
| 138 | + |
| 139 | +void loop() { |
| 140 | + btn.tick(); |
| 141 | +} |
| 142 | + |
| 143 | +void handleClick() { /* single click logic */ } |
| 144 | +void handleDoubleClick() { /* double click logic */ } |
| 145 | +void handleLongStart() { /* long press started */ } |
| 146 | +void handleLongStop() { /* long press ended */ } |
| 147 | +``` |
| 148 | +
|
| 149 | +### Multiple Buttons with Shared Handler |
| 150 | +```cpp |
| 151 | +#define NUM_BUTTONS 3 |
| 152 | +OneButton buttons[NUM_BUTTONS]; |
| 153 | +int btn_pins[NUM_BUTTONS] = {2, 3, 4}; |
| 154 | +
|
| 155 | +void setup() { |
| 156 | + for (int i = 0; i < NUM_BUTTONS; i++) { |
| 157 | + buttons[i].setup(btn_pins[i], INPUT_PULLUP, true); |
| 158 | + buttons[i].attachClick(handleButtonClick, (void*)i); |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +void loop() { |
| 163 | + for (int i = 0; i < NUM_BUTTONS; i++) { |
| 164 | + buttons[i].tick(); |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +void handleButtonClick(void *param) { |
| 169 | + int buttonId = (int)param; |
| 170 | + // Handle click for specific button |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### OneButtonTiny Usage (Memory-Constrained Devices) |
| 175 | +```cpp |
| 176 | +#include <OneButtonTiny.h> |
| 177 | + |
| 178 | +OneButtonTiny btn; |
| 179 | + |
| 180 | +void setup() { |
| 181 | + btn.setup(BUTTON_PIN, INPUT_PULLUP, true); |
| 182 | + btn.attachClick(handleClick); // ✓ Available |
| 183 | + btn.attachDoubleClick(handleDblClick); // ✓ Available |
| 184 | + btn.attachLongPressStart(handleLong); // ✓ Available |
| 185 | + // Note: MultiClick, LongPressStop NOT available in Tiny version |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +## Event Handling Best Practices |
| 190 | + |
| 191 | +### Debouncing |
| 192 | +- Default debounce is 20ms, usually sufficient |
| 193 | +- Increase `setDebounceMs()` if experiencing phantom clicks or noise |
| 194 | + |
| 195 | +### Timing Configuration Priorities |
| 196 | +``` |
| 197 | +debounce < click < press < longPress |
| 198 | +Example: 20ms < 400ms < 800ms < varies |
| 199 | +``` |
| 200 | + |
| 201 | +### Avoiding Common Issues |
| 202 | +1. **Multiple event triggers**: Ensure `tick()` is called once per loop iteration |
| 203 | +2. **Slow response**: Call `tick()` more frequently (no delays in loop) |
| 204 | +3. **Button state changes not detected**: Verify PIN configuration and `activeLow` setting |
| 205 | +4. **Long press interferes with click**: Adjust `setClickMs()` and `setPressMs()` values |
| 206 | + |
| 207 | +## Testing Patterns |
| 208 | + |
| 209 | +When generating test code: |
| 210 | +```cpp |
| 211 | +// Simulate button press duration (in mock/test environment) |
| 212 | +void testSingleClick(OneButton &btn) { |
| 213 | + // Simulate 100ms press |
| 214 | + pressButton(btn); |
| 215 | + delay(100); |
| 216 | + releaseButton(btn); |
| 217 | + delay(500); // Wait for single-click detection |
| 218 | +} |
| 219 | + |
| 220 | +void testDoubleClick(OneButton &btn) { |
| 221 | + pressButton(btn); |
| 222 | + delay(50); |
| 223 | + releaseButton(btn); |
| 224 | + delay(100); // Brief interval between clicks |
| 225 | + pressButton(btn); |
| 226 | + delay(50); |
| 227 | + releaseButton(btn); |
| 228 | +} |
| 229 | +``` |
| 230 | +
|
| 231 | +## Code Generation Tips for Copilot |
| 232 | +
|
| 233 | +When using Copilot to generate OneButton code, be specific about: |
| 234 | +
|
| 235 | +1. **Button behavior**: "Generate code for detecting single and double clicks" |
| 236 | +2. **Timing requirements**: "Set debounce to 50ms and click detection to 500ms" |
| 237 | +3. **Callback style**: Request either simple or parameterized callbacks |
| 238 | +4. **Multi-button scenarios**: Specify number of buttons and how to differentiate them |
| 239 | +5. **Memory constraints**: Mention if OneButtonTiny should be used instead of OneButton |
| 240 | +
|
| 241 | +### Example Prompts |
| 242 | +- "Add a long-press handler to toggle an LED when held for 1 second" |
| 243 | +- "Create a multi-button setup with 3 buttons, each calling a different function" |
| 244 | +- "Generate a debounced button click counter that prints to Serial" |
| 245 | +
|
| 246 | +## Examples Location |
| 247 | +
|
| 248 | +The library includes comprehensive examples: |
| 249 | +- `examples/SimpleOneButton` - Basic single button usage |
| 250 | +- `examples/FunctionalButton` - Using C++ lambda callbacks |
| 251 | +- `examples/TwoButtons` - Multiple button management |
| 252 | +- `examples/LongPressEvents` - Long press detection |
| 253 | +- `examples/SpecialInput` - Custom input sources |
| 254 | +- `examples/InterruptOneButton` - Interrupt-based detection |
| 255 | +- `examples/BlinkMachine` - State machine example |
| 256 | +
|
| 257 | +## Deprecated Methods to Avoid |
| 258 | +
|
| 259 | +When generating code, avoid these legacy method names: |
| 260 | +- `setDebounceTicks()` → Use `setDebounceMs()` instead |
| 261 | +- `setClickTicks()` → Use `setClickMs()` instead |
| 262 | +- `setPressTicks()` → Use `setPressMs()` instead |
| 263 | +
|
| 264 | +## Library Version |
| 265 | +
|
| 266 | +Current version: **2.6.2** |
| 267 | +
|
| 268 | +## References |
| 269 | +
|
| 270 | +- **Homepage**: http://www.mathertel.de/Arduino/OneButtonLibrary.aspx |
| 271 | +- **Repository**: https://github.com/mathertel/OneButton |
| 272 | +- **License**: BSD-3-Clause (see LICENSE file) |
| 273 | +- **Author**: Matthias Hertel (mathertel@hotmail.com) |
0 commit comments