From 3744142e491adc7fe387bce3a94af5116b1811d1 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 12 Dec 2021 20:23:28 +0100 Subject: [PATCH] Added i2c-amp-control service --- .gitignore | 2 + deploy.sh | 26 +++++++ i2c-amp-control/i2c-amp-control.service | 17 +++++ i2c-amp-control/index.js | 98 +++++++++++++++++++++++++ i2c-amp-control/install.sh | 17 +++++ i2c-amp-control/package.json | 7 ++ install.sh | 25 +++++++ 7 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100755 deploy.sh create mode 100644 i2c-amp-control/i2c-amp-control.service create mode 100644 i2c-amp-control/index.js create mode 100644 i2c-amp-control/install.sh create mode 100644 i2c-amp-control/package.json create mode 100644 install.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..211c2de --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +audio-box.tar.gz +.idea diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..4f893ae --- /dev/null +++ b/deploy.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +while [ True ]; do +if [ "$1" = "--user" -o "$1" = "-u" ]; then + USERNAME=$2 + shift 2 +elif [ "$1" = "--host" -o "$1" = "-h" ]; then + HOST=$2 + shift 2 +else + break +fi +done + +ARG=( "${@}" ) + +echo "Deploying to $HOST" + +TARFILE="audio-box.tar.gz" +FOLDER="~/audio-box" + +tar -czf $TARFILE --exclude='.git' --exclude='.idea' --exclude='.gitignore' --exclude='build-deploy.sh' . +scp $TARFILE $USERNAME@$HOST:~ +ssh -t $USERNAME@$HOST "rm -rf $FOLDER ; mkdir -p $FOLDER ; tar -xvzf $TARFILE -C $FOLDER ; chmod +x $FOLDER/install.sh" + +echo "Deployed" diff --git a/i2c-amp-control/i2c-amp-control.service b/i2c-amp-control/i2c-amp-control.service new file mode 100644 index 0000000..f7b3e08 --- /dev/null +++ b/i2c-amp-control/i2c-amp-control.service @@ -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 diff --git a/i2c-amp-control/index.js b/i2c-amp-control/index.js new file mode 100644 index 0000000..ebfd8e7 --- /dev/null +++ b/i2c-amp-control/index.js @@ -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(); diff --git a/i2c-amp-control/install.sh b/i2c-amp-control/install.sh new file mode 100644 index 0000000..2717ee2 --- /dev/null +++ b/i2c-amp-control/install.sh @@ -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 diff --git a/i2c-amp-control/package.json b/i2c-amp-control/package.json new file mode 100644 index 0000000..ee29327 --- /dev/null +++ b/i2c-amp-control/package.json @@ -0,0 +1,7 @@ +{ + "scripts": {}, + "dependencies": { + "i2c-bus": "^5.2.2", + "onoff": "^6.0.3" + } +} diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..b3415f3 --- /dev/null +++ b/install.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +if [ "#{0:0:1}" = "/" ]; then + spath=$0 +else + spath=$PWD/$(dirname "$0") +fi + +execute_script() +{ + SERVICE=$1 + + read -p "Install service $SERVICE? (yes/NO): " xcute + xcute="${xcute,,}" # To lowercase + if [[ "$xcute" = "y" || "$xcute" = "yes" ]]; then + echo "------------------------------------ Installing $SERVICE ------------------------------------" + cd "$spath/$SERVICE" || return + . ./install.sh + echo "------------------------------------ $SERVICE installed ------------------------------------" + else + echo "Skipping installation of $SERVICE" + fi +} + +execute_script i2c-amp-control