import ch.psi.pshell.device.ReadonlyRegisterBase as ReadonlyRegisterBase
import ch.psi.pshell.device.ReadonlyRegister.ReadonlyRegisterArray as ReadonlyRegisterArray

class Mean(Readable):
    def __init__(self, dev):
        self.dev =dev    

    def read(self):  
        return mean(self.dev.take())

class Variance(Readable):
    def __init__(self, dev):
        self.dev =dev    

    def read(self):  
        return variance(self.dev.take())

class Measures(ReadonlyRegisterBase, ReadonlyRegisterArray):
    def __init__(self, dev, count, interval=0):        
        self.dev = dev
        self.count = count
        self.interval = interval        
        self.initialize()
        
    def getSize(self):
        return self.count
                
    def doRead(self):      
        measures = []
        for i in range(self.count):
            measures.append(self.dev.read())
            if self.interval>0 and i<(self.count-1):
                time.sleep(self.interval)
        return measures  
 

def average(dev, count, interval=0.0):
    #av = Measures(dev, count, interval)
    #return [av, Mean(av), Variance(av)]
    av = Averager(dev, count, int(interval*1000))
    av.initialize()    
    return [av, av.samples, av.variance]
                   
res= lscan(inp, average(sin,5,0.1), 0, 40, 20, 0.1)
