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

class TestDataset(EnvExperiment):
    """Test dataset con Pitaya dummy
    """
    def build(self):
        self.setattr_device("core")
        # global attenuation
        self.setattr_argument("nro_meds", NumberValue(10, min=1, step=1, ndecimals=0))

        self.RP = RedPitaya()
        self.setattr_device("ccb")

    def run(self):
        self.set_dataset("tensiones", np.full(self.nro_meds, 0.), broadcast=True, archive=False)
        self.ccb.issue("create_applet", "datos_medidos",
                        "${artiq_applet}plot_xy "
                        "tensiones")

        self.kernel_run()

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

    @kernel
    def kernel_run(self):
        self.core.reset()
        delay(10*ms)

        for i in range(self.nro_meds):
            self.get_measurement(i)
            delay(1*s)
            self.core.break_realtime()

    def analyze(self):
        datos = self.get_dataset("tensiones")
        print(f"Media: {np.mean(datos)}")
        print(f"Std: {np.std(datos)}")

