Skip to content

Commit 52f4407

Browse files
committed
add rotaryDecoder
1 parent b210c8b commit 52f4407

File tree

16 files changed

+750
-0
lines changed

16 files changed

+750
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
- leonardo
6+
- due
7+
- zero
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
arduino_ci:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: Arduino-CI/action@master
13+
# Arduino-CI/action@v0.1.1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

libraries/rotaryDecoder/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021-2021 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libraries/rotaryDecoder/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
[![Arduino CI](https://github.com/RobTillaart/rotaryDecoder/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/rotaryDecoder/blob/master/LICENSE)
4+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/rotaryDecoder.svg?maxAge=3600)](https://github.com/RobTillaart/rotaryDecoder/releases)
5+
6+
7+
# rotaryDecoder
8+
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+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// FILE: rotaryDecoder_demo_interrupt.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2021-05-08
5+
//
6+
// PUPROSE: demo
7+
8+
// connect up to 4 rotary encoders to 1 PCF8574.
9+
//
10+
// RotaryEncoder PCF8574 UNO
11+
// --------------------------------------
12+
// pin A pin 0
13+
// pin B pin 1
14+
// .... .... (up to 4 RE)
15+
//
16+
// SDA A4
17+
// SCL A5
18+
// INT 2
19+
//
20+
21+
22+
#include "Wire.h"
23+
#include "rotaryDecoder.h"
24+
25+
rotaryDecoder decoder(0x20);
26+
27+
volatile bool flag = false;
28+
29+
void moved()
30+
{
31+
flag = true;
32+
}
33+
34+
35+
void setup()
36+
{
37+
Serial.begin(115200);
38+
Serial.println(__FILE__);
39+
Serial.print("ROTARY_DECODER_LIB_VERSION:\t");
40+
Serial.println(ROTARY_DECODER_LIB_VERSION);
41+
42+
pinMode(2, INPUT_PULLUP);
43+
attachInterrupt(0, moved, FALLING);
44+
flag = false;
45+
46+
Wire.begin();
47+
Wire.setClock(100000);
48+
decoder.begin(4);
49+
decoder.readInitialState();
50+
}
51+
52+
53+
void loop()
54+
{
55+
if (flag)
56+
{
57+
decoder.update();
58+
flag = false;
59+
for (uint8_t i = 0; i < 4; i++)
60+
{
61+
Serial.print("\t");
62+
Serial.print(decoder.getValue(i));
63+
}
64+
Serial.println();
65+
}
66+
67+
// other tasks...
68+
}
69+
70+
71+
// -- END OF FILE --
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// FILE: rotaryDecoder_demo_polling.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2021-05-08
5+
//
6+
// PUPROSE: demo
7+
8+
// connect up to 4 rotary encoders to 1 PCF8574.
9+
//
10+
// RotaryEncoder PCF8574 UNO
11+
// --------------------------------------
12+
// pin A pin 0
13+
// pin B pin 1
14+
// .... .... (up to 4 RE)
15+
//
16+
// SDA A4
17+
// SCL A5
18+
//
19+
20+
#include "Wire.h"
21+
#include "rotaryDecoder.h"
22+
23+
rotaryDecoder decoder(0x20);
24+
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
Serial.println(__FILE__);
29+
Serial.print("ROTARY_DECODER_LIB_VERSION:\t");
30+
Serial.println(ROTARY_DECODER_LIB_VERSION);
31+
32+
Wire.begin();
33+
Wire.setClock(100000);
34+
decoder.begin(4);
35+
decoder.readInitialState();
36+
}
37+
38+
39+
void loop()
40+
{
41+
if (decoder.checkChange())
42+
{
43+
decoder.update();
44+
for (uint8_t i = 0; i < 4; i++)
45+
{
46+
Serial.print("\t");
47+
Serial.print(decoder.getValue(i));
48+
}
49+
Serial.println();
50+
}
51+
52+
// other tasks...
53+
}
54+
55+
56+
// -- END OF FILE --
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// FILE: rotaryDecoder_demo_simple.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2021-05-08
5+
//
6+
// PUPROSE: demo
7+
8+
// connect up to 4 rotary encoders to 1 PCF8574.
9+
//
10+
// RotaryEncoder PCF8574 UNO
11+
// --------------------------------------
12+
// pin A pin 0
13+
// pin B pin 1
14+
// .... .... (up to 4 RE)
15+
//
16+
// SDA A4
17+
// SCL A5
18+
//
19+
20+
#include "Wire.h"
21+
#include "rotaryDecoder.h"
22+
23+
rotaryDecoder decoder(0x20);
24+
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
Serial.println(__FILE__);
29+
Serial.print("ROTARY_DECODER_LIB_VERSION:\t");
30+
Serial.println(ROTARY_DECODER_LIB_VERSION);
31+
32+
Wire.begin();
33+
Wire.setClock(100000);
34+
decoder.begin(4);
35+
decoder.readInitialState();
36+
}
37+
38+
39+
void loop()
40+
{
41+
// if one of the counters is updated, print them.
42+
if (decoder.update())
43+
{
44+
for (uint8_t i = 0; i < 4; i++)
45+
{
46+
Serial.print("\t");
47+
Serial.print(decoder.getValue(i));
48+
}
49+
Serial.println();
50+
}
51+
52+
// other tasks...
53+
}
54+
55+
56+
// -- END OF FILE --
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// FILE: rotaryDecoder_demo_single.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2021-05-08
5+
//
6+
// PUPROSE: demo
7+
8+
// note this is used for e.g. RPM counters that are unidirectional.
9+
10+
// connect up to 4 rotary encoders to 1 PCF8574.
11+
//
12+
// RotaryEncoder PCF8574 UNO
13+
// --------------------------------------
14+
// pin A pin 0
15+
// pin B pin 1
16+
// .... .... (up to 4 RE)
17+
//
18+
// SDA A4
19+
// SCL A5
20+
//
21+
22+
#include "Wire.h"
23+
#include "rotaryDecoder.h"
24+
25+
rotaryDecoder decoder(0x20);
26+
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
Serial.println(__FILE__);
31+
Serial.print("ROTARY_DECODER_LIB_VERSION:\t");
32+
Serial.println(ROTARY_DECODER_LIB_VERSION);
33+
34+
Wire.begin();
35+
Wire.setClock(100000);
36+
decoder.begin(4);
37+
decoder.readInitialState();
38+
}
39+
40+
41+
void loop()
42+
{
43+
// if one of the counters is updated, print them.
44+
if (decoder.updateSingle()) // note values will only go up!
45+
{
46+
for (uint8_t i = 0; i < 4; i++)
47+
{
48+
Serial.print("\t");
49+
Serial.print(decoder.getValue(i));
50+
}
51+
Serial.println();
52+
}
53+
54+
// other tasks...
55+
}
56+
57+
58+
// -- END OF FILE --

0 commit comments

Comments
 (0)