rpi-io 2.0.11 → 2.1.1
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/CHANGELOG.md +49 -0
- package/README.md +8 -288
- package/documentation/api.md +244 -0
- package/documentation/benchmark.md +22 -0
- package/documentation/examples.md +25 -0
- package/esm/main.mjs +47 -4
- 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/history.md +0 -41
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
## [2.1.1] - 2026-03-26
|
|
3
|
+
### Changed
|
|
4
|
+
- Updated doc organization and content
|
|
5
|
+
|
|
6
|
+
## [2.1.0] - 2026-03-26
|
|
7
|
+
### Added
|
|
8
|
+
- Default option in *constructor* method: `exporTime: -1` for automatic configuration of `exportTime` depending on device performance.
|
|
9
|
+
- Static method *Rio.model()* to return current RPi model.
|
|
10
|
+
|
|
11
|
+
## [2.0.11] - 2026-02-13
|
|
12
|
+
### Changed
|
|
13
|
+
- Minor updates in `README.md`:
|
|
14
|
+
* Typos in code examples, where imported class is *RIO*, not *Rio*.
|
|
15
|
+
* Details about Node.js pre-requisites.
|
|
16
|
+
|
|
17
|
+
## [2.0.9] - 2026-02-03
|
|
18
|
+
### Changed
|
|
19
|
+
- `package.json`: node-gyp version update to fix *tar* vulnerabilities
|
|
20
|
+
- `README.md`: Explanations for *tar* warnings.
|
|
21
|
+
|
|
22
|
+
## [2.0.5] - 2025-12-01
|
|
23
|
+
### Changed
|
|
24
|
+
- Typos
|
|
25
|
+
- Internal refactoring
|
|
26
|
+
|
|
27
|
+
## [2.0.0] - 2025-11-26
|
|
28
|
+
### Added
|
|
29
|
+
* Hybrid architecture Javascript + C addon to improve performances
|
|
30
|
+
* New API
|
|
31
|
+
|
|
32
|
+
## [1.2.2] - 2025-11-10
|
|
33
|
+
### Changed
|
|
34
|
+
* Utility API function name: `Rio.log` renamed to `Rio.logCfg`.
|
|
35
|
+
|
|
36
|
+
## [1.2.2] - 2025-11-07
|
|
37
|
+
### Added
|
|
38
|
+
* Option parameter for method `monitor` to take care of bias and bounces.
|
|
39
|
+
### Fixed
|
|
40
|
+
* Minor bugs and optimization
|
|
41
|
+
|
|
42
|
+
## [1.1.0] - 2025-10-31
|
|
43
|
+
### Added
|
|
44
|
+
* Support libgpiod 2.2.1
|
|
45
|
+
* Tested with *Trixie*
|
|
46
|
+
|
|
47
|
+
## [1.0.0] - 2025-10-23
|
|
48
|
+
### Added
|
|
49
|
+
Initial release: *Bookworm*, libgpiod 1.63
|
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# rpi-io
|
|
2
|
-
    
|
|
3
3
|
|
|
4
|
+
Current version is 2.1.1. More details in [CHANGELOG.md](CHANGELOG.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
|
|
|
@@ -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
|
|
|
@@ -333,291 +334,10 @@ import {RIO, sleep, ctrlC, } from "rpi-io"
|
|
|
333
334
|
})()
|
|
334
335
|
```
|
|
335
336
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
```bash
|
|
341
|
-
# Simple write
|
|
342
|
-
node /your-project/node_modules/rpi-io/test/write.js
|
|
343
|
-
|
|
344
|
-
# Read and monitor
|
|
345
|
-
node /your-project/node_modules/rpi-io/test/read.js
|
|
346
|
-
|
|
347
|
-
# LED fade-in on PWM line
|
|
348
|
-
node /your-project/node_modules/rpi-io/test/pwm-led.js
|
|
349
|
-
|
|
350
|
-
# Servo-motor SG90 controlled by PWM
|
|
351
|
-
node /your-project/node_modules/rpi-io/test/pwm-motor.js
|
|
352
|
-
|
|
353
|
-
# Test duplicated instance error
|
|
354
|
-
node /your-project/node_modules/rpi-io/test/duplicate-error.js
|
|
355
|
-
|
|
356
|
-
# Test close of all instances
|
|
357
|
-
node /your-project/node_modules/rpi-io/test/close-all.js
|
|
358
|
-
|
|
359
|
-
# Test GPIO line configuration
|
|
360
|
-
node /your-project/node_modules/rpi-io/test/line-configuration.js
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
## API
|
|
366
|
-
|
|
367
|
-
The **rpi-io** API is based on the class `RIO` with instance methods.
|
|
368
|
-
|
|
369
|
-
### RIO class and methods
|
|
370
|
-
|
|
371
|
-
#### constructor RIO(line, mode, opt)
|
|
372
|
-
To create instance according to parameters.
|
|
373
|
-
##### Example
|
|
374
|
-
```javascript
|
|
375
|
-
import {RIO} from "rpi-io"
|
|
376
|
-
const myOutput = new RIO(17, "output")
|
|
377
|
-
```
|
|
378
|
-
##### Parameter(s)
|
|
379
|
-
- **line** *{Number}* Must be one of the GPIO number as defined in [pinout.xyz](https://pinout.xyz).
|
|
380
|
-
- **mode** *{String}* Must be one of the following values: "output", "input", "pwm".
|
|
381
|
-
- **opt** *{Object}* Various options depending on selected mode. See details and default values below.
|
|
382
|
-
|
|
383
|
-
```javascript
|
|
384
|
-
{
|
|
385
|
-
// output - Initial value
|
|
386
|
-
value: 0,
|
|
387
|
-
// input - Circuit bias: "disable", "pull-up", "pull-down"
|
|
388
|
-
bias: "disable",
|
|
389
|
-
// pwm - Delay time (ms) required on instance creation
|
|
390
|
-
// depending on device performance. If this value is
|
|
391
|
-
// too small, the PWM instance creation fails.
|
|
392
|
-
// Can be reduced for latest RPi 5
|
|
393
|
-
// Must be increased for old RPi Zero.
|
|
394
|
-
exportTime: 100,
|
|
395
|
-
// pwm - period defined in μs. Default value is equivalent
|
|
396
|
-
// to a 50 Hz frequency.
|
|
397
|
-
period: 20000,
|
|
398
|
-
// pwm - dutyMin and dutyMax defines the duty cycle use range in µs
|
|
399
|
-
// especially for servo-motors (See their specs!).
|
|
400
|
-
dutyMin: 0,
|
|
401
|
-
dutyMax: 20000
|
|
402
|
-
}
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
##### Return
|
|
406
|
-
|
|
407
|
-
*{Object}* RIO instance
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
#### close()
|
|
412
|
-
To close instance i.e.
|
|
413
|
-
|
|
414
|
-
- to prevent future use of the line with the current instance
|
|
415
|
-
- to stop related monitoring process if any
|
|
416
|
-
- to free PWM resources if any
|
|
417
|
-
|
|
418
|
-
To reuse a line when closed, a new instance must be created.
|
|
419
|
-
|
|
420
|
-
##### Example
|
|
421
|
-
|
|
422
|
-
```javascript
|
|
423
|
-
import {RIO} from "rpi-io"
|
|
424
|
-
const myOutput = new RIO(17, "output")
|
|
425
|
-
myOutput.close()
|
|
426
|
-
```
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
#### write(value)
|
|
431
|
-
|
|
432
|
-
To write some value to "output" instance.
|
|
433
|
-
|
|
434
|
-
##### Example
|
|
435
|
-
|
|
436
|
-
```javascript
|
|
437
|
-
import {RIO} from "rpi-io"
|
|
438
|
-
const myOutput = new RIO(17, "output")
|
|
439
|
-
myOuput.write(1)
|
|
440
|
-
```
|
|
441
|
-
|
|
442
|
-
##### Parameter(s)
|
|
443
|
-
|
|
444
|
-
- **value** *{Number}* 0 or 1
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
#### read()
|
|
449
|
-
|
|
450
|
-
To read value from "input" instance.
|
|
451
|
-
|
|
452
|
-
##### Example
|
|
453
|
-
|
|
454
|
-
```javascript
|
|
455
|
-
import {RIO} from "rpi-io"
|
|
456
|
-
const myInput = new RIO(18, "input")
|
|
457
|
-
const result = myInput.read()
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
##### Return
|
|
461
|
-
|
|
462
|
-
*{Number}* 0 or 1
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
#### monitoringStart(callback, edge, bounce)
|
|
467
|
-
|
|
468
|
-
To start event monitoring of "input" instance.
|
|
469
|
-
|
|
470
|
-
##### Example
|
|
471
|
-
|
|
472
|
-
```javascript
|
|
473
|
-
import {RIO} from "rpi-io"
|
|
474
|
-
const myButton = new RIO(18, "input")
|
|
475
|
-
const callback = edge => {
|
|
476
|
-
console.log("edge:", edge)
|
|
477
|
-
}
|
|
478
|
-
myButton.monitoringStart(callback, "both", 30)
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
##### Parameter(s)
|
|
482
|
-
|
|
483
|
-
- **callback** *{Function}* Function triggered by input events where parameter is *edge* that can be either "rising" (input change from 0 to 1) or "falling" (input change from 1 to 0).
|
|
484
|
-
- **edge** *{String}* Filter of monitored events: "rising", "falling", "both" (default value).
|
|
485
|
-
- **bounce** *{Number}* Set threshold in ms to filter consecutive events of same type. Default value is 0.
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
#### monitoringStop()
|
|
490
|
-
|
|
491
|
-
To stop event monitoring of "input" instance.
|
|
492
|
-
|
|
493
|
-
##### Example
|
|
494
|
-
|
|
495
|
-
```javascript
|
|
496
|
-
import {RIO} from "rpi-io"
|
|
497
|
-
const myButton = new RIO(18, "input")
|
|
498
|
-
const callback =
|
|
499
|
-
myButton.monitoringStart(edge => {console.log("edge:", edge)})
|
|
500
|
-
// Stop monitoring after 10s
|
|
501
|
-
setTimeout(() => {
|
|
502
|
-
myButton.monitoringStop()
|
|
503
|
-
}, 10000)
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
#### pwmDuty(percent)
|
|
509
|
-
|
|
510
|
-
To change the *duty cycle* of a "pwm" instance. The parameter is defined as a percentage to compute a *duty cycle* based on the *dutyMin* and *dutyMax* values of instance definition.
|
|
511
|
-
|
|
512
|
-
##### Example
|
|
513
|
-
|
|
514
|
-
```javascript
|
|
515
|
-
import {RIO} from "rpi-io"
|
|
516
|
-
// Servo-motor instance
|
|
517
|
-
const servo = new RIO(13, "pwm", {
|
|
518
|
-
exportTime: 100,
|
|
519
|
-
period: 20000,
|
|
520
|
-
dutyMin: 500,
|
|
521
|
-
dutyMax: 2500})
|
|
522
|
-
|
|
523
|
-
// duty cycle = 50% between dutyMin and dutyMax
|
|
524
|
-
servo.pwmDuty(50)
|
|
525
|
-
```
|
|
526
|
-
|
|
527
|
-
##### Parameter(s)
|
|
528
|
-
|
|
529
|
-
- **percent** *{Number}* 0 ≤ percent ≤ 100
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
### Utilities
|
|
534
|
-
|
|
535
|
-
#### RIO.closeAll()
|
|
536
|
-
Function to close all instances
|
|
537
|
-
##### Example
|
|
538
|
-
```javascript
|
|
539
|
-
import {RIO} from "rpi-io"
|
|
540
|
-
const led = new RIO(17, "output", {value: 0})
|
|
541
|
-
const btn = new RIO(18, "input")
|
|
542
|
-
RIO.closeAll()
|
|
543
|
-
// led and btn are closed
|
|
544
|
-
```
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
#### log(args)
|
|
549
|
-
|
|
550
|
-
Function similar to `console.log` but with timestamped prompt. Must be enabled or not by function `traceCfg` defined thereafter.
|
|
551
|
-
|
|
552
|
-
#### warn(args)
|
|
553
|
-
|
|
554
|
-
Function similar to `log` but colored and prefixed by sign ⚠️.
|
|
555
|
-
|
|
556
|
-
#### traceCfg(level)
|
|
557
|
-
|
|
558
|
-
Function to enable functions `log` and `warn`.
|
|
559
|
-
|
|
560
|
-
##### Example for log, warn and traceCfg
|
|
561
|
-
|
|
562
|
-
```javascript
|
|
563
|
-
import {traceCfg, log, warn} from "rpi-io"
|
|
564
|
-
// traceCfg(0) => warn and log are disabled
|
|
565
|
-
// traceCfg(1) => warn is enabled and log is disabled
|
|
566
|
-
// traceCfg(2) => warn and log are enabled
|
|
567
|
-
traceCfg(2)
|
|
568
|
-
warn("This is important info")
|
|
569
|
-
log("This is useful info")
|
|
570
|
-
```
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
#### sleep(time)
|
|
575
|
-
|
|
576
|
-
Similar to `setTimeout` but in async mode.
|
|
577
|
-
|
|
578
|
-
#### ctrlC(function)
|
|
579
|
-
|
|
580
|
-
To run a callback function on ctrl+c event.
|
|
581
|
-
|
|
582
|
-
##### Example for sleep and ctrlC
|
|
583
|
-
|
|
584
|
-
```javascript
|
|
585
|
-
import {sleep, ctrlC} from "rpi-io"
|
|
586
|
-
(async () => {
|
|
587
|
-
ctrlC(() => {
|
|
588
|
-
console.log("displayed on script interrupt")
|
|
589
|
-
})
|
|
590
|
-
await sleep(10000)
|
|
591
|
-
console.log("displayed after 10s")
|
|
592
|
-
})()
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
## Benchmark
|
|
599
|
-
|
|
600
|
-
The following tables summarize main operation times on various devices. Results are in micro-seconds (µs).
|
|
601
|
-
|
|
602
|
-
**PLEASE NOTE**: As PWM operations rely on files, performance of microSD cards installed on RPi may have a significant impact on results for `pwmDuty()`.
|
|
603
|
-
|
|
604
|
-
**OS Bookworm with libgpiod v1.6.3**
|
|
605
|
-
|
|
606
|
-
| | RPi 5B | RPi 4B | RPi Zero2 |
|
|
607
|
-
| --------------- | ------- | ------- | --------- |
|
|
608
|
-
| write | 0.72 µs | 1.30 µs | 2,37 µs |
|
|
609
|
-
| read | 1.32 µs | 1,09 µs | 1,90 µs |
|
|
610
|
-
| pwmDuty (1 KHz) | 15.6 µs | 25,6 µs | 48,4 µs |
|
|
611
|
-
|
|
612
|
-
**OS Trixie with libgpiod v2.2.1**
|
|
613
|
-
|
|
614
|
-
| | RPi 5B | RPi 4B | RPi Zero2 |
|
|
615
|
-
| --------------- | ------------ | ------- | ------------ |
|
|
616
|
-
| write | *not tested* | 1.20 µs | *not tested* |
|
|
617
|
-
| read | *not tested* | 1.37 µs | *not tested* |
|
|
618
|
-
| pwmDuty (1 KHz) | *not tested* | 24,9 µs | *not tested* |
|
|
619
|
-
|
|
620
|
-
|
|
337
|
+
## More documentation
|
|
338
|
+
- [Examples](documentation/examples.md)
|
|
339
|
+
- [API reference guide](documentation/api.md)
|
|
340
|
+
- [Benchmark](documentation/benchmark.md)
|
|
621
341
|
|
|
622
342
|
## Licence
|
|
623
343
|
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# rpi-io API reference
|
|
2
|
+
The **rpi-io** API is based on the class `RIO` with instance methods and a few static functions and utilities
|
|
3
|
+
|
|
4
|
+
## RIO class and methods
|
|
5
|
+
|
|
6
|
+
### constructor RIO(line, mode, opt)
|
|
7
|
+
|
|
8
|
+
To create instance according to parameters.
|
|
9
|
+
#### Example
|
|
10
|
+
```javascript
|
|
11
|
+
import {RIO} from "rpi-io"
|
|
12
|
+
const myOutput = new RIO(17, "output")
|
|
13
|
+
```
|
|
14
|
+
#### Parameter(s)
|
|
15
|
+
- **line** *{Number}* Must be one of the GPIO number as defined in [pinout.xyz](https://pinout.xyz).
|
|
16
|
+
- **mode** *{String}* Must be one of the following values: "output", "input", "pwm".
|
|
17
|
+
- **opt** *{Object}* Various options depending on selected mode. See details and default values below.
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
{
|
|
21
|
+
// For 'output' mode: Initial value {0,1}.
|
|
22
|
+
value: 0,
|
|
23
|
+
|
|
24
|
+
// For 'input' mode: Circuit bias {"disable", "pull-up", "pull-down"}.
|
|
25
|
+
bias: "disable",
|
|
26
|
+
|
|
27
|
+
// For 'pwm' mode: Delay (ms) required on instance creation
|
|
28
|
+
// to prevent failure due to device performance.
|
|
29
|
+
//
|
|
30
|
+
// Since v2.1.0 the default value is set to -1, which means auto delay.
|
|
31
|
+
// It is computed according to device model e.g. 50 ms for 5Pi or 1000 ms RPi Zero.
|
|
32
|
+
exportTime: -1,
|
|
33
|
+
|
|
34
|
+
// For 'pwm' mode: Period defined in μs. Default value is equivalent to 50 Hz.
|
|
35
|
+
period: 20000,
|
|
36
|
+
|
|
37
|
+
// For 'pwm' mode: dutyMin and dutyMax defines the duty cycle use range in µs
|
|
38
|
+
// especially for servo-motors (See their specs!).
|
|
39
|
+
dutyMin: 0,
|
|
40
|
+
dutyMax: 20000
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### Return
|
|
45
|
+
|
|
46
|
+
*{Object}* RIO instance
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### close()
|
|
51
|
+
To close instance i.e.
|
|
52
|
+
|
|
53
|
+
- to prevent future use of the line with the current instance
|
|
54
|
+
- to stop related monitoring process if any
|
|
55
|
+
- to free PWM resources if any
|
|
56
|
+
|
|
57
|
+
To reuse a line when closed, a new instance must be created.
|
|
58
|
+
|
|
59
|
+
#### Example
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import {RIO} from "rpi-io"
|
|
63
|
+
const myOutput = new RIO(17, "output")
|
|
64
|
+
myOutput.close()
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
### write(value)
|
|
70
|
+
|
|
71
|
+
To write some value to "output" instance.
|
|
72
|
+
|
|
73
|
+
#### Example
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
import {RIO} from "rpi-io"
|
|
77
|
+
const myOutput = new RIO(17, "output")
|
|
78
|
+
myOuput.write(1)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Parameter(s)
|
|
82
|
+
|
|
83
|
+
- **value** *{Number}* 0 or 1
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
### read()
|
|
88
|
+
|
|
89
|
+
To read value from "input" instance.
|
|
90
|
+
|
|
91
|
+
#### Example
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
import {RIO} from "rpi-io"
|
|
95
|
+
const myInput = new RIO(18, "input")
|
|
96
|
+
const result = myInput.read()
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Return
|
|
100
|
+
|
|
101
|
+
*{Number}* 0 or 1
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
### monitoringStart(callback, edge, bounce)
|
|
106
|
+
|
|
107
|
+
To start event monitoring of "input" instance.
|
|
108
|
+
|
|
109
|
+
#### Example
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
import {RIO} from "rpi-io"
|
|
113
|
+
const myButton = new RIO(18, "input")
|
|
114
|
+
const callback = edge => {
|
|
115
|
+
console.log("edge:", edge)
|
|
116
|
+
}
|
|
117
|
+
myButton.monitoringStart(callback, "both", 30)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Parameter(s)
|
|
121
|
+
|
|
122
|
+
- **callback** *{Function}* Function triggered by input events where parameter is *edge* that can be either "rising" (input change from 0 to 1) or "falling" (input change from 1 to 0).
|
|
123
|
+
- **edge** *{String}* Filter of monitored events: "rising", "falling", "both" (default value).
|
|
124
|
+
- **bounce** *{Number}* Set threshold in ms to filter consecutive events of same type. Default value is 0.
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
### monitoringStop()
|
|
129
|
+
|
|
130
|
+
To stop event monitoring of "input" instance.
|
|
131
|
+
|
|
132
|
+
#### Example
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
import {RIO} from "rpi-io"
|
|
136
|
+
const myButton = new RIO(18, "input")
|
|
137
|
+
const callback =
|
|
138
|
+
myButton.monitoringStart(edge => {console.log("edge:", edge)})
|
|
139
|
+
// Stop monitoring after 10s
|
|
140
|
+
setTimeout(() => {
|
|
141
|
+
myButton.monitoringStop()
|
|
142
|
+
}, 10000)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
### pwmDuty(percent)
|
|
148
|
+
|
|
149
|
+
To change the *duty cycle* of a "pwm" instance. The parameter is defined as a percentage to compute a *duty cycle* based on the *dutyMin* and *dutyMax* values of instance definition.
|
|
150
|
+
|
|
151
|
+
#### Example
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
import {RIO} from "rpi-io"
|
|
155
|
+
// Servo-motor instance
|
|
156
|
+
const servo = new RIO(13, "pwm", {
|
|
157
|
+
exportTime: 100,
|
|
158
|
+
period: 20000,
|
|
159
|
+
dutyMin: 500,
|
|
160
|
+
dutyMax: 2500})
|
|
161
|
+
|
|
162
|
+
// duty cycle = 50% between dutyMin and dutyMax
|
|
163
|
+
servo.pwmDuty(50)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### Parameter(s)
|
|
167
|
+
|
|
168
|
+
- **percent** *{Number}* 0 ≤ percent ≤ 100
|
|
169
|
+
|
|
170
|
+
## Static functions
|
|
171
|
+
|
|
172
|
+
### RIO.closeAll()
|
|
173
|
+
Function to close all instances
|
|
174
|
+
#### Example
|
|
175
|
+
```javascript
|
|
176
|
+
import {RIO} from "rpi-io"
|
|
177
|
+
const led = new RIO(17, "output", {value: 0})
|
|
178
|
+
const btn = new RIO(18, "input")
|
|
179
|
+
RIO.closeAll()
|
|
180
|
+
// led and btn are closed
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
### RIO.model()
|
|
186
|
+
|
|
187
|
+
Function to return current model of RPi.
|
|
188
|
+
|
|
189
|
+
```javascript
|
|
190
|
+
import {RIO} from "rpi-io"
|
|
191
|
+
console.log("model:", RIO.model())
|
|
192
|
+
// Returns '5B', '4B', '3B', 'Zero2', 'Zero' or '' when unknown.
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Utilities
|
|
196
|
+
|
|
197
|
+
### log(args)
|
|
198
|
+
|
|
199
|
+
Function similar to `console.log` but with timestamped prompt. Must be enabled or not by function `traceCfg` defined thereafter.
|
|
200
|
+
|
|
201
|
+
### warn(args)
|
|
202
|
+
|
|
203
|
+
Function similar to `log` but colored and prefixed by sign ⚠️.
|
|
204
|
+
|
|
205
|
+
### traceCfg(level)
|
|
206
|
+
|
|
207
|
+
Function to enable functions `log` and `warn`.
|
|
208
|
+
|
|
209
|
+
#### Example for log, warn and traceCfg
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
import {traceCfg, log, warn} from "rpi-io"
|
|
213
|
+
// traceCfg(0) => warn and log are disabled
|
|
214
|
+
// traceCfg(1) => warn is enabled and log is disabled
|
|
215
|
+
// traceCfg(2) => warn and log are enabled
|
|
216
|
+
traceCfg(2)
|
|
217
|
+
warn("This is important info")
|
|
218
|
+
log("This is useful info")
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
### sleep(time)
|
|
224
|
+
|
|
225
|
+
Similar to `setTimeout` but in async mode.
|
|
226
|
+
|
|
227
|
+
### ctrlC(function)
|
|
228
|
+
|
|
229
|
+
To run a callback function on ctrl+c event.
|
|
230
|
+
|
|
231
|
+
#### Example for sleep and ctrlC
|
|
232
|
+
|
|
233
|
+
```javascript
|
|
234
|
+
import {sleep, ctrlC} from "rpi-io"
|
|
235
|
+
(async () => {
|
|
236
|
+
ctrlC(() => {
|
|
237
|
+
console.log("displayed on script interrupt")
|
|
238
|
+
})
|
|
239
|
+
await sleep(10000)
|
|
240
|
+
console.log("displayed after 10s")
|
|
241
|
+
})()
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
# End of File
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Benchmark
|
|
2
|
+
|
|
3
|
+
The following tables summarize main operation times on various devices. Results are in micro-seconds (µs).
|
|
4
|
+
|
|
5
|
+
**PLEASE NOTE**: As PWM operations rely on files, performance of microSD cards installed on RPi may have a significant impact on results for `pwmDuty()`.
|
|
6
|
+
|
|
7
|
+
**OS Bookworm with libgpiod v1.6.3**
|
|
8
|
+
|
|
9
|
+
| | RPi 5B | RPi 4B | RPi Zero2 |
|
|
10
|
+
| --------------- | ------- | ------- | --------- |
|
|
11
|
+
| write | 0.72 µs | 1.30 µs | 2,37 µs |
|
|
12
|
+
| read | 1.32 µs | 1,09 µs | 1,90 µs |
|
|
13
|
+
| pwmDuty (1 KHz) | 15.6 µs | 25,6 µs | 48,4 µs |
|
|
14
|
+
|
|
15
|
+
**OS Trixie with libgpiod v2.2.1**
|
|
16
|
+
|
|
17
|
+
| | RPi 5B | RPi 4B | RPi Zero2 |
|
|
18
|
+
| --------------- | ------------ | ------- | ------------ |
|
|
19
|
+
| write | *not tested* | 1.20 µs | *not tested* |
|
|
20
|
+
| read | *not tested* | 1.37 µs | *not tested* |
|
|
21
|
+
| pwmDuty (1 KHz) | *not tested* | 24,9 µs | *not tested* |
|
|
22
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
To get familiar with the **rpi-io**, you might have a glance on examples and play with them with the GPIO lines of your own electronic circuit.
|
|
3
|
+
|
|
4
|
+
```bash
|
|
5
|
+
# Simple write
|
|
6
|
+
node /your-project/node_modules/rpi-io/test/write.js
|
|
7
|
+
|
|
8
|
+
# Read and monitor
|
|
9
|
+
node /your-project/node_modules/rpi-io/test/read.js
|
|
10
|
+
|
|
11
|
+
# LED fade-in on PWM line
|
|
12
|
+
node /your-project/node_modules/rpi-io/test/pwm-led.js
|
|
13
|
+
|
|
14
|
+
# Servo-motor SG90 controlled by PWM
|
|
15
|
+
node /your-project/node_modules/rpi-io/test/pwm-motor.js
|
|
16
|
+
|
|
17
|
+
# Test duplicated instance error
|
|
18
|
+
node /your-project/node_modules/rpi-io/test/duplicate-error.js
|
|
19
|
+
|
|
20
|
+
# Test close of all instances
|
|
21
|
+
node /your-project/node_modules/rpi-io/test/close-all.js
|
|
22
|
+
|
|
23
|
+
# Test GPIO line configuration
|
|
24
|
+
node /your-project/node_modules/rpi-io/test/line-configuration.js
|
|
25
|
+
```
|
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/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
|
package/history.md
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
2.0.11 - Feb 13, 2026
|
|
2
|
-
===================
|
|
3
|
-
* Minor updates in `README.md`:
|
|
4
|
-
* Fixes in code examples, where imported class is *RIO*, not *Rio*.
|
|
5
|
-
* Details about Node.js pre-requisites.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
2.0.9 - Feb 03, 2026
|
|
9
|
-
===================
|
|
10
|
-
* `package.json`: node-gyp version update to fix *tar* vulnerabilities
|
|
11
|
-
* `README.md`: Explanations for *tar* warnings.
|
|
12
|
-
|
|
13
|
-
2.0.5 - Dec 01, 2025
|
|
14
|
-
===================
|
|
15
|
-
* Typos
|
|
16
|
-
* Internal refactoring
|
|
17
|
-
|
|
18
|
-
2.0.0 - Nov 26, 2025
|
|
19
|
-
===================
|
|
20
|
-
* New hybrid architecture Javascript + C addon to improve performances
|
|
21
|
-
* New API
|
|
22
|
-
* Fixes
|
|
23
|
-
|
|
24
|
-
1.2.2 - Nov 10, 2025
|
|
25
|
-
===================
|
|
26
|
-
* Change utility API function name from `Rio.log` to `Rio.logCfg`.
|
|
27
|
-
* Minor fixes
|
|
28
|
-
|
|
29
|
-
1.2.0 - Nov 07, 2025
|
|
30
|
-
===================
|
|
31
|
-
* New option parameter for method `monitor` to take care of bias and bounces.
|
|
32
|
-
* Fixes and optimization
|
|
33
|
-
|
|
34
|
-
1.1.0 - Oct 31, 2025
|
|
35
|
-
===================
|
|
36
|
-
* Added support libgpiod 2.2.1
|
|
37
|
-
* Tested with *Trixie*
|
|
38
|
-
|
|
39
|
-
1.0.0 - Oct 23, 2025
|
|
40
|
-
===================
|
|
41
|
-
Initial release: *Bookworm*, libgpiod 1.63
|