From d29de1ede6584abb023106d8d35e5f7cad140d86 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Thu, 15 Apr 2021 15:32:46 -0700 Subject: [PATCH] Updated vars check and removed unused var codes --- scripts/check-config-vars.py | 58 ++++++++++++++++++++++-------- src/avr/Makefile | 2 +- src/avr/src/vars.json.in | 2 +- src/resources/config-template.json | 6 +--- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/scripts/check-config-vars.py b/scripts/check-config-vars.py index 6c255ab..68a532d 100755 --- a/scripts/check-config-vars.py +++ b/scripts/check-config-vars.py @@ -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) diff --git a/src/avr/Makefile b/src/avr/Makefile index dc490c4..53b7c96 100644 --- a/src/avr/Makefile +++ b/src/avr/Makefile @@ -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: diff --git a/src/avr/src/vars.json.in b/src/avr/src/vars.json.in index 267e916..b7c82dc 100644 --- a/src/avr/src/vars.json.in +++ b/src/avr/src/vars.json.in @@ -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) \ }, diff --git a/src/resources/config-template.json b/src/resources/config-template.json index 9212ebf..7966094 100644 --- a/src/resources/config-template.json +++ b/src/resources/config-template.json @@ -168,8 +168,7 @@ "type": "enum", "values": [ "manual", "switch-min", "switch-max", "stall-min", "stall-max"], - "default": "manual", - "code": "ho" + "default": "manual" }, "stall-microstep": { "type": "int", @@ -196,7 +195,6 @@ "iunit": "IPM", "scale": 0.0254, "default": 0.1, - "code": "lv", "hmodes": ["switch-min", "switch-max"] }, "latch-backoff": { @@ -206,7 +204,6 @@ "iunit": "in", "scale": 25.4, "default": 100, - "code": "lb", "hmodes": ["switch-min", "switch-max"] }, "stall-volts": { @@ -240,7 +237,6 @@ "iunit": "in", "scale": 25.4, "default": 5, - "code": "zb", "hmodes": ["switch-min", "switch-max", "stall-min", "stall-max"] } } -- 2.27.0