################################################################################################### 
# Calibrating array and matrix pseudo-devices
################################################################################################### 



class Waveform(ReadonlyRegisterBase, ReadonlyRegisterArray):
    def doRead(self):
        time.sleep(0.001)
        self.val = to_array(self.calc(), 'd')
        return self.val

    def getSize(self):
        return len(self.take(-1))    #only reads if cache is None

class Image(ReadonlyRegisterBase, ReadonlyRegisterMatrix):
    def doRead(self):
        time.sleep(0.001)
        self.val = to_array(self.calc(), 'd')
        return self.val

    def getWidth(self):
        return len(self.take(-1)[0])

    def getHeight(self):
        return len(self.take(-1))



class SinusoidWaveform(Waveform):
    def calc(self):
        ret = []
        x = random.random()
        for i in range (20):
            ret.append(math.sin(x))
            x = x + 0.1
        return ret

class SinusoidImage(Image):
    def calc(self):     
        (width, height) = (200, 100)
        ret = []
        x = random.random();
        base = []        
        for i in range (width):
            base.append( math.sin(x))
            x = x + 0.05
        for i in range (height):
            noise = (random.random() - 0.5)/5.0 
            ret.append([x+noise for x in base])
        return ret

add_device(SinusoidWaveform("wf1"), True)
add_device(SinusoidImage("im1"), True)


class ArrayCalibrated(ReadonlyRegisterBase, ReadonlyRegisterArray, ReadableCalibratedArray):
    def doRead(self):
        return wf1.read()
        
    def getSize(self):
        return wf1.size
        
    def getCalibration(self):
        return ArrayCalibration(5,1000)
        
        

class MatrixCalibrated(ReadonlyRegisterBase, ReadonlyRegisterMatrix, ReadableCalibratedMatrix):
    def doRead(self):
        return im1.read()
        
    def getWidth(self):
        return im1.width

    def getHeight(self):
        return im1.height

    def getCalibration(self):
        return MatrixCalibration(2,4,100,200)       

add_device(ArrayCalibrated()
, True)
add_device( MatrixCalibrated()
, True)
ArrayCalibrated.polling=100
MatrixCalibrated.polling=250

  