0.1.4 PCF8575

This commit is contained in:
rob tillaart 2021-12-23 13:53:35 +01:00
parent 19952ad1ae
commit a78374791f
16 changed files with 153 additions and 43 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020-2021 Rob Tillaart
Copyright (c) 2020-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -2,7 +2,7 @@
// FILE: PCF8575.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-20
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8575
//
@ -15,6 +15,7 @@
// 0.1.2 2021-07-09 fix #10 add set/getAddress() function
// 0.1.3 2021-12-01 update build-CI, readme
// add getButtonMask()
// 0.1.4 2021-12-23 update library.json, license, minor edits
#include "PCF8575.h"
@ -32,7 +33,7 @@ PCF8575::PCF8575(const uint8_t deviceAddress, TwoWire *wire)
#if defined (ESP8266) || defined(ESP32)
bool PCF8575::begin(uint8_t dataPin, uint8_t clockPin, uint16_t val)
bool PCF8575::begin(uint8_t dataPin, uint8_t clockPin, uint16_t value)
{
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
@ -42,17 +43,17 @@ bool PCF8575::begin(uint8_t dataPin, uint8_t clockPin, uint16_t val)
_wire->begin();
}
if (! isConnected()) return false;
PCF8575::write16(val);
PCF8575::write16(value);
return true;
}
#endif
bool PCF8575::begin(uint16_t val)
bool PCF8575::begin(uint16_t value)
{
_wire->begin();
if (! isConnected()) return false;
PCF8575::write16(val);
PCF8575::write16(value);
return true;
}
@ -152,7 +153,7 @@ void PCF8575::toggleMask(const uint16_t mask)
void PCF8575::shiftRight(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 15) _dataOut = 0; // shift 8++ clears all, valid...
if (n > 15) _dataOut = 0; // shift 15++ clears all, valid...
if (_dataOut != 0) _dataOut >>= n; // only shift if there are bits set
PCF8575::write16(_dataOut);
}
@ -161,7 +162,7 @@ void PCF8575::shiftRight(const uint8_t n)
void PCF8575::shiftLeft(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 15) _dataOut = 0; // shift 8++ clears all, valid...
if (n > 15) _dataOut = 0; // shift 15++ clears all, valid...
if (_dataOut != 0) _dataOut <<= n; // only shift if there are bits set
PCF8575::write16(_dataOut);
}
@ -229,3 +230,4 @@ uint8_t PCF8575::readButton(const uint8_t pin)
// -- END OF FILE --

View File

@ -3,7 +3,7 @@
// FILE: PCF8575.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-20
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8575
//
@ -16,7 +16,7 @@
#include "Wire.h"
#define PCF8575_LIB_VERSION (F("0.1.3"))
#define PCF8575_LIB_VERSION (F("0.1.4"))
#ifndef PCF8575_INITIAL_VALUE
@ -32,12 +32,12 @@ class PCF8575
{
public:
// deviceAddress base = 0x20 + depends on address bits
explicit PCF8575(const uint8_t deviceAddress, TwoWire *wire = &Wire);
explicit PCF8575(const uint8_t deviceAddress = 0x20, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl, uint16_t val = PCF8575_INITIAL_VALUE);
bool begin(uint8_t sda, uint8_t scl, uint16_t value = PCF8575_INITIAL_VALUE);
#endif
bool begin(uint16_t val = PCF8575_INITIAL_VALUE);
bool begin(uint16_t value = PCF8575_INITIAL_VALUE);
bool isConnected();
@ -90,3 +90,4 @@ private:
// -- END OF FILE --

View File

@ -51,10 +51,10 @@ the include of "pcf8575.h" to overrule the default value used with the
### Constructor
- **PCF8575(uint8_t deviceAddress, TwoWire \*wire = &Wire)** Constructor with I2C device address,
and optional the Wire interface as parameter.
- **bool begin(uint8_t val = PCF8575_INITIAL_VALUE)** set the initial value for the pins and masks.
- **bool begin(uint8_t sda, uint8_t scl, uint8_t val = PCF8575_INITIAL_VALUE)** idem,
- **PCF8575(uint8_t deviceAddress = 0x20, TwoWire \*wire = &Wire)** Constructor with the optional
I2C device address, default 0x20, and the optional Wire interface as parameter.
- **bool begin(uint8_t value = PCF8575_INITIAL_VALUE)** set the initial value for the pins and masks.
- **bool begin(uint8_t dataPin, uint8_t clockPin, uint8_t value = PCF8575_INITIAL_VALUE)** idem,
for the ESP32 where one can choose the I2C pins.
- **bool isConnected()** checks if the address is visible on the I2C bus.
- **bool setAddress(const uint8_t deviceAddress)** sets the device address after construction.
@ -132,3 +132,5 @@ See examples.
- update documentation.
- keep in sync with pcf8574 (as far as meaningful)
- **value()** => **lastRead16()** ??

View File

@ -2,15 +2,15 @@
// FILE: PCF8575_Wire2.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
//
// PUPROSE: demo
//
// PUPROSE: demo
#include "PCF8575.h"
// adjust addresses if needed
PCF8575 PCF(0x39, &Wire2);
void setup()
{
Serial.begin(115200);
@ -34,6 +34,7 @@ void setup()
delay(1000);
}
void loop()
{
Serial.println("HLT");
@ -46,6 +47,7 @@ void loop()
}
}
void doHigh()
{
PCF.write(4, HIGH);
@ -54,6 +56,7 @@ void doHigh()
Serial.println(x, HEX);
}
void doLow()
{
PCF.write(4, LOW);
@ -62,6 +65,7 @@ void doLow()
Serial.println(x, HEX);
}
void doToggle()
{
PCF.toggle(4);
@ -70,4 +74,6 @@ void doToggle()
Serial.println(x, HEX);
}
// -- END OF FILE --

View File

@ -0,0 +1,56 @@
//
// FILE: PCF8575_array.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-12-13
// PUPROSE: demo array of PCF - not tested
#include "PCF8575.h"
// adjust addresses if needed
PCF8575 A(0x38);
PCF8575 B(0x39);
PCF8575 C(0x3A);
PCF8575 PCF[3] = { A, B, C };
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);
for (int i = 0; i < 3; i++)
{
PCF[i].begin();
}
}
void loop()
{
for (int i = 0; i < 3; i++)
{
for (uint8_t port = 0; port < 15; port++)
{
PCF[i].write(port, 1);
delay(200);
}
}
for (int i = 0; i < 3; i++)
{
for (uint8_t port = 0; port < 15; port++)
{
PCF[i].write(port, 0);
delay(400);
}
}
}
// -- END OF FILE --

View File

@ -4,18 +4,19 @@
// DATE: 2021-01-03
// PUPROSE: test PCF8575 library
//
// TEST SETUP
// Connect INT pin of the PCF8575 to UNO pin 2
// Connect INT pin of the PCF8575 to UNO pin 2
//
// (from figure 4 datasheet
// Place a pull up resistor 4K7 between pin and 5V
// Place a capacitor 10-400pF between pin and GND
// (from figure 4 datasheet
// Place a pull up resistor 4K7 between pin and 5V
// Place a capacitor 10-400 pF between pin and GND
#include "PCF8575.h"
PCF8575 PCF(0x38);
////////////////////////////////////
//
// INTERRUPT ROUTINE + FLAG
@ -47,6 +48,7 @@ void setup()
attachInterrupt(digitalPinToInterrupt(IRQPIN), pcf_irq, FALLING);
}
void loop()
{
uint32_t now = millis();
@ -64,4 +66,6 @@ void loop()
delay(10);
}
// -- END OF FILE --

View File

@ -2,15 +2,15 @@
// FILE: PCF8575_isConnected.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
//
// PUPROSE: demo
//
// PUPROSE: demo device detection
#include "PCF8575.h"
// adjust addresses if needed
PCF8575 PCF(0x39);
void setup()
{
Serial.begin(115200);
@ -32,8 +32,11 @@ void setup()
}
}
void loop()
{
}
// -- END OF FILE --

View File

@ -2,9 +2,9 @@
// FILE: PCF8575_performance.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-24
//
// PUPROSE: test PCF8575 library
#include "PCF8575.h"
PCF8575 PCF(0x38);
@ -13,6 +13,7 @@ uint32_t start, stop;
volatile uint16_t x;
void setup()
{
Serial.begin(115200);
@ -41,10 +42,13 @@ void setup()
Serial.println(stop - start);
delay(1000);
}
}
void loop()
{
}
// -- END OF FILE --

View File

@ -5,17 +5,19 @@
// PUPROSE: test PCF8575 library
// URL: https://github.com/RobTillaart/PCF8575
#include "PCF8575.h"
PCF8575 PCF(0x38);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8575_test version: ");
Serial.println(PCF8575_LIB_VERSION);
PCF.begin();
uint16_t x = PCF.read16();
@ -24,6 +26,7 @@ void setup()
delay(1000);
}
void loop()
{
Serial.println("HLT");
@ -36,6 +39,7 @@ void loop()
}
}
void doHigh()
{
PCF.write(4, HIGH);
@ -44,6 +48,7 @@ void doHigh()
printHex(x);
}
void doLow()
{
PCF.write(4, LOW);
@ -52,6 +57,7 @@ void doLow()
printHex(x);
}
void doToggle()
{
PCF.toggle(4);
@ -69,4 +75,6 @@ void printHex(uint16_t x)
Serial.println(x, HEX);
}
// -- END OF FILE --

View File

@ -2,9 +2,8 @@
// FILE: pcf8575_test.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
//
// PUPROSE: demo
//
#include "PCF8575.h"
@ -12,6 +11,7 @@
PCF8575 PCF_38(0x38); // add switches to lines (used as input)
PCF8575 PCF_39(0x39); // add leds to lines (used as output)
void setup()
{
Serial.begin(115200);
@ -62,6 +62,7 @@ void setup()
}
}
void loop()
{
// echos the lines
@ -70,4 +71,6 @@ void loop()
delay(100);
}
// END OF FILE
// -- END OF FILE --

View File

@ -2,14 +2,14 @@
// FILE: pcf8575_test2.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
//
// PUPROSE: demo rotateLeft, -Right and toggleMask
//
#include "PCF8575.h"
// adjust addresses if needed
PCF8575 PCF(0x39); // add leds to lines (used as output)
PCF8575 PCF(0x39); // add LEDs to lines (used as output)
void setup()
{
@ -61,8 +61,11 @@ void setup()
}
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,8 +1,10 @@
# Syntax Colouring Map For PCF8575
# Data types (KEYWORD1)
PCF8575 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
@ -32,5 +34,11 @@ reverse KEYWORD2
lastError KEYWORD2
# Constants (LITERAL1)
# Constants (LITERAL1)
PCF8575_LIB_VERSION LITERAL1
PCF8575_INITIAL_VALUE LITERAL1
PCF8575_OK LITERAL1
PCF8575_PIN_ERROR LITERAL1
PCF8575_I2C_ERROR LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/PCF8575.git"
},
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=PCF8575
version=0.1.3
version=0.1.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for PCF8575 - 16 channel I2C IO expander

View File

@ -42,16 +42,26 @@ PCF8575 PCF(0x38);
unittest_setup()
{
fprintf(stderr, "PCF8575_LIB_VERSION: %s\n", (char *) PCF8575_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constants)
{
assertEqual(PCF8575_INITIAL_VALUE, 0xFFFF);
assertEqual(PCF8575_OK , 0x00);
assertEqual(PCF8575_PIN_ERROR , 0x81);
assertEqual(PCF8575_I2C_ERROR , 0x82);
}
unittest(test_begin)
{
fprintf(stderr, "VERSION: %s\n", PCF8575_LIB_VERSION);
PCF8575 PCF(0x38);
PCF.begin();