import ch.psi.pshell.epics.CAS as CAS



class ScanDevice(ReadonlyAsyncRegisterBase, ReadonlyRegisterArray):
    def getSize(self):
        global scan_result
        return len(self.take())   

    def append(self, value):
        c = self.take()
        c.append(value)
        self.set(c)
        
    def set(self, value):
        self.onReadout(to_array(value, 'd'))



if get_device("scan_pos") is None:
    add_device(ScanDevice("scan_pos"), True)
    add_device(ScanDevice("scan_val"), True)
    scan_pos.set([])
    scan_val.set([])
    
    cas1 = CAS("PSHELL:scanpos",scan_pos, "double")
    cas2 = CAS("PSHELL:scanval",scan_val, "double")
    cas3 = CAS("PSHELL:var",ai1)

    scan_pos.set([])
    scan_val.set([])



scan_pos.set([])
scan_val.set([])

def after(rec):
    scan_pos.append(rec.positions[0])
    scan_val.append(rec.values[0])
    
res = lscan(ao1, ai1, 0, 40, 40, 0.1, after_read = after)
#scan_pos.set(res.getPositions(0))
#scan_val.set(res.getReadable(0))

set_return(res.print())


