diff --git a/classes/StargateMilkyWay/dialers.py b/classes/StargateMilkyWay/dialers.py index 3d37679..93fef48 100644 --- a/classes/StargateMilkyWay/dialers.py +++ b/classes/StargateMilkyWay/dialers.py @@ -1,4 +1,5 @@ import os +from glob import glob from time import sleep from serial.serialutil import SerialException import StargateCmdMessenger @@ -52,7 +53,10 @@ def _connect_dhd(self): # Get a Semaphore lock on the parent process so when the PyCmdMessenger class # prints to STDOUT, it doesn't step on STDOUT from other threads ### Connect to the DHD object. Will throw exception if not present - dhd = DHDv2(self.dhd_port, self.dhd_serial_baud_rate, self.log) + dhd_port = DHDv2.get_dhd_port(self.dhd_port) + if dhd_port is None: + raise SerialException + dhd = DHDv2(dhd_port, self.dhd_serial_baud_rate, self.log) self.log.log('DHDv2 Found. Connected.') except SerialException: # pylint: disable=try-except-raise raise @@ -199,18 +203,26 @@ def set_color_symbols(self, color_tuple): self.color_symbols = color_tuple @staticmethod - def get_dhd_port(): + def get_dhd_port(configured_port=None): """ This is a simple helper function to help locate the port for the DHD :return: The file path for the DHD is returned. If it is not found, returns None. """ # A list for places to check for the DHD - possible_files = ["/dev/serial/by-id/usb-Adafruit_ItsyBitsy_32u4_5V_16MHz_HIDPC-if00", "/dev/ttyACM0", "/dev/ttyACM1"] + possible_files = [ + configured_port, + "/dev/serial/by-id/usb-SparkFun_SparkFun_Pro_Micro_HIDPC-if00", + *sorted(glob("/dev/serial/by-id/*SparkFun*Pro_Micro*")), + "/dev/serial/by-id/usb-Adafruit_ItsyBitsy_32u4_5V_16MHz_HIDPC-if00", + *sorted(glob("/dev/serial/by-id/*ItsyBitsy*")), + "/dev/ttyACM0", + "/dev/ttyACM1" + ] # Run through the list and check if the file exists. for file in possible_files: # If the file exists, return the path and end the function. - if os.path.exists(file): + if file and os.path.exists(file): return file # If the DHD is not detected diff --git a/classes/stargate_audio.py b/classes/stargate_audio.py index 56db910..7bba706 100644 --- a/classes/stargate_audio.py +++ b/classes/stargate_audio.py @@ -169,8 +169,8 @@ def set_correct_audio_output_device(self): ctl = 'defaults.ctl.card ' + str(self.get_usb_audio_device_card_number()) pcm = 'defaults.pcm.card ' + str(self.get_usb_audio_device_card_number()) # replace the lines in the alsa.conf file. - subprocess.run(['sudo', 'sed', '-i', f"/defaults.ctl.card /c\{ctl}", '/usr/share/alsa/alsa.conf'], check=False) #TODO: Check should be true - subprocess.run(['sudo', 'sed', '-i', f"/defaults.pcm.card /c\{pcm}", '/usr/share/alsa/alsa.conf'], check=False) #TODO: Check should be true + subprocess.run(['sudo', 'sed', '-i', f"s/^defaults\\.ctl\\.card .*/{ctl}/", '/usr/share/alsa/alsa.conf'], check=False) #TODO: Check should be true + subprocess.run(['sudo', 'sed', '-i', f"s/^defaults\\.pcm\\.card .*/{pcm}/", '/usr/share/alsa/alsa.conf'], check=False) #TODO: Check should be true except subprocess.CalledProcessError: self.log.log("Failed to set audio adapter config") diff --git a/config/defaults-milkyway/config.json.dist b/config/defaults-milkyway/config.json.dist index 97b1b13..36676a0 100644 --- a/config/defaults-milkyway/config.json.dist +++ b/config/defaults-milkyway/config.json.dist @@ -174,7 +174,7 @@ "max_value": 115200 }, "dhd_serial_port": { - "value": "/dev/serial/by-id/usb-Adafruit_ItsyBitsy_32u4_5V_16MHz_HIDPC-if00", + "value": "/dev/serial/by-id/usb-SparkFun_SparkFun_Pro_Micro_HIDPC-if00", "desc": "The USB Serial Device path for the DHD", "type": "str" }, diff --git a/install/functions.sh b/install/functions.sh index ec30c08..704fd86 100644 --- a/install/functions.sh +++ b/install/functions.sh @@ -53,7 +53,7 @@ function apt_update_and_install() { # Install system-level dependencies echo 'Installing system-level dependencies...this may take a while.' - sudo apt-get install --no-install-recommends -y nano clang python3-dev python3-venv libasound2-dev avahi-daemon apache2 wireguard ufw python3-smbus i2c-tools netcat-traditional | sed 's/^/ /' + sudo apt-get install --no-install-recommends -y nano clang python3-dev python3-venv libasound2-dev avahi-daemon apache2 wireguard ufw python3-smbus i2c-tools iw netcat-traditional | sed 's/^/ /' } function init_venv() { @@ -156,25 +156,39 @@ function disable_pwr_mgmt() { echo "Created $CONFIG file" fi - if grep -Fq '/sbin/iw wlan0 set power_save off' $CONFIG + WIFI_POWER_SAVE_OFF='command -v iw >/dev/null 2>&1 && iw dev wlan0 set power_save off || true' + + if grep -Fq "$WIFI_POWER_SAVE_OFF" $CONFIG then echo 'WiFi power management is already disabled' else echo 'Disabling WiFi power management' - sudo sed -i '$i\\r\n/sbin\/iw wlan0 set power_save off\r\n' $CONFIG + TEMP_CONFIG=$(mktemp) + awk -v wifi_power_save_off="$WIFI_POWER_SAVE_OFF" ' + /\/sbin\/iw wlan0 set power_save off/ { next } + /^exit 0$/ && !inserted { print wifi_power_save_off; inserted=1 } + { print } + END { if (!inserted) print wifi_power_save_off } + ' "$CONFIG" > "$TEMP_CONFIG" + sudo cp "$TEMP_CONFIG" "$CONFIG" + rm "$TEMP_CONFIG" fi } function disable_onboard_audio() { # Disable the onboard audio adapter - sudo cp /boot/config.txt /boot/config.bak echo 'Disabling RaspberryPi on-board audio adapter' - CONFIG='/boot/firmware/config.txt' + if [ -f '/boot/firmware/config.txt' ]; then + CONFIG='/boot/firmware/config.txt' + else + CONFIG='/boot/config.txt' + fi + sudo cp "$CONFIG" "$CONFIG.bak" SETTING='off' - sudo sed $CONFIG -i -r -e "s/^((device_tree_param|dtparam)=([^,]*,)*audio?)(=[^,]*)?/\1=$SETTING/" - if ! grep -q -E '^(device_tree_param|dtparam)=([^,]*,)*audio?=[^,]*' $CONFIG; then + sudo sed "$CONFIG" -i -r -e "s/^((device_tree_param|dtparam)=([^,]*,)*audio?)(=[^,]*)?/\1=$SETTING/" + if ! grep -q -E '^(device_tree_param|dtparam)=([^,]*,)*audio?=[^,]*' "$CONFIG"; then echo 'pattern not found, creating' - printf "dtparam=audio=$SETTING\n" >> $CONFIG + printf "dtparam=audio=$SETTING\n" | sudo tee -a "$CONFIG" > /dev/null fi }