rpi-io 2.0.10 → 2.1.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/README.md +36 -18
- package/esm/main.mjs +47 -4
- package/history.md +12 -2
- package/package.json +1 -1
- package/test/model.js +10 -0
- package/test/pwm-led.js +1 -1
- package/test/pwm-motor.js +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# rpi-io
|
|
2
|
-
    
|
|
3
3
|
|
|
4
|
+
Current version is 2.1.0. See latest updates in *history.md*.
|
|
4
5
|
|
|
5
6
|
**rpi-io** is a lite [ESM](https://nodejs.org/api/esm.html#modules-ecmascript-modules) module for **Node.js** to control **Raspberry Pi** GPIO: access (in, out), input event detection and [PWM](https://en.wikipedia.org/wiki/Pulse-width_modulation) peripheral control.
|
|
6
7
|
|
|
@@ -14,7 +15,7 @@
|
|
|
14
15
|
- Input/Output - [libgpiod](https://libgpiod.readthedocs.io/en/stable/) v1.6.3 (*Bookworm*) and v2.2.1 (*Trixie*)
|
|
15
16
|
- PWM - [sysfs](https://en.wikipedia.org/wiki/Sysfs) interface.
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
|
|
18
19
|
## Prerequisites
|
|
19
20
|
|
|
20
21
|
### libgpiod
|
|
@@ -40,9 +41,9 @@ sudo apt-get install -y build-essential python3
|
|
|
40
41
|
|
|
41
42
|
### Node.js
|
|
42
43
|
|
|
43
|
-
Please make sure that you a recent version of Node.js
|
|
44
|
-
|
|
44
|
+
Please make sure that you a recent version of Node.js. Recommended tested versions are v23 and further as they support `require(esm)` by default.
|
|
45
45
|
|
|
46
|
+
Older versions starting at v20.19 where `require(esm)` has been backported should work as well, but they haven't been tested extensively.
|
|
46
47
|
|
|
47
48
|
## Installation
|
|
48
49
|
|
|
@@ -244,7 +245,7 @@ const btn = new RIO(18, "input", {bias: "pull-up"})
|
|
|
244
245
|
|
|
245
246
|
#### Servo motor
|
|
246
247
|
|
|
247
|
-
REMINDER: PWM peripherals used in hardware mode need some specific [configuration](
|
|
248
|
+
REMINDER: PWM peripherals used in hardware mode need some specific [configuration](# Configuration for PWM-based peripherals).
|
|
248
249
|
|
|
249
250
|
##### Diagram for servo-motor *SG90*
|
|
250
251
|
```
|
|
@@ -304,7 +305,7 @@ import {RIO, sleep, ctrlC, } from "rpi-io"
|
|
|
304
305
|
})()
|
|
305
306
|
```
|
|
306
307
|
|
|
307
|
-
|
|
308
|
+
#### Fade-in LED
|
|
308
309
|
|
|
309
310
|
PWM can also be used for progressive LED light with the same electronic circuit as write operations. See example below.
|
|
310
311
|
|
|
@@ -382,20 +383,23 @@ const myOutput = new RIO(17, "output")
|
|
|
382
383
|
|
|
383
384
|
```javascript
|
|
384
385
|
{
|
|
385
|
-
// output
|
|
386
|
+
// For 'output' mode: Initial value {0,1}.
|
|
386
387
|
value: 0,
|
|
387
|
-
|
|
388
|
+
|
|
389
|
+
// For 'input' mode: Circuit bias {"disable", "pull-up", "pull-down"}.
|
|
388
390
|
bias: "disable",
|
|
389
|
-
|
|
390
|
-
//
|
|
391
|
-
//
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
391
|
+
|
|
392
|
+
// For 'pwm' mode: Delay (ms) required on instance creation
|
|
393
|
+
// to prevent failure due to device performance.
|
|
394
|
+
//
|
|
395
|
+
// Since v2.1.0 the default value is set to -1, which means auto delay.
|
|
396
|
+
// It is computed according to device model e.g. 50 ms for 5Pi or 1000 ms RPi Zero.
|
|
397
|
+
exportTime: -1,
|
|
398
|
+
|
|
399
|
+
// For 'pwm' mode: Period defined in μs. Default value is equivalent to 50 Hz.
|
|
397
400
|
period: 20000,
|
|
398
|
-
|
|
401
|
+
|
|
402
|
+
// For 'pwm' mode: dutyMin and dutyMax defines the duty cycle use range in µs
|
|
399
403
|
// especially for servo-motors (See their specs!).
|
|
400
404
|
dutyMin: 0,
|
|
401
405
|
dutyMax: 20000
|
|
@@ -530,7 +534,7 @@ servo.pwmDuty(50)
|
|
|
530
534
|
|
|
531
535
|
|
|
532
536
|
|
|
533
|
-
###
|
|
537
|
+
### Static functions
|
|
534
538
|
|
|
535
539
|
#### RIO.closeAll()
|
|
536
540
|
Function to close all instances
|
|
@@ -545,6 +549,20 @@ RIO.closeAll()
|
|
|
545
549
|
|
|
546
550
|
|
|
547
551
|
|
|
552
|
+
#### RIO.model()
|
|
553
|
+
|
|
554
|
+
Function to return current model of RPi.
|
|
555
|
+
|
|
556
|
+
```javascript
|
|
557
|
+
import {RIO} from "rpi-io"
|
|
558
|
+
console.log("model:", RIO.model())
|
|
559
|
+
// Returns '5B', '4B', '3B', 'Zero2', 'Zero' or '' when unknown.
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
### Utilities
|
|
565
|
+
|
|
548
566
|
#### log(args)
|
|
549
567
|
|
|
550
568
|
Function similar to `console.log` but with timestamped prompt. Must be enabled or not by function `traceCfg` defined thereafter.
|
package/esm/main.mjs
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// RPI-IO: Nodejs GPIO control module
|
|
3
3
|
// -------------------------------------------------------------------
|
|
4
4
|
import {createRequire} from "node:module"
|
|
5
|
-
import {writeFileSync} from "node:fs"
|
|
5
|
+
import {writeFileSync, readFileSync} from "node:fs"
|
|
6
6
|
import {traceCfg, log, warn} from "./log.mjs"
|
|
7
|
-
import {sleep, ctrlC,lineNumber} from "./ctl.mjs"
|
|
7
|
+
import {sleep, ctrlC, lineNumber} from "./ctl.mjs"
|
|
8
8
|
import {wait, lineConfig} from "./nut.mjs"
|
|
9
9
|
|
|
10
10
|
export {traceCfg, log, warn, sleep, ctrlC, lineConfig, lineNumber}
|
|
@@ -45,7 +45,7 @@ export class RIO {
|
|
|
45
45
|
// input, output
|
|
46
46
|
bias: "disable", // "disable", "pull-up", "pull-down"
|
|
47
47
|
// pwm
|
|
48
|
-
exportTime:
|
|
48
|
+
exportTime: -1,
|
|
49
49
|
period: 20000, // μs ~50Hz
|
|
50
50
|
dutyMin: 0, // μs
|
|
51
51
|
dutyMax: 20000 // µs
|
|
@@ -69,6 +69,26 @@ export class RIO {
|
|
|
69
69
|
this.config = lineConfig(this.line) // Required for pwm
|
|
70
70
|
this.pwmExported = false
|
|
71
71
|
this.pwmEnabled = false
|
|
72
|
+
// Define exportTime when defined to automatic by default
|
|
73
|
+
if (opt.exportTime === -1) {
|
|
74
|
+
switch (RIO.model()) {
|
|
75
|
+
case "5B":
|
|
76
|
+
opt.exportTime = 50
|
|
77
|
+
break
|
|
78
|
+
case "4B":
|
|
79
|
+
opt.exportTime = 200
|
|
80
|
+
break
|
|
81
|
+
case "3B":
|
|
82
|
+
opt.exportTime = 500
|
|
83
|
+
break
|
|
84
|
+
case "Zero2":
|
|
85
|
+
opt.exportTime = 1000
|
|
86
|
+
break
|
|
87
|
+
case "Zero":
|
|
88
|
+
opt.exportTime = 1500
|
|
89
|
+
break
|
|
90
|
+
}
|
|
91
|
+
}
|
|
72
92
|
|
|
73
93
|
switch (this.mode) {
|
|
74
94
|
case "output":
|
|
@@ -293,8 +313,10 @@ export class RIO {
|
|
|
293
313
|
writeFileSync(this.pwmPathChannel + "duty_cycle", (this.dutyMin + ((percent / 100) * (this.dutyMax - this.dutyMin))).toString())
|
|
294
314
|
}
|
|
295
315
|
|
|
316
|
+
// -------------------------------------------------------------------
|
|
317
|
+
// STATIC FUNCTIONS
|
|
296
318
|
/** ------------------------------------------------------------------
|
|
297
|
-
* @
|
|
319
|
+
* @function RIO.closeAll
|
|
298
320
|
* @description Static method to close all instances
|
|
299
321
|
*/
|
|
300
322
|
static closeAll() {
|
|
@@ -302,6 +324,27 @@ export class RIO {
|
|
|
302
324
|
instance.close()
|
|
303
325
|
}
|
|
304
326
|
}
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
/** ------------------------------------------------------------------
|
|
330
|
+
* @function RIO.model
|
|
331
|
+
* @description Return Raspberry Pi model or empty string if not RPi
|
|
332
|
+
* @return {String}
|
|
333
|
+
*/
|
|
334
|
+
static model() {
|
|
335
|
+
try {
|
|
336
|
+
const model = readFileSync("/proc/device-tree/model", "utf8")
|
|
337
|
+
if (!model.includes("Raspberry Pi")) return ""
|
|
338
|
+
if (model.includes("5 Model B")) return "5B"
|
|
339
|
+
if (model.includes("4 Model B")) return "4B"
|
|
340
|
+
if (model.includes("3 Model B")) return "3B"
|
|
341
|
+
if (model.includes("Zero 2")) return "Zero2"
|
|
342
|
+
if (model.includes("Zero")) return "Zero"
|
|
343
|
+
return ""
|
|
344
|
+
} catch (e) {
|
|
345
|
+
return ""
|
|
346
|
+
}
|
|
347
|
+
}
|
|
305
348
|
}
|
|
306
349
|
|
|
307
350
|
// -------------------------------------------------------------------
|
package/history.md
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
2.0
|
|
1
|
+
2.1.0 - March 26, 2026
|
|
2
2
|
===================
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
* New default option in *constructor* method: `exporTime: -1` for automatic configuration of `exportTime` depending on device performance.
|
|
5
|
+
* New static method *Rio.model()* to return current RPi model.
|
|
6
|
+
|
|
7
|
+
2.0.11 - Feb 13, 2026
|
|
8
|
+
===================
|
|
9
|
+
|
|
10
|
+
* Minor updates in `README.md`:
|
|
11
|
+
* Fixes in code examples, where imported class is *RIO*, not *Rio*.
|
|
12
|
+
* Details about Node.js pre-requisites.
|
|
13
|
+
|
|
4
14
|
|
|
5
15
|
2.0.9 - Feb 03, 2026
|
|
6
16
|
===================
|
package/package.json
CHANGED
package/test/model.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// -------------------------------------------------------------------
|
|
2
|
+
// TEST - Simple write
|
|
3
|
+
// -------------------------------------------------------------------
|
|
4
|
+
import {RIO} from "../esm/main.mjs"
|
|
5
|
+
|
|
6
|
+
console.log("model:", RIO.model())
|
|
7
|
+
|
|
8
|
+
// -------------------------------------------------------------------
|
|
9
|
+
// EoF
|
|
10
|
+
// -------------------------------------------------------------------
|
package/test/pwm-led.js
CHANGED
|
@@ -10,7 +10,7 @@ import {RIO, traceCfg, log, sleep, ctrlC, lineNumber} from "../esm/main.mjs"
|
|
|
10
10
|
|
|
11
11
|
// Init pwm line and set duty = 0%
|
|
12
12
|
const led = new RIO(line, "pwm", {
|
|
13
|
-
exportTime:
|
|
13
|
+
exportTime: -1,// delay for export on init
|
|
14
14
|
period: 1000, // 1,000,000 ns ~ 1 KHz
|
|
15
15
|
dutyMin: 0,
|
|
16
16
|
dutyMax: 1000
|
package/test/pwm-motor.js
CHANGED
|
@@ -11,7 +11,7 @@ import {RIO, traceCfg, log, sleep, ctrlC, lineNumber} from "../esm/main.mjs"
|
|
|
11
11
|
|
|
12
12
|
// Init pwm line and set duty = 0%
|
|
13
13
|
const servo = new RIO(line, "pwm", {
|
|
14
|
-
exportTime:
|
|
14
|
+
exportTime: -1, // delay for export on init
|
|
15
15
|
period: 20000, // 20,000,000 ns ~ 50 Hz
|
|
16
16
|
dutyMin: 500, // 500,000 ns ~ 0.5 ms
|
|
17
17
|
dutyMax: 2500 // 2,500,000 ns ~ 2.5 ms
|