from artiq.experiment import *
from pyLIAF.artiq.controllers import UrukulCh
import numpy as np

class ControlAllRF(EnvExperiment):
    """Urukul control all RF withe the driver

    Set the frecuencies/amplitudes of every Urukul channel
    """
    def build(self):
        self.setattr_device("core")
        # global attenuation
        self.setattr_argument("attenuation",
                            NumberValue(0*dB, unit='dB', scale=dB, min=0*dB, max=31*dB),
                            )

        RF_ch0 = UrukulCh(self, ch=0, freq=100.0, amp=1)
        RF_ch1 = UrukulCh(self, ch=1, freq=208.0, amp=0.35)
        RF_ch2 = UrukulCh(self, ch=2, freq=110.0, amp=0.3)
        RF_ch3 = UrukulCh(self, ch=3, freq=50.0, amp=1.0)

        self.RF_chs = [RF_ch0, RF_ch1, RF_ch2, RF_ch3]

        #self.set_dataset("Potencias", np.full(0., 10))

    #@rpc
    #def get_measurement(self, i):
    #    val = self.RP.give_measurement()
    #    self.mutate_dataset("Potencias", i, val)

    @kernel
    def run(self):
        self.core.reset()
        delay(1000*ms)
        # initialises CPLD all the selected channels
        for rf_ch in self.RF_chs:
            rf_ch.initialize_channel()
            self.core.break_realtime()

        delay(100*ms)
        
        for rf_ch in self.RF_chs:
            rf_ch.set_channel()
            self.core.break_realtime()

        for rf_ch in self.RF_chs:
            rf_ch.channel.set_att(0.)
            self.core.break_realtime()

        #self.get_measurement(medN)
#        self.core.break_realtime()
    
    
