Skip to content

Commit 5a63607

Browse files
authored
Add Wire1 support for ESP32 (#7)
1 parent 1e91d55 commit 5a63607

File tree

6 files changed

+92
-37
lines changed

6 files changed

+92
-37
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.1.4] - 2023-09-22
10+
- add Wire1 support for ESP32
11+
- moved code from .h to .cpp
12+
- update readme.md
13+
- minor edits
14+
15+
916
## [0.1.3] - 2022-11-23
1017
- add changelog.md
1118
- add RP2040 to build-CI
1219
- fix version number .cpp
1320
- minor edits
1421

15-
1622
## [0.1.2] - 2021-12-27
1723
- update library.json
1824
- update readme.md

README.md

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
[![Arduino CI](https://github.com/RobTillaart/rotaryDecoder/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
33
[![Arduino-lint](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/arduino-lint.yml)
44
[![JSON check](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/jsoncheck.yml)
5+
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/rotaryDecoder.svg)](https://github.com/RobTillaart/rotaryDecoder/issues)
6+
57
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/rotaryDecoder/blob/master/LICENSE)
68
[![GitHub release](https://img.shields.io/github/release/RobTillaart/rotaryDecoder.svg?maxAge=3600)](https://github.com/RobTillaart/rotaryDecoder/releases)
9+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/rotaryDecoder.svg)](https://registry.platformio.org/libraries/robtillaart/rotaryDecoder)
710

811

912
# rotaryDecoder
@@ -24,6 +27,12 @@ pins to GND so you will not get unintended interrupts.
2427

2528
## Interface
2629

30+
```cpp
31+
#include "rotaryEncoder.h"
32+
```
33+
34+
#### Constructor
35+
2736
- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire);**
2837
- **bool begin(uint8_t sda, uint8_t scl, uint8_t count = 4)** ESP32 ea initializes the class
2938
by setting the I2C sda and scl pins.
@@ -35,23 +44,27 @@ Returns true if the PCF8574 is on the I2C bus.
3544
- **bool isConnected()** returns true if the PCF8574 is on the I2C bus.
3645

3746

38-
## Core functions
47+
#### Core functions
3948

4049
- **void readInitialState()** read the initial state of the 4 rotary encoders.
4150
Typically called in setup only, or after a sleep e.g. in combination with **setValue()**
42-
- **bool checkChange()** polling to see if one or more RE have changed, without updating the counters.
43-
- **void update()** update the internal counters of the RE. These will add +1 or -1 depending on direction.
44-
- **void updateSingle()** update the internal counters of the RE. This will add +1 +2 or +3
45-
as it assumes that the rotary encoder only goes into a single direction.
51+
- **bool checkChange()** polling to see if one or more RE have changed,
52+
without updating the counters.
53+
- **void update()** update the internal counters of the RE.
54+
These will add +1 or -1 depending on direction.
55+
- **void updateSingle()** update the internal counters of the RE.
56+
This will add +1 +2 or +3 as it assumes that the rotary encoder
57+
only goes into a single direction.
4658

4759

48-
## Counters
60+
#### Counters
4961

5062
- **uint32_t getValue(uint8_r re)** returns the RE counter.
51-
- **void setValue(uint8_r re, uint32_t value = 0)** (re)set the internal counter to value, default 0
63+
- **void setValue(uint8_r re, uint32_t value = 0)** (re)set the internal counter to value,
64+
default 0
5265

5366

54-
## Debugging
67+
#### Debugging
5568

5669
- **int8_t getLastPosition(uint8_r re)** returns last position.
5770

@@ -63,20 +76,21 @@ clock speed of the I2C bus. All four core functions have one call to **\_read8()
6376
is the most expensive part.
6477

6578
Early tests gave the following indicative times (Arduino UNO) for the **update()**
66-
function (with no updates it is ~8 us faster). Note that above 500KHz the gain becomes less
67-
while reliability of signal decreases. (500KHz is about ~3x faster than 100 KHz)
79+
function (with no updates it is ~8 us faster).
80+
Note that above 500 KHz the gain becomes less while reliability of signal decreases.
81+
(500 KHz is about ~3x faster than 100 KHz in practice.)
6882
As 400 KHz is a standard I2C clock speed it is the preferred one.
6983

7084

71-
| I2C speed | time (us) | delta | %% |
72-
|:---------:|:---------:|:-----:|:-----:|
73-
| 100 KHz | 247 | | |
74-
| 200 KHz | 146 | 99 | 40% |
75-
| 300 KHz | 110 | 36 | 24% |
76-
| 400 KHz | 95 | 15 | 14% | preferred max
77-
| 500 KHz | 84 | 11 | 12% |
78-
| 600 KHz | 79 | 5 | 6% |
79-
| 700 KHz | 73 | 6 | 8% |
85+
| I2C speed | time (us) | delta | %% | Notes |
86+
|:-----------:|:-----------:|:-------:|:-----:|:--------|
87+
| 100 KHz | 247 | | |
88+
| 200 KHz | 146 | 99 | 40% |
89+
| 300 KHz | 110 | 36 | 24% |
90+
| 400 KHz | 95 | 15 | 14% | preferred max
91+
| 500 KHz | 84 | 11 | 12% |
92+
| 600 KHz | 79 | 5 | 6% |
93+
| 700 KHz | 73 | 6 | 8% |
8094

8195

8296
At @400KHz it can update 4 rotary encoders in ~100us.
@@ -95,14 +109,33 @@ See examples..
95109

96110
## Future
97111

98-
#### must
112+
#### Must
113+
99114
- update documentation
100-
- picture how to connect e.g 2 RE's which pins to used
115+
- picture how to connect e.g two rotary encoders which pins to used
116+
117+
118+
#### Should
101119

102-
#### should
103120
- test with a high speed drill like a Dremel-tool.
104121

105-
#### could
122+
123+
#### Could
124+
106125
- invert flag to adjust to RE that give their pulse just the other way around?
107126
- setInvert(bool); getInvert();
108-
- per channel / all?
127+
- per channel / all?
128+
129+
130+
#### Wont
131+
132+
133+
134+
## Support
135+
136+
If you appreciate my libraries, you can support the development and maintenance.
137+
Improve the quality of the libraries by providing issues and Pull Requests, or
138+
donate through PayPal or GitHub sponsors.
139+
140+
Thank you,
141+

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/rotaryDecoder.git"
1717
},
18-
"version": "0.1.3",
18+
"version": "0.1.4",
1919
"license": "MIT",
20-
"frameworks": "arduino",
20+
"frameworks": "*",
2121
"platforms": "*",
2222
"headers": "rotaryDecoder.h"
2323
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=rotaryDecoder
2-
version=0.1.3
2+
version=0.1.4
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library to rotary decoder with a PCF8574

rotaryDecoder.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: rotaryDecoder.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.1.4
55
// DATE: 2021-05-08
66
// PURPOSE: rotary decoder library for Arduino
77
// URL: https://github.com/RobTillaart/rotaryDecoder
@@ -26,7 +26,6 @@ bool rotaryDecoder::begin(uint8_t sda, uint8_t scl, uint8_t count)
2626
{
2727
_count = count;
2828
if (_count > 4) _count = 4;
29-
_wire = &Wire;
3029
_wire->begin(sda, scl);
3130
if (! isConnected()) return false;
3231
return true;
@@ -144,6 +143,23 @@ bool rotaryDecoder::updateSingle()
144143
}
145144

146145

146+
int32_t rotaryDecoder::getValue(uint8_t re)
147+
{
148+
return _encoder[re];
149+
}
150+
151+
152+
void rotaryDecoder::setValue(uint8_t re, int32_t value)
153+
{
154+
_encoder[re] = value;
155+
}
156+
157+
158+
uint8_t rotaryDecoder::getLastPosition(uint8_t re)
159+
{
160+
return _lastPos[re];
161+
};
162+
147163
/////////////////////////////////////////////////////
148164
//
149165
// PRIVATE

rotaryDecoder.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: rotaryDecoder.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.1.4
66
// DATE: 2021-05-08
77
// PURPOSE: rotary decoder library for Arduino
88
// URL: https://github.com/RobTillaart/rotaryDecoder
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212
#include "Wire.h"
1313

14-
#define ROTARY_DECODER_LIB_VERSION (F("0.1.3"))
14+
#define ROTARY_DECODER_LIB_VERSION (F("0.1.4"))
1515

1616

1717
class rotaryDecoder
@@ -38,18 +38,18 @@ class rotaryDecoder
3838
bool updateSingle(); // assumes single direction => + ++ +++
3939

4040
// re = rotary encoder
41-
int32_t getValue(uint8_t re) { return _encoder[re]; };
42-
void setValue(uint8_t re, int32_t value = 0) { _encoder[re] = value; };
41+
int32_t getValue(uint8_t re);
42+
void setValue(uint8_t re, int32_t value = 0);
4343

4444
// DEBUG
45-
uint8_t getLastPosition(uint8_t re) { return _lastPos[re]; };
45+
uint8_t getLastPosition(uint8_t re);
4646

4747

4848
private:
4949
uint8_t _count = 0;
5050
uint8_t _lastValue = 0;
51-
uint8_t _lastPos[4] = { 0,0,0,0 };
52-
int32_t _encoder[4] = { 0,0,0,0 };
51+
uint8_t _lastPos[4] = { 0, 0, 0, 0 };
52+
int32_t _encoder[4] = { 0, 0, 0, 0 };
5353

5454
uint8_t _read8();
5555
uint8_t _address;

0 commit comments

Comments
 (0)