Skip to content

Commit

Permalink
Add Wire1 support for ESP32 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Sep 22, 2023
1 parent 1e91d55 commit 5a63607
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 37 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.4] - 2023-09-22
- add Wire1 support for ESP32
- moved code from .h to .cpp
- update readme.md
- minor edits


## [0.1.3] - 2022-11-23
- add changelog.md
- add RP2040 to build-CI
- fix version number .cpp
- minor edits


## [0.1.2] - 2021-12-27
- update library.json
- update readme.md
Expand Down
81 changes: 57 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
[![Arduino CI](https://github.com/RobTillaart/rotaryDecoder/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/rotaryDecoder/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/rotaryDecoder.svg)](https://github.com/RobTillaart/rotaryDecoder/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/rotaryDecoder/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/rotaryDecoder.svg?maxAge=3600)](https://github.com/RobTillaart/rotaryDecoder/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/rotaryDecoder.svg)](https://registry.platformio.org/libraries/robtillaart/rotaryDecoder)


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

## Interface

```cpp
#include "rotaryEncoder.h"
```

#### Constructor

- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire);**
- **bool begin(uint8_t sda, uint8_t scl, uint8_t count = 4)** ESP32 ea initializes the class
by setting the I2C sda and scl pins.
Expand All @@ -35,23 +44,27 @@ Returns true if the PCF8574 is on the I2C bus.
- **bool isConnected()** returns true if the PCF8574 is on the I2C bus.


## Core functions
#### Core functions

- **void readInitialState()** read the initial state of the 4 rotary encoders.
Typically called in setup only, or after a sleep e.g. in combination with **setValue()**
- **bool checkChange()** polling to see if one or more RE have changed, without updating the counters.
- **void update()** update the internal counters of the RE. These will add +1 or -1 depending on direction.
- **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.
- **bool checkChange()** polling to see if one or more RE have changed,
without updating the counters.
- **void update()** update the internal counters of the RE.
These will add +1 or -1 depending on direction.
- **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.


## Counters
#### Counters

- **uint32_t getValue(uint8_r re)** returns the RE counter.
- **void setValue(uint8_r re, uint32_t value = 0)** (re)set the internal counter to value, default 0
- **void setValue(uint8_r re, uint32_t value = 0)** (re)set the internal counter to value,
default 0


## Debugging
#### Debugging

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

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

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


| I2C speed | time (us) | delta | %% |
|:---------:|:---------:|:-----:|:-----:|
| 100 KHz | 247 | | |
| 200 KHz | 146 | 99 | 40% |
| 300 KHz | 110 | 36 | 24% |
| 400 KHz | 95 | 15 | 14% | preferred max
| 500 KHz | 84 | 11 | 12% |
| 600 KHz | 79 | 5 | 6% |
| 700 KHz | 73 | 6 | 8% |
| I2C speed | time (us) | delta | %% | Notes |
|:-----------:|:-----------:|:-------:|:-----:|:--------|
| 100 KHz | 247 | | |
| 200 KHz | 146 | 99 | 40% |
| 300 KHz | 110 | 36 | 24% |
| 400 KHz | 95 | 15 | 14% | preferred max
| 500 KHz | 84 | 11 | 12% |
| 600 KHz | 79 | 5 | 6% |
| 700 KHz | 73 | 6 | 8% |


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

## Future

#### must
#### Must

- update documentation
- picture how to connect e.g 2 RE's which pins to used
- picture how to connect e.g two rotary encoders which pins to used


#### Should

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

#### could

#### Could

- invert flag to adjust to RE that give their pulse just the other way around?
- setInvert(bool); getInvert();
- per channel / all?
- per channel / all?


#### Wont



## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,

4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/rotaryDecoder.git"
},
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"frameworks": "arduino",
"frameworks": "*",
"platforms": "*",
"headers": "rotaryDecoder.h"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=rotaryDecoder
version=0.1.3
version=0.1.4
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library to rotary decoder with a PCF8574
Expand Down
20 changes: 18 additions & 2 deletions rotaryDecoder.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: rotaryDecoder.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// DATE: 2021-05-08
// PURPOSE: rotary decoder library for Arduino
// URL: https://github.com/RobTillaart/rotaryDecoder
Expand All @@ -26,7 +26,6 @@ bool rotaryDecoder::begin(uint8_t sda, uint8_t scl, uint8_t count)
{
_count = count;
if (_count > 4) _count = 4;
_wire = &Wire;
_wire->begin(sda, scl);
if (! isConnected()) return false;
return true;
Expand Down Expand Up @@ -144,6 +143,23 @@ bool rotaryDecoder::updateSingle()
}


int32_t rotaryDecoder::getValue(uint8_t re)
{
return _encoder[re];
}


void rotaryDecoder::setValue(uint8_t re, int32_t value)
{
_encoder[re] = value;
}


uint8_t rotaryDecoder::getLastPosition(uint8_t re)
{
return _lastPos[re];
};

/////////////////////////////////////////////////////
//
// PRIVATE
Expand Down
14 changes: 7 additions & 7 deletions rotaryDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: rotaryDecoder.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// DATE: 2021-05-08
// PURPOSE: rotary decoder library for Arduino
// URL: https://github.com/RobTillaart/rotaryDecoder
Expand All @@ -11,7 +11,7 @@
#include "Arduino.h"
#include "Wire.h"

#define ROTARY_DECODER_LIB_VERSION (F("0.1.3"))
#define ROTARY_DECODER_LIB_VERSION (F("0.1.4"))


class rotaryDecoder
Expand All @@ -38,18 +38,18 @@ class rotaryDecoder
bool updateSingle(); // assumes single direction => + ++ +++

// re = rotary encoder
int32_t getValue(uint8_t re) { return _encoder[re]; };
void setValue(uint8_t re, int32_t value = 0) { _encoder[re] = value; };
int32_t getValue(uint8_t re);
void setValue(uint8_t re, int32_t value = 0);

// DEBUG
uint8_t getLastPosition(uint8_t re) { return _lastPos[re]; };
uint8_t getLastPosition(uint8_t re);


private:
uint8_t _count = 0;
uint8_t _lastValue = 0;
uint8_t _lastPos[4] = { 0,0,0,0 };
int32_t _encoder[4] = { 0,0,0,0 };
uint8_t _lastPos[4] = { 0, 0, 0, 0 };
int32_t _encoder[4] = { 0, 0, 0, 0 };

uint8_t _read8();
uint8_t _address;
Expand Down

0 comments on commit 5a63607

Please sign in to comment.