main
John Lauer Bind output-locked temperature diagnostics to selected NTCLE428 sensor d2b6d40
"""Exercise the linked nominal sensor curve and complete ADC domain."""
from pathlib import Path
import ctypes,json,math,hashlib,subprocess,tempfile
R=Path(__file__).resolve().parents[1];O=R/'checkpoints/sensor-firmware-review';source=R/'design/sensor-candidate.json';rows=json.loads(source.read_text())['rows']
with tempfile.TemporaryDirectory(prefix='load-ntc-test-') as tmp:
 lib=Path(tmp)/'candidate.so'
 subprocess.run(['gcc','-std=c11','-Wall','-Wextra','-Werror','-shared','-fPIC',str(R/'firmware/ntcle428.c'),str(R/'firmware/adc_conversion.c'),'-lm','-o',str(lib)],check=True)
 f=ctypes.CDLL(str(lib)).load_ntcle428_nominal_c;f.argtypes=[ctypes.c_float,ctypes.POINTER(ctypes.c_float)];f.restype=ctypes.c_bool
 errors=[]
 for t,r,lo,hi in rows:
  out=ctypes.c_float(-999);assert f(r,ctypes.byref(out));errors.append(abs(out.value-t))
 assert max(errors)<0.001
 for resistance in [float('nan'),float('inf'),-float('inf'),0,-1,rows[0][1]*1.001,rows[-1][1]*.999]:
  out=ctypes.c_float(-999);assert not f(resistance,ctypes.byref(out));assert out.value==-999
 assert not f(10000,None)
 last=-math.inf;count=10001
 for i in range(count):
  r=math.exp(math.log(rows[0][1])+(math.log(rows[-1][1])-math.log(rows[0][1]))*i/(count-1));out=ctypes.c_float()
  assert f(r,ctypes.byref(out));assert out.value>=last;assert -40.001<=out.value<=105.001;last=out.value
 adc=ctypes.CDLL(str(lib)).load_adc_convert;adc.argtypes=[ctypes.c_uint16]*4+[ctypes.POINTER(ctypes.c_float)]*2;adc.restype=ctypes.c_bool
 valid=0;invalid=0;last=math.inf
 for code in range(4096):
  v=ctypes.c_float(-999);t=ctypes.c_float(-999)
  resistance=10000.0*code/(4095-code) if code<4095 else math.inf
  expected=16<=code<=4079 and rows[-1][1]<=resistance<=rows[0][1]
  ok=adc(1650,1500,564,code,ctypes.byref(v),ctypes.byref(t));assert ok==expected,(code,resistance,ok,expected)
  if ok:
   valid+=1;assert t.value<=last;last=t.value;assert abs(v.value-5)<.005
  else:
   invalid+=1;assert v.value==t.value==-999
 report={'status':'diagnostic-software-checks-pass-not-qualified','sourceSha256':hashlib.sha256(source.read_bytes()).hexdigest(),'curveSourceSha256':hashlib.sha256((R/'firmware/ntcle428.c').read_bytes()).hexdigest(),'adcCodesChecked':4096,'validAdcCodes':valid,'rejectedAdcCodes':invalid,'manufacturerTableNodes':len(rows),'maxNodeErrorC':max(errors),'monotonicSamples':count,'invalidCases':8,'diagnosticFirmwareChanged':True,'outputLockUnchanged':True,'limitations':['No between-node manufacturer error bound','No physical or sensor calibration','No installed thermal tracking or trip qualification']}
 (O/'checks.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))