Skip to content

Commit

Permalink
Arduino ci (#3)
Browse files Browse the repository at this point in the history
* initial arduino-ci
* add unit test
* refactor (to get tests running)
  • Loading branch information
RobTillaart authored Dec 7, 2020
1 parent 61daac7 commit 7199c76
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 149 deletions.
7 changes: 7 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
13 changes: 13 additions & 0 deletions .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: Arduino CI

on: [push, pull_request]

jobs:
arduino_ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/[email protected]
161 changes: 82 additions & 79 deletions PCF8574.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// FILE: PCF8574.cpp
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.2.1
// VERSION: 0.2.2
// PURPOSE: Arduino library for PCF8574 - I2C IO expander
// URL: https://github.com/RobTillaart/PCF8574
// http://forum.arduino.cc/index.php?topic=184800
//
// HISTORY:
// 0.2.2 2020-12-07 add Arduino-ci + start unit test + Wire.h in PCF8574.h
// 0.2.1 2020-06-19 fix library.json
// 0.2.0 2020-05-22 #pragma once; refactor;
// removed pre 1.0 support
Expand Down Expand Up @@ -40,16 +41,14 @@

#include "PCF8574.h"

#include <Wire.h>


PCF8574::PCF8574(const uint8_t deviceAddress)
{
_address = deviceAddress;
_dataIn = 0;
_dataOut = 0xFF;
_buttonMask = 0xFF;
_error = PCF8574_OK;
_address = deviceAddress;
_dataIn = 0;
_dataOut = 0xFF;
_buttonMask = 0xFF;
_error = PCF8574_OK;
}

#if defined (ESP8266) || defined(ESP32)
Expand All @@ -73,102 +72,103 @@ void PCF8574::begin(uint8_t val)
// TODO @800KHz -> ??
uint8_t PCF8574::read8()
{
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
{
_error = PCF8574_I2C_ERROR;
return _dataIn; // last value
}
_dataIn = Wire.read();
return _dataIn;
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
{
_error = PCF8574_I2C_ERROR;
return _dataIn; // last value
}
_dataIn = Wire.read();
return _dataIn;
}

void PCF8574::write8(const uint8_t value)
{
_dataOut = value;
Wire.beginTransmission(_address);
Wire.write(_dataOut);
_error = Wire.endTransmission();
_dataOut = value;
Wire.beginTransmission(_address);
Wire.write(_dataOut);
_error = Wire.endTransmission();
}

uint8_t PCF8574::read(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}
PCF8574::read8();
return (_dataIn & (1 << pin)) > 0;
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}
PCF8574::read8();
return (_dataIn & (1 << pin)) > 0;
}

void PCF8574::write(const uint8_t pin, const uint8_t value)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
if (value == LOW)
{
_dataOut &= ~(1 << pin);
}
else
{
_dataOut |= (1 << pin);
}
write8(_dataOut);
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
if (value == LOW)
{
_dataOut &= ~(1 << pin);
}
else
{
_dataOut |= (1 << pin);
}
write8(_dataOut);
}

void PCF8574::toggle(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
toggleMask(1 << pin);
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return;
}
toggleMask(1 << pin);
}

void PCF8574::toggleMask(const uint8_t mask)
{
_dataOut ^= mask;
PCF8574::write8(_dataOut);
_dataOut ^= mask;
PCF8574::write8(_dataOut);
}

void PCF8574::shiftRight(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut >>= n;
PCF8574::write8(_dataOut);
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut >>= n;
PCF8574::write8(_dataOut);
}

void PCF8574::shiftLeft(const uint8_t n)
{
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut <<= n; // only shift if there are bits set
PCF8574::write8(_dataOut);
if ((n == 0) || (_dataOut == 0)) return;
if (n > 7) _dataOut = 0; // shift 8++ clears all, valid...
if (_dataOut != 0) _dataOut <<= n; // only shift if there are bits set
PCF8574::write8(_dataOut);
}

int PCF8574::lastError()
{
int e = _error;
_error = PCF8574_OK;
return e;
int e = _error;
_error = PCF8574_OK;
return e;
}

void PCF8574::rotateRight(const uint8_t n)
{
if ((n % 8) == 0) return;
uint8_t r = n & 7;
_dataOut = (_dataOut >> r) | (_dataOut << (8 - r));
PCF8574::write8(_dataOut);
if ((n % 8) == 0) return;

uint8_t r = n & 7;
_dataOut = (_dataOut >> r) | (_dataOut << (8 - r));
PCF8574::write8(_dataOut);
}

void PCF8574::rotateLeft(const uint8_t n)
{
rotateRight(8- (n & 7));
rotateRight(8- (n & 7));
}

void PCF8574::reverse() // quite fast: 14 shifts, 3 or, 3 assignment.
Expand All @@ -183,26 +183,29 @@ void PCF8574::reverse() // quite fast: 14 shifts, 3 or, 3 assignment.
//added 0.1.07/08 Septillion
uint8_t PCF8574::readButton8(const uint8_t mask)
{
uint8_t temp = _dataOut;
PCF8574::write8(mask | _dataOut);
PCF8574::read8();
PCF8574::write8(temp);
return _dataIn;
uint8_t temp = _dataOut;
PCF8574::write8(mask | _dataOut);
PCF8574::read8();
PCF8574::write8(temp);

return _dataIn;
}

//added 0.1.07 Septillion
uint8_t PCF8574::readButton(const uint8_t pin)
{
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}
uint8_t temp = _dataOut;
PCF8574::write(pin, HIGH);
uint8_t rtn = PCF8574::read(pin);
PCF8574::write8(temp);
return rtn;
if (pin > 7)
{
_error = PCF8574_PIN_ERROR;
return 0;
}

uint8_t temp = _dataOut;
PCF8574::write(pin, HIGH);
uint8_t rtn = PCF8574::read(pin);
PCF8574::write8(temp);

return rtn;
}

// -- END OF FILE --
62 changes: 31 additions & 31 deletions PCF8574.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: PCF8574.H
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.2.1
// VERSION: 0.2.2
// PURPOSE: Arduino library for PCF8574 - I2C IO expander
// URL: https://github.com/RobTillaart/PCF8574
// http://forum.arduino.cc/index.php?topic=184800
Expand All @@ -13,8 +13,9 @@
//

#include "Arduino.h"
#include "Wire.h"

#define PCF8574_LIB_VERSION "0.2.1"
#define PCF8574_LIB_VERSION "0.2.2"

#define PCF8574_OK 0x00
#define PCF8574_PIN_ERROR 0x81
Expand All @@ -24,45 +25,44 @@
class PCF8574
{
public:
explicit PCF8574(const uint8_t deviceAddress);
explicit PCF8574(const uint8_t deviceAddress);

#if defined (ESP8266) || defined(ESP32)
void begin(uint8_t sda, uint8_t scl, uint8_t val = 0xFF);
void begin(uint8_t sda, uint8_t scl, uint8_t val = 0xFF);
#endif
void begin(uint8_t val = 0xFF);
void begin(uint8_t val = 0xFF);

uint8_t read8();
uint8_t read(uint8_t pin);
uint8_t value() const { return _dataIn; };
uint8_t read8();
uint8_t read(uint8_t pin);
uint8_t value() const { return _dataIn; };

void write8(const uint8_t value);
void write(const uint8_t pin, const uint8_t value);
uint8_t valueOut() const { return _dataOut; }
void write8(const uint8_t value);
void write(const uint8_t pin, const uint8_t value);
uint8_t valueOut() const { return _dataOut; }

//added 0.1.07/08 Septillion
inline uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
uint8_t readButton8(const uint8_t mask = 0xFF);
uint8_t readButton(const uint8_t pin);
inline void setButtonMask(uint8_t mask) { _buttonMask = mask; };
//added 0.1.07/08 Septillion
inline uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
uint8_t readButton8(const uint8_t mask = 0xFF);
uint8_t readButton(const uint8_t pin);
inline void setButtonMask(uint8_t mask) { _buttonMask = mask; };

// rotate, shift, toggle, reverse expect all lines are output
void toggle(const uint8_t pin);
void toggleMask(const uint8_t mask = 0xFF); // default 0xFF ==> invertAll()
void shiftRight(const uint8_t n = 1);
void shiftLeft(const uint8_t n = 1);
void rotateRight(const uint8_t n = 1);
void rotateLeft(const uint8_t n = 1);
void reverse();
// rotate, shift, toggle, reverse expect all lines are output
void toggle(const uint8_t pin);
void toggleMask(const uint8_t mask = 0xFF); // default 0xFF ==> invertAll()
void shiftRight(const uint8_t n = 1);
void shiftLeft(const uint8_t n = 1);
void rotateRight(const uint8_t n = 1);
void rotateLeft(const uint8_t n = 1);
void reverse();

int lastError();
int lastError();

private:
uint8_t _address;
uint8_t _dataIn;
uint8_t _dataOut;
uint8_t _buttonMask;
int _error;
uint8_t _address;
uint8_t _dataIn;
uint8_t _dataOut;
uint8_t _buttonMask;
int _error;
};


// -- END OF FILE --
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

# PCF8574

Arduino library for PCF8574 - I2C IO expander

# Description
## Description

This library gives easy control over the 8 pins of a PCF8574 and PCF8574A chip.
These chips are identical in behavior although there are two distinct address ranges.
Expand All @@ -17,7 +18,7 @@ Furthermore some additional functions are implemented that are a little more
playfull but still are useful.


### Interface
## Interface

**PCF8574(deviceAddress)** Constructor with device address as parameter.

Expand Down Expand Up @@ -47,7 +48,7 @@ in the class this is faster than reread the pins.

----

TODO
TODO description

**setButtonMask(mask)**

Expand Down Expand Up @@ -76,6 +77,6 @@ Fills the lower lines with zero's.

**reverse()** revers the "bit pattern" of the lines, high to low and vice versa.

# Operation
## Operation

See examples
Loading

0 comments on commit 7199c76

Please sign in to comment.