mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.2 PIR
This commit is contained in:
parent
0d98f11aba
commit
8a2f0e7b16
@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.2] - 2022-12-15
|
||||
- add changed(), read() + compare with the last value.
|
||||
- update documentation / comments
|
||||
- add performance measure example
|
||||
|
||||
|
||||
## [0.1.1] - 2022-12-12
|
||||
- first released version
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: PIR.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// VERSION: 0.1.2
|
||||
// DATE: 2022-08-13
|
||||
// PURPOSE: PIR library for Arduino.
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
@ -55,6 +55,14 @@ uint8_t PIR::read()
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::changed()
|
||||
{
|
||||
uint8_t prev = _lastValue;
|
||||
read();
|
||||
return prev ^ _lastValue;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIR::read(uint8_t index)
|
||||
{
|
||||
if (_count == 0) return PIR_ERR_NO_SENSOR;
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: PIR.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// VERSION: 0.1.2
|
||||
// DATE: 2022-08-13
|
||||
// PURPOSE: PIR library
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
@ -11,9 +11,9 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define PIR_LIB_VERSION (F("0.1.1"))
|
||||
#define PIR_LIB_VERSION (F("0.1.2"))
|
||||
|
||||
// ??
|
||||
//
|
||||
#define PIR_OK 0x00
|
||||
#define PIR_ERR_NO_SENSOR 0xFF
|
||||
#define PIR_ERR_ARRAY_FULL 0xFE
|
||||
@ -31,28 +31,49 @@ public:
|
||||
|
||||
// returns number of PIR sensors added.
|
||||
uint8_t count();
|
||||
|
||||
|
||||
// MULTI PIN INTERFACE
|
||||
// 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);
|
||||
|
||||
|
||||
// returns last values read (bit mask) with read().
|
||||
uint8_t lastValue();
|
||||
|
||||
// returns bit mask of changed pins since last read().
|
||||
// updates lastValue().
|
||||
uint8_t changed();
|
||||
|
||||
private:
|
||||
|
||||
// SINGLE PIN INTERFACE
|
||||
// read a specific PIR, faster than read() above.
|
||||
// returns 0 or 1. (in fact just a digitalRead(pin).
|
||||
// does not affect lastValue()
|
||||
uint8_t read(uint8_t index);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
uint8_t _pins[8] = {0,0,0,0, 0,0,0,0};
|
||||
uint8_t _count = 0;
|
||||
uint8_t _lastValue = 0;
|
||||
};
|
||||
|
||||
|
||||
// PIR16 (think MEGA)
|
||||
|
||||
// PIR16 or dynamic memory in base class
|
||||
//
|
||||
// class PIR16 : public PIR // MEGA
|
||||
// {
|
||||
// static array of pins[16].
|
||||
// }
|
||||
//
|
||||
// class PIR
|
||||
// {
|
||||
// PIR(uint8_t size = 4);
|
||||
// ...
|
||||
// }
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -12,12 +12,12 @@ 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 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.
|
||||
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 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.
|
||||
|
||||
@ -28,18 +28,22 @@ Instead of PIR sensors one can add other DigitalOut sensors or even switches.
|
||||
|
||||
#### Base
|
||||
|
||||
- **PIR()** constructor.
|
||||
- **PIR()** constructor. Allocated room for 8 PIRs.
|
||||
- **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.
|
||||
Returns a bit 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.
|
||||
- **uint8_t lastValue()** returns last values read (bit mask) with read().
|
||||
- **uint8_t changed()** returns bit mask of pins that changed since last read().
|
||||
This can improve processing in some cases.
|
||||
|
||||
|
||||
## Future
|
||||
@ -54,12 +58,22 @@ Faster than read() above.
|
||||
#### Could
|
||||
- investigate PIR16 PIR32 class that can hold more
|
||||
- think MEGA2560.
|
||||
- investigate noise
|
||||
- or dynamic allocation? 0.2.0
|
||||
- one lastRead for all?
|
||||
- **uint8_t pin(uint8_t index)** to get back configuration
|
||||
- **bool add(uint8_t array, length)** faster configuration. Return true if there was enough room.
|
||||
- **clear()** to reset whole object?
|
||||
|
||||
#### Wont
|
||||
- PIR class based upon a PCF8574?
|
||||
- separate class
|
||||
- timestamp per PIR
|
||||
- lastRead, lastTriggered?
|
||||
- taking "a lot of RAM" for every pin
|
||||
- add statistics?
|
||||
- # calls # high% # low%
|
||||
- duration
|
||||
- timestamp per PIR
|
||||
- lastRead, lastTriggered?
|
||||
- PIR class based upon a PCF8574?
|
||||
|
||||
- investigate noise (what noise)
|
||||
- **remove(pin)**
|
||||
- difficult, more admin, expectations...
|
||||
|
||||
|
91
libraries/PIR/examples/PIR_performance/PIR_performance.ino
Normal file
91
libraries/PIR/examples/PIR_performance/PIR_performance.ino
Normal file
@ -0,0 +1,91 @@
|
||||
//
|
||||
// FILE: PIR_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo pir sensor class
|
||||
// URL: https://github.com/RobTillaart/PIR
|
||||
|
||||
|
||||
#include "PIR.h"
|
||||
|
||||
PIR P;
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PIR_LIB_VERSION: ");
|
||||
Serial.println(PIR_LIB_VERSION);
|
||||
|
||||
// fully loaded
|
||||
P.add(3);
|
||||
P.add(4);
|
||||
P.add(5);
|
||||
P.add(6);
|
||||
P.add(7);
|
||||
P.add(8);
|
||||
P.add(9);
|
||||
P.add(10);
|
||||
|
||||
test_read();
|
||||
test_lastValue();
|
||||
test_changed();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test_read()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = micros();
|
||||
uint8_t x = P.read();
|
||||
stop = micros();
|
||||
Serial.print("TIME:\t");
|
||||
Serial.println(stop - start);
|
||||
Serial.print("VAL:\t");
|
||||
Serial.println(x, HEX);
|
||||
}
|
||||
|
||||
|
||||
void test_lastValue()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = micros();
|
||||
uint8_t x = P.lastValue();
|
||||
stop = micros();
|
||||
Serial.print("TIME:\t");
|
||||
Serial.println(stop - start);
|
||||
Serial.print("VAL:\t");
|
||||
Serial.println(x, HEX);
|
||||
}
|
||||
|
||||
|
||||
void test_changed()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = micros();
|
||||
uint8_t x = P.changed();
|
||||
stop = micros();
|
||||
Serial.print("TIME:\t");
|
||||
Serial.println(stop - start);
|
||||
Serial.print("VAL:\t");
|
||||
Serial.println(x, HEX);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
19
libraries/PIR/examples/PIR_performance/performance_0.1.2.txt
Normal file
19
libraries/PIR/examples/PIR_performance/performance_0.1.2.txt
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
IDE: 1.8.19
|
||||
BOARD: UNO
|
||||
|
||||
8 pins
|
||||
|
||||
PIR_LIB_VERSION: 0.1.2
|
||||
|
||||
test_read
|
||||
TIME: 36
|
||||
VAL: F7
|
||||
|
||||
test_lastValue
|
||||
TIME: 4
|
||||
VAL: F7
|
||||
|
||||
test_changed
|
||||
TIME: 36
|
||||
VAL: 0
|
@ -5,9 +5,12 @@ PIR KEYWORD1
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
add KEYWORD2
|
||||
read KEYWORD2
|
||||
count KEYWORD2
|
||||
|
||||
read KEYWORD2
|
||||
lastValue KEYWORD2
|
||||
changed KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
PIR_LIB_VERSION LITERAL1
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PIR.git"
|
||||
},
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PIR
|
||||
version=0.1.1
|
||||
version=0.1.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=PIR library for Arduino.
|
||||
|
@ -52,7 +52,7 @@ unittest(test_add)
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
assertEqual(i, P.add(i+2));
|
||||
assertEqual(i, P.add(i + 2));
|
||||
}
|
||||
|
||||
assertEqual(PIR_ERR_ARRAY_FULL, P.add(10));
|
||||
@ -80,10 +80,30 @@ unittest(test_read)
|
||||
PIR P;
|
||||
|
||||
// no PIR added yet
|
||||
uint8_t p = P.read();
|
||||
assertEqual(0, p);
|
||||
uint8_t x = P.read();
|
||||
assertEqual(0, x);
|
||||
uint8_t y = P.lastValue();
|
||||
assertEqual(x, y);
|
||||
|
||||
P.add(3);
|
||||
x = P.read();
|
||||
assertEqual(0, x);
|
||||
y = P.lastValue();
|
||||
assertEqual(x, y);
|
||||
}
|
||||
|
||||
// TODO add pins, HW stub etc
|
||||
|
||||
unittest(test_single_read)
|
||||
{
|
||||
PIR P;
|
||||
|
||||
// no PIR added yet => error
|
||||
uint8_t x = P.read(0);
|
||||
assertEqual((int)PIR_ERR_NO_SENSOR, x);
|
||||
|
||||
P.add(3);
|
||||
x = P.read(0);
|
||||
assertNotEqual((int)PIR_ERR_INDEX, x);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user