initial version of PinInGroup class

This commit is contained in:
RobTillaart 2017-08-20 22:12:07 +02:00
parent ca9b517af5
commit 335a893f4b
5 changed files with 229 additions and 0 deletions

View File

@ -0,0 +1,57 @@
//
// FILE: PinInGroup.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2017-04-26
// PURPOSE: PinInGroup library for Arduino
// goal is to easily change a group of pins that logically
// belong to each other.
// The pins can be in any order.
// URL: http://forum.arduino.cc/index.php?topic=469599.0
//
// Released to the public domain
//
// 0.1.0 - initial version (based upon pinGroup)
//
#include "PinInGroup.h"
PinInGroup::PinInGroup()
{
_size = 0;
}
bool PinInGroup::add(uint8_t size, int* ar)
{
bool b = true;
for (uint8_t i = 0; i < size; i++)
{
b = b && add(ar[i]);
}
return b;
}
bool PinInGroup::add(uint8_t pin)
{
if (_size < PININGROUP_MAXSIZE)
{
_pins[_size] = pin;
pinMode(pin, INPUT);
_size++;
return true;
}
return false;
}
uint16_t PinInGroup::read()
{
uint16_t value = 0;
for (uint8_t i = 0; i < _size; i++)
{
value <<= 1;
value = value | (digitalRead(_pins[i]) > 0 ? 1: 0);
}
return value;
}
// --- END OF FILE ---

View File

@ -0,0 +1,41 @@
#ifndef PININGROUP_H
// FILE: PinInGroup.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.0
// PURPOSE: PinInGroup library for Arduino
// HISTORY: See PinInGroup.cpp
//
// Released to the public domain
//
#include "Arduino.h"
#define PININGROUP_LIB_VERSION "0.1.0"
#define PININGROUP_MAXSIZE 16
//
// a PinInGroup is a group of up to sixteen input pins that can be read
// by means of one read command and combined into one uint16_t.
//
class PinInGroup
{
public:
PinInGroup();
// adds a predefined array of pinnumbers to the PinInGroup
bool add(uint8_t s, int* ar);
// adds a single pin to the PinInGroup
bool add(uint8_t pin);
// read up to 8 pins "simultaneously" into one byte.
uint16_t read();
// get the current size of the PinInGroup.
uint8_t size() { return _size; };
// check how many free slots there are...
uint8_t free() { return PININGROUP_MAXSIZE - _size; };
private:
uint8_t _pins[PININGROUP_MAXSIZE]; // should be malloced dynamically
uint8_t _size = 0;
};
#endif

View File

@ -0,0 +1,99 @@
// FILE: testPinInGroup.ino
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.0
// PURPOSE: demo PinInGroup library for Arduino
// HISTORY: 20-08-2017
//
// Released to the public domain
//
#include "PinInGroup.h"
int ar[4] = {2, 3, 4, 13};
void setup()
{
Serial.begin(115200);
Serial.print("\nPININGROUP_LIB_VERSION: ");
Serial.println(PININGROUP_LIB_VERSION);
test0();
test1();
test2();
Serial.println("done...");
}
void loop()
{
}
// TEST1 verifies and times basic working
void test0()
{
pinMode(2, INPUT);
uint32_t t1 = micros();
digitalRead(2);
digitalRead(2);
digitalRead(2);
digitalRead(2);
uint32_t t2 = micros();
Serial.print("time: ");
Serial.println(t2 - t1);
Serial.println("Test0 done...");
Serial.println();
}
void test1()
{
uint16_t n1;
PinInGroup PIG;
PIG.add(2);
PIG.add(4);
PIG.add(5);
PIG.add(13);
Serial.print("size: ");
Serial.println(PIG.size());
Serial.print("free: ");
Serial.println(PIG.free());
uint32_t t1 = micros();
n1 = PIG.read();
uint32_t t2 = micros();
Serial.print(" val: ");
Serial.println(n1);
Serial.print("time: ");
Serial.println(t2 - t1);
Serial.println("Test1 done...");
Serial.println();
}
void test2()
{
uint16_t n1;
PinInGroup PIG;
for (int p=2; p<14; p++) PIG.add(p);
Serial.print("size: ");
Serial.println(PIG.size());
Serial.print("free: ");
Serial.println(PIG.free());
uint32_t t1 = micros();
n1 = PIG.read();
uint32_t t2 = micros();
Serial.print(" val: ");
Serial.println(n1);
Serial.print("time: ");
Serial.println(t2 - t1);
Serial.println("Test1 done...");
Serial.println();
}
// END OF FILE

View File

@ -0,0 +1,23 @@
{
"name": "PinInGroup",
"keywords": "PinInGroup",
"description": "A class that groups input pins so they can be read in one logical step",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"frameworks": "arduino",
"platforms": "*",
"export": {
"include": "libraries/PinInGroup
}
}

View File

@ -0,0 +1,9 @@
name=pinGroup
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=A class that groups input pins so they can be read in one logical step.
paragraph=
category=Uncategorized
url=https://github.com/RobTillaart/Arduino/tree/master/libraries
architectures=*