Thanks for the script. I made some tweaks, put it in /opt/initDisplay/initDisplay.sh and start it via /etc/rc.local instead. I added a check to not do endless loop if something breaks, so for the second time when PI is booted the configuration is not checked. In addition if there's a file called /opt/initDisplay/skipcheck the check is also not performed, plus I added delay of 3 seconds before reboot. So if something is wrong, user is able to quickly SSH into the PI and make it skip the script on next reboot.
Note that now script checks if below lines exist in /boot/config.txt to distinguish which config is present:
for HDMI: # this is config HDMI
for LCD: # this is config LCD
I added that lines in first line of the config. Here's tweaked script:
#!/usr/bin/env bash
ROOT_FOLDER=/opt/initDisplay
if [ -f $ROOT_FOLDER/skipcheck ]; then
exit 0
fi
if [ -f $ROOT_FOLDER/justbooted ]; then
rm $ROOT_FOLDER/justbooted
exit 0
fi
timeout=3
rm -f hdmi.name
tvservice -n > $ROOT_FOLDER/hdmi.name 2>&1
HDMI_NAME=`cat $ROOT_FOLDER/hdmi.name`
echo $HDMI_NAME
if [ "$HDMI_NAME" == "[E] No device present" ]; then
LCD_ON=`cat /boot/config.txt | grep "# this is config HDMI"`
if [ "$LCD_ON" == "# this is config HDMI" ]; then
echo "reboot to configuration LCD, press ESC to cancel"
sudo rm -f /boot/config.txt
sudo cp /boot/config_lcd.txt /boot/config.txt
#sudo cp /usr/share/alsa/alsa.conf.lcd /usr/share/alsa/alsa.conf
while [ "$timeout" -gt "0" ]; do
echo -n " $timeout"
if read -n1 -t1 -r -s x; then
echo " ...canceled"
exit 0
fi
let timeout--
done
echo "skip reboot" > $ROOT_FOLDER/justbooted
echo " Reboot now!"
sudo reboot -n
else
echo "LCD/HDMI configuration OK (no HDMI)"
fi
else
HDMI_ON=`cat /boot/config.txt | grep "# this is config LCD"`
echo $HDMI_ON
if [ "$HDMI_ON" == "# this is config LCD" ]; then
echo "reboot to configuration HDMI, press ESC to cancel"
sudo rm -f /boot/config.txt
sudo cp /boot/config_hdmi.txt /boot/config.txt
#sudo cp /usr/share/alsa/alsa.conf.hdmi /usr/share/alsa/alsa.conf
while [ "$timeout" -gt "0" ]; do
echo -n " $timeout"
if read -n1 -t1 -r -s x; then
echo " ...canceled"
exit 0
fi
let timeout--
done
echo "skip reboot" > $ROOT_FOLDER/justbooted
echo " Reboot now!"
sudo reboot -n
else
echo "LCD/HDMI configuration OK (HDMI $HDMI_NAME)"
fi
fi