-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmake-gpio-script
executable file
·71 lines (61 loc) · 2.42 KB
/
make-gpio-script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const util = require('util');
const OUTPUT_NAME = "set-gpio.sh";
var config = [];
console.log("\nRebuild the set-gpio.sh script using your Homebridge config.json\n");
if (process.argv.length < 4) {
console.log("Usage: ./make-gpio-script input-config.json set-gpio.sh\n\n");
process.exit();
} else {
try {
console.log("INFO: Parsing %s", process.argv[2]);
config = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
}
catch (err) {
console.log("ERROR: Cannot parse your config file (" + process.argv[2] + ")\nCheck it exists and is valid!", err);
process.exit();
}
try {
//Force the output file to be named set-gpio.sh
var inputPath = path.parse(process.argv[3]);
if (inputPath.name + inputPath.ext != OUTPUT_NAME) {
console.log("ERROR: Output file is not called %s - aborting", OUTPUT_NAME);
process.exit();
} else {
console.log("INFO: Truncating %s script", OUTPUT_NAME);
if ( fs.existsSync(process.argv[3])) {
fs.truncateSync(process.argv[3],0);
}
var scriptContents = configToScript(config);
console.log("INFO: Writing to %s", OUTPUT_NAME);
fs.writeFileSync(process.argv[3], scriptContents);
}
}
catch (err) {
console.log("ERROR: Cannot write out to " + process.argv[3], err);
process.exit();
}
}
process.exit();
function configToScript(inputConfig)
{
var piPlatform = inputConfig.platforms.filter(function (el) { return el.platform == "WiringPiPlatform"});
var gpioPins = piPlatform[0].gpiopins;
script = "#!/bin/sh\n\n";
script += "####\n";
script += "# Do not edit this script - it is generated automatically by make-gpio-script\n";
script += "####\n\n";
script += "GPIO=/usr/local/bin/gpio\n\n";
for (var i = 0, len = gpioPins.length; i < len; i++)
{
if (gpioPins[i].enabled.toUpperCase() == "TRUE") {
script += util.format("## %s\n", gpioPins[i].name)
script += util.format("$GPIO -g mode %s %s\n", gpioPins[i].pin, gpioPins[i].mode);
script += util.format("$GPIO -g mode %s %s\n", gpioPins[i].pin, gpioPins[i].pull);
script += util.format("$GPIO -g export %s %s\n\n", gpioPins[i].pin, gpioPins[i].mode);
}
}
return script;
}