var SinusoidImage = Java.extend(Java.type("ch.psi.pshell.device.ReadonlyRegisterBase"), Java.type("ch.psi.pshell.device.ReadonlyRegister.ReadonlyRegisterMatrix"))
var dev = new SinusoidImage("im1") {	    
        doRead: function () {        
            sleep(0.001)
            var ret = []
            var width = 200
            var height = 100
            x = Math.random()
            var base = []
            for (var i = 0; i < width ; i++) { 
                base.push(Math.sin(x))
                x = x  + 0.05      
            } 
            for (var i = 0; i < height ; i++) { 
            	var noise = (Math.random() - 0.5)/5.0 
            	var row = []
            	for (var j = 0; j < width ; j++) { 
            		row.push(base[j]+noise)
            	}            	
                ret.push(row)	
            }
            print ("len = " + ret.length)
            return to_array(ret, 'd')
        },
   
        getWidth: function () {             
            return SinusoidSuper.take(-1)[0].length    
        },


        getHeight: function () {             
            return SinusoidSuper.take(-1).length    
        },
        
    }   
var SinusoidSuper = Java.super(dev)    
add_device(dev, true)


var SinusoidWaveform = Java.extend(Java.type("ch.psi.pshell.device.ReadonlyRegisterBase"), Java.type("ch.psi.pshell.device.ReadonlyRegister.ReadonlyRegisterArray"))
var dev = new SinusoidWaveform("wf1") {	    
        doRead: function () {        
            sleep(0.001)
            var ret = []
            x = Math.random()
            for (var i = 0; i < 20; i++) { 
                ret.push(Math.sin(x))
                x = x + 0.1            
            } 
            return ret
        },
   
        getSize: function () {             
            //return wf1.take(-1).length    
            return SinusoidWaveformSuper.take(-1).length    
        },
        
    }    
var SinusoidWaveformSuper = Java.super(dev)    
add_device(dev, true)
print (wf1.read())
print (wf1.take(-1).length)
print (wf1.getSize())


tscan(wf1, 10, 0.1)





var AnalogOutput = Java.extend(Java.type("ch.psi.pshell.device.RegisterBase"))
var dev = new AnalogOutput("ao1") {	    
	    i:double = 0.0,
        doRead: function () {
        	return this.i
        },
        doWrite: function (value) {
            i=value
        },
    }
add_device(dev, true)


var SinusoidSample = Java.extend(Java.type("ch.psi.pshell.device.ReadonlyRegisterBase"))
var dev = new SinusoidSample("ai1") {	    
	    x:double = 0.0,
        doRead: function () {
            sleep(0.001)
            this.x += 0.1 
            var noise = (Math.random()  - 0.5) / 10.0
            return Math.sin(this.x)  + noise
        },
    }    
add_device(dev, true)


var SinusoidTime = Java.extend(Java.type("ch.psi.pshell.device.ReadonlyRegisterBase"))
var dev = new SinusoidTime("ai2") {	    
        doRead: function () {        
            sleep(0.001)
            var noise = (Math.random()  - 0.5) / 10.0
            return Math.sin(Date.now())  + noise
        },
    }    
add_device(dev, true)


var Random = Java.extend(Java.type("ch.psi.pshell.device.ReadonlyRegisterBase"))
var dev = new Random("ai3") {	    
        doRead: function () {
            sleep(0.001)
            return Math.random() 
        },
    }
    
add_device(dev, true)


print(ao1.read())
print(ai1.read())
print(ai2.read())
print(ai3.read())








