Skip to content

Commit 808d25f

Browse files
committed
0.2.0 PIR
1 parent d834eab commit 808d25f

File tree

14 files changed

+195
-38
lines changed

14 files changed

+195
-38
lines changed

libraries/PIR/CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9-
## [0.1.3] - 2023-11-15
9+
## [0.2.0] - 2025-06-26
10+
- add **reset()** to clean up internal admin
11+
- add **free()** to return available slots
12+
- add **add(array, length)** for easier configuration.
13+
- add **PIR_MAX_COUNT** to be compile time configurable
14+
- update unit tests
15+
- add examples PIR_demo_add_array.ino
16+
- update keywords.txt
1017
- update readme.md
1118

19+
----
20+
21+
## [0.1.3] - 2023-11-15
22+
- update readme.md
1223

1324
## [0.1.2] - 2022-12-15
1425
- add changed(), read() + compare with the last value.

libraries/PIR/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022-2024 Rob Tillaart
3+
Copyright (c) 2022-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/PIR/PIR.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: PIR.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.2.0
55
// DATE: 2022-08-13
66
// PURPOSE: PIR library for Arduino.
77
// URL: https://github.com/RobTillaart/PIR
@@ -12,30 +12,53 @@
1212

1313
PIR::PIR()
1414
{
15-
for (int i = 0; i < 8; i++)
16-
{
17-
_pins[i] = 0;
18-
}
19-
_count = 0;
20-
_lastValue = 0;
15+
reset();
2116
}
2217

2318

2419
uint8_t PIR::add(uint8_t pin)
2520
{
26-
if (_count >= 8) return PIR_ERR_ARRAY_FULL;
21+
if (_count >= PIR_MAX_COUNT) return PIR_ERR_ARRAY_FULL;
2722
_pins[_count++] = pin;
2823
pinMode(pin, INPUT_PULLUP);
2924
return _count - 1;
3025
}
3126

3227

28+
uint8_t PIR::add(uint8_t * pins, uint8_t length)
29+
{
30+
if ((_count + length) > PIR_MAX_COUNT) return PIR_ERR_ARRAY_FULL;
31+
for (int i = 0; i < length; i++)
32+
{
33+
add(pins[i]);
34+
}
35+
return _count - 1;
36+
}
37+
38+
39+
void PIR::reset()
40+
{
41+
for (int i = 0; i < PIR_MAX_COUNT; i++)
42+
{
43+
_pins[i] = 0;
44+
}
45+
_count = 0;
46+
_lastValue = 0;
47+
}
48+
49+
3350
uint8_t PIR::count()
3451
{
3552
return _count;
3653
}
3754

3855

56+
uint8_t PIR::free()
57+
{
58+
return PIR_MAX_COUNT - _count;
59+
}
60+
61+
3962
uint8_t PIR::read()
4063
{
4164
if (_count == 0) return 0;

libraries/PIR/PIR.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: PIR.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.2.0
66
// DATE: 2022-08-13
77
// PURPOSE: PIR library
88
// URL: https://github.com/RobTillaart/PIR
@@ -11,13 +11,19 @@
1111
#include "Arduino.h"
1212

1313

14-
#define PIR_LIB_VERSION (F("0.1.3"))
14+
#define PIR_LIB_VERSION (F("0.2.0"))
15+
16+
17+
#ifndef PIR_MAX_COUNT
18+
#define PIR_MAX_COUNT 8
19+
#endif
1520

16-
//
17-
#define PIR_OK 0x00
18-
#define PIR_ERR_NO_SENSOR 0xFF
19-
#define PIR_ERR_ARRAY_FULL 0xFE
20-
#define PIR_ERR_INDEX 0xFD
21+
22+
// ERROR CODES
23+
#define PIR_OK 0x00
24+
#define PIR_ERR_NO_SENSOR 0xFF
25+
#define PIR_ERR_ARRAY_FULL 0xFE
26+
#define PIR_ERR_INDEX 0xFD
2127

2228

2329
class PIR
@@ -28,9 +34,16 @@ class PIR
2834
// adds a pin to the set
2935
// returns the index or PIR_ARRAY_FULL
3036
uint8_t add(uint8_t pin);
37+
// adds multiple pins in one call.
38+
uint8_t add(uint8_t * pins, uint8_t length);
39+
40+
// resets the internal set to empty.
41+
void reset();
3142

3243
// returns number of PIR sensors added.
3344
uint8_t count();
45+
// returns number of PIR slots available.
46+
uint8_t free();
3447

3548
// MULTI PIN INTERFACE
3649
// read all PIR sensors in the set
@@ -54,7 +67,7 @@ class PIR
5467

5568

5669
protected:
57-
uint8_t _pins[8] = {0,0,0,0, 0,0,0,0};
70+
uint8_t _pins[PIR_MAX_COUNT];
5871
uint8_t _count = 0;
5972
uint8_t _lastValue = 0;
6073
};

libraries/PIR/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,46 @@ PIR library for Arduino. Supports up to 8 PIR sensors.
1616
## Description
1717

1818
The PIR library implements a class to monitor 1 or more PIR sensors.
19-
The library supports up to 8 PIR sensors per object, which typically are added in **setup()**.
20-
It is possible to add a sensor (pin) multiple times.
21-
The library accepts duplicates.
19+
The library supports default 8 PIR sensors per object, which typically are added in **setup()**.
20+
It is possible to add a sensor (pin) multiple times as the library accepts duplicates.
2221

2322
The library has two **read()** functions, one to read a specific sensor, and one to read all of them.
2423
The latter will return a mask indicating HIGH and LOW.
2524
The first added PIR will have the LSB.
2625

26+
The maximum number of PIR sensors supported can be adjusted with a define
27+
in the .h file named **PIR_MAX_COUNT**. The default maximum is 8.
28+
2729
Instead of PIR sensors one can add other DigitalOut sensors or even switches.
2830

31+
As always feedback is welcome.
32+
33+
### Related
34+
35+
- https://github.com/RobTillaart/TCRT5000
36+
2937

3038
## Interface
3139

3240
```cpp
3341
#include "PIR.h"
3442
```
3543

36-
#### Base
44+
### Base
3745

38-
- **PIR()** constructor. Allocated room for 8 PIRs.
46+
- **PIR()** constructor. Default allocated room for 8 PIRs.
3947
- **uint8_t add(uint8_t pin)** adds a PIR pin to the set of pins.
4048
Returns the index or PIR_ARRAY_FULL (0xFE)
41-
- **uint8_t count** returns number of PIR sensors added.
49+
- **uint8_t add(uint8_t \* pins, uint8_t length)** adds an array of PIR pins
50+
to the set of pins if there is enough room for all pins of the array.
51+
If there is no room in the internal set to add length pins, no pin is added.
52+
Returns the (last) index or PIR_ARRAY_FULL (0xFE) in case no pin is added.
53+
- **void reset()** removes all pins from the internal set, reset lastRead too.
54+
- **uint8_t count()** returns number of PIR sensors added.
55+
- **uint8_t free()** returns number of free slots to add.
4256

4357

44-
#### Read
58+
### Read
4559

4660
- **uint8_t read()** read all PIR sensors in the set.
4761
Returns a bit mask of HIGH / LOW values.
@@ -64,16 +78,14 @@ This can improve processing in some cases.
6478

6579
- add examples
6680
- interrupts?
81+
- investigate dynamic allocation in constructor (0.3.0)
82+
- 0.2.0 has compile time define.
6783

6884
#### Could
6985

70-
- investigate PIR16 PIR32 class that can hold more
71-
- think MEGA2560.
72-
- or dynamic allocation? 0.2.0
73-
- one lastRead for all?
74-
- **uint8_t pin(uint8_t index)** to get back configuration
75-
- **bool add(uint8_t array, length)** faster configuration. Return true if there was enough room.
76-
- **clear()** to reset whole object?
86+
- one lastRead for all
87+
- **uint8_t getPinAtIndex(uint8_t index)** to get back configuration
88+
7789

7890
#### Wont
7991

libraries/PIR/examples/PIR_demo/PIR_demo.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ PIR P;
1313

1414
void setup()
1515
{
16-
Serial.begin(115200);
1716
while (!Serial);
17+
Serial.begin(115200);
18+
Serial.println();
1819
Serial.println(__FILE__);
1920
Serial.print("PIR_LIB_VERSION: ");
2021
Serial.println(PIR_LIB_VERSION);
22+
Serial.println();
2123

2224
P.add(3);
2325
P.add(4);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// FILE: PIR_demo_add_array.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo pir sensor class
5+
// URL: https://github.com/RobTillaart/PIR
6+
7+
8+
#include "PIR.h"
9+
10+
11+
PIR P;
12+
13+
uint8_t pins[4] = {3, 4, 5, 6};
14+
15+
void setup()
16+
{
17+
while (!Serial);
18+
Serial.begin(115200);
19+
Serial.println();
20+
Serial.println(__FILE__);
21+
Serial.print("PIR_LIB_VERSION: ");
22+
Serial.println(PIR_LIB_VERSION);
23+
Serial.println();
24+
25+
P.add(pins, 4);
26+
}
27+
28+
29+
void loop()
30+
{
31+
uint8_t x = P.read();
32+
Serial.println(x, HEX);
33+
delay(1000);
34+
}
35+
36+
37+
// -- END OF FILE --

libraries/PIR/examples/PIR_demo_lastValue/PIR_demo_lastValue.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ PIR P;
1111

1212
void setup()
1313
{
14-
Serial.begin(115200);
1514
while (!Serial);
15+
Serial.begin(115200);
16+
Serial.println();
1617
Serial.println(__FILE__);
1718
Serial.print("PIR_LIB_VERSION: ");
1819
Serial.println(PIR_LIB_VERSION);
20+
Serial.println();
1921

2022
// add 3 PIR sensors
2123
P.add(3);

libraries/PIR/examples/PIR_performance/PIR_performance.ino

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ uint32_t start, stop;
1313

1414
void setup()
1515
{
16-
Serial.begin(115200);
1716
while (!Serial);
17+
Serial.begin(115200);
18+
Serial.println();
1819
Serial.println(__FILE__);
1920
Serial.print("PIR_LIB_VERSION: ");
2021
Serial.println(PIR_LIB_VERSION);
22+
Serial.println();
2123

2224
// fully loaded
2325
P.add(3);
@@ -29,6 +31,11 @@ void setup()
2931
P.add(9);
3032
P.add(10);
3133

34+
Serial.print("pins: ");
35+
Serial.println(P.count());
36+
Serial.println();
37+
delay(10);
38+
3239
test_read();
3340
test_lastValue();
3441
test_changed();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
IDE: 1.8.19
3+
BOARD: UNO R3
4+
5+
PIR_performance.ino
6+
PIR_LIB_VERSION: 0.2.0
7+
8+
pins: 8
9+
10+
11+
test_read
12+
TIME: 32
13+
VAL: FF
14+
15+
test_lastValue
16+
TIME: 4
17+
VAL: FF
18+
19+
test_changed
20+
TIME: 36
21+
VAL: 0
22+
23+

0 commit comments

Comments
 (0)