102 lines
1.6 KiB
C++
Raw Normal View History

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