Added config-screen script
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 12 Oct 2019 23:44:10 +0000 (16:44 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 12 Oct 2019 23:44:10 +0000 (16:44 -0700)
CHANGELOG.md
scripts/config-screen [new file with mode: 0755]
scripts/edit-boot-config [new file with mode: 0755]
scripts/edit-config [new file with mode: 0755]
scripts/install.sh
setup.py

index 80989afcbc95c0c732b412fc146e1f8b284b3975..09e2b04ced29ca79b67ac27d3f5d737570c9a290 100644 (file)
@@ -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 (executable)
index 0000000..364c150
--- /dev/null
@@ -0,0 +1,32 @@
+#!/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
diff --git a/scripts/edit-boot-config b/scripts/edit-boot-config
new file mode 100755 (executable)
index 0000000..d7dd4d4
--- /dev/null
@@ -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 (executable)
index 0000000..05377d6
--- /dev/null
@@ -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 = '<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)
index eeacf8ff6168b7b0a1604b863f78c402da9ba133..a426ccaa64634e8c2c3797137523af7f99dbb3b3 100755 (executable)
@@ -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
index 34eb89d89bf3f878515ffacb0319d27c0dba5c3b..cf4069dd5f7da577eb81e135b71dee07e990651b 100755 (executable)
--- 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(),