Added i2c-amp-control service
This commit is contained in:
parent
9f3e00f093
commit
3744142e49
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
audio-box.tar.gz
|
||||||
|
.idea
|
||||||
26
deploy.sh
Executable file
26
deploy.sh
Executable file
@ -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"
|
||||||
17
i2c-amp-control/i2c-amp-control.service
Normal file
17
i2c-amp-control/i2c-amp-control.service
Normal 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
98
i2c-amp-control/index.js
Normal 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();
|
||||||
17
i2c-amp-control/install.sh
Normal file
17
i2c-amp-control/install.sh
Normal 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
|
||||||
7
i2c-amp-control/package.json
Normal file
7
i2c-amp-control/package.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"scripts": {},
|
||||||
|
"dependencies": {
|
||||||
|
"i2c-bus": "^5.2.2",
|
||||||
|
"onoff": "^6.0.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
25
install.sh
Normal file
25
install.sh
Normal file
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user