Added i2c-amp-control service

This commit is contained in:
2021-12-12 20:23:28 +01:00
parent 9f3e00f093
commit 3744142e49
7 changed files with 192 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
[Unit]
Description=I2C Amplifier controller
[Service]
ExecStart=node /data/services/i2c-amp-control/index.js
WorkingDirectory=/data/services/i2c-amp-control
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=i2c-amp-control
#Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target

98
i2c-amp-control/index.js Normal file
View File

@@ -0,0 +1,98 @@
const Gpio = require('onoff').Gpio;
const i2c = require('i2c-bus');
const MAX9744_I2CADDR = 0x4b;
const MAX_VOLUME = 63;
const MIN_VOLUME = 0;
const VOLUME_STEPS = 1;
let currentVolume = MIN_VOLUME;
let i2c1;
async function readVolume() {
const rBuf = Buffer.alloc(1);
const data = await i2c1.i2cRead(MAX9744_I2CADDR, rBuf.length, rBuf);
return data.buffer.readUInt8();
}
async function writeVolume(volume) {
const wBuf = Buffer.from([volume]);
await i2c1.i2cWrite(MAX9744_I2CADDR, wBuf.length, wBuf);
console.log("Volume set: ", volume);
}
async function increaseVolume() {
console.log("Increasing volume");
currentVolume = currentVolume < MAX_VOLUME - VOLUME_STEPS ? currentVolume + VOLUME_STEPS : MAX_VOLUME;
await writeVolume(currentVolume);
}
async function decreaseVolume() {
console.log("Decreasing volume");
currentVolume = currentVolume > MIN_VOLUME + VOLUME_STEPS ? currentVolume - VOLUME_STEPS : MIN_VOLUME;
await writeVolume(currentVolume);
}
async function handleButtonTick(button) {
console.log("Handling button tick");
if(button.active) {
await button.action();
button.cycles++;
let timeout = 1400 - (button.cycles * 200);
if(timeout < 200) {
timeout = 200;
}
button.watcher = setTimeout(async () => {
await handleButtonTick(button);
}, timeout);
} else if(button.watcher) {
clearTimeout(button.watcher);
button.cycles = 0;
}
}
const buttons = {
volumeUp: {pin: 17, gpio: null, active: false, watcher: null, action: increaseVolume, cycles: 0},
volumeDown: {pin: 27, gpio: null, active: false, watcher: null, action: decreaseVolume, cycles: 0}
};
function watchButton(button) {
button.gpio.watch(async (err, value) => {
if(err) {
button.active = false;
if(button.watcher) {
clearInterval(button.watcher);
button.cycles = 0;
}
console.error(err);
} else {
if (value === 1) {
button.active = true;
await handleButtonTick(button);
} else {
button.active = false;
if(button.watcher) {
clearInterval(button.watcher);
}
}
}
});
}
function initButton(button) {
button.gpio = new Gpio(button.pin, 'in', 'both', { activeLow: true});
watchButton(button);
}
async function main() {
try {
i2c1 = await i2c.openPromisified(1);
currentVolume = await readVolume();
initButton(buttons.volumeUp);
initButton(buttons.volumeDown);
} catch (err) {
console.error(err);
}
}
main();

View File

@@ -0,0 +1,17 @@
npm i
SERVICE="i2c-amp-control"
SERVICE_FOLDER="/data/services/$SERVICE"
mkdir -p "$SERVICE_FOLDER"
cp ./index.js "$SERVICE_FOLDER"
cp -r ./node_modules "$SERVICE_FOLDER"
cp $SERVICE.service /etc/systemd/system
if systemctl list-units | grep "$SERVICE"; then
systemctl restart "$SERVICE.service"
echo "$SERVICE service restarted"
else
systemctl enable "$SERVICE.service"
systemctl start "$SERVICE.service"
echo "Enabled and started $SERVICE"
fi

View File

@@ -0,0 +1,7 @@
{
"scripts": {},
"dependencies": {
"i2c-bus": "^5.2.2",
"onoff": "^6.0.3"
}
}