def set_energy(v):
    motor.move(v)  
energy_setpoint = motor 
energy = motor.getReadback()
set_device_alias(energy, "energy")


START, STOP, STEP = 0, 40, 2
ENERGIES = [0.0, 0.5, 0.25]
sensors = (sin,out, energy)



#def before_pass(pass_num):
#    set_energy(ENERGIES[pass_num-1])   
#ret= lscan(inp, sensors, START, STOP, STEP, 0.2, passes = len(ENERGIES), before_pass = before_pass, latency = 0.5)


#positions = frange(START, STOP, STEP , True)
#vector = [[pos, en]  for en in ENERGIES for pos in positions]
#ret = vscan((inp,energy_setpoint), sensors, vector, line = True, latency = 0.5)


"""
plots = plot([None]*len(sensors), name = [d.name for d in sensors])
for p in plots: p.clear()
def AfterReadout(record, scan):
    if record.setpoints[1] == scan.getStart()[1]:
        for p in plots: p.addSeries(LinePlotSeries(str(record.positions[0])))
    for i in range(len(plots)):
        plots[i].getSeries(plots[i].numberOfSeries-1).appendData(record.positions[1], record.values[i])    

class EnergyIndex("Writable):
    def write(self, value):  
        if not hasattr(self, 'setpoint') or self.setpoint != value:
            self.setpoint = value              
            set_energy(ENERGIES[int(value)])   
        
positions = frange(START, STOP, STEP , True)
ret = ascan([EnergyIndex(), inp], sensors, (0,START), (len(ENERGIES)-1,STOP), (1.0,STEP), latency = 0.1, after_read=AfterReadout, plot_disabled=True)
"""

plots = plot([None]*len(sensors), name = [d.name for d in sensors])
for p in plots: 
    p.clear()
    p.legendVisible = True  
def after_readout(record, scan):
    for i in range(len(plots)):
        plots[i].getSeries(plots[i].numberOfSeries-1).appendData(record.positions[0], record.values[i])    
def before_pass(pass_num):
    en = ENERGIES[pass_num-1]
    set_energy(en)  
    for p in plots: p.addSeries(LinePlotSeries(str(en))) 
    
ret= lscan(inp, sensors, START, STOP, STEP, latency = 0.2, passes = len(ENERGIES), before_pass = before_pass, after_read=after_readout, plot_disabled=True)
