|
| 1 | + |
| 2 | +[](https://github.com/marketplace/actions/arduino_ci) |
| 3 | +[](https://github.com/RobTillaart/rotaryDecoder/blob/master/LICENSE) |
| 4 | +[](https://github.com/RobTillaart/rotaryDecoder/releases) |
| 5 | + |
| 6 | + |
1 | 7 | # rotaryDecoder |
| 8 | + |
2 | 9 | Arduino library for a PCF8574 based rotary decoder - supports 4 RE. |
| 10 | + |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +Experimental - not tested yet |
| 15 | + |
| 16 | +## Interface |
| 17 | + |
| 18 | +- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire);** |
| 19 | +- **bool begin(uint8_t sda, uint8_t scl, uint8_t cnt = 4)** ESP32 ea initializes the class. |
| 20 | +sets I2C pins. |
| 21 | +cnt is the number of rotary encoders connected. |
| 22 | +returns true if the PCF8574 is on the I2C bus. |
| 23 | +- **bool begin(uint8_t cnt = 4)** UNO ea. initializes the class. |
| 24 | +cnt is the number of rotary encoders connected. |
| 25 | +returns true if the PCF8574 is on the I2C bus. |
| 26 | +- **bool isConnected()** returns true if the PCF8574 is on the I2C bus. |
| 27 | + |
| 28 | +## Core functions |
| 29 | + |
| 30 | +- **void readInitialState()** read the inital state of the 4 rotary encoders. typically called in setup only, or after a sleep e.g. in combination with **setValue()** |
| 31 | +- **bool checkChange()** polling to see if one or more RE have changed, without updating the counters. |
| 32 | +- **void update()** update the internal counters of the RE. These will add +1 or -1 depending on direction. |
| 33 | +- **void updateSingle()** update the internal counters of the RE. This will add +1 +2 or +3 as it assumes that the rotary encoder only goes into a single direction. |
| 34 | + |
| 35 | +## Counters |
| 36 | + |
| 37 | +- **uint32_t getValue(uint8_r re)** returns the RE counter. |
| 38 | +- **void setValue(uint8_r re, uint32_t val = 0)** (re)set the internal counter to val, default 0 |
| 39 | + |
| 40 | + |
| 41 | +## Debugging |
| 42 | + |
| 43 | +- **int8_t getLastPosition(uint8_r re)** |
| 44 | + |
| 45 | + |
| 46 | +## Performance |
| 47 | + |
| 48 | +As the decoder is based upon a PCF8574, a I2C device, the performance is affected by the |
| 49 | +clockspeed of the I2C bus. All four core functions have one call to **\_read()** which is the most expensive part. |
| 50 | + |
| 51 | +Early tests gave the following indicative times (Arduino UNO) for the **update()** |
| 52 | +function (with no updates it is ~8 us faster). Note that above 500KHz the gain becomes less |
| 53 | +while reliability of signal decreases. (500KHz is ~3x faster than 100 KHz) |
| 54 | + |
| 55 | +| I2C speed | time (us) | delta | %% | |
| 56 | +|:---------:|:---------:|:-----:|:-----:| |
| 57 | +| 100 KHz | 247 | | | |
| 58 | +| 200 KHz | 146 | 99 | 40% | |
| 59 | +| 300 KHz | 110 | 36 | 24% | |
| 60 | +| 400 KHz | 95 | 15 | 14% | preferred max |
| 61 | +| 500 KHz | 84 | 11 | 12% | |
| 62 | +| 600 KHz | 79 | 5 | 6% | |
| 63 | +| 700 KHz | 73 | 6 | 8% | |
| 64 | + |
| 65 | +At @400KHz it can update 4 rotary encoders in ~100us. |
| 66 | +At a 50% update percentage this implies a max of about |
| 67 | +5000 **update()** calls per second in theory |
| 68 | +**to be tested in practice** |
| 69 | + |
| 70 | +Note that a high speed drill goes up to 30000 RPM = 500 RPS = 2000 interrupts per second, |
| 71 | +assuming 4 pulses == 360°. (not tested) |
| 72 | + |
| 73 | + |
| 74 | +## Operational |
| 75 | + |
| 76 | +See examples.. |
| 77 | + |
0 commit comments