153 lines
2.8 KiB
C++
Raw Normal View History

2017-08-20 22:11:38 +02:00
//
// FILE: PinOutGroup.cpp
// AUTHOR: Rob Tillaart
2021-01-29 12:31:58 +01:00
// VERSION: 0.1.3
2017-08-20 22:11:38 +02:00
// DATE: 2017-04-26
// PURPOSE: PinOutGroup library for Arduino
2020-11-27 11:28:57 +01:00
// goal is to easily change a group of pins that logically
2017-08-20 22:11:38 +02:00
// belong to each other e.g. 8 data pins of a parallel printer.
// these pins can be in any order.
2021-01-29 12:31:58 +01:00
// URL: https://github.com/RobTillaart/PinOutGroup
2020-05-20 09:53:04 +02:00
// http://forum.arduino.cc/index.php?topic=469599.0
2021-01-29 12:31:58 +01:00
//
// HISTORY
2017-08-20 22:11:38 +02:00
//
2021-01-29 12:31:58 +01:00
// 0.1.0 20-08-2017 initial version (based upon experimental pinGroup)
// 0.1.1 2020-05-19 main refactor;
// added tests; added clear(); added write(idx, value)
// renamed set to write() to be in line with digitalWrite()
// 0.1.2 2020-06-19 fix library.json
// 0.1.3 2021-01-05 add Arduino-CI + unit test
2020-05-20 09:53:04 +02:00
2017-08-20 22:11:38 +02:00
#include "PinOutGroup.h"
2021-01-29 12:31:58 +01:00
2017-08-20 22:11:38 +02:00
PinOutGroup::PinOutGroup()
{
2020-05-20 09:53:04 +02:00
clear();
}
2021-01-29 12:31:58 +01:00
2020-05-20 09:53:04 +02:00
void PinOutGroup::clear()
{
// safety: set all to LOW before cleaning up.
2021-01-29 12:31:58 +01:00
allLOW();
2017-08-20 22:11:38 +02:00
_size = 0;
}
2021-01-29 12:31:58 +01:00
uint8_t PinOutGroup::add(uint8_t sz, uint8_t* ar, uint8_t value)
2017-08-20 22:11:38 +02:00
{
2021-01-29 12:31:58 +01:00
int n = 0;
for (uint8_t i = 0; i < sz; i++)
2017-08-20 22:11:38 +02:00
{
2021-01-29 12:31:58 +01:00
n += add(ar[i], value);
2017-08-20 22:11:38 +02:00
}
2021-01-29 12:31:58 +01:00
return n;
2017-08-20 22:11:38 +02:00
}
2021-01-29 12:31:58 +01:00
uint8_t PinOutGroup::add(uint8_t pin, uint8_t value)
2017-08-20 22:11:38 +02:00
{
2021-01-29 12:31:58 +01:00
if (_size >= PINOUTGROUP_MAXSIZE) return 0;
2020-05-20 09:53:04 +02:00
_pins[_size] = pin;
pinMode(pin, OUTPUT);
2021-01-29 12:31:58 +01:00
write(_size, value); // takes care of _lastValue
2020-05-20 09:53:04 +02:00
_size++;
2021-01-29 12:31:58 +01:00
return 1;
2017-08-20 22:11:38 +02:00
}
2021-01-29 12:31:58 +01:00
uint8_t PinOutGroup::isInGroup(uint8_t pin)
{
uint8_t count = 0;
for (uint8_t i = 0; i < _size; i++)
{
if (_pins[i] == pin) count++;
}
return count;
}
2020-05-20 09:53:04 +02:00
uint8_t PinOutGroup::write(uint16_t value)
2017-08-20 22:11:38 +02:00
{
uint16_t changed = _lastValue ^ value; // detect pins that changed
if (changed == 0) return 0;
uint16_t bitMask = 1;
uint8_t changeCount = 0;
for (uint8_t i = 0; i < _size; i++)
{
2020-11-27 11:28:57 +01:00
if ((changed & bitMask) > 0)
2017-08-20 22:11:38 +02:00
{
digitalWrite(_pins[i], (value & bitMask) > 0);
changeCount++;
}
bitMask <<= 1;
}
_lastValue = value;
return changeCount;
}
2021-01-29 12:31:58 +01:00
2020-05-20 09:53:04 +02:00
uint8_t PinOutGroup::write(uint8_t idx, uint8_t value)
{
if (idx >= _size) return 0;
2021-01-29 12:31:58 +01:00
2020-05-20 09:53:04 +02:00
uint16_t mask = (1 << idx);
uint16_t lv = _lastValue & mask;
if ((value > 0) == (lv > 0)) return 0; // no change
digitalWrite(_pins[idx], value);
if (value == LOW) _lastValue &= ~mask;
else _lastValue |= mask;
2021-01-29 12:31:58 +01:00
2020-05-20 09:53:04 +02:00
return 1;
}
2017-08-20 22:11:38 +02:00
2021-01-29 12:31:58 +01:00
void PinOutGroup::allLOW()
{
for (uint8_t i = 0; i < _size; i++)
{
digitalWrite(_pins[i], LOW);
}
_lastValue = 0;
}
void PinOutGroup::allHIGH()
{
uint16_t value = 0;
for (uint8_t i = 0; i < _size; i++)
{
digitalWrite(_pins[i], HIGH);
value = (1 << i); // set flags.
}
_lastValue = value;
}
uint8_t PinOutGroup::getPin(uint8_t idx)
{
if (idx >= _size) return 0xFF;
return _pins[idx];
}
uint8_t PinOutGroup::getIdx(uint8_t pin)
{
for (uint8_t i = 0; i < _size; i++)
{
if (_pins[i] == pin) return i;
}
return 0xFF;
}
2020-05-20 09:53:04 +02:00
// --- END OF FILE ---