onoffrpi5 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +124 -0
- package/onoff.d.ts +49 -0
- package/onoff.js +437 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 piotranonimowy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# onoffrpi5
|
|
2
|
+
|
|
3
|
+
**Drop-in replacement for [onoff](https://github.com/fivdi/onoff) with full Raspberry Pi 5 compatibility.**
|
|
4
|
+
|
|
5
|
+
## Why this exists
|
|
6
|
+
|
|
7
|
+
The original `onoff` uses the Linux **sysfs GPIO interface** (`/sys/class/gpio`).
|
|
8
|
+
The Raspberry Pi 5 uses the **RP1** I/O controller chip, whose GPIO is only
|
|
9
|
+
accessible via the **character device API** (`/dev/gpiochipN`).
|
|
10
|
+
The sysfs interface is absent on Pi 5, making the original `onoff` non-functional.
|
|
11
|
+
|
|
12
|
+
This rewrite replaces sysfs with `node-libgpiod` (character device API) while
|
|
13
|
+
maintaining **100% API compatibility** with the original onoff public interface.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install onoffrpi5
|
|
21
|
+
# or if replacing onoff in an existing project:
|
|
22
|
+
npm uninstall onoff
|
|
23
|
+
npm install onoffrpi5
|
|
24
|
+
# then in your code replace:
|
|
25
|
+
# require('onoff') → require('onoffrpi5')
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**System requirement:** `libgpiod` must be installed on the Pi:
|
|
29
|
+
```bash
|
|
30
|
+
sudo apt install libgpiod2 libgpiod-dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Key differences from original onoff
|
|
36
|
+
|
|
37
|
+
| Concern | Original onoff | onoffrpi5 |
|
|
38
|
+
|---|---|---|
|
|
39
|
+
| GPIO backend | sysfs `/sys/class/gpio` | Character device `/dev/gpiochipN` |
|
|
40
|
+
| Pi 5 support | ❌ No | ✅ Yes |
|
|
41
|
+
| Pi 1–4 support | ✅ Yes | ✅ Yes (auto-detects chip) |
|
|
42
|
+
| Interrupt detection | epoll on sysfs `value` file | 1ms polling via `setInterval` |
|
|
43
|
+
| Native dependency | `epoll` (compiled) | `node-libgpiod` (compiled) |
|
|
44
|
+
| API compatibility | — | 100% drop-in |
|
|
45
|
+
|
|
46
|
+
> **Interrupt note:** The polling approach works well for buttons and switches
|
|
47
|
+
> (debounce tolerance already covers the 1ms poll jitter). For high-frequency
|
|
48
|
+
> interrupts (>500/sec), replace `_startWatch()` with `line.eventWait()` in a
|
|
49
|
+
> `worker_thread`.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## gpiochip auto-detection
|
|
54
|
+
|
|
55
|
+
The library automatically selects the gpiochip with the most lines:
|
|
56
|
+
|
|
57
|
+
| Board | Chip | Lines |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| Pi 5 (RP1) | `gpiochip4` | 54 |
|
|
60
|
+
| Pi 4 / 3 / 2 | `gpiochip0` | 54 |
|
|
61
|
+
|
|
62
|
+
You do not need to configure this manually.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Usage (identical to original onoff)
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
const { Gpio } = require('onoffrpi5');
|
|
70
|
+
|
|
71
|
+
// LED on GPIO17, button on GPIO4
|
|
72
|
+
const led = new Gpio(17, 'out');
|
|
73
|
+
const button = new Gpio(4, 'in', 'both');
|
|
74
|
+
|
|
75
|
+
button.watch((err, value) => {
|
|
76
|
+
if (err) throw err;
|
|
77
|
+
led.writeSync(value);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
process.on('SIGINT', () => {
|
|
81
|
+
led.unexport();
|
|
82
|
+
button.unexport();
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
// Debounced button with toggle
|
|
88
|
+
const { Gpio } = require('onoffrpi5');
|
|
89
|
+
const led = new Gpio(17, 'out');
|
|
90
|
+
const button = new Gpio(4, 'in', 'rising', { debounceTimeout: 10 });
|
|
91
|
+
|
|
92
|
+
button.watch((err) => {
|
|
93
|
+
if (err) throw err;
|
|
94
|
+
led.writeSync(led.readSync() ^ 1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
process.on('SIGINT', () => {
|
|
98
|
+
led.unexport();
|
|
99
|
+
button.unexport();
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
// Check accessibility (useful for dev machines without GPIO)
|
|
105
|
+
const { Gpio } = require('onoffrpi5');
|
|
106
|
+
|
|
107
|
+
if (Gpio.accessible) {
|
|
108
|
+
const led = new Gpio(17, 'out');
|
|
109
|
+
led.writeSync(Gpio.HIGH);
|
|
110
|
+
setTimeout(() => { led.writeSync(Gpio.LOW); led.unexport(); }, 1000);
|
|
111
|
+
} else {
|
|
112
|
+
console.log('No GPIO available on this system');
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## API
|
|
119
|
+
|
|
120
|
+
Identical to the original onoff — see https://github.com/fivdi/onoff#api
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
MIT
|
package/onoff.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
|
|
5
|
+
export type Direction = 'in' | 'out' | 'high' | 'low';
|
|
6
|
+
export type Edge = 'none' | 'rising' | 'falling' | 'both';
|
|
7
|
+
export type BinaryValue = 0 | 1;
|
|
8
|
+
|
|
9
|
+
export interface Options {
|
|
10
|
+
debounceTimeout?: number;
|
|
11
|
+
activeLow?: boolean;
|
|
12
|
+
reconfigureDirection?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type Callback = (err: Error | null | undefined, value: BinaryValue) => void;
|
|
16
|
+
|
|
17
|
+
export class Gpio extends EventEmitter {
|
|
18
|
+
constructor(gpio: number, direction: Direction, edge?: Edge, options?: Options);
|
|
19
|
+
constructor(gpio: number, direction: Direction, options?: Options);
|
|
20
|
+
|
|
21
|
+
read(callback: Callback): void;
|
|
22
|
+
read(): Promise<BinaryValue>;
|
|
23
|
+
|
|
24
|
+
readSync(): BinaryValue;
|
|
25
|
+
|
|
26
|
+
write(value: BinaryValue, callback: (err: Error | null | undefined) => void): void;
|
|
27
|
+
write(value: BinaryValue): Promise<void>;
|
|
28
|
+
|
|
29
|
+
writeSync(value: BinaryValue): void;
|
|
30
|
+
|
|
31
|
+
watch(callback: Callback): void;
|
|
32
|
+
unwatch(callback?: Callback): void;
|
|
33
|
+
unwatchAll(): void;
|
|
34
|
+
|
|
35
|
+
direction(): 'in' | 'out';
|
|
36
|
+
setDirection(direction: Direction): void;
|
|
37
|
+
|
|
38
|
+
edge(): Edge;
|
|
39
|
+
setEdge(edge: Edge): void;
|
|
40
|
+
|
|
41
|
+
activeLow(): boolean;
|
|
42
|
+
setActiveLow(invert: boolean): void;
|
|
43
|
+
|
|
44
|
+
unexport(): void;
|
|
45
|
+
|
|
46
|
+
static readonly accessible: boolean;
|
|
47
|
+
static readonly HIGH: 1;
|
|
48
|
+
static readonly LOW: 0;
|
|
49
|
+
}
|
package/onoff.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* onoff - Pi 5 compatible rewrite
|
|
5
|
+
*
|
|
6
|
+
* Original onoff uses the deprecated Linux sysfs GPIO interface
|
|
7
|
+
* (/sys/class/gpio) which is NOT available on Raspberry Pi 5 (RP1 chip).
|
|
8
|
+
*
|
|
9
|
+
* This rewrite uses the Linux GPIO character device API via node-libgpiod,
|
|
10
|
+
* maintaining 100% API compatibility with the original onoff public interface.
|
|
11
|
+
*
|
|
12
|
+
* Dependency: npm install node-libgpiod
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const EventEmitter = require('events').EventEmitter;
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const debounce = require('lodash.debounce');
|
|
18
|
+
|
|
19
|
+
let gpiod;
|
|
20
|
+
try {
|
|
21
|
+
gpiod = require('node-libgpiod');
|
|
22
|
+
} catch (e) {
|
|
23
|
+
// Not on a GPIO-capable system; accessible will be false
|
|
24
|
+
gpiod = null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Helpers
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Auto-detect the correct gpiochip device for the running board.
|
|
33
|
+
* Pi 5 exposes the RP1 GPIO bank as gpiochip4 (or gpiochip0 on some kernels).
|
|
34
|
+
* We find the chip that advertises the most lines (typically 54 for Pi).
|
|
35
|
+
*/
|
|
36
|
+
function detectGpioChip() {
|
|
37
|
+
if (!gpiod) return null;
|
|
38
|
+
|
|
39
|
+
let bestChip = null;
|
|
40
|
+
let bestLines = 0;
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i <= 9; i++) {
|
|
43
|
+
const path = `/dev/gpiochip${i}`;
|
|
44
|
+
if (!fs.existsSync(path)) continue;
|
|
45
|
+
try {
|
|
46
|
+
const chip = new gpiod.Chip(i);
|
|
47
|
+
const numLines = chip.numberOfLines;
|
|
48
|
+
if (numLines > bestLines) {
|
|
49
|
+
bestLines = numLines;
|
|
50
|
+
bestChip = i;
|
|
51
|
+
}
|
|
52
|
+
} catch (_) {
|
|
53
|
+
// skip chips we can't open
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return bestChip;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const CHIP_INDEX = detectGpioChip();
|
|
60
|
+
|
|
61
|
+
// Direction constants used internally
|
|
62
|
+
const DIRECTION_IN = 'in';
|
|
63
|
+
const DIRECTION_OUT = 'out';
|
|
64
|
+
|
|
65
|
+
// Edge constants
|
|
66
|
+
const EDGE_NONE = 'none';
|
|
67
|
+
const EDGE_RISING = 'rising';
|
|
68
|
+
const EDGE_FALLING = 'falling';
|
|
69
|
+
const EDGE_BOTH = 'both';
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// Gpio class
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
class Gpio extends EventEmitter {
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {number} gpio - BCM GPIO number
|
|
79
|
+
* @param {string} direction - 'in' | 'out' | 'high' | 'low'
|
|
80
|
+
* @param {string} [edge] - 'none' | 'rising' | 'falling' | 'both'
|
|
81
|
+
* @param {object} [options]
|
|
82
|
+
* @param {number} [options.debounceTimeout]
|
|
83
|
+
* @param {boolean} [options.activeLow]
|
|
84
|
+
* @param {boolean} [options.reconfigureDirection]
|
|
85
|
+
*/
|
|
86
|
+
constructor(gpio, direction, edge, options) {
|
|
87
|
+
super();
|
|
88
|
+
|
|
89
|
+
// Argument normalisation (edge is optional)
|
|
90
|
+
if (typeof edge === 'object' && edge !== null) {
|
|
91
|
+
options = edge;
|
|
92
|
+
edge = undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this._gpio = gpio;
|
|
96
|
+
this._options = Object.assign({
|
|
97
|
+
activeLow: false,
|
|
98
|
+
reconfigureDirection: true,
|
|
99
|
+
debounceTimeout: 0
|
|
100
|
+
}, options || {});
|
|
101
|
+
|
|
102
|
+
this._direction = (direction === 'high' || direction === 'out') ? DIRECTION_OUT
|
|
103
|
+
: (direction === 'low') ? DIRECTION_OUT
|
|
104
|
+
: DIRECTION_IN;
|
|
105
|
+
|
|
106
|
+
this._edge = edge || EDGE_NONE;
|
|
107
|
+
this._line = null;
|
|
108
|
+
this._chip = null;
|
|
109
|
+
this._watchers = [];
|
|
110
|
+
this._watchPoll = null;
|
|
111
|
+
this._lastValue = null;
|
|
112
|
+
|
|
113
|
+
this._export(direction);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// -------------------------------------------------------------------------
|
|
117
|
+
// Internal: export / configure the GPIO line
|
|
118
|
+
// -------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
_export(direction) {
|
|
121
|
+
if (!gpiod || CHIP_INDEX === null) return; // non-GPIO system
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
this._chip = new gpiod.Chip(CHIP_INDEX);
|
|
125
|
+
this._line = this._chip.getLine(this._gpio);
|
|
126
|
+
|
|
127
|
+
const activeLow = !!this._options.activeLow;
|
|
128
|
+
const flags = activeLow ? gpiod.Line.RequestFlags.ACTIVE_LOW : 0;
|
|
129
|
+
|
|
130
|
+
if (this._direction === DIRECTION_OUT) {
|
|
131
|
+
const initialValue = (direction === 'high') ? 1 : 0;
|
|
132
|
+
if (flags) {
|
|
133
|
+
this._line.requestOutputModeFlags('onoff', flags, initialValue);
|
|
134
|
+
} else {
|
|
135
|
+
this._line.requestOutputMode('onoff', initialValue);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
const isEvent = this._edge !== EDGE_NONE;
|
|
139
|
+
if (isEvent) {
|
|
140
|
+
if (flags) {
|
|
141
|
+
this._line.requestBothEdgesEventFlags('onoff', flags);
|
|
142
|
+
} else {
|
|
143
|
+
this._line.requestBothEdgesEvents('onoff');
|
|
144
|
+
}
|
|
145
|
+
this._startWatch();
|
|
146
|
+
} else {
|
|
147
|
+
if (flags) {
|
|
148
|
+
this._line.requestInputModeFlags('onoff', flags);
|
|
149
|
+
} else {
|
|
150
|
+
this._line.requestInputMode('onoff');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
this._lastValue = this._line.getValue();
|
|
154
|
+
}
|
|
155
|
+
} catch (err) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Failed to export GPIO${this._gpio} on gpiochip${CHIP_INDEX}: ${err.message}`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// -------------------------------------------------------------------------
|
|
163
|
+
// Internal: polling-based interrupt detection
|
|
164
|
+
// Using setInterval + getValue diff since node-libgpiod event streaming
|
|
165
|
+
// varies across versions; a 1ms poll is used for interrupts.
|
|
166
|
+
// For production use with high-frequency interrupts, replace with
|
|
167
|
+
// line.eventWait() in a worker_thread.
|
|
168
|
+
// -------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
_startWatch() {
|
|
171
|
+
if (this._watchPoll) return;
|
|
172
|
+
|
|
173
|
+
const intervalMs = 1; // 1ms poll — acceptable for most switch/button use cases
|
|
174
|
+
|
|
175
|
+
this._watchPoll = setInterval(() => {
|
|
176
|
+
if (!this._line) return;
|
|
177
|
+
try {
|
|
178
|
+
const val = this._line.getValue();
|
|
179
|
+
const activeLow = this._options.activeLow;
|
|
180
|
+
const adjusted = activeLow ? (val ^ 1) : val;
|
|
181
|
+
|
|
182
|
+
if (this._lastValue === null) {
|
|
183
|
+
this._lastValue = adjusted;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const changed = adjusted !== this._lastValue;
|
|
188
|
+
const risingOk = this._edge === EDGE_RISING && adjusted === 1;
|
|
189
|
+
const fallingOk = this._edge === EDGE_FALLING && adjusted === 0;
|
|
190
|
+
const bothOk = this._edge === EDGE_BOTH;
|
|
191
|
+
|
|
192
|
+
if (changed && (risingOk || fallingOk || bothOk)) {
|
|
193
|
+
this._lastValue = adjusted;
|
|
194
|
+
this._fireWatchers(null, adjusted);
|
|
195
|
+
}
|
|
196
|
+
} catch (err) {
|
|
197
|
+
this._fireWatchers(err, 0);
|
|
198
|
+
}
|
|
199
|
+
}, intervalMs);
|
|
200
|
+
|
|
201
|
+
// Don't keep Node.js alive just for GPIO polling
|
|
202
|
+
if (this._watchPoll.unref) this._watchPoll.unref();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_stopWatch() {
|
|
206
|
+
if (this._watchPoll) {
|
|
207
|
+
clearInterval(this._watchPoll);
|
|
208
|
+
this._watchPoll = null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
_fireWatchers(err, value) {
|
|
213
|
+
const debounceMs = this._options.debounceTimeout;
|
|
214
|
+
|
|
215
|
+
if (debounceMs > 0) {
|
|
216
|
+
if (!this._debouncedFire) {
|
|
217
|
+
this._debouncedFire = debounce((e, v) => {
|
|
218
|
+
for (const cb of this._watchers) cb(e, v);
|
|
219
|
+
}, debounceMs);
|
|
220
|
+
}
|
|
221
|
+
this._debouncedFire(err, value);
|
|
222
|
+
} else {
|
|
223
|
+
for (const cb of this._watchers) cb(err, value);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// -------------------------------------------------------------------------
|
|
228
|
+
// Public API — matches original onoff exactly
|
|
229
|
+
// -------------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Read GPIO value asynchronously.
|
|
233
|
+
* Returns a Promise if no callback supplied.
|
|
234
|
+
*/
|
|
235
|
+
read(callback) {
|
|
236
|
+
if (callback) {
|
|
237
|
+
try {
|
|
238
|
+
callback(null, this.readSync());
|
|
239
|
+
} catch (err) {
|
|
240
|
+
callback(err);
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
return new Promise((resolve, reject) => {
|
|
245
|
+
try {
|
|
246
|
+
resolve(this.readSync());
|
|
247
|
+
} catch (err) {
|
|
248
|
+
reject(err);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Read GPIO value synchronously. Returns 0 or 1.
|
|
255
|
+
*/
|
|
256
|
+
readSync() {
|
|
257
|
+
if (!this._line) return 0;
|
|
258
|
+
const raw = this._line.getValue();
|
|
259
|
+
return this._options.activeLow ? (raw ^ 1) : raw;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Write GPIO value asynchronously.
|
|
264
|
+
* Returns a Promise if no callback supplied.
|
|
265
|
+
*/
|
|
266
|
+
write(value, callback) {
|
|
267
|
+
if (callback) {
|
|
268
|
+
try {
|
|
269
|
+
this.writeSync(value);
|
|
270
|
+
callback(null);
|
|
271
|
+
} catch (err) {
|
|
272
|
+
callback(err);
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
return new Promise((resolve, reject) => {
|
|
277
|
+
try {
|
|
278
|
+
this.writeSync(value);
|
|
279
|
+
resolve();
|
|
280
|
+
} catch (err) {
|
|
281
|
+
reject(err);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Write GPIO value synchronously.
|
|
288
|
+
*/
|
|
289
|
+
writeSync(value) {
|
|
290
|
+
if (!this._line) return;
|
|
291
|
+
if (value !== 0 && value !== 1) {
|
|
292
|
+
throw new Error(`Value must be 0 or 1, got ${value}`);
|
|
293
|
+
}
|
|
294
|
+
const toWrite = this._options.activeLow ? (value ^ 1) : value;
|
|
295
|
+
this._line.setValue(toWrite);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Watch for hardware interrupts on the GPIO.
|
|
300
|
+
*/
|
|
301
|
+
watch(callback) {
|
|
302
|
+
if (this._edge === EDGE_NONE) {
|
|
303
|
+
// No edge configured — start watching anyway (caller's responsibility)
|
|
304
|
+
this._startWatch();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const debounceMs = this._options.debounceTimeout;
|
|
308
|
+
if (debounceMs > 0) {
|
|
309
|
+
const debounced = debounce(callback, debounceMs);
|
|
310
|
+
debounced._original = callback;
|
|
311
|
+
this._watchers.push(debounced);
|
|
312
|
+
} else {
|
|
313
|
+
this._watchers.push(callback);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Stop watching for hardware interrupts.
|
|
319
|
+
*/
|
|
320
|
+
unwatch(callback) {
|
|
321
|
+
if (!callback) {
|
|
322
|
+
this._watchers = [];
|
|
323
|
+
} else {
|
|
324
|
+
this._watchers = this._watchers.filter(cb => {
|
|
325
|
+
return cb !== callback && cb._original !== callback;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
if (this._watchers.length === 0) {
|
|
329
|
+
this._stopWatch();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Remove all hardware interrupt watchers.
|
|
335
|
+
*/
|
|
336
|
+
unwatchAll() {
|
|
337
|
+
this.unwatch();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Get GPIO direction.
|
|
342
|
+
*/
|
|
343
|
+
direction() {
|
|
344
|
+
return this._direction;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Set GPIO direction.
|
|
349
|
+
* NOTE: on the character device API this requires releasing and
|
|
350
|
+
* re-requesting the line.
|
|
351
|
+
*/
|
|
352
|
+
setDirection(direction) {
|
|
353
|
+
const flags = this._options.activeLow ? gpiod.Line.RequestFlags.ACTIVE_LOW : 0;
|
|
354
|
+
|
|
355
|
+
this._line.release();
|
|
356
|
+
|
|
357
|
+
if (direction === 'in') {
|
|
358
|
+
this._direction = DIRECTION_IN;
|
|
359
|
+
if (flags) this._line.requestInputModeFlags('onoff', flags);
|
|
360
|
+
else this._line.requestInputMode('onoff');
|
|
361
|
+
} else {
|
|
362
|
+
this._direction = DIRECTION_OUT;
|
|
363
|
+
const initial = (direction === 'high') ? 1 : 0;
|
|
364
|
+
if (flags) this._line.requestOutputModeFlags('onoff', flags, initial);
|
|
365
|
+
else this._line.requestOutputMode('onoff', initial);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Get GPIO interrupt generating edge.
|
|
371
|
+
*/
|
|
372
|
+
edge() {
|
|
373
|
+
return this._edge;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Set GPIO interrupt generating edge.
|
|
378
|
+
*/
|
|
379
|
+
setEdge(edge) {
|
|
380
|
+
this._edge = edge;
|
|
381
|
+
this._stopWatch();
|
|
382
|
+
if (edge !== EDGE_NONE) {
|
|
383
|
+
this._startWatch();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Get activeLow setting.
|
|
389
|
+
*/
|
|
390
|
+
activeLow() {
|
|
391
|
+
return this._options.activeLow;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Set activeLow setting.
|
|
396
|
+
*/
|
|
397
|
+
setActiveLow(invert) {
|
|
398
|
+
this._options.activeLow = invert;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Release the GPIO line and free resources.
|
|
403
|
+
* Always call this on process exit / SIGINT.
|
|
404
|
+
*/
|
|
405
|
+
unexport() {
|
|
406
|
+
this._stopWatch();
|
|
407
|
+
this._watchers = [];
|
|
408
|
+
|
|
409
|
+
if (this._line) {
|
|
410
|
+
try { this._line.release(); } catch (_) {}
|
|
411
|
+
this._line = null;
|
|
412
|
+
}
|
|
413
|
+
this._chip = null;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// -------------------------------------------------------------------------
|
|
417
|
+
// Static members
|
|
418
|
+
// -------------------------------------------------------------------------
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* True if the current process can access GPIO hardware.
|
|
422
|
+
*/
|
|
423
|
+
static get accessible() {
|
|
424
|
+
if (!gpiod || CHIP_INDEX === null) return false;
|
|
425
|
+
try {
|
|
426
|
+
new gpiod.Chip(CHIP_INDEX);
|
|
427
|
+
return true;
|
|
428
|
+
} catch (_) {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
static get HIGH() { return 1; }
|
|
434
|
+
static get LOW() { return 0; }
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
module.exports = { Gpio };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "onoffrpi5",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "GPIO access and interrupt detection with Node.js — Raspberry Pi 5 compatible (character device API). Drop-in replacement for onoff.",
|
|
5
|
+
"main": "onoff.js",
|
|
6
|
+
"types": "onoff.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"onoff.js",
|
|
9
|
+
"onoff.d.ts",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"gpio",
|
|
15
|
+
"raspberry-pi",
|
|
16
|
+
"raspberry-pi-5",
|
|
17
|
+
"pi5",
|
|
18
|
+
"rp1",
|
|
19
|
+
"onoff",
|
|
20
|
+
"libgpiod",
|
|
21
|
+
"gpiochip",
|
|
22
|
+
"interrupt",
|
|
23
|
+
"iot"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/piotranonimowy/onoff-pi5.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/piotranonimowy/onoff-pi5/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/piotranonimowy/onoff-pi5#readme",
|
|
33
|
+
"author": "piotranonimowy",
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=14.0.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"node-libgpiod": "^0.6.0",
|
|
39
|
+
"lodash.debounce": "^4.0.8"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT"
|
|
42
|
+
}
|