Reimplement GPIO initialization

This commit is contained in:
Skylar Grant 2022-12-03 19:55:47 -05:00
parent a29701e6eb
commit a062831376
2 changed files with 47 additions and 23 deletions

View File

@ -3,15 +3,7 @@ const dotenv = require('dotenv').config();
// Module for working with files // Module for working with files
const fs = require('fs'); const fs = require('fs');
const { resolve } = require('path'); const { resolve } = require('path');
// Setup for use with the Pi's GPIO pins
if (process.env.ONPI == 'true') {
console.log('Running on a Raspberry Pi.');
const gpio = require('rpi-gpio');
} else if (process.env.ONPI == 'false') {
console.log('Not running on a Raspberry Pi.');
} else {
console.log('Problem with ENV file.');
}
// The functions we'll export to be used in other files // The functions we'll export to be used in other files
const functions = { const functions = {
@ -22,7 +14,7 @@ const functions = {
return; return;
}, },
// Turns the auger on (Pin 7 high) // Turns the auger on (Pin 7 high)
on() { on(gpio) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (process.env.ONPI == 'true') { if (process.env.ONPI == 'true') {
gpio.write(7, true, function(err) { gpio.write(7, true, function(err) {
@ -129,17 +121,22 @@ const functions = {
}); });
}, },
init() { init(gpio) {
return new Promise((resolve, reject) => {
// Write the current env vars to console // Write the current env vars to console
console.log('Environment variables:'); console.log('Environment variables:');
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`); console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
return true; // Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
if (process.env.ONPI == 'true') {
gpio.setup(7, gpio.DIR_OUT, (err) => {
if (err) reject(err);
resolve('GPIO Initialized');
});
} else {
resolve('GPIO Not Available');
}
});
}, },
} }
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
if (process.env.ONPI == 'true') {
gpio.setup(7, gpio.DIR_OUT, functions.auger.ready());
}
module.exports = { functions }; module.exports = { functions };

33
main.js
View File

@ -4,12 +4,39 @@ const fn = require('./functions.js').functions;
// Environment Variables Importing // Environment Variables Importing
const dotenv = require('dotenv').config(); const dotenv = require('dotenv').config();
// Setup for use with the Pi's GPIO pins
if (process.env.ONPI == 'true') {
console.log('Running on a Raspberry Pi.');
const gpio = require('rpi-gpio');
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else if (process.env.ONPI == 'false') {
console.log('Not running on a Raspberry Pi.');
const gpio = 'gpio';
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else {
console.log('Problem with ENV file.');
}
// TODO Add logic for other sensors // TODO Add logic for other sensors
main(fn);
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
async function main(fn) { async function main(fn, gpio) {
fn.files.check().then((res,rej) => { fn.files.check().then((res,rej) => {
console.log('File Check: ' + res); console.log('File Check: ' + res);
switch (res) { switch (res) {
@ -27,7 +54,7 @@ async function main(fn) {
fn.commands.quit(); fn.commands.quit();
break; break;
case "none": case "none":
fn.auger.cycle().then(() => { fn.auger.cycle(gpio).then(() => {
main(fn); main(fn);
}); });
break; break;