hestia/src/custom_modules/functions.js

47 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-08-18 18:26:28 +00:00
const dotenv = require('dotenv').config();
const debug = process.env.DEBUG === "TRUE";
module.exports = {
log(message) {
if (debug) {
console.log(message);
}
},
gpio: {
// Boot up sanity check during debug mode
async debugInit() {
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;
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;
exports.log(`Toggled ${pin.key}`);
});
// Wait 1000ms before toggling again.
await sleep(1000);
this.togglePin(pin.board, err => {
if (err) throw err;
exports.log(`Toggled ${pin.key}`);
});
break;
case 'IN':
this.readPin(pin.board, (err, state) => {
if (err) throw err;
exports.log(`${pin.key} state: ${state}`);
});
default:
break;
}
};
}
}
}