Skip to content

Commit

Permalink
refactor interface + update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jun 20, 2024
1 parent ca3f4e4 commit 6ed339f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.1] - 2024-06-20
-
- add example **rotaryDecoder_demo_RE_IO.ino**
- update readme.md, interface section.
- minor edits

## [0.3.0] - 2024-02-14
- Fix #10
- Fix #10, add read and write for free IO pins.
- add **read1(pin)**
- add **write1(pin, value)** experimental see #10
- made **read8()** and **write8()** public for faster multi pin access
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pins to GND so you will not get unintended interrupts.

#### Constructor

- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire);**
- **rotaryDecoder(const int8_t address, TwoWire \*wire = Wire)**
- **bool begin(uint8_t count = 4)** UNO ea. initializes the class.
count is the number of rotary encoders connected. (Max 4 per PCF8574)
Returns true if the PCF8574 is on the I2C bus.
Expand All @@ -52,19 +52,22 @@ Returns true if the PCF8574 is on the I2C bus.
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 internal counters.
- **void update()** update the internal counters of the RE.
- **bool update()** update the internal counters of the RE.
The counters will add +1 or -1 depending on rotation direction.
Need to be called before **getValue()** or before **getKeyPressed()**.
Note that **update()** must be called as soon as possible after the interrupt occurs (or as often as possible when polling).
- **void updateSingle()** update the internal counters of the RE.
Note that **update()** must be called as soon as possible after the interrupt occurs
or as often as possible when polling.
Returns false if there is no change since last read.
- **bool 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.
only goes into a single direction.
Returns false if there is no change since last read.


#### 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
- **int32_t getValue(uint8_r re)** returns the RE counter.
- **void setValue(uint8_r re, int32_t value = 0)** (re)set the internal counter to value, default 0


#### Read1 - Write1 - experimental
Expand Down Expand Up @@ -98,7 +101,7 @@ rotary encoder pins.

As the decoder is based upon a PCF8574, a I2C device, the performance is affected by the
clock speed of the I2C bus.
All four core functions have one call to **\read8()** which is the most expensive part.
All four core functions have one call to **read8()** which 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).
Expand Down
90 changes: 90 additions & 0 deletions examples/rotaryDecoder_demo_RE_IO/rotaryDecoder_demo_RE_IO.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// FILE: rotaryDecoder_demo_RE_IO.ino
// AUTHOR: Rob Tillaart
// DATE: 2024-02-13
// PURPOSE: demo
// URL: https://github.com/RobTillaart/rotaryDecoder
//
// example configuration
// connect one rotary encoders
// connect multiple switches
// connect one line to the other end of the switches to enable them
// connect a buzzer
//
// RotaryEncoder PCF8574 UNO
// --------------------------------------
// pin A pin 0
// pin B pin 1
// switch pin 2
// switch pin 3
// switch pin 4
// enable line pin 5
// buzzer pin 6
// switch pin 7
//
// SDA A4
// SCL A5
//


#include "rotaryDecoder.h"

rotaryDecoder decoder(0x39); // 0x39 = 57


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("ROTARY_DECODER_LIB_VERSION:\t");
Serial.println(ROTARY_DECODER_LIB_VERSION);

Wire.begin();
Wire.setClock(100000);

// only one rotary encoder
decoder.begin(1);
decoder.readInitialState();

// other lines are switches (INPUT)
decoder.write1(2, LOW);
decoder.write1(3, LOW);
decoder.write1(4, LOW);
decoder.write1(7, LOW);

// enable/disable switches
// HIGH == switches enabled
decoder.write1(5, HIGH);

// line 6 == buzzer
decoder.write1(6, LOW);
}


void loop()
{
// if one of the lines is updated, print them.
if (decoder.update())
{
Serial.print("\t");
Serial.print(decoder.getValue(0));
Serial.print("\t");
Serial.print(decoder.read1(2));
Serial.print("\t");
Serial.print(decoder.read1(3));
Serial.print("\t");
Serial.print(decoder.read1(4));
Serial.print("\t");
Serial.print(decoder.read1(5));
Serial.print("\t");
Serial.print(decoder.read1(6));
Serial.print("\t");
Serial.print(decoder.read1(7));
Serial.println();
}

// other tasks...
}


// -- END OF FILE --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/rotaryDecoder.git"
},
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
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.3.0
version=0.3.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for rotary decoder with a PCF8574.
Expand Down

0 comments on commit 6ed339f

Please sign in to comment.