0.1.5 PinInGroup

This commit is contained in:
rob tillaart 2021-11-12 15:01:35 +01:00
parent d163d90937
commit aca0869f43
9 changed files with 115 additions and 69 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,7 +1,7 @@
//
// FILE: PinInGroup.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.5
// DATE: 2017-04-26
// PURPOSE: PinInGroup library for Arduino
// goal is to easily read a group of pins that logically
@ -13,10 +13,17 @@
// HISTORY
// 0.1.0 2017-08-20 initial version (based upon pinGroup)
// 0.1.1 2020-05-19 refactor; added clear();
// 0.1.1 2020-05-19 refactor; added clear();
// added param for INPUT or INPUT_PULLUP
// 0.1.2 2020-06-19 fix library.json
// 0.1.3 2021-01-05 add arduino-CI + unit test
// 0.1.3 2021-01-05 add arduino-CI + unit test
// 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.
#include "PinInGroup.h"
@ -34,12 +41,12 @@ void PinInGroup::clear()
}
uint8_t PinInGroup::add(uint8_t sz, uint8_t * ar, uint8_t mode)
uint8_t PinInGroup::add(uint8_t size, uint8_t * pinArray, uint8_t mode)
{
int n = 0;
for (uint8_t i = 0; i < sz; i++)
for (uint8_t i = 0; i < size; i++)
{
n += add(ar[i], mode);
n += add(pinArray[i], mode);
}
return n;
}
@ -70,7 +77,7 @@ uint8_t PinInGroup::isInGroup(uint8_t pin)
uint16_t PinInGroup::read()
{
uint16_t value = 0;
uint16_t mask = 0x01;
uint16_t mask = 0x0001;
for (uint8_t i = 0; i < _size; i++)
{
if (digitalRead(_pins[i])) value |= mask;
@ -80,22 +87,22 @@ uint16_t PinInGroup::read()
}
uint16_t PinInGroup::read(uint8_t idx)
uint16_t PinInGroup::read(uint8_t index)
{
if (idx >= _size) return 0xFFFF; // sort of error
if (index >= _size) return 0xFFFF; // sort of error
return (digitalRead(_pins[idx])) ? 1 : 0;
return (digitalRead(_pins[index])) ? 1 : 0;
}
uint8_t PinInGroup::getPin(uint8_t idx)
uint8_t PinInGroup::getPin(uint8_t index)
{
if (idx >= _size) return 0xFF;
return _pins[idx];
if (index >= _size) return 0xFF;
return _pins[index];
}
uint8_t PinInGroup::getIdx(uint8_t pin)
uint8_t PinInGroup::getIndex(uint8_t pin)
{
for (uint8_t i = 0; i < _size; i++)
{

View File

@ -1,7 +1,7 @@
#pragma once
// FILE: PinInGroup.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.4
// VERSION: 0.1.5
// DATE: 2017-04-26
// PURPOSE: PinInGroup library for Arduino
// HISTORY: See PinInGroup.cpp
@ -13,7 +13,7 @@
#include "Arduino.h"
#define PININGROUP_LIB_VERSION (F("0.1.4"))
#define PININGROUP_LIB_VERSION (F("0.1.5"))
// smaller MAXSIZE will reduce memory footprint with ditto bytes.
@ -30,28 +30,31 @@ public:
// enables one to reset the pinGroup and repopulate it
void clear();
// adds a predefined array of pin numbers to the PinInGroup
// sets all to either INPUT (default) or INPUT_PULLUP.
uint8_t add(uint8_t sz, uint8_t * ar, uint8_t mode = INPUT);
uint8_t add(uint8_t size, uint8_t * pinArray, uint8_t mode = INPUT);
// adds a single pin to the PinInGroup
uint8_t add(uint8_t pin, uint8_t mode = INPUT);
uint8_t add(uint8_t pin, uint8_t mode = INPUT);
// counts how often a pin is in the group
uint8_t isInGroup(uint8_t pin);
uint8_t isInGroup(uint8_t pin);
uint8_t size() { return _size; };
uint8_t getMaxSize() { return PININGROUP_MAXSIZE; };
// check how many "slots" are available
uint8_t available() { return PININGROUP_MAXSIZE - _size; };
// read up to 16 pins "simultaneously" in one call.
uint16_t read();
// read specific index.
uint16_t read(uint8_t idx);
uint16_t read(uint8_t index);
uint8_t size() { return _size; };
// check how many "slots" are available
uint8_t available() { return PININGROUP_MAXSIZE - _size; };
uint8_t getPin(uint8_t idx);
uint8_t getIdx(uint8_t pin);
uint8_t getPin(uint8_t index);
uint8_t getIndex(uint8_t pin);
uint8_t getIdx(uint8_t pin) { getIndex(pin); }; // will be obsolete
private:
uint8_t _pins[PININGROUP_MAXSIZE];

View File

@ -1,11 +1,15 @@
[![Arduino CI](https://github.com/RobTillaart/PinInGroup/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/PinInGroup/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PinInGroup/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/PinInGroup/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PinInGroup/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PinInGroup/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PinInGroup.svg?maxAge=3600)](https://github.com/RobTillaart/PinInGroup/releases)
# PinInGroup
Arduino library to group reading to 16 input pins in one command
Arduino library to read a group of up to 16 input pins in one command.
# Description
@ -17,12 +21,15 @@ One of the interesting possibilities of the pinInGroup is to add a single pin mu
That allows one to read a pin e.g. in a burst of 8.
Another application of adding a pin twice could be reading a pin as first and as last of a group.
This allows you to check that state of e.g. a parallel bus has not changed during read.
This allows you to check that state of e.g. a parallel bus has not changed during read (or changed an even number :).
Default **PININGROUP_MAXSIZE** = 16.
## Performance
The PinInGroup is not more efficient as reading the pins in a loop yourself.
Hoewever it is less programming and can give clearer code.
However it is less programming and can give clearer code.
**Note** that the pins are not read at the same microsecond.
A small time is needed to go through all pins.
@ -33,39 +40,48 @@ This is platform, group size and pin state dependent.
### Constructor
- **PinInGroup()** Constructor
- **PinInGroup()** Constructor.
### Administration
- **clear()** sets the size to zero so one can repopulate.
- **add(size, pinArray, mode)** adds a predefined array of pins to the group. Returns the number of pins added.
- **add(pin, mode)** adds a single pin to the group. Returns the number of pins added (1 or 0). Mode can be **INPUT**(default) or **INPUT_PULLUP**.
- **getPin(idx)** idx = 0..15; returns the pin at slot idx or 255 (0xFF) when out of range
- **getIdx(pin)** returns the (first) index of the slot with pin number. 255 (0xFF) if not found.
- **isInGroup(pin)** returns how often a pin is added to a group. Can be more than once.
- **size()** how many slots are used
- **available()** how many slots are free
- **void clear()** sets the size to zero so one can repopulate.
- **uint8_t add(uint8_t size, uint8_t \* pinArray, uint8_t mode)** adds a predefined array of pins to the group. Returns the number of pins added.
- **uint8_t add(uint8_t pin, uint8_t mode)** adds a single pin to the group. Returns the number of pins added (1 or 0). Mode can be **INPUT**(default) or **INPUT_PULLUP**.
- **uint8_t getPin(uint8_t index)** index = 0..15; returns the pin at slot index or 255 (0xFF) when out of range.
- **uint8_t getIndex(uint8_t pin)** returns the (first) index of the slot with pin number. 255 (0xFF) if not found.
- **uint8_t isInGroup(uint8_t pin)** returns how often a pin is added to a group. Can be more than once.
- **uint8_t size()** how many slots are used.
- **uint8_t getMaxSize()** how many slots are there in total.
- **uint8_t available()** how many slots are free.
### Read
- **read()** reads a 16 bits unsigned int from max 16 pins. Every bit represents an input value. Note that the bits are in LSB order of the adding.
- **read(idx)** idx = 0..size-1. Reads the single pin at idx from the group. Returns 0 or 1 if OK and 0xFFFF when idx >= size.
- **uint16_t read()** reads a 16 bits unsigned int from max 16 pins. Every bit represents an input value. Note that the bits are in LSB order of the adding.
- **uint16_t read(uint8_t index)** index = 0..size-1.
Reads the single pin at index from the group.
Returns 0 or 1 if OK and 0xFFFF when index >= size.
# Operation
See examples.
## Future
- Optimize the low level reading e.g. reading registers only once.
- Hold register and bit info per pin. Especially for AVR this could be interesting.
- Hold register and bit info per pin.
- Especially for AVR this could be interesting performance wise.
- Create an analogPinGroup and a PWMGroup
- extend to 32 bits / pins.
- Allocate dynamic memory? (fragmentation)
- extend to 32 bits / pins. class hierarchy. 8, 24 ?
- Allocate dynamic memory (0.2.0)
- fragmentation?
- would be memory efficient.
- clear() => reset() or clearGroup() ???
- do we need yield() somewhere?
These ideas will be explored when time permits or needs arise.
These ideas will be explored when time permits.
# Operation
See examples

View File

@ -1,17 +1,17 @@
// FILE: testPinInGroup.ino
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo PinInGroup library for Arduino
// HISTORY: 20-08-2017
//
// Released to the public domain
//
#include "PinInGroup.h"
uint8_t ar[4] = {2, 3, 4, 23};
uint8_t alt[] = {23, 22, 1, 3, 21, 19, 18, 5, 23, 22, 1, 3, 21, 19, 18, 5};
void setup()
{
Serial.begin(115200);
@ -32,11 +32,13 @@ void setup()
Serial.println(F("done..."));
}
void loop()
{
}
// TEST1 verifies and times basic working
// TEST0 verifies and times basic working
void test0()
{
Serial.println();
@ -65,6 +67,7 @@ void test0()
Serial.println();
}
void test1()
{
Serial.println();
@ -97,6 +100,7 @@ void test1()
Serial.println();
}
void test2()
{
Serial.println();
@ -107,7 +111,7 @@ void test2()
PinInGroup PIG;
#if defined(ESP32) || defined(ESP8266)
// be carefull which pins you use for ESP32 (and probably 8266 too, not tested)
// be careful which pins you use for ESP32 (and probably 8266 too, not tested)
// note: GPIO06 through GPIO11 are reserved for the FLASH.
// You cannot use them. ==> pinMode() fails.
// https://github.com/espressif/arduino-esp32/issues/1411
@ -137,6 +141,7 @@ void test2()
Serial.println();
}
void test3()
{
Serial.println();
@ -187,6 +192,7 @@ void test3()
Serial.println();
}
void test4()
{
Serial.println();
@ -228,6 +234,7 @@ void test4()
Serial.println();
}
void test5()
{
Serial.println();
@ -274,7 +281,6 @@ void test5()
delay(20);
}
Serial.println(F("Test5 done..."));
Serial.println();
}
@ -307,4 +313,6 @@ void test6()
Serial.println();
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,17 +1,21 @@
# Syntax Coloring Map For PinInGroup
# Syntax Colouring Map For PinInGroup
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
PinInGroup KEYWORD1
# Methods and Functions (KEYWORD2)
clear KEYWORD2
add KEYWORD2
isInGroup KEYWORD2
read KEYWORD2
size KEYWORD2
getMaxSize KEYWORD2
available KEYWORD2
getPin KEYWORD2
getIdx KEYWORD2
getIndex KEYWORD2
# Constants (LITERAL1)

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/PinInGroup.git"
},
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,5 +1,5 @@
name=PinInGroup
version=0.1.4
version=0.1.5
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.