49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
const dotenv = require('dotenv').config();
|
|
const debug = process.env.DEBUG === "TRUE";
|
|
const { pins } = require('./config.json');
|
|
|
|
module.exports = {
|
|
log(message) {
|
|
if (debug) {
|
|
console.log(message);
|
|
}
|
|
},
|
|
gpio: {
|
|
// Boot up sanity check during debug mode
|
|
async debugInit() {
|
|
module.exports.log('Resetting all output pins.');
|
|
pins.forEach(async (pin) => {
|
|
if (pin.mode === 'OUT') {
|
|
this.setPin(pin.board, pin.defaultState, err => {
|
|
if (err) throw err;
|
|
module.exports.log(`Set ${pin.key} pin to ${pin.defaultState}.`);
|
|
});
|
|
};
|
|
});
|
|
for (const pin of pins) {
|
|
switch (pin.mode) {
|
|
case 'OUT':
|
|
this.togglePin(pin.board, err => {
|
|
if (err) throw err;
|
|
module.exports.log(`Toggled ${pin.key}`);
|
|
});
|
|
// Wait 1000ms before toggling again.
|
|
await sleep(1000);
|
|
this.togglePin(pin.board, err => {
|
|
if (err) throw err;
|
|
module.exports.log(`Toggled ${pin.key}`);
|
|
});
|
|
break;
|
|
case 'IN':
|
|
this.readPin(pin.board, (err, state) => {
|
|
if (err) throw err;
|
|
module.exports.log(`${pin.key} state: ${state}`);
|
|
});
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|