Update script to have flexible rotation sets

This commit is contained in:
Melissa LeBlanc-Williams 2021-02-25 14:05:41 -08:00
parent 5c00ae3e0a
commit 5cdf2f862c
2 changed files with 73 additions and 46 deletions

View file

@ -16,7 +16,7 @@ except ImportError:
shell = Shell()
shell.group = 'PITFT'
__version__ = "3.0.0"
__version__ = "3.1.0"
"""
This is the main configuration. Displays should be placed in the order
@ -120,6 +120,12 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"overlay": "dtoverlay=drm-minipitft13,rotation={pitftrot}",
"width": 240,
"height": 240,
"fbcp_rotations": {
"0", None
"90": "1",
"180": "2",
"270": "3",
},
},
{
"type": "st7789_240x320",
@ -131,7 +137,6 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"overlay": "dtoverlay=drm-st7789v_240x320,rotate={pitftrot}",
"width": 320,
"height": 240,
"rotate": False,
},
{
"type": "st7789_240x135",
@ -143,17 +148,29 @@ dtoverlay=pitft28-capacitive,{rotation}""",
"overlay": "dtoverlay=drm-minipitft114,rotation={pitftrot}",
"width": 240,
"height": 135,
},
"fbcp_rotations": {
"0", "3"
"90": "1",
"180": "2",
"270": None,
},
},
]
# default rotations
fbcp_rotations = {
"0": "1",
"90": None,
"180": "3",
"270": "2",
}
PITFT_ROTATIONS = ("90", "180", "270", "0")
UPDATE_DB = False
SYSTEMD = None
pitft_config = None
pitftrot = None
auto_reboot = None
force_kernel = None
force_kernel_release = "1.20201126-1"
def warn_exit(message):
shell.warn(message)
@ -220,6 +237,36 @@ def uninstall_etc_modules():
shell.pattern_replace("/etc/modules", 'fbtft_device')
return True
def compile_driver():
"""Compile display driver and overlay if required"""
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
print("Compiling Device Tree Overlay")
shell.run_command("dtc -@ -I dts -O dtb -o {dest} {src}".format(dest=pitft_config['overlay_dest'], src=pitft_config['overlay_src']))
if pitft_config['kernel_upgrade']:
print("############# UPGRADING KERNEL ###############")
print("Updating packages...")
if not shell.run_command("sudo apt-get update", True):
warn_exit("Apt failed to update itself!")
print("Upgrading packages...")
if not shell.run_command("sudo apt-get -y upgrade", False):
warn_exit("Apt failed to install software!")
print("Installing Kernel Headers. This may take a few minutes...")
if not shell.run_command("apt-get install -y raspberrypi-kernel-headers", True):
warn_exit("Apt failed to install software!")
if not shell.isdir("/lib/modules/{}/build".format(shell.release())):
warn_exit("Kernel was updated, but needs to be loaded. Please reboot now and re-run script!")
print("Compiling and installing display driver...")
shell.pushd("st7789_module")
if not shell.run_command("make"):
warn_exit("Apt failed to compile ST7789V drivers!")
# Only back up if a copy doesn't already exist, so if script is ran more than once we don't lose the original
if not shell.exists("/lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.BACK".format(rel=shell.release())):
shell.run_command("mv /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.BACK".format(rel=shell.release()))
shell.run_command("mv fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko".format(rel=shell.release()))
shell.popd()
return True
def update_configtxt(rotation_override=None):
"""update /boot/config.txt with appropriate values"""
uninstall_bootconfigtxt()
@ -230,33 +277,6 @@ def update_configtxt(rotation_override=None):
overlay = overlay.format(pitftrot=rotation)
if "{rotation}" in overlay and isinstance(pitft_config['rotations'], dict):
overlay = overlay.format(rotation=pitft_config['rotations'][pitftrot])
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
shell.run_command("dtc -@ -I dts -O dtb -o {dest} {src}".format(dest=pitft_config['overlay_dest'], src=pitft_config['overlay_src']))
print("############# UPGRADING KERNEL ###############")
print("Updating packages...")
if not shell.run_command("sudo apt-get update", True):
warn_exit("Apt failed to update itself!")
print("Upgrading packages...")
if not shell.run_command("sudo apt-get -y upgrade", False):
warn_exit("Apt failed to install software!")
if force_kernel and force_kernel + '.' not in shell.release():
print("Pinning kernel to version {}. This may take a while...".format(force_kernel))
if not shell.run_command("sudo sh rpi-pin-kernel-firmware.sh {}".format(force_kernel_release), False):
warn_exit("Failed to pin all kernel files!")
print("Installing Kernel Headers...")
if not shell.run_command("apt-get install -y raspberrypi-kernel-headers", True):
warn_exit("Apt failed to install software!")
if not shell.isdir("/lib/modules/{}/build".format(shell.release())):
warn_exit("Kernel was updated, please reboot now and re-run script!")
shell.pushd("st7789_module")
if not shell.run_command("make"):
warn_exit("Apt failed to compile ST7789V drivers!")
# Only back up if a copy doesn't already exist, so if script is ran more than once we don't lose the original
if not shell.exists("/lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.BACK".format(rel=shell.release())):
shell.run_command("mv /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.BACK".format(rel=shell.release()))
shell.run_command("mv fb_st7789v.ko /lib/modules/{rel}/kernel/drivers/staging/fbtft/fb_st7789v.ko".format(rel=shell.release()))
shell.popd()
shell.write_text_file("/boot/config.txt", """
# --- added by adafruit-pitft-helper {date} ---
@ -356,6 +376,9 @@ def install_fbcp():
shell.popd()
shell.run_command("rm -rf /tmp/rpi-fbcp-master")
if "fbcp_rotations" in pitft_config:
fbcp_rotations = pitft_config['fbcp_rotations']
# Start fbcp in the appropriate place, depending on init system:
if SYSTEMD:
# Add fbcp to /etc/rc.local:
@ -400,20 +423,19 @@ def install_fbcp():
shell.reconfig("/boot/config.txt", "^.*hdmi_cvt.*$", "hdmi_cvt={} {} 60 1 0 0 0".format(WIDTH, HEIGHT))
if pitftrot == "90" or ("rotate" in pitft_config and not pitft_config['rotate']):
# dont rotate HDMI on 90
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "")
try:
default_orientation = int(list(fbcp_rotations.keys())[list(fbcp_rotations.values()).index(None)])
except ValueError:
default_orientation = 90
if pitftrot in ("0", "180", "270"):
if pitftrot == "180":
display_rotate = "3"
elif pitftrot == "270":
display_rotate = "2"
else:
display_rotate = "1"
if fbcp_rotations[pitftrot] is None:
# dont rotate HDMI on default orientation
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "")
else:
display_rotate = fbcp_rotations[pitftrot]
shell.reconfig("/boot/config.txt", "^.*display_hdmi_rotate.*$", "display_hdmi_rotate={}".format(display_rotate))
# Because we rotate HDMI we have to 'unrotate' the TFT by overriding pitftrot!
if not update_configtxt(90):
if not update_configtxt(default_orientation):
shell.bail("Unable to update /boot/config.txt")
return True
@ -612,6 +634,11 @@ Run time of up to 5 minutes. Reboot required!
if not softwareinstall():
shell.bail("Unable to install software")
if "overlay_src" in pitft_config and "overlay_dest" in pitft_config:
shell.info("Compiling display driver and device tree overlay...")
if not compile_driver():
shell.bail("Unable to compile display driver or device tree overlay")
shell.info("Updating /boot/config.txt...")
if not update_configtxt():
shell.bail("Unable to update /boot/config.txt")

View file

@ -189,9 +189,9 @@ static int set_var(struct fbtft_par *par)
row1_offset = row2_offset = (int)((320 - height + 1) / 2);
col1_offset = (int)((240 - width) / 2);
col2_offset = (int)((240 - width + 1) / 2);
} else {
row1_offset = 0;
row2_offset = (320 - height);
} else {
row1_offset = 0;
row2_offset = (320 - height);
col1_offset = col2_offset = (240 - width);
}