rpi-io 1.2.3 → 2.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/README.md +432 -246
- package/addon/gpio.c +875 -0
- package/binding.gyp +33 -0
- package/esm/_util.mjs +122 -0
- package/esm/main.mjs +307 -0
- package/history.md +7 -0
- package/package.json +54 -7
- package/script/check-env.sh +192 -0
- package/script/detect-gpiod-version.sh +21 -0
- package/test/benchmark-pwm.js +44 -0
- package/test/benchmark-read.js +37 -0
- package/test/benchmark-write.js +38 -0
- package/test/close-all.js +27 -0
- package/test/duplicate-error.js +29 -0
- package/test/line-configuration.js +16 -0
- package/test/pwm-led.js +36 -0
- package/test/pwm-motor.js +40 -0
- package/test/read.js +30 -0
- package/test/write.js +29 -0
- package/esm/dev.mjs +0 -59
- package/esm/rio.mjs +0 -647
- package/esm/utl.mjs +0 -100
- package/scr/test.js +0 -168
package/esm/utl.mjs
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
// -------------------------------------------------------------------
|
|
2
|
-
// RPi GPIO CONTROL - UTILITIES
|
|
3
|
-
// -------------------------------------------------------------------
|
|
4
|
-
import {log, trap} from "./dev.mjs"
|
|
5
|
-
import {execSync, execFileSync, spawnSync} from "child_process"
|
|
6
|
-
|
|
7
|
-
// -------------------------------------------------------------------
|
|
8
|
-
// JS UTILITIES
|
|
9
|
-
/** ------------------------------------------------------------------
|
|
10
|
-
* @function sleep
|
|
11
|
-
* @description Wait before continuing
|
|
12
|
-
* @param {Number} ms
|
|
13
|
-
*/
|
|
14
|
-
export const sleep = ms => {
|
|
15
|
-
log("sleeping", ms, "ms")
|
|
16
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** ------------------------------------------------------------------
|
|
20
|
-
* @function ctrlC
|
|
21
|
-
* @description Intercept ctrl+c then exec callback
|
|
22
|
-
* @param {Function} callback
|
|
23
|
-
*/
|
|
24
|
-
export const ctrlC = callback => {
|
|
25
|
-
process.on("SIGINT", () => {
|
|
26
|
-
log("ctrl+c pressed")
|
|
27
|
-
typeof callback === "function" ? callback() : false
|
|
28
|
-
process.exit(0)
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// -------------------------------------------------------------------
|
|
33
|
-
// NODEJS UTILITIES
|
|
34
|
-
/** ------------------------------------------------------------------
|
|
35
|
-
* @function exeShell
|
|
36
|
-
* @description Async run command in Linux shell
|
|
37
|
-
* @param {String} cmd
|
|
38
|
-
* @return {String} stdout or ""
|
|
39
|
-
*/
|
|
40
|
-
export const exeShell = cmd => {
|
|
41
|
-
try {
|
|
42
|
-
return execSync(cmd, {
|
|
43
|
-
stdio: "pipe",
|
|
44
|
-
encoding: "utf8",
|
|
45
|
-
})
|
|
46
|
-
} catch (err) {
|
|
47
|
-
if (err.code) {
|
|
48
|
-
// Spawning child process failed
|
|
49
|
-
trap("execFileSync error code", err.code)
|
|
50
|
-
} else {
|
|
51
|
-
// Child was spawned but exited with non-zero exit code
|
|
52
|
-
// Error contains any stdout and stderr from the child
|
|
53
|
-
const {stdout, stderr} = err
|
|
54
|
-
trap({stdout, stderr})
|
|
55
|
-
}
|
|
56
|
-
return ""
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** ------------------------------------------------------------------
|
|
61
|
-
* @function exeFile
|
|
62
|
-
* @description Async run executable file
|
|
63
|
-
* @param {String} file
|
|
64
|
-
* @param {Array} args
|
|
65
|
-
* @return {String} stdout or ""
|
|
66
|
-
*/
|
|
67
|
-
export const exeFile = (file, args = []) => {
|
|
68
|
-
try {
|
|
69
|
-
return execFileSync(file, args, {
|
|
70
|
-
stdio: "pipe",
|
|
71
|
-
encoding: "utf8",
|
|
72
|
-
})
|
|
73
|
-
} catch (err) {
|
|
74
|
-
if (err.code) {
|
|
75
|
-
// Spawning child process failed
|
|
76
|
-
trap("execFileSync error code", err.code)
|
|
77
|
-
} else {
|
|
78
|
-
// Child was spawned but exited with non-zero exit code
|
|
79
|
-
// Error contains any stdout and stderr from the child
|
|
80
|
-
const {stdout, stderr} = err
|
|
81
|
-
trap({stdout, stderr})
|
|
82
|
-
}
|
|
83
|
-
return ""
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** ------------------------------------------------------------------
|
|
88
|
-
* @function wait
|
|
89
|
-
* @description Wait before continuing (sync blocking mode)
|
|
90
|
-
* @param {Number} ms
|
|
91
|
-
*/
|
|
92
|
-
export const wait = ms => {
|
|
93
|
-
log("waiting", ms, "ms")
|
|
94
|
-
spawnSync("sleep", [ms / 1000])
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// -------------------------------------------------------------------
|
|
99
|
-
// EoF
|
|
100
|
-
// -------------------------------------------------------------------
|
package/scr/test.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
// -------------------------------------------------------------------
|
|
2
|
-
// RPi GPIO CONTROL TEST SCRIPT
|
|
3
|
-
// -------------------------------------------------------------------
|
|
4
|
-
import {log, trap, nowStr} from "../esm/dev.mjs"
|
|
5
|
-
import {ctrlC} from "../esm/utl.mjs"
|
|
6
|
-
import {Rio} from "../esm/rio.mjs"
|
|
7
|
-
|
|
8
|
-
/** ------------------------------------------------------------------
|
|
9
|
-
* @function info
|
|
10
|
-
* @description Colored log for info messages Node.js only ~ CSS color #F50
|
|
11
|
-
* @param {String} cmd
|
|
12
|
-
* @param {String} txt
|
|
13
|
-
*/
|
|
14
|
-
function info(cmd, txt) {
|
|
15
|
-
console.log(nowStr(), "🔎", "\x1b[38;2;0;160;16m", cmd, "\x1b[0m", txt)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** ------------------------------------------------------------------
|
|
19
|
-
* @function isNumber
|
|
20
|
-
* @description Test if param is number
|
|
21
|
-
* @param {String} str
|
|
22
|
-
* @return {Number}
|
|
23
|
-
*/
|
|
24
|
-
function isNumber(str) {
|
|
25
|
-
const num = parseInt(str)
|
|
26
|
-
if (typeof num !== "number" || num !== num) {
|
|
27
|
-
trap("<gpio_number> is not a number or not defined")
|
|
28
|
-
return -1
|
|
29
|
-
} else {
|
|
30
|
-
return num
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// -------------------------------------------------------------------
|
|
35
|
-
// TOP LEVEL TEST
|
|
36
|
-
// -------------------------------------------------------------------
|
|
37
|
-
(function () {
|
|
38
|
-
Rio.logCfg(2)
|
|
39
|
-
ctrlC(Rio.stopMonitoring)
|
|
40
|
-
|
|
41
|
-
if (process.argv.length < 3) {
|
|
42
|
-
trap("Arguments are missing! Type 'help' to display list of commands.")
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
let gpio
|
|
46
|
-
switch (process.argv[2]) {
|
|
47
|
-
case "help":
|
|
48
|
-
info("help", "To display this list.")
|
|
49
|
-
info("detect", "To display gpio chipsets")
|
|
50
|
-
info("lines", "To display gpiochip0 lines")
|
|
51
|
-
info("version", "To display libgpiod version")
|
|
52
|
-
info("system", "To test if your system (hw,sw, mw) meets the tested requirements.")
|
|
53
|
-
info("config", "To display gpio system configuration.")
|
|
54
|
-
info("out <gpio_number>", "To define a gpio as 'out' and set it to 1 for 5s.")
|
|
55
|
-
info("in <gpio_number>", "To define a gpio as 'in' to display its value ")
|
|
56
|
-
info("monitor <gpio_number>", "To define a gpio as 'in' to monitor events until ctrl+c.")
|
|
57
|
-
info("pwm <gpio_number>", "Test SG90~like servo from 0° to 180° i.e. period=20ms, duty-cycle-range=0.5-2.5ms")
|
|
58
|
-
info("benchmark <gpio_out> <gpio_in>", "Displays elapsed time of 1000 write + read operations")
|
|
59
|
-
break
|
|
60
|
-
case "version":
|
|
61
|
-
log("libgpiod version:", Rio.version())
|
|
62
|
-
break
|
|
63
|
-
case "detect":
|
|
64
|
-
log("chipsets:", Rio.detect())
|
|
65
|
-
break
|
|
66
|
-
case "lines":
|
|
67
|
-
log("lines:", Rio.info())
|
|
68
|
-
break
|
|
69
|
-
case "system":
|
|
70
|
-
Rio.isSystemSupported()
|
|
71
|
-
break
|
|
72
|
-
case "config":
|
|
73
|
-
Rio.configs()
|
|
74
|
-
break
|
|
75
|
-
case "out":
|
|
76
|
-
gpio = isNumber(process.argv[3])
|
|
77
|
-
if (gpio > 0) {
|
|
78
|
-
const led = new Rio(gpio, "out")
|
|
79
|
-
led.set(1, 5000)
|
|
80
|
-
}
|
|
81
|
-
break
|
|
82
|
-
case "in":
|
|
83
|
-
gpio = isNumber(process.argv[3])
|
|
84
|
-
if (gpio > 0) {
|
|
85
|
-
const btn = new Rio(gpio, "in")
|
|
86
|
-
btn.get()
|
|
87
|
-
}
|
|
88
|
-
break
|
|
89
|
-
case "monitor":
|
|
90
|
-
const gpio1 = isNumber(process.argv[3])
|
|
91
|
-
const gpio2 = isNumber(process.argv[4])
|
|
92
|
-
let btn1, btn2
|
|
93
|
-
if (gpio1 > 0) {
|
|
94
|
-
btn1 = new Rio(gpio1, "in")
|
|
95
|
-
btn1.monitor("rising")
|
|
96
|
-
}
|
|
97
|
-
if (gpio2 > 0) {
|
|
98
|
-
btn2 = new Rio(gpio2, "in")
|
|
99
|
-
btn2.monitor("falling", () => {
|
|
100
|
-
log(gpio2, "falling")
|
|
101
|
-
}, {bias: "pull-down"})
|
|
102
|
-
}
|
|
103
|
-
setTimeout(() => {
|
|
104
|
-
btn1.monitor("stop")
|
|
105
|
-
}, 6000)
|
|
106
|
-
break
|
|
107
|
-
case "benchmark":
|
|
108
|
-
const gpioOut = isNumber(process.argv[3])
|
|
109
|
-
const gpioIn = isNumber(process.argv[4])
|
|
110
|
-
let len = 1000
|
|
111
|
-
if (gpioOut > 0) {
|
|
112
|
-
const led = new Rio(gpioOut, "out")
|
|
113
|
-
const start1 = new Date()
|
|
114
|
-
while (len--) {
|
|
115
|
-
led.write(1)
|
|
116
|
-
}
|
|
117
|
-
const stop1 = new Date()
|
|
118
|
-
const time1 = Math.abs(stop1 - start1)
|
|
119
|
-
log("Elapsed time for 1000 write ops:", time1, "ms")
|
|
120
|
-
}
|
|
121
|
-
len = 1000
|
|
122
|
-
if (gpioIn > 0) {
|
|
123
|
-
const btn = new Rio(gpioIn, "in")
|
|
124
|
-
const start2 = new Date()
|
|
125
|
-
while (len--) {
|
|
126
|
-
btn.read()
|
|
127
|
-
}
|
|
128
|
-
const stop2 = new Date()
|
|
129
|
-
const time2 = Math.abs(stop2 - start2)
|
|
130
|
-
log("Elapsed time for 1000 read ops:", time2, "ms")
|
|
131
|
-
}
|
|
132
|
-
break
|
|
133
|
-
case "pwm":
|
|
134
|
-
const servo = new Rio(isNumber(process.argv[3]), "pwm")
|
|
135
|
-
|
|
136
|
-
// 50hz, 0.5 ms
|
|
137
|
-
servo.pwmInit(20000000, 500000, {
|
|
138
|
-
dutyMin: 500000,
|
|
139
|
-
dutyMax: 2500000
|
|
140
|
-
})
|
|
141
|
-
log("servo position 0°")
|
|
142
|
-
|
|
143
|
-
// after 2s -> duty=1.5 ms
|
|
144
|
-
setTimeout(() => {
|
|
145
|
-
servo.pwmDuty(1500000)
|
|
146
|
-
log("servo position = 90°")
|
|
147
|
-
}, 2000)
|
|
148
|
-
|
|
149
|
-
// after 4s -> duty=1.5 ms
|
|
150
|
-
setTimeout(() => {
|
|
151
|
-
servo.pwmDuty(2500000)
|
|
152
|
-
log("servo position = 180°")
|
|
153
|
-
}, 4000)
|
|
154
|
-
|
|
155
|
-
// after 5s -> stop
|
|
156
|
-
setTimeout(() => {
|
|
157
|
-
servo.pwmStop()
|
|
158
|
-
log("servo stopped")
|
|
159
|
-
}, 5000)
|
|
160
|
-
break
|
|
161
|
-
default:
|
|
162
|
-
trap("Unknown command: Type 'test help' to display list of commands.")
|
|
163
|
-
}
|
|
164
|
-
})()
|
|
165
|
-
|
|
166
|
-
// -------------------------------------------------------------------
|
|
167
|
-
// EoF
|
|
168
|
-
// -------------------------------------------------------------------
|