Skip to content

Commit ea5c3e0

Browse files
authored
refactor interface + update readme.md (#13)
- changed **uint8_t readInitialState()** to return the read state. - changed **bool setValue(uint8_t re, int32_t value = 0)** to return false if parameter re is out of range, prevent possible bug. - changed **int32_t getValue(uint8_t re)** to return 0 if parameter re is out of range, prevent possible bug. - add example **rotaryDecoder_demo_RE_IO.ino** - update readme.md, interface section. - minor edits
1 parent ca3f4e4 commit ea5c3e0

File tree

7 files changed

+138
-23
lines changed

7 files changed

+138
-23
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ 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.3.1] - 2024-06-20
10+
- changed **uint8_t readInitialState()** to return the read state.
11+
- changed **bool setValue(uint8_t re, int32_t value = 0)** to return false
12+
if parameter re is out of range, prevent possible bug.
13+
- changed **int32_t getValue(uint8_t re)** to return 0
14+
if parameter re is out of range, prevent possible bug.
15+
- add example **rotaryDecoder_demo_RE_IO.ino**
16+
- update readme.md, interface section.
17+
- minor edits
18+
919
## [0.3.0] - 2024-02-14
10-
- Fix #10
20+
- Fix #10, add read and write for free IO pins.
1121
- add **read1(pin)**
1222
- add **write1(pin, value)** experimental see #10
1323
- made **read8()** and **write8()** public for faster multi pin access

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ pins to GND so you will not get unintended interrupts.
3939

4040
#### Constructor
4141

42-
- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire);**
42+
- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire)**
43+
constructor to set the address and optional the Wire bus.
4344
- **bool begin(uint8_t count = 4)** UNO ea. initializes the class.
4445
count is the number of rotary encoders connected. (Max 4 per PCF8574)
4546
Returns true if the PCF8574 is on the I2C bus.
@@ -48,23 +49,31 @@ Returns true if the PCF8574 is on the I2C bus.
4849

4950
#### Core functions
5051

51-
- **void readInitialState()** read the initial state of the 4 rotary encoders.
52-
Typically called in setup only, or after a sleep e.g. in combination with **setValue()**
53-
- **bool checkChange()** polling to see if one or more RE have changed,
54-
without updating the internal counters.
55-
- **void update()** update the internal counters of the RE.
52+
- **uint8_t readInitialState()** read the initial state of the 4 rotary encoders.
53+
Typically called in setup only, or after a sleep e.g. in combination with **setValue()**.
54+
Since 0.3.1 this function returns the read state, saves an additional read8() call.
55+
- **bool checkChange()** used for polling to see if one or more RE have changed.
56+
This function does NOT update the internal counters.
57+
- **bool update()** returns true if there is a change detected.
58+
It updates the internal counters of the RE.
5659
The counters will add +1 or -1 depending on rotation direction.
5760
Need to be called before **getValue()** or before **getKeyPressed()**.
58-
Note that **update()** must be called as soon as possible after the interrupt occurs (or as often as possible when polling).
59-
- **void updateSingle()** update the internal counters of the RE.
60-
This will add +1 +2 or +3 as it assumes that the rotary encoder
61-
only goes into a single direction.
61+
Note that **update()** must be called as soon as possible after the interrupt occurs
62+
or as often as possible when polling.
63+
Returns false if there is no change since last read.
64+
- **bool updateSingle()** returns true if there is a change detected.
65+
It updates the internal counters of the RE.
66+
This will add +1, +2 or +3 as it assumes that the rotary encoder
67+
only goes into a single direction.
68+
Returns false if there is no change since last read.
6269

6370

6471
#### Counters
6572

66-
- **uint32_t getValue(uint8_r re)** returns the RE counter.
67-
- **void setValue(uint8_r re, uint32_t value = 0)** (re)set the internal counter to value, default 0
73+
- **int32_t getValue(uint8_r re)** returns the RE counter.
74+
If the parameter re > 3 then 0 is returned.
75+
- **bool setValue(uint8_r re, int32_t value = 0)** (re)set the internal counter to value, default 0.
76+
If the parameter re > 3 then false is returned, true otherwise.
6877

6978

7079
#### Read1 - Write1 - experimental
@@ -73,6 +82,7 @@ Warning the **write1(pin, value)** might alter the state of the rotary encoder p
7382
So this functionality should be tested thoroughly for your application.
7483
Especially the **write()** is **experimental**, see issue #10, feedback welcome.
7584

85+
See example **rotaryDecoder_demo_RE_IO.ino** (since 0.3.1).
7686

7787
**Read1()** and **write1()** are functions to access the pins of the PCF8574 that
7888
are not used for rotary encoders.
@@ -98,7 +108,7 @@ rotary encoder pins.
98108

99109
As the decoder is based upon a PCF8574, a I2C device, the performance is affected by the
100110
clock speed of the I2C bus.
101-
All four core functions have one call to **\read8()** which is the most expensive part.
111+
All four core functions have one call to **read8()** which is the most expensive part.
102112

103113
Early tests gave the following indicative times (Arduino UNO) for the **update()**
104114
function (with no updates it is ~8 us faster).
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// FILE: rotaryDecoder_demo_RE_IO.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2024-02-13
5+
// PURPOSE: demo
6+
// URL: https://github.com/RobTillaart/rotaryDecoder
7+
//
8+
// example configuration
9+
// connect one rotary encoders
10+
// connect multiple switches
11+
// connect one line to the other end of the switches to enable them
12+
// connect a buzzer
13+
//
14+
// RotaryEncoder PCF8574 UNO
15+
// --------------------------------------
16+
// pin A pin 0
17+
// pin B pin 1
18+
// switch pin 2
19+
// switch pin 3
20+
// switch pin 4
21+
// enable line pin 5
22+
// buzzer pin 6
23+
// switch pin 7
24+
//
25+
// SDA A4
26+
// SCL A5
27+
//
28+
29+
30+
#include "rotaryDecoder.h"
31+
32+
rotaryDecoder decoder(0x39); // 0x39 = 57
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+
Wire.begin();
43+
Wire.setClock(100000);
44+
45+
// only one rotary encoder
46+
decoder.begin(1);
47+
decoder.readInitialState();
48+
49+
// other lines are switches (INPUT)
50+
decoder.write1(2, LOW);
51+
decoder.write1(3, LOW);
52+
decoder.write1(4, LOW);
53+
decoder.write1(7, LOW);
54+
55+
// enable/disable switches
56+
// HIGH == switches enabled
57+
decoder.write1(5, HIGH);
58+
59+
// line 6 == buzzer
60+
decoder.write1(6, LOW);
61+
}
62+
63+
64+
void loop()
65+
{
66+
// if one of the lines is updated, print them.
67+
if (decoder.update())
68+
{
69+
Serial.print("\t");
70+
Serial.print(decoder.getValue(0));
71+
Serial.print("\t");
72+
Serial.print(decoder.read1(2));
73+
Serial.print("\t");
74+
Serial.print(decoder.read1(3));
75+
Serial.print("\t");
76+
Serial.print(decoder.read1(4));
77+
Serial.print("\t");
78+
Serial.print(decoder.read1(5));
79+
Serial.print("\t");
80+
Serial.print(decoder.read1(6));
81+
Serial.print("\t");
82+
Serial.print(decoder.read1(7));
83+
Serial.println();
84+
}
85+
86+
// other tasks...
87+
}
88+
89+
90+
// -- END OF FILE --

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/rotaryDecoder.git"
1717
},
18-
"version": "0.3.0",
18+
"version": "0.3.1",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

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.3.0
2+
version=0.3.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for rotary decoder with a PCF8574.

rotaryDecoder.cpp

Lines changed: 7 additions & 3 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.3.0
4+
// VERSION: 0.3.1
55
// DATE: 2021-05-08
66
// PURPOSE: Arduino library for rotary decoder
77
// URL: https://github.com/RobTillaart/rotaryDecoder
@@ -38,7 +38,7 @@ bool rotaryDecoder::isConnected()
3838
}
3939

4040

41-
void rotaryDecoder::readInitialState()
41+
uint8_t rotaryDecoder::readInitialState()
4242
{
4343
uint8_t value = read8();
4444
_lastValue = value;
@@ -47,6 +47,7 @@ void rotaryDecoder::readInitialState()
4747
_lastPos[i] = value & 0x03;
4848
value >>= 2;
4949
}
50+
return _lastValue;
5051
}
5152

5253

@@ -133,13 +134,16 @@ bool rotaryDecoder::updateSingle()
133134

134135
int32_t rotaryDecoder::getValue(uint8_t re)
135136
{
137+
if (re > 3) return 0;
136138
return _encoder[re];
137139
}
138140

139141

140-
void rotaryDecoder::setValue(uint8_t re, int32_t value)
142+
bool rotaryDecoder::setValue(uint8_t re, int32_t value)
141143
{
144+
if (re > 3) return false;
142145
_encoder[re] = value;
146+
return true;
143147
}
144148

145149

rotaryDecoder.h

Lines changed: 5 additions & 4 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.3.0
5+
// VERSION: 0.3.1
66
// DATE: 2021-05-08
77
// PURPOSE: Arduino library for rotary decoder
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.3.0"))
14+
#define ROTARY_DECODER_LIB_VERSION (F("0.3.1"))
1515

1616

1717
class rotaryDecoder
@@ -22,7 +22,7 @@ class rotaryDecoder
2222
bool begin(uint8_t count = 4);
2323
bool isConnected();
2424

25-
void readInitialState();
25+
uint8_t readInitialState();
2626

2727
// for polling version,
2828
// checkChange is bit faster than a call to update
@@ -34,8 +34,9 @@ class rotaryDecoder
3434
bool updateSingle(); // assumes single direction => + ++ +++
3535

3636
// re = rotary encoder
37+
// returns 0, false if re > 3.
3738
int32_t getValue(uint8_t re);
38-
void setValue(uint8_t re, int32_t value = 0);
39+
bool setValue(uint8_t re, int32_t value = 0);
3940

4041

4142
// READ - WRITE interface

0 commit comments

Comments
 (0)