|
29 | 29 | #include <Arduino.h>
|
30 | 30 | #include <stdint.h>
|
31 | 31 |
|
| 32 | + |
| 33 | +/* Simpler namespace use |
| 34 | + This library uses a namespace (e.g., SimpleHacks::QDecoder instead of just QDecoder). |
| 35 | + However, it's very easy to make this effectively disappear! |
| 36 | + As an example, at the start of a function (or right after all headers included) |
| 37 | + simply add the following lines: |
| 38 | + using SimpleHacks::QDECODER_EVENT; |
| 39 | + using SimpleHacks::QDecoder; |
| 40 | + using SimpleHacks::QDECODER_EVENT; |
| 41 | + Or, if you want all the types to be avabile, you could instead add the line: |
| 42 | + using namespace SimpleHacks; |
| 43 | + See the examples for more options... |
| 44 | + */ |
| 45 | + |
| 46 | +/* User-configurable: SIMPLEHACKS_QDECODER_NEVER_INLINE |
| 47 | + Default: not defined |
| 48 | +
|
| 49 | + By default, attributes are used to strongly hint that the functions |
| 50 | + should be inlined. If this symbol is defined, then the hint will |
| 51 | + instead be that the functions should NOT be inlined (which may be |
| 52 | + easier to debug in some cases). |
| 53 | + */ |
32 | 54 | #if defined(SIMPLEHACKS_QDECODER_NEVER_INLINE)
|
33 | 55 | // yes, the __noinline__ attribute still uses inline, so that even non-template
|
34 | 56 | // functions can be defined in the header file.
|
35 | 57 | #define SIMPLEHACKS_INLINE_ATTRIBUTE __attribute__((noinline)) inline
|
36 | 58 | #else
|
37 | 59 | #define SIMPLEHACKS_INLINE_ATTRIBUTE __attribute__((always_inline)) inline
|
38 |
| -#endif |
| 60 | +#endif |
39 | 61 |
|
40 |
| -// This library uses a namespace (e.g., SimpleHacks::QDecoder instead of just QDecoder). |
41 |
| -// However, it's very easy to make this effectively disappear! |
42 |
| -// As an example, at the start of a function (or right after all headers included) |
43 |
| -// simply add the following lines: |
44 |
| -// using SimpleHacks::QDECODER_EVENT; |
45 |
| -// using SimpleHacks::QDecoder; |
46 |
| -// using SimpleHacks::QDECODER_EVENT; |
47 |
| -// Or, if you want all the types to be avabile, you could instead add the line: |
48 |
| -// using namespace SimpleHacks; |
49 |
| -// |
50 |
| -// See the examples for more options... |
| 62 | +/* User-configurable: SIMPLEHACKS_QDECODER_PIN_MODE |
| 63 | + Default: INPUT_PULLDOWN |
| 64 | + |
| 65 | + By default, this library sets the pins to use internal |
| 66 | + pull-up resistors. Should a project prefer to only use |
| 67 | + external resistors, and/or a different pin mode |
| 68 | + (such as INPUT_PULLDOWN, or just INPUT), this can be |
| 69 | + defined accordingly. |
| 70 | + */ |
| 71 | +#if defined(SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN) |
| 72 | + #error "Not user configurable: SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN" |
| 73 | +#endif |
| 74 | +#if defined(SIMPLEHACKS_QDECODER_PIN_MODE) |
| 75 | + // Allow user configuration to override default |
| 76 | + #define SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN |
| 77 | +#elif defined(ARDUINO) && (ARDUINO >= 101) && defined(INPUT_PULLUP) |
| 78 | + // Default: non-ancient Arduino environment |
| 79 | + #define SIMPLEHACKS_QDECODER_PIN_MODE INPUT_PULLUP |
| 80 | + #define SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN |
| 81 | +#else |
| 82 | + // Ancient arduino environment split pin mode |
| 83 | + // from whether pull-up resistor |
| 84 | + #define SIMPLEHACKS_QDECODER_PIN_MODE INPUT |
| 85 | +#endif |
51 | 86 |
|
52 | 87 | namespace SimpleHacks {
|
53 | 88 |
|
@@ -169,11 +204,15 @@ namespace SimpleHacks {
|
169 | 204 | if (_isStarted) return; // only call begin() once
|
170 | 205 | if (_pinA == QDECODER_INVALID_PIN) return;
|
171 | 206 | if (_pinB == QDECODER_INVALID_PIN) return;
|
| 207 | + pinMode(_pinA, SIMPLEHACKS_QDECODER_PIN_MODE); |
| 208 | +#if !defined(SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN) |
| 209 | + digitalWrite(_pinA, HIGH); // turn on pullup resistor (very old Arduino versions require this) |
| 210 | +#endif |
| 211 | + pinMode(_pinB, SIMPLEHACKS_QDECODER_PIN_MODE); |
| 212 | +#if !defined(SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN) |
| 213 | + digitalWrite(_pinB, HIGH); // turn on pullup resistor (very old Arduino versions require this) |
| 214 | +#endif |
172 | 215 |
|
173 |
| - pinMode(_pinA, INPUT_PULLUP); |
174 |
| - digitalWrite(_pinA, HIGH); // turn on pullup resistor |
175 |
| - pinMode(_pinB, INPUT_PULLUP); |
176 |
| - digitalWrite(_pinB, HIGH); // turn on pullup resistor |
177 | 216 | _CurrentState = Internal::QDECODER_STATE_START;
|
178 | 217 | _isStarted = true;
|
179 | 218 | };
|
@@ -319,10 +358,15 @@ namespace SimpleHacks {
|
319 | 358 | public: // public API
|
320 | 359 | // Initialization -- sets pins to correct input state
|
321 | 360 | SIMPLEHACKS_INLINE_ATTRIBUTE static void begin() {
|
322 |
| - pinMode(_ARDUINO_PIN_A, INPUT); |
323 |
| - digitalWrite(_ARDUINO_PIN_A, HIGH); // turn on pullup resistor |
324 |
| - pinMode(_ARDUINO_PIN_B, INPUT_PULLUP); |
325 |
| - digitalWrite(_ARDUINO_PIN_B, HIGH); // turn on pullup resistor |
| 361 | + |
| 362 | + pinMode(_ARDUINO_PIN_A, SIMPLEHACKS_QDECODER_PIN_MODE); |
| 363 | +#if !defined(SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN) |
| 364 | + digitalWrite(_ARDUINO_PIN_A, HIGH); // turn on pullup resistor (very old Arduino versions require this) |
| 365 | +#endif |
| 366 | + pinMode(_ARDUINO_PIN_B, SIMPLEHACKS_QDECODER_PIN_MODE); |
| 367 | +#if !defined(SIMPLEHACKS_QDECODER_NO_WRITE_TO_PIN) |
| 368 | + digitalWrite(_ARDUINO_PIN_B, HIGH); // turn on pullup resistor (very old Arduino versions require this) |
| 369 | +#endif |
326 | 370 | }
|
327 | 371 |
|
328 | 372 | // update() reads the pins, changes state appropriately, and returns a
|
|
0 commit comments