from artiq.experiment import *

# EXTRACTED LOOSELY FROM: https://github.com/cnourshargh/Bham-ARTIQ-examples/blob/master/Urukul_SingleOutput.py
class Urukul_Frequency_Pulse(EnvExperiment):
    """Urukul Single Frequency Pulse:

    Initial Urukul test:
        Outputs a single sine wave of specified frecuency and duration.
    """
    def build(self):
        # sets core device drivers as attributes
        self.setattr_device("core")

        # sets urukul0, channel 0 device drivers as attributes
        # this name is the same as seen in the device_db.py
        self.setattr_device("urukul0_ch0")
        # This is not the 'Urukul' phy, but actually is the one corresponding to
        # the inside DDS chip, which has the methods described in:
        # https://m-labs.hk/artiq/manual-legacy/core_drivers_reference.html#artiq.coredevice.ad9910.AD9910
        # BEWARE the manual-legacy thing.
		# self.freq = NumberValue(21, unit='MHz', scale=1e6) 
        self.setattr_argument("freq", NumberValue(21, unit='MHz', scale=1e6))

    @kernel
    def run(self):
        self.core.reset()
        # initialises CPLD on channel 0
        self.urukul0_ch0.cpld.init()
        self.urukul0_ch0.init()

        ### Variable declaration:

        # Defines frequency variable, range: 1 MHz - 400MHz
        # defines amplitude variable as an amplitude scale factor(0 to 1)
        amp = 1.0
        # defines attenuation variable from 0 (no attenuation) to max val (check) -31,5dB
        attenuation= 3.0*dB
        duration = 10 * (1/self.freq)

        delay(10 * ms)

        # writes attenuation to urukul channel
        self.urukul0_ch0.set_att(attenuation)
        #self.urukul0_ch0.set_phase(0.0)

        # writes frequency and amplitude variables to urukul channel thus outputting function
        self.urukul0_ch0.set(self.freq, amplitude = amp)

        # We can access this switch thanks to the entry in this constructor method:
        # ...
        # sw_device – Name of the RF switch device. The RF switch is a TTLOut channel available as the sw attribute of this instance.
        # ...
        self.urukul0_ch0.sw.on()
        delay(duration)
        delay(20*s)
        self.urukul0_ch0.sw.off()
        delay(duration)

