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