mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.0 PIR
This commit is contained in:
parent
d1ec1565a6
commit
0d98f11aba
28
libraries/PIR/.arduino-ci.yml
Normal file
28
libraries/PIR/.arduino-ci.yml
Normal file
@ -0,0 +1,28 @@
|
||||
platforms:
|
||||
rpipico:
|
||||
board: rp2040:rp2040:rpipico
|
||||
package: rp2040:rp2040
|
||||
gcc:
|
||||
features:
|
||||
defines:
|
||||
- ARDUINO_ARCH_RP2040
|
||||
warnings:
|
||||
flags:
|
||||
|
||||
packages:
|
||||
rp2040:rp2040:
|
||||
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
- esp8266
|
||||
# - mega2560
|
||||
- rpipico
|
||||
|
4
libraries/PIR/.github/FUNDING.yml
vendored
Normal file
4
libraries/PIR/.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: RobTillaart
|
||||
|
13
libraries/PIR/.github/workflows/arduino-lint.yml
vendored
Normal file
13
libraries/PIR/.github/workflows/arduino-lint.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
name: Arduino-lint
|
||||
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
compliance: strict
|
17
libraries/PIR/.github/workflows/arduino_test_runner.yml
vendored
Normal file
17
libraries/PIR/.github/workflows/arduino_test_runner.yml
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
name: Arduino CI
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
18
libraries/PIR/.github/workflows/jsoncheck.yml
vendored
Normal file
18
libraries/PIR/.github/workflows/jsoncheck.yml
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
name: JSON check
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- '**.json'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
pattern: "\\.json$"
|
||||
|
16
libraries/PIR/CHANGELOG.md
Normal file
16
libraries/PIR/CHANGELOG.md
Normal file
@ -0,0 +1,16 @@
|
||||
# Change Log PIR
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.1] - 2022-12-12
|
||||
- first released version
|
||||
|
||||
## [0.1.0] - 2022-08-13
|
||||
- initial version
|
||||
|
||||
|
||||
|
21
libraries/PIR/LICENSE
Normal file
21
libraries/PIR/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
73
libraries/PIR/PIR.cpp
Normal file
73
libraries/PIR/PIR.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
//
|
||||
// FILE: PIR.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2022-08-13
|
||||
// PURPOSE: PIR library for Arduino.
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
|
||||
|
||||
#include "PIR.h"
|
||||
|
||||
|
||||
PIR::PIR()
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
_pins[i] = 0;
|
||||
}
|
||||
_count = 0;
|
||||
_lastValue = 0;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::add(uint8_t pin)
|
||||
{
|
||||
if (_count >= 8) return PIR_ERR_ARRAY_FULL;
|
||||
_pins[_count++] = pin;
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
return _count - 1;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::count()
|
||||
{
|
||||
return _count;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::read()
|
||||
{
|
||||
if (_count == 0) return 0;
|
||||
uint8_t rv = 0x00;
|
||||
int i = _count -1;
|
||||
uint8_t mask = 1 << i;
|
||||
for (; i >= 0; i--)
|
||||
{
|
||||
if (digitalRead(_pins[i]) == HIGH)
|
||||
{
|
||||
rv |= mask;
|
||||
}
|
||||
mask >>= 1;
|
||||
}
|
||||
_lastValue = rv;
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::read(uint8_t index)
|
||||
{
|
||||
if (_count == 0) return PIR_ERR_NO_SENSOR;
|
||||
if (_count <= index) return PIR_ERR_INDEX;
|
||||
return digitalRead(_pins[index]) == HIGH; // 0 or 1
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::lastValue()
|
||||
{
|
||||
return _lastValue;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
59
libraries/PIR/PIR.h
Normal file
59
libraries/PIR/PIR.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
//
|
||||
// FILE: PIR.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2022-08-13
|
||||
// PURPOSE: PIR library
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define PIR_LIB_VERSION (F("0.1.1"))
|
||||
|
||||
// ??
|
||||
#define PIR_OK 0x00
|
||||
#define PIR_ERR_NO_SENSOR 0xFF
|
||||
#define PIR_ERR_ARRAY_FULL 0xFE
|
||||
#define PIR_ERR_INDEX 0xFD
|
||||
|
||||
|
||||
class PIR
|
||||
{
|
||||
public:
|
||||
PIR();
|
||||
|
||||
// adds a pin to the set
|
||||
// returns the index or PIR_ARRAY_FULL
|
||||
uint8_t add(uint8_t pin);
|
||||
|
||||
// returns number of PIR sensors added.
|
||||
uint8_t count();
|
||||
|
||||
// read all PIR sensors in the set
|
||||
// returns a mask of HIGH / LOW values.
|
||||
// not used slots will return 0.
|
||||
uint8_t read();
|
||||
|
||||
// read a specific PIR, faster than read() above.
|
||||
// return 0 or 1.
|
||||
uint8_t read(uint8_t index);
|
||||
|
||||
|
||||
uint8_t lastValue();
|
||||
|
||||
|
||||
private:
|
||||
uint8_t _pins[8] = {0,0,0,0, 0,0,0,0};
|
||||
uint8_t _count = 0;
|
||||
uint8_t _lastValue = 0;
|
||||
};
|
||||
|
||||
|
||||
// PIR16 (think MEGA)
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
65
libraries/PIR/README.md
Normal file
65
libraries/PIR/README.md
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/PIR/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/PIR/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PIR/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/PIR/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PIR/actions/workflows/jsoncheck.yml)
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PIR/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PIR.svg?maxAge=3600)](https://github.com/RobTillaart/PIR/releases)
|
||||
|
||||
|
||||
# PIR
|
||||
|
||||
PIR library for Arduino. Supports up to 8 PIR sensors.
|
||||
|
||||
## Description
|
||||
|
||||
The PIR library implements a class to monitor 1 or more PIR sensors.
|
||||
The library supports up to 8 PIR sensors per object, which typically are added in **setup()**.
|
||||
It is possible to add a sensor (pin) multiple times.
|
||||
The library accepts duplicates.
|
||||
|
||||
The library has two **read()** functions, one to read a specific sensor, and one to read all of them.
|
||||
The latter will return a mask indicating HIGH and LOW.
|
||||
The first added PIR will have the LSB.
|
||||
|
||||
Instead of PIR sensors one can add other DigitalOut sensors or even switches.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
#### Base
|
||||
|
||||
- **PIR()** constructor.
|
||||
- **uint8_t add(uint8_t pin)** adds a PIR pin to the set of pins.
|
||||
Returns the index or PIR_ARRAY_FULL (0xFE)
|
||||
- **uint8_t count** returns number of PIR sensors added.
|
||||
|
||||
#### Read
|
||||
|
||||
- **uint8_t read()** read all PIR sensors in the set.
|
||||
Returns a mask of HIGH / LOW values.
|
||||
Not used slots will return 0.
|
||||
- **uint8_t read(uint8_t index)** read a specific PIR sensor.
|
||||
Faster than read() above.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
- update documentation
|
||||
|
||||
#### Should
|
||||
- add examples
|
||||
- interrupts?
|
||||
|
||||
#### Could
|
||||
- investigate PIR16 PIR32 class that can hold more
|
||||
- think MEGA2560.
|
||||
- investigate noise
|
||||
- add statistics?
|
||||
- # calls # high% # low%
|
||||
- duration
|
||||
- timestamp per PIR
|
||||
- lastRead, lastTriggered?
|
||||
- PIR class based upon a PCF8574?
|
||||
|
||||
|
37
libraries/PIR/examples/PIR_demo/PIR_demo.ino
Normal file
37
libraries/PIR/examples/PIR_demo/PIR_demo.ino
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// FILE: PIR_demo.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo pir sensor class
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
|
||||
|
||||
#include "PIR.h"
|
||||
|
||||
|
||||
PIR P;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PIR_LIB_VERSION: ");
|
||||
Serial.println(PIR_LIB_VERSION);
|
||||
|
||||
P.add(3);
|
||||
P.add(4);
|
||||
P.add(5);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint8_t x = P.read();
|
||||
Serial.println(x, HEX);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -0,0 +1,41 @@
|
||||
//
|
||||
// FILE: PIR_demo.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo pir sensor class
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
|
||||
|
||||
#include "PIR.h"
|
||||
|
||||
PIR P;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PIR_LIB_VERSION: ");
|
||||
Serial.println(PIR_LIB_VERSION);
|
||||
|
||||
// add 3 PIR sensors
|
||||
P.add(3);
|
||||
P.add(4);
|
||||
P.add(5);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint8_t lastValue = P.lastValue();
|
||||
uint8_t newValue = P.read();
|
||||
if (newValue != lastValue) // change detected?
|
||||
{
|
||||
Serial.print(millis());
|
||||
Serial.print("\t");
|
||||
Serial.println(newValue, HEX);
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
15
libraries/PIR/keywords.txt
Normal file
15
libraries/PIR/keywords.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# Syntax Colouring Map For PIR
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
PIR KEYWORD1
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
add KEYWORD2
|
||||
read KEYWORD2
|
||||
count KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
PIR_LIB_VERSION LITERAL1
|
||||
|
||||
|
23
libraries/PIR/library.json
Normal file
23
libraries/PIR/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "PIR",
|
||||
"keywords": "PIR,array",
|
||||
"description": "PIR library for Arduino. Supports up to 8 PIR sensors.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PIR.git"
|
||||
},
|
||||
"version": "0.1.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"headers": "PIR.h"
|
||||
}
|
11
libraries/PIR/library.properties
Normal file
11
libraries/PIR/library.properties
Normal file
@ -0,0 +1,11 @@
|
||||
name=PIR
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=PIR library for Arduino.
|
||||
paragraph=Supports up to 8 PIR sensors.
|
||||
category=Signal Input/Output
|
||||
url=https://github.com/RobTillaart/PIR
|
||||
architectures=*
|
||||
includes=PIR.h
|
||||
depends=
|
93
libraries/PIR/test/unit_test_001.cpp
Normal file
93
libraries/PIR/test/unit_test_001.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
//
|
||||
// FILE: unit_test_001.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2022-08-13
|
||||
// PURPOSE: unit tests for the PIR current sensor
|
||||
// https://github.com/RobTillaart/PIR
|
||||
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
||||
//
|
||||
|
||||
// supported assertions
|
||||
// ----------------------------
|
||||
// assertEqual(expected, actual)
|
||||
// assertNotEqual(expected, actual)
|
||||
// assertLess(expected, actual)
|
||||
// assertMore(expected, actual)
|
||||
// assertLessOrEqual(expected, actual)
|
||||
// assertMoreOrEqual(expected, actual)
|
||||
// assertTrue(actual)
|
||||
// assertFalse(actual)
|
||||
// assertNull(actual)
|
||||
|
||||
|
||||
#include <ArduinoUnitTests.h>
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PIR.h"
|
||||
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "PIR_LIB_VERSION: %s\n", (char *) PIR_LIB_VERSION);
|
||||
}
|
||||
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constants)
|
||||
{
|
||||
assertEqual(0x00, PIR_OK);
|
||||
assertEqual(0xFF, PIR_ERR_NO_SENSOR);
|
||||
assertEqual(0xFE, PIR_ERR_ARRAY_FULL);
|
||||
assertEqual(0xFD, PIR_ERR_INDEX);
|
||||
}
|
||||
|
||||
|
||||
unittest(test_add)
|
||||
{
|
||||
PIR P;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
assertEqual(i, P.add(i+2));
|
||||
}
|
||||
|
||||
assertEqual(PIR_ERR_ARRAY_FULL, P.add(10));
|
||||
}
|
||||
|
||||
|
||||
unittest(test_count)
|
||||
{
|
||||
PIR P;
|
||||
|
||||
assertEqual(0, P.count());
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
P.add(i+2);
|
||||
assertEqual(i+1, P.count());
|
||||
}
|
||||
|
||||
P.add(10);
|
||||
assertEqual(8, P.count());
|
||||
}
|
||||
|
||||
|
||||
unittest(test_read)
|
||||
{
|
||||
PIR P;
|
||||
|
||||
// no PIR added yet
|
||||
uint8_t p = P.read();
|
||||
assertEqual(0, p);
|
||||
|
||||
// TODO add pins, HW stub etc
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
Loading…
Reference in New Issue
Block a user