GY-63_MS5611/libraries/PinOutGroup/PinOutGroup.cpp

145 lines
2.5 KiB
C++
Raw Normal View History

2017-08-20 16:11:38 -04:00
//
// FILE: PinOutGroup.cpp
// AUTHOR: Rob Tillaart
2022-11-22 07:46:07 -05:00
// VERSION: 0.1.7
2017-08-20 16:11:38 -04:00
// DATE: 2017-04-26
// PURPOSE: PinOutGroup library for Arduino
2020-11-27 05:28:57 -05:00
// goal is to easily change a group of pins that logically
2017-08-20 16:11:38 -04:00
// belong to each other e.g. 8 data pins of a parallel printer.
// these pins can be in any order.
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/PinOutGroup
2020-05-20 03:53:04 -04:00
// http://forum.arduino.cc/index.php?topic=469599.0
2022-11-22 07:46:07 -05:00
2020-05-20 03:53:04 -04:00
2017-08-20 16:11:38 -04:00
#include "PinOutGroup.h"
2021-01-29 06:31:58 -05:00
2017-08-20 16:11:38 -04:00
PinOutGroup::PinOutGroup()
{
2020-05-20 03:53:04 -04:00
clear();
}
2021-01-29 06:31:58 -05:00
2020-05-20 03:53:04 -04:00
void PinOutGroup::clear()
{
2022-11-22 07:46:07 -05:00
// safety: set all to LOW before cleaning up.
2021-01-29 06:31:58 -05:00
allLOW();
2017-08-20 16:11:38 -04:00
_size = 0;
}
2021-01-29 06:31:58 -05:00
2021-11-13 09:19:09 -05:00
uint8_t PinOutGroup::add(uint8_t size, uint8_t* pinArray, uint8_t value)
2017-08-20 16:11:38 -04:00
{
2021-01-29 06:31:58 -05:00
int n = 0;
2021-11-13 09:19:09 -05:00
for (uint8_t i = 0; i < size; i++)
2017-08-20 16:11:38 -04:00
{
2021-11-13 09:19:09 -05:00
n += add(pinArray[i], value);
2017-08-20 16:11:38 -04:00
}
2021-01-29 06:31:58 -05:00
return n;
2017-08-20 16:11:38 -04:00
}
2021-01-29 06:31:58 -05:00
uint8_t PinOutGroup::add(uint8_t pin, uint8_t value)
2017-08-20 16:11:38 -04:00
{
2021-01-29 06:31:58 -05:00
if (_size >= PINOUTGROUP_MAXSIZE) return 0;
2020-05-20 03:53:04 -04:00
_pins[_size] = pin;
pinMode(pin, OUTPUT);
2022-11-22 07:46:07 -05:00
write(_size, value); // takes care of _lastValue
2020-05-20 03:53:04 -04:00
_size++;
2021-01-29 06:31:58 -05:00
return 1;
2017-08-20 16:11:38 -04:00
}
2021-01-29 06:31:58 -05: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 03:53:04 -04:00
uint8_t PinOutGroup::write(uint16_t value)
2017-08-20 16:11:38 -04:00
{
2022-11-22 07:46:07 -05:00
uint16_t changed = _lastValue ^ value; // detect pins that changed
2017-08-20 16:11:38 -04:00
if (changed == 0) return 0;
uint16_t bitMask = 1;
uint8_t changeCount = 0;
for (uint8_t i = 0; i < _size; i++)
{
2020-11-27 05:28:57 -05:00
if ((changed & bitMask) > 0)
2017-08-20 16:11:38 -04:00
{
digitalWrite(_pins[i], (value & bitMask) > 0);
changeCount++;
}
bitMask <<= 1;
}
_lastValue = value;
return changeCount;
}
2021-01-29 06:31:58 -05:00
2021-11-13 09:19:09 -05:00
uint8_t PinOutGroup::write(uint8_t index, uint8_t value)
2020-05-20 03:53:04 -04:00
{
2021-11-13 09:19:09 -05:00
if (index >= _size) return 0;
2021-01-29 06:31:58 -05:00
2021-11-13 09:19:09 -05:00
uint16_t mask = (1 << index);
uint16_t lastValue = _lastValue & mask;
2020-05-20 03:53:04 -04:00
2022-11-22 07:46:07 -05:00
if ((value > 0) == (lastValue > 0)) return 0; // no change
2020-05-20 03:53:04 -04:00
2021-11-13 09:19:09 -05:00
digitalWrite(_pins[index], value);
2020-05-20 03:53:04 -04:00
if (value == LOW) _lastValue &= ~mask;
else _lastValue |= mask;
2021-01-29 06:31:58 -05:00
2020-05-20 03:53:04 -04:00
return 1;
}
2017-08-20 16:11:38 -04:00
2021-01-29 06:31:58 -05: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);
2022-11-22 07:46:07 -05:00
value |= (1 << i); // set flags.
2021-01-29 06:31:58 -05:00
}
_lastValue = value;
}
2021-11-13 09:19:09 -05:00
uint8_t PinOutGroup::getPin(uint8_t index)
2021-01-29 06:31:58 -05:00
{
2021-11-13 09:19:09 -05:00
if (index >= _size) return 0xFF;
return _pins[index];
2021-01-29 06:31:58 -05:00
}
2021-11-13 09:19:09 -05:00
uint8_t PinOutGroup::getIndex(uint8_t pin)
2021-01-29 06:31:58 -05:00
{
for (uint8_t i = 0; i < _size; i++)
{
if (_pins[i] == pin) return i;
}
return 0xFF;
}
2022-11-22 07:46:07 -05:00
// -- END OF FILE --
2021-12-23 12:59:45 -05:00