Updated vars check and removed unused var codes
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 15 Apr 2021 22:32:46 +0000 (15:32 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 15 Apr 2021 22:32:46 +0000 (15:32 -0700)
scripts/check-config-vars.py
src/avr/Makefile
src/avr/src/vars.json.in
src/resources/config-template.json

index 6c255ab9e686e2c617d888b06ce2fb989bf698f3..68a532d834bddc578be6ebb967bb307e26dcbb79 100755 (executable)
@@ -1,37 +1,65 @@
 #!/usr/bin/env python3
 
-'''Check that the configuration variable template used on the RPi matches the
-variables used in the AVR'''
+'''
+Check that the configuration variable template used on the RPi matches the
+variables used in the AVR.
+'''
 
 import sys
 import json
 
 templ = json.load(open('src/resources/config-template.json', 'r'))
-vars = json.load(open('src/avr/build/vars.json', 'r'))
+vars  = json.load(open('src/avr/build/vars.json', 'r'))
 
 
-def check(section):
+def get_codes(vars):
+    for code in vars:
+        if 'index' in vars[code]:
+            for c in vars[code]['index']: yield c + code
+        else: yield code
+
+
+def check(section, codes, prefix = ''):
     errors = 0
 
     for name, entry in section.items():
         if 'type' in entry:
-            ok = False
+            if entry['type'] == 'list' and 'index' in entry:
+                index = entry['index']
 
-            # TODO check that defaults are valid
-            # TODO check that types match
+                for c in index:
+                    errors += check(entry['template'], codes, c)
 
-            if 'code' in entry and not entry['code'] in vars:
-                print('"%s" with code "%s" not found' % (name, entry['code']))
+                return errors
+
+            # TODO check that defaults are valid
 
-            else: ok = True
+            # Check that code exists
+            if 'code' in entry:
+                code = prefix + entry['code']
+                if not code in codes:
+                    errors += 1
+                    print('"%s" with code "%s" not found' % (name, code))
 
-            if not ok: errors += 1
+                else:
+                    # Check that types match
+                    type1 = vars[entry['code']]['type']
+                    type2 = entry['type']
+                    if ((type1 == 'u8' and type2 not in ('int', 'enum')) or
+                        (type1 == 'u16' and type2 != 'int') or
+                        (type1 == 'f32' and type2 != 'float') or
+                        (type1 == 'b8' and type2 != 'bool')):
+                        errors += 1
+                        print('%s, %s type mismatch %s != %s' % (
+                            name, code, type2, type1))
 
-        else: errors += check(entry)
+        else: errors += check(entry, codes, prefix)
 
     return errors
 
 
-errors = check(templ)
-print('\n%d errors' % errors)
-sys.exit(errors != 0)
+codes = set(get_codes(vars))
+errors = check(templ, codes)
+if errors:
+    print('\n%d errors' % errors)
+    sys.exit(1)
index dc490c4239180f4b3cdc470af2421adb429b1701..53b7c9643c432a00a680b4c3d6a27e13be55fc62 100644 (file)
@@ -24,7 +24,7 @@ $(PROJECT).elf: $(OBJ)
 
 # JSON
 build/%.json: src/%.json.in src/%.def
-       cpp -Isrc $< | sed "/^#.*$$/d;s/'\(.\)'/\"\1\"/g" > $@
+       cpp -Isrc $< | sed "/^#.*$$/d;s/'\(.\)'/\"\1\"/g" | json_pp > $@
 
 # Program
 init:
index 267e916162f6e2482fe59e57986173f4bf1425b5..b7c82dc7c4efa32ad7745caa041d5e6d20afdeca 100644 (file)
@@ -4,7 +4,7 @@
 #CODE: {                                        \
     "name": #NAME,                              \
     "type": #TYPE,                              \
-    "index": IF_ELSE(INDEX)(true, false),       \
+    IF(INDEX)("index": INDEX##_LABEL COMMA())   \
     "setable": IF_ELSE(SET)(true, false),       \
     "report": IF_ELSE(REPORT)(true, false)      \
   },
index 9212ebf2fdb360e668dab9ceea75b5268f184636..796609466fb05b3473e692e90b9682ee72bc3cb5 100644 (file)
           "type": "enum",
           "values": [
             "manual", "switch-min", "switch-max", "stall-min", "stall-max"],
-          "default": "manual",
-          "code": "ho"
+          "default": "manual"
         },
         "stall-microstep": {
           "type": "int",
           "iunit": "IPM",
           "scale": 0.0254,
           "default": 0.1,
-          "code": "lv",
           "hmodes": ["switch-min", "switch-max"]
         },
         "latch-backoff": {
           "iunit": "in",
           "scale": 25.4,
           "default": 100,
-          "code": "lb",
           "hmodes": ["switch-min", "switch-max"]
         },
         "stall-volts": {
           "iunit": "in",
           "scale": 25.4,
           "default": 5,
-          "code": "zb",
           "hmodes": ["switch-min", "switch-max", "stall-min", "stall-max"]
         }
       }