--- /dev/null
+#!/bin/bash
+
+if [ $# != 3 ]; then
+ echo "Usage: $0 <width> <height> <rotation>"
+ 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
--- /dev/null
+#!/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 = '<name>=<value>', 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)
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
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