#version 1; November 2025
#Assumes laser has been connected with it.connect(x,9600)
#Assumes PPCL600 (or PPCL500) or PPCL700 (or PPCL550) device with recent firmware (within last 2 yrs)
#Run with: it.script("examples\\RecordDebugInfo.txt")
#---- Settings
outputfile="logdebuginfo.log"
#-----

import time


temp=it.serNo()[1][1]
product="None"
if temp.upper()[0:4]=="CRTM": product="PPCL600"
if temp.upper()[0:4]=="PP70": product="PPCL700"
f=open(temp+"_"+outputfile,"w")

it.nop()
readpacket=it.toModulePacket()
it.pwr(it.pwr()[1])
writepacket=it.toModulePacket()


tests=[]


def SetupTests():
    global product
    AddTest(0x00,0x00,0,0,1,"NOP")
    AddTest(0x42,0x00,0,1,0.01,"OOP")
    AddTest(0xFD,0x5000,0,0,1,"TunerState")
    if product=="PPCL600":
        AddTest(0xFD,0x2000,0,1,0.01,"Filter1_Domain")
        AddTest(0xFD,0x2001,0,1,0.01,"Filter2_Domain")
        AddTest(0xFD,0x2003,0,0,0.01,"Filter3_Domain")
        AddTest(0xFD,0x4007,0,0,1,"Filter1_Output")
        AddTest(0xFD,0x4003,0,0,1,"Filter2_Output")
        AddTest(0xFD,0x4001,0,0,1,"Filter3_Output")
    if product=="PPCL700":
        AddTest(0xFD,0x2100,0,1,0.01,"Filter1_Domain")
        AddTest(0xFD,0x2101,0,1,0.01,"Filter2_Domain")
        AddTest(0xFD,0x2002,0,0,0.01,"Filter3_Domain")
        AddTest(0xFD,0x4080,0,0,1,"Filter1_Output")
        AddTest(0xFD,0x4008,0,0,1,"Filter2_Output")
        AddTest(0xFD,0x4002,0,0,1,"Filter3_Output")
    AddTest(0xFD,0x2007,0,1,0.0001,"Demod")
    AddTest(0x20,0,0,0,1,"StatusF")

def AddTest(register,data,rw,signed,multiplier,name):
    global tests
    if register<0: register=0
    if register>0xff: register=0xff
    if data<0: data=0
    if data>0xffff: data=0xffff
    if rw: rw=1
    if signed: signed=1
    tests.append([register,data,rw,signed,multiplier,name])

def LogHeader():
    global f,tests
    counter=0
    f.write("Time ")
    while counter<len(tests):
        f.write("%s " %tests[counter][5])
        counter=counter+1
    f.write("\\n")

def LogData():
    global readpacket,writepacket
    global f, tests,reftime
    outp=[]
    outp.append("%.2f" %(time.perf_counter()-reftime))
    counter=0
    while counter<len(tests):
        if tests[counter][2]:
            writepacket.register(tests[counter][0])
            writepacket.data(tests[counter][1])
            result=it.packet(writepacket).data()
        else:
            readpacket.register(tests[counter][0])
            readpacket.data(tests[counter][1])
            result=it.packet(readpacket).data()
        if tests[counter][3]:
            if result>0x7fff: result=result-65536.0
        if tests[counter][4]==1: outp.append("%d" %result)
        else:
            result=result*tests[counter][4]
            outp.append("%.3f" %result)
        counter=counter+1
    while len(outp)>0:
        f.write(outp[0]+" ")
        outp.pop(0)
    f.write("\\n")
    f.flush()


SetupTests()
LogHeader()
reftime=time.perf_counter()
while time.perf_counter()-reftime<90:
    it.resena(sena=1)  #put this in here as the script delays will make us miss the first couple of seconds.
    LogData()

f.close()

print ("Completed device turnon with datalogging")
