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

102 lines
1.6 KiB
C++
Raw Normal View History

2017-08-20 16:12:07 -04:00
//
// FILE: PinInGroup.cpp
// AUTHOR: Rob Tillaart
2022-11-22 06:37:06 -05:00
// VERSION: 0.1.8
2017-08-20 16:12:07 -04:00
// DATE: 2017-04-26
// PURPOSE: PinInGroup library for Arduino
2020-05-22 10:00:06 -04:00
// goal is to easily read a group of pins that logically
2017-08-20 16:12:07 -04:00
// belong to each other.
2022-11-22 06:37:06 -05:00
// The pins can be in any order, even duplicates are allowed.
2020-05-22 10:00:06 -04:00
// URL: https://github.com/RobTillaart/PinInGroup
// http://forum.arduino.cc/index.php?topic=469599.0
2021-01-29 06:31:58 -05:00
2017-08-20 16:12:07 -04:00
#include "PinInGroup.h"
2021-01-29 06:31:58 -05:00
2017-08-20 16:12:07 -04:00
PinInGroup::PinInGroup()
2020-05-22 10:00:06 -04:00
{
clear();
}
2021-01-29 06:31:58 -05:00
2020-05-22 10:00:06 -04:00
void PinInGroup::clear()
2017-08-20 16:12:07 -04:00
{
_size = 0;
}
2021-01-29 06:31:58 -05:00
2021-11-12 09:01:35 -05:00
uint8_t PinInGroup::add(uint8_t size, uint8_t * pinArray, uint8_t mode)
2017-08-20 16:12:07 -04:00
{
2020-05-22 10:00:06 -04:00
int n = 0;
2021-11-12 09:01:35 -05:00
for (uint8_t i = 0; i < size; i++)
2017-08-20 16:12:07 -04:00
{
2021-11-12 09:01:35 -05:00
n += add(pinArray[i], mode);
2017-08-20 16:12:07 -04:00
}
2020-05-22 10:00:06 -04:00
return n;
2017-08-20 16:12:07 -04:00
}
2021-01-29 06:31:58 -05:00
uint8_t PinInGroup::add(uint8_t pin, uint8_t mode)
2017-08-20 16:12:07 -04:00
{
2020-05-22 10:00:06 -04:00
if (_size >= PININGROUP_MAXSIZE) return 0;
2021-01-29 06:31:58 -05:00
2020-05-22 10:00:06 -04:00
_pins[_size] = pin;
2021-01-29 06:31:58 -05:00
pinMode(pin, mode);
2020-05-22 10:00:06 -04:00
_size++;
return 1;
2017-08-20 16:12:07 -04:00
}
2021-01-29 06:31:58 -05:00
uint8_t PinInGroup::isInGroup(uint8_t pin)
{
uint8_t count = 0;
for (uint8_t i = 0; i < _size; i++)
{
if (_pins[i] == pin) count++;
}
return count;
}
2017-08-20 16:12:07 -04:00
uint16_t PinInGroup::read()
{
uint16_t value = 0;
2021-11-12 09:01:35 -05:00
uint16_t mask = 0x0001;
2017-08-20 16:12:07 -04:00
for (uint8_t i = 0; i < _size; i++)
{
2020-05-22 10:00:06 -04:00
if (digitalRead(_pins[i])) value |= mask;
2021-01-29 06:31:58 -05:00
mask <<= 1;
2017-08-20 16:12:07 -04:00
}
return value;
}
2021-01-29 06:31:58 -05:00
2021-11-12 09:01:35 -05:00
uint16_t PinInGroup::read(uint8_t index)
2021-01-29 06:31:58 -05:00
{
2021-11-12 09:01:35 -05:00
if (index >= _size) return 0xFFFF; // sort of error
2021-01-29 06:31:58 -05:00
2021-11-12 09:01:35 -05:00
return (digitalRead(_pins[index])) ? 1 : 0;
2021-01-29 06:31:58 -05:00
}
2021-11-12 09:01:35 -05:00
uint8_t PinInGroup::getPin(uint8_t index)
2021-01-29 06:31:58 -05:00
{
2021-11-12 09:01:35 -05:00
if (index >= _size) return 0xFF;
return _pins[index];
2021-01-29 06:31:58 -05:00
}
2021-11-12 09:01:35 -05:00
uint8_t PinInGroup::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-08-04 11:25:38 -04:00
// --- END OF FILE ---
2021-12-23 11:54:57 -05:00