from artiq.experiment import *


class MirnyEnv(EnvExperiment):
    def build(self):
        self.setattr_device("core")
        self.cpld = self.get_device("mirny0_cpld")
        self.pll0 = self.get_device("mirny0_ch0")

    @kernel
    def init_mirny(self):
        self.core.reset()
        self.cpld.init()
        self.pll0.init()
        self.pll0.set_frequency(1*GHz)
        self.pll0.set_att(12*dB)
        self.pll0.sw.on()


class PowerControl(MirnyEnv):
    @kernel
    def run(self):
        self.core.reset()
        self.init_mirny()

        # Run other code here
        delay(5*s)
        self.pll0.set_output_power_mu(0)
        print(self.pll0.output_power_mu())


class ToggleSwitch(MirnyEnv):
    @kernel
    def run(self):
        self.core.reset()
        self.init_mirny()

        delay_mu(8)     # Avoid RTIO collision
        self.pll0.sw.off()
        delay(1*s)
        
        while True:
            self.pll0.sw.pulse(100*us)
            delay(900*us)
