diff --git a/libraries/PCF8575/CHANGELOG.md b/libraries/PCF8575/CHANGELOG.md index 4b7cd33e..2fac40bc 100644 --- a/libraries/PCF8575/CHANGELOG.md +++ b/libraries/PCF8575/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.6] - 2022-06-18 + +### Added +- add select(), selectN(), selectAll(), selectNone() +- update documentation + + ## [0.1.5] - 2022-04-11 ### Added diff --git a/libraries/PCF8575/PCF8575.cpp b/libraries/PCF8575/PCF8575.cpp index 75f69ddc..8d059dfe 100644 --- a/libraries/PCF8575/PCF8575.cpp +++ b/libraries/PCF8575/PCF8575.cpp @@ -2,7 +2,7 @@ // FILE: PCF8575.cpp // AUTHOR: Rob Tillaart // DATE: 2020-07-20 -// VERSION: 0.1.5 +// VERSION: 0.1.6 // PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander // URL: https://github.com/RobTillaart/PCF8575 // @@ -221,5 +221,22 @@ uint8_t PCF8575::readButton(const uint8_t pin) } +void PCF8575::select(const uint8_t pin) +{ + uint16_t n = 0x0000; + if (pin < 16) n = 1L << pin; + write16(n); +}; + + +void PCF8575::selectN(const uint8_t pin) +{ + uint16_t n = 0xFFFF; + if (pin < 16) n = (2L << pin) - 1; + write16(n); +}; + + + // -- END OF FILE -- diff --git a/libraries/PCF8575/PCF8575.h b/libraries/PCF8575/PCF8575.h index 5667a447..d595f692 100644 --- a/libraries/PCF8575/PCF8575.h +++ b/libraries/PCF8575/PCF8575.h @@ -3,7 +3,7 @@ // FILE: PCF8575.h // AUTHOR: Rob Tillaart // DATE: 2020-07-20 -// VERSION: 0.1.5 +// VERSION: 0.1.6 // PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander // URL: https://github.com/RobTillaart/PCF8575 // @@ -13,7 +13,7 @@ #include "Wire.h" -#define PCF8575_LIB_VERSION (F("0.1.5")) +#define PCF8575_LIB_VERSION (F("0.1.6")) #ifndef PCF8575_INITIAL_VALUE @@ -72,6 +72,12 @@ public: void reverse(); + void select(const uint8_t pin); + void selectN(const uint8_t pin); + void selectNone() { write16(0x0000); }; + void selectAll() { write16(0xFFFF); }; + + int lastError(); diff --git a/libraries/PCF8575/README.md b/libraries/PCF8575/README.md index 0abc7eab..7dafa324 100644 --- a/libraries/PCF8575/README.md +++ b/libraries/PCF8575/README.md @@ -41,6 +41,8 @@ Testing showed that the PCF8575 still works at 600 KHz and failed at 800 KHz. These values are outside the specs of the datasheet so they are not recommended. However when performance is needed you can try to overclock the chip. +TODO table (see PCF8574) + ## Interface @@ -88,19 +90,35 @@ during program execution. Note this can be a subset of the pins set with **setButtonMask()** if one wants to process not all. - **uint8_t readButton(uint8_t pin)** read a singe input pin. +Background - https://github.com/RobTillaart/Arduino/issues/38 + ### Special - **void toggle(uint8_t pin)** toggles a single pin. - **void toggleMask(uint16_t mask)** toggles a selection of pins, if you want to invert all pins use 0xFFFF (default value). -- **void shiftRight(uint8_t n = 1)** shifts output channels n pins (default 1) pins right (e.g. leds ). +- **void shiftRight(uint8_t n = 1)** shifts output channels n pins (default 1) pins right (e.g. LEDs ). Fills the higher lines with zero's. -- **void shiftLeft(uint8_t n = 1)** shifts output channels n pins (default 1) pins left (e.g. leds ). +- **void shiftLeft(uint8_t n = 1)** shifts output channels n pins (default 1) pins left (e.g. LEDs ). Fills the lower lines with zero's. - **void rotateRight(uint8_t n = 1)** rotates output channels to right, moving lowest line to highest line. - **void rotateLeft(uint8_t n = 1)** rotates output channels to left, moving highest line to lowest line. -- **void reverse()** reverse the "bit pattern" of the lines, high to low and vice versa. +- **void reverse()** reverse the "bit pattern" of the lines, swapping pin 15 with 0, 14 with 1, 13 with 2 etc.. + + +### Select + +Some convenience wrappers. + +- **void select(const uint8_t pin)** sets a single pin to HIGH, all others are set to LOW. +If pin > 15 all pins are set to LOW. +Can be used to select one of n devices. +- **void selectN(const uint8_t pin)** sets pins 0..pin to HIGH, all others are set to LOW. +If pin > 15 all pins are set to LOW. +This can typical be used to implement a VU meter. +- **void selectNone()** sets all pins to LOW. +- **void selectAll()** sets all pins to HIGH. ### Miscellaneous @@ -112,9 +130,9 @@ Fills the lower lines with zero's. | name | value | description | |:-------------------|:-----:|:------------------------| -| PCF8574_OK | 0x00 | no error | -| PCF8574_PIN_ERROR | 0x81 | pin number out of range | -| PCF8574_I2C_ERROR | 0x82 | I2C communication error | +| PCF8575_OK | 0x00 | no error | +| PCF8575_PIN_ERROR | 0x81 | pin number out of range | +| PCF8575_I2C_ERROR | 0x82 | I2C communication error | ## Testing @@ -127,6 +145,8 @@ Platforms used for testing include: Nano, ESP32 and Seeed Xiao. See examples. +It is advised to use pull-up or pull-down resistors so the lines have a defined state at startup. + ## Future diff --git a/libraries/PCF8575/examples/PCF8575_select/PCF8575_select.ino b/libraries/PCF8575/examples/PCF8575_select/PCF8575_select.ino new file mode 100644 index 00000000..391e7702 --- /dev/null +++ b/libraries/PCF8575/examples/PCF8575_select/PCF8575_select.ino @@ -0,0 +1,64 @@ +// +// FILE: PCF8575_select.ino +// AUTHOR: Rob Tillaart +// DATE: 2022-06-18 +// PUPROSE: demo PCF8575 library select functions + + + +#include "PCF8575.h" + +PCF8575 PCF(0x38); + +uint32_t start, stop; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("PCF8575_LIB_VERSION:\t"); + Serial.println(PCF8575_LIB_VERSION); + + PCF.begin(); + Serial.println(PCF.isConnected()); + Serial.println(); + + PCF.selectAll(); + delay(1000); + PCF.selectNone(); + delay(1000); + + // VU meter up + for (int i = 0; i < 15; i++) + { + PCF.selectN(i); + delay(100); + } + + // VU meter down + for (int i = 15; i >= 0; i--) + { + PCF.selectN(i); + delay(100); + } +} + + +void loop() +{ + // night rider + for (int i = 0; i < 15; i++) + { + PCF.select(i); + delay(100); + } + for (int i = 15; i >= 0; i--) + { + PCF.select(i); + delay(100); + } +} + + +// -- END OF FILE -- diff --git a/libraries/PCF8575/keywords.txt b/libraries/PCF8575/keywords.txt index 8ccbe434..bcbb14ec 100644 --- a/libraries/PCF8575/keywords.txt +++ b/libraries/PCF8575/keywords.txt @@ -1,4 +1,4 @@ -# Syntax Colouring Map For PCF8575 +# Syntax Colouring Map for PCF8575 # Data types (KEYWORD1) @@ -32,7 +32,10 @@ rotateRight KEYWORD2 rotateLeft KEYWORD2 reverse KEYWORD2 -lastError KEYWORD2 +select KEYWORD2 +selectN KEYWORD2 +selectNone KEYWORD2 +selectAll KEYWORD2 # Constants (LITERAL1) diff --git a/libraries/PCF8575/library.json b/libraries/PCF8575/library.json index d9ebaffa..503e5519 100644 --- a/libraries/PCF8575/library.json +++ b/libraries/PCF8575/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/PCF8575.git" }, - "version": "0.1.5", + "version": "0.1.6", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/PCF8575/library.properties b/libraries/PCF8575/library.properties index 17940cc9..b439f4ec 100644 --- a/libraries/PCF8575/library.properties +++ b/libraries/PCF8575/library.properties @@ -1,5 +1,5 @@ name=PCF8575 -version=0.1.5 +version=0.1.6 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for PCF8575 - 16 channel I2C IO expander