Skip to content

Commit

Permalink
reenable i2csound.service
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Jan 22, 2025
1 parent e0c94d8 commit 29964e9
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build_audio_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fi

echo "The UID for '$USER' is: $TUID"


# Update package list and install necessary system tools.
# Installs required packages and purges unnecessary ones.
echo "Updating base system..."
Expand Down Expand Up @@ -138,7 +137,8 @@ bash /mounted-github-repo/scripts/setup_fstab.sh
# Enable necessary system services.
echo "Enabling system services..."
chmod 644 /etc/systemd/system/kdeconnect.service
ln -s /etc/systemd/system/sshd.service /etc/systemd/system/multi-user.target.wants/
ln -s /etc/systemd/system/i2csound.service /etc/systemd/system/multi-user.target.wants/i2csound.service
ln -s /etc/systemd/system/sshd.service /etc/systemd/system/multi-user.target.wants/sshd.service
ln -s /etc/systemd/system/kdeconnect.service /etc/systemd/system/multi-user.target.wants/kdeconnect.service
ln -s /usr/lib/systemd/system/mpd.service /etc/systemd/system/multi-user.target.wants/mpd.service
ln -s /usr/lib/systemd/system/[email protected] /etc/systemd/system/multi-user.target.wants/[email protected]
Expand Down
10 changes: 10 additions & 0 deletions overlays/base/etc/systemd/system/i2csound.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=I2C Sound configuration service
Requires=dev-i2c\x2d1.device
After=dev-i2c\x2d1.device
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
Type=oneshot
ExecStart=/usr/libexec/ovos-i2csound
341 changes: 341 additions & 0 deletions overlays/base/usr/libexec/ovos-i2csound
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
#!/bin/bash
##########################################################################
# ovos-i2csound
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

# Define a list of valid boards
declare -A boards=(
[RPI3]="Pi 3"
[RPI4]="Pi 4"
[RPI5]="Pi 5"
)

declare board_result

# Define a list of device names and their I2C addresses
declare -A devices=(
[WM8XXX]=1a
[RESPEAKER4]=3b
[RESPEAKER6]=35
[ADAFRUIT]=4b
[TAS5806]=2f
[SJ201LED]=04
[AIYVOICEBONNET]=52
)

declare -A detection_results

# Detects the presence of an I2C device at the specified address range
detect_i2c_device() {
local address="0x$1"
if i2cdetect -y -a 1 "$address" "$address" | grep -qE "($1|UU)"; then
return 0 # true
else
return 1 # false
fi
}

# Gets the board version
get_board_version() {
v=$(tr -d '\0' </proc/device-tree/model)
for board in "${!boards[@]}"; do
b="${boards[$board]}"
if [[ $v =~ "$b" ]]; then
board_result="$board"
break # Exit the loop once the match is found.
fi
done
}

# Checks for kernal compat
compare_kernel_version() {
current_kernel=$(uname -r | sed -re 's/(^[0-9]*\.[0-9]*\.[0-9]*-[0-9]*)(.*)/\1/')
if [[ "$current_kernel" < "$1" ]]; then
return 0
else
return 1
fi
}

# Check for a Mark 1 device
check_mark_1() {
local timeout=3
local dev="/dev/ttyAMA0"

# Check if device exists and is accessible
if [[ ! -c "$dev" ]] || [[ ! -r "$dev" ]] || [[ ! -w "$dev" ]]; then
echo "Error: $dev not accessible" >&2
return 1
fi

# Use file descriptor to ensure proper cleanup
exec 3<>"$dev"

# Clear any pending input
read -t 1 -n 1000 <&3 || true

# Send command
echo "system.version" >&3

# Read response with timeout
read -t "$timeout" -n 128 RESP <&3
local status=$?

# Cleanup
exec 3>&-

# Check response
if [[ $status -eq 0 ]] && [[ "$RESP" == *"Command"* ]]; then
echo "true"
return 0
fi
return 1
}

# Main execution
main() {
# Create a variable to hold the detected device name
local i2c_device_name
local i2c_device_file="/etc/OpenVoiceOS/i2c_platform"

# Check for an existing file and remove it if there
if [[ -f $i2c_device_file ]]; then
rm -f $i2c_device_file
fi

get_board_version

# Detecting I2C devices
for device in "${!devices[@]}"; do
address="${devices[$device]}"
if detect_i2c_device "$address"; then
detection_results[$device]=true
else
detection_results[$device]=false
fi
echo "$device detection result: ${detection_results[$device]}"
done

# Handling hardware-specific configurations
if [[ ${detection_results[WM8XXX]} == true ]] ; then
echo "WM8XXX based HAT found"
mk1=$(check_mark_1)
if [[ "$mk1" == "true" ]]; then
detection_results[MARK1]=true
echo "Mark-1 enclosure $MARK1"
else
detection_results[WM8960]=true
echo "WM8960 based 2-mic $WM8960"
fi
fi

if [[ ${detection_results[WM8960]} == true ]] && [[ ${detection_results[RESPEAKER4]} == false ]] ; then
echo "Installing and configuring WM8960 based 2-mic HAT"
dtoverlay wm8960-soundcard
echo "Configuring board"
sleep 3 # Allow some time to fully initialise the hardware / driver
amixer -c "wm8960soundcard" cset numid=1 34,34
amixer -c "wm8960soundcard" cset numid=26 3
amixer -c "wm8960soundcard" cset numid=27 4
amixer -c "wm8960soundcard" cset numid=30 5
amixer -c "wm8960soundcard" cset numid=32 5
amixer -c "wm8960soundcard" cset numid=33 5
amixer -c "wm8960soundcard" cset numid=34 25
amixer -c "wm8960soundcard" cset numid=35 on
amixer -c "wm8960soundcard" cset numid=9 3
amixer -c "wm8960soundcard" cset numid=8 3
amixer -c "wm8960soundcard" cset numid=49 on
amixer -c "wm8960soundcard" cset numid=51 on
amixer -c "wm8960soundcard" cset numid=37 0
amixer -c "wm8960soundcard" cset numid=38 0
amixer -c "wm8960soundcard" cset numid=39 5
amixer -c "wm8960soundcard" cset numid=48 on
amixer -c "wm8960soundcard" cset numid=50 on
amixer -c "wm8960soundcard" cset numid=54 on
amixer -c "wm8960soundcard" cset numid=16 5
amixer -c "wm8960soundcard" cset numid=15 4
amixer -c "wm8960soundcard" cset numid=11 120,120
amixer -c "wm8960soundcard" cset numid=13 120,120
echo "Configuring button"
dtoverlay wm8960-button-overlay
i2c_device_name="WM8960"
fi

if [[ ${detection_results[MARK1]} == true ]] ; then
echo "Installing and configuring WM8731 based sound HAT"
dtoverlay proto-codec

echo "Configuring board"
sleep 3 # Allow some time to fully initialise the hardware / driver
amixer -c "sndrpiproto" cset numid=1 107,107
amixer -c "sndrpiproto" cset numid=2 on
amixer -c "sndrpiproto" cset numid=6 on
amixer -c "sndrpiproto" cset numid=10 on
amixer -c "sndrpiproto" cset numid=14 1
amixer -c "sndrpiproto" cset numid=13 on
amixer -c "sndrpiproto" cset numid=9 on
echo "Resetting Mark-1 faceplate"
echo "eyes.color=7365993" > /dev/ttyAMA0 # color=soft gray, #706569
echo "mouth.text=" > /dev/ttyAMA0
i2c_device_name="MARK1"
fi

if [[ ${detection_results[RESPEAKER4]} == true ]] && [[ ${detection_results[RESPEAKER6]} == false ]] ; then
echo "Installing and configuring ReSpeaker 4-mic"
dtoverlay seeed-4mic-voicecard
echo "Configuring board"
sleep 3 # Allow some time to fully initialise the hardware / driver
amixer -c "seeed4micvoicec" cset numid=1 222
amixer -c "seeed4micvoicec" cset numid=2 222
amixer -c "seeed4micvoicec" cset numid=3 222
amixer -c "seeed4micvoicec" cset numid=4 222
amixer -c "seeed4micvoicec" cset numid=5 13
amixer -c "seeed4micvoicec" cset numid=6 13
amixer -c "seeed4micvoicec" cset numid=7 13
amixer -c "seeed4micvoicec" cset numid=8 13
if [[ $board_result == "RPI3" ]]; then
amixer -c "seeed4micvoicec" cset numid=5 3
amixer -c "seeed4micvoicec" cset numid=6 3
amixer -c "seeed4micvoicec" cset numid=7 3
amixer -c "seeed4micvoicec" cset numid=8 3
else
amixer -c "seeed4micvoicec" cset numid=5 13
amixer -c "seeed4micvoicec" cset numid=6 13
amixer -c "seeed4micvoicec" cset numid=7 13
amixer -c "seeed4micvoicec" cset numid=8 13
fi
i2c_device_name="RESPEAKER4"
fi

if [[ ${detection_results[RESPEAKER6]} == true ]] && [[ ${detection_results[RESPEAKER4]} == true ]] ; then
echo "Installing and configuring ReSpeaker 6mic"
dtoverlay seeed-8mic-voicecard
echo "Configuring board"
sleep 3 # Allow some time to fully initialise the hardware / driver
amixer -c "seeed8micvoicec" cset numid=1 208
amixer -c "seeed8micvoicec" cset numid=2 208
amixer -c "seeed8micvoicec" cset numid=3 208
amixer -c "seeed8micvoicec" cset numid=4 208
amixer -c "seeed8micvoicec" cset numid=5 13
amixer -c "seeed8micvoicec" cset numid=6 13
amixer -c "seeed8micvoicec" cset numid=7 13
amixer -c "seeed8micvoicec" cset numid=8 13
amixer -c "seeed8micvoicec" cset numid=9 208
amixer -c "seeed8micvoicec" cset numid=10 208
amixer -c "seeed8micvoicec" cset numid=11 208
amixer -c "seeed8micvoicec" cset numid=12 208
amixer -c "seeed8micvoicec" cset numid=13 13
amixer -c "seeed8micvoicec" cset numid=14 13
amixer -c "seeed8micvoicec" cset numid=15 13
amixer -c "seeed8micvoicec" cset numid=16 13
i2c_device_name="RESPEAKER6"
fi

if [[ ${detection_results[ADAFRUIT]} == true ]] ; then
echo "Installing and configuring Adafruit"
/usr/sbin/i2cset -y 1 0x4b 30 # Set maximum volume to 30
i2c_device_name="ADAFRUIT"
fi

if [[ ${detection_results[TAS5806]} == true ]] ; then
echo "Installing and configuring SJ-201 HAT"
# Initializing XMOS xvf3510

# Check for kernel version
if $(compare_kernel_version "6.6"); then
dtoverlay xvf3510
else
if [[ "$board_result" == "RPI5" ]]; then
dtoverlay sj201-pi5
else
dtoverlay sj201
fi
fi

/usr/libexec/xvf3510-flash --direct "/usr/lib/firmware/xvf3510/app_xvf3510_int_spi_boot_v4_1_0.bin"
# Initializing Texas Instruments 5806 Amplifier
/usr/bin/tas5806-init
if [[ ${detection_results[SJ201LED]} == true ]] ; then
echo "Found revision-6 SJ-201 board"
# Initializing and resetting LED ring
/usr/bin/sj201-reset-led
# Reset FAN to low speed
/usr/sbin/i2cset -a -y 1 0x04 101 30 i
i2c_device_name="SJ201V6"
else
echo "Assume revision-10 SJ-201 board"
if [[ "$board_result" == "RPI5" ]]; then
dtoverlay sj201-rev10-pwm-fan-overlay-pi5
else
dtoverlay sj201-rev10-pwm-fan-overlay
fi
i2c_device_name="SJ201V10"
fi
echo "Configuring buttons"
dtoverlay sj201-buttons-overlay
if [[ "$board_result" == "RPI5" ]]; then
dtoverlay sj201-buttons-overlay-pi5
else
dtoverlay sj201-buttons-overlay
fi
fi

if [[ ${detection_results[AIYVOICEBONNET]} == true ]] ; then
echo "Installing and configuring AIY VoiceBonnet"
# TODO by someone that has that HAT
dtoverlay aiy-voicebonnet
echo "Configuring board"
sleep 3 # Allow some time to fully initialise the hardware / driver

amixer -c "aiyvoicebonnet" cset numid=10 80,80
amixer -c "aiyvoicebonnet" cset numid=58 on
amixer -c "aiyvoicebonnet" cset numid=60 on
amixer -c "aiyvoicebonnet" cset numid=65 on
amixer -c "aiyvoicebonnet" cset numid=67 on
amixer -c "aiyvoicebonnet" cset numid=101 on
amixer -c "aiyvoicebonnet" cset numid=105 on
amixer -c "aiyvoicebonnet" cset numid=109 on
amixer -c "aiyvoicebonnet" cset numid=110 on
amixer -c "aiyvoicebonnet" cset numid=116 on
amixer -c "aiyvoicebonnet" cset numid=25 on
amixer -c "aiyvoicebonnet" cset numid=5 75
amixer -c "aiyvoicebonnet" cset numid=83 on
amixer -c "aiyvoicebonnet" cset numid=87 on
amixer -c "aiyvoicebonnet" cset numid=99 on
amixer -c "aiyvoicebonnet" cset numid=100 on
amixer -c "aiyvoicebonnet" cset numid=112 on
amixer -c "aiyvoicebonnet" cset numid=114 on
amixer -c "aiyvoicebonnet" cset numid=26 on
amixer -c "aiyvoicebonnet" cset numid=1 on
amixer -c "aiyvoicebonnet" cset numid=2 75
amixer -c "aiyvoicebonnet" cset numid=16 65,65
amixer -c "aiyvoicebonnet" cset numid=19 1,1
amixer -c "aiyvoicebonnet" cset numid=31 on
amixer -c "aiyvoicebonnet" cset numid=35 on
amixer -c "aiyvoicebonnet" cset numid=47 on
amixer -c "aiyvoicebonnet" cset numid=49 on
amixer -c "aiyvoicebonnet" cset numid=27 on
i2c_device_name="AIYVOICEBONNET"
fi

# Write the detection results to a file only if something is detected
if [ "$i2c_device_name" ]; then
[ -d /etc/OpenVoiceOS ] || mkdir /etc/OpenVoiceOS
echo "$i2c_device_name" > $i2c_device_file
fi
}

# Run the main function
main

0 comments on commit 29964e9

Please sign in to comment.