Skip to content

Commit 31ec31c

Browse files
authored
Fix issue with use of input-only pins (#72)
* Fix Github action builds by disabling mbed_portenta:envie_m4 * Fix #66 - fix usage of input-only pins * Update library version to 2.1.0
1 parent a904b3f commit 31ec31c

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

.github/workflows/arduino.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
- arduino:mbed_nano:nano33ble # Arduino Nano 33 BLE
8383
- arduino:mbed_nano:nanorp2040connect # Arduino Nano RP2040 Connect
8484
- arduino:mbed_portenta:envie_m7 # Arduino Portenta H7 (M7 core)
85-
- arduino:mbed_portenta:envie_m4 # Arduino Portenta H7 (M4 core)
85+
# - arduino:mbed_portenta:envie_m4 # Arduino Portenta H7 (M4 core)
8686
- arduino:mbed_rp2040:pico # Raspberry Pi Pico
8787
- arduino:mbed_nicla:nicla_sense # Nicla Sense ME
8888
- arduino:mbed_nicla:nicla_vision # Nicla Vision

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=QDEC
2-
version=2.0.0-beta
2+
version=2.1.0
33
author=SimpleHacks
44
maintainer=SimpleHacks
55
sentence=High-efficiency, state-machine based quadrature decoder.

src/qdec.h

+64-20
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,60 @@
2929
#include <Arduino.h>
3030
#include <stdint.h>
3131

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+
*/
3254
#if defined(SIMPLEHACKS_QDECODER_NEVER_INLINE)
3355
// yes, the __noinline__ attribute still uses inline, so that even non-template
3456
// functions can be defined in the header file.
3557
#define SIMPLEHACKS_INLINE_ATTRIBUTE __attribute__((noinline)) inline
3658
#else
3759
#define SIMPLEHACKS_INLINE_ATTRIBUTE __attribute__((always_inline)) inline
38-
#endif
60+
#endif
3961

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
5186

5287
namespace SimpleHacks {
5388

@@ -169,11 +204,15 @@ namespace SimpleHacks {
169204
if (_isStarted) return; // only call begin() once
170205
if (_pinA == QDECODER_INVALID_PIN) return;
171206
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
172215

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
177216
_CurrentState = Internal::QDECODER_STATE_START;
178217
_isStarted = true;
179218
};
@@ -319,10 +358,15 @@ namespace SimpleHacks {
319358
public: // public API
320359
// Initialization -- sets pins to correct input state
321360
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
326370
}
327371

328372
// update() reads the pins, changes state appropriately, and returns a

0 commit comments

Comments
 (0)