From 254377a5348cd2db7e02f0ffd725471b5bfff245 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sat, 12 Oct 2019 16:44:10 -0700 Subject: [PATCH] Added config-screen script --- CHANGELOG.md | 1 + scripts/config-screen | 32 ++++++++++++++++ scripts/edit-boot-config | 12 ++++++ scripts/edit-config | 83 ++++++++++++++++++++++++++++++++++++++++ scripts/install.sh | 23 ++--------- setup.py | 3 ++ 6 files changed, 135 insertions(+), 19 deletions(-) create mode 100755 scripts/config-screen create mode 100755 scripts/edit-boot-config create mode 100755 scripts/edit-config diff --git a/CHANGELOG.md b/CHANGELOG.md index 80989af..09e2b04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Buildbotics CNC Controller Firmware Changelog - Acknowledging a message on one browser clears it for all. - Automatically reload Web view when file changes. - Changed "Message" field to "Reason" in Web interface. + - Added ``config-screen`` script. Web based screen config to come later. ## v0.4.11 - Don't reset global offsets on M2. diff --git a/scripts/config-screen b/scripts/config-screen new file mode 100755 index 0000000..364c150 --- /dev/null +++ b/scripts/config-screen @@ -0,0 +1,32 @@ +#!/bin/bash + +if [ $# != 3 ]; then + echo "Usage: $0 " + exit 1 +fi + +WIDTH="$1" +HEIGHT="$2" +ROTATION="$3" + +if [[ ! "$WIDTH" =~ ^[0-9]+$ ]]; then + echo "Invalid width '$WIDTH'." + exit 1 +fi + +if [[ ! "$HEIGHT" =~ ^[0-9]+$ ]]; then + echo "Invalid height '$HEIGHT'." + exit 1 +fi + +if [[ ! "$ROTATION" =~ ^[0-3]$ ]]; then + echo "Invalid rotation '$ROTATION'." + exit 1 +fi + + +OPTIONS="framebuffer_width=$WIDTH " +OPTIONS+="framebuffer_height=$HEIGHT " +OPTIONS+="display_rotate=$ROTATION" + +edit-boot-config $OPTIONS diff --git a/scripts/edit-boot-config b/scripts/edit-boot-config new file mode 100755 index 0000000..d7dd4d4 --- /dev/null +++ b/scripts/edit-boot-config @@ -0,0 +1,12 @@ +#!/bin/bash + +cp /boot/config.txt /tmp/ + +edit-config /tmp/config.txt "$@" + +diff /boot/config.txt /tmp/config.txt >/dev/null +if [ $? -eq 1 ]; then + mount -o remount,rw /boot + mv /tmp/config.txt /boot/ + mount -o remount,ro /boot +fi diff --git a/scripts/edit-config b/scripts/edit-config new file mode 100755 index 0000000..05377d6 --- /dev/null +++ b/scripts/edit-config @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +import sys +import argparse +import re + + +def option_type(s, pat = re.compile(r'\w+=.*')): + if not pat.match(s): raise argparse.ArgumentTypeError + return s + + +parser = argparse.ArgumentParser(description = 'Edit config file.') +parser.add_argument('config', help = 'The configuration file to edit.') +parser.add_argument('options', nargs = '*', type = option_type, + metavar = '=', help = 'An option to set.') +args = parser.parse_args() + + +# Parse config lines and options +lines = [] +options = {} +optRE = re.compile(r'(\w+)\s*=\s*([^#]+)(#.*)?') + + +class Line: + def __init__(self, text, name = None, value = None, comment = None): + self.text = text + self.name = name + self.value = value + self.comment = comment + + + def __str__(self): + if self.name is not None: + if self.value.strip(): + text = '%s=%s' % (self.name, self.value) + if self.comment: text += ' # ' + self.comment + text += '\n' + return text + + else: return '' + + return self.text + + +with open(args.config, 'r') as f: + for line in f: + m = optRE.match(line.strip()) + + if m: name, value, comment = m.groups() + else: name, value, comment = None, None, None + + l = Line(line, name, value, comment) + lines.append(l) + if name is not None: options[name] = l + + +# Save original config +config = ''.join(map(str, lines)) + + +# Set options +first = True +for opt in args.options: + name, value = opt.split('=', 1) + + if name in options: options[name].value = value + else: + if first and len(lines) and str(lines[:-1]).strip() != '': + first = False + lines.append(Line('\n')) + + lines.append(Line(None, name, value, None)) + + +# Assemble new config +new_config = ''.join(map(str, lines)) + + +if new_config != config: + with open(args.config, 'w') as f: + f.write(new_config) diff --git a/scripts/install.sh b/scripts/install.sh index eeacf8f..a426cca 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -13,21 +13,6 @@ while [ $# -gt 0 ]; do done -function update_config_txt() { - MATCH="$1" - CONFIG="$2" - - grep "$MATCH" /boot/config.txt - - if [ $? -ne 0 ]; then - mount -o remount,rw /boot && - echo "$CONFIG" >> /boot/config.txt - mount -o remount,ro /boot - fi - -} - - if $UPDATE_PY; then systemctl stop bbctrl @@ -43,12 +28,12 @@ if $UPDATE_AVR; then fi # Update config.txt -update_config_txt ^max_usb_current max_usb_current=1 -update_config_txt ^config_hdmi_boost config_hdmi_boost=8 +./scripts/edit-boot-config max_usb_current=1 +./scripts/edit-boot-config config_hdmi_boost=8 # TODO Enable GPU -#update_config_txt dtoverlay=vc4-kms "dtoverlay=vc4-kms-v3d" -#update_config_txt gpu_mem "gpu_mem=16" +#./scripts/edit-boot-config dtoverlay=vc4-kms-v3d +#./scripts/edit-boot-config gpu_mem=16 #chmod ug+s /usr/lib/xorg/Xorg # Fix camera diff --git a/setup.py b/setup.py index 34eb89d..cf4069d 100755 --- a/setup.py +++ b/setup.py @@ -30,6 +30,9 @@ setup( 'scripts/sethostname', 'scripts/reset-video', 'scripts/config-wifi', + 'scripts/config-screen', + 'scripts/edit-config', + 'scripts/edit-boot-config', 'scripts/browser', ], install_requires = 'tornado sockjs-tornado pyserial pyudev smbus2'.split(), -- 2.27.0