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

118 lines
2.3 KiB
C++
Raw Normal View History

2017-08-20 16:12:07 -04:00
//
// FILE: PinInGroup.cpp
// AUTHOR: Rob Tillaart
2021-12-23 11:54:57 -05:00
// VERSION: 0.1.6
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.
// The pins can be in any order.
2020-05-22 10:00:06 -04:00
// URL: https://github.com/RobTillaart/PinInGroup
// http://forum.arduino.cc/index.php?topic=469599.0
2017-08-20 16:12:07 -04:00
//
2021-01-29 06:31:58 -05:00
// HISTORY
// 0.1.0 2017-08-20 initial version (based upon pinGroup)
2021-11-12 09:01:35 -05:00
// 0.1.1 2020-05-19 refactor; added clear();
2021-01-29 06:31:58 -05:00
// added param for INPUT or INPUT_PULLUP
// 0.1.2 2020-06-19 fix library.json
2021-12-23 11:54:57 -05:00
// 0.1.3 2021-01-05 add Arduino-CI + unit test
2021-11-12 09:01:35 -05:00
// 0.1.4 2021-01-22
// 0.1.5 2021-11-12 update build-CI
// update readme, badges
// rename variables for readability,
// add getIndex() to replace getIdx(),
// add getMaxSize(),
// fix version number and history.
2021-12-23 11:54:57 -05:00
// 0.1.6 2021-12-23 update library.json, license, minor edits
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;
}
2020-05-22 10:00:06 -04:00
// --- END OF FILE ---
2021-12-23 11:54:57 -05:00