Example #1
0
class ScopeConnection(wx.Panel):
    """This is the view and controller for connecting to the scope"""
    def __init__(self, parent, env):
        """Initialize the panel for setting up the scope"""
        wx.Panel.__init__(self, parent, wx.ID_ANY, style=wx.BORDER_SIMPLE)

        self.env = env
        self.scope = self.env.scope_source

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)

        input_host = wx.BoxSizer(wx.HORIZONTAL)
        target_cpu = wx.BoxSizer(wx.HORIZONTAL)
        scope_cpu = wx.BoxSizer(wx.HORIZONTAL)
        buttons = wx.BoxSizer(wx.HORIZONTAL)

        self.status = wx.StaticText(self, wx.ID_ANY, "")
        font = self.status.GetFont()
        font.SetWeight(wx.BOLD)
        self.status.SetFont(font)

        self.btn_enable = wx.Button(self, wx.ID_ANY, "")
        self.btn_collect = wx.Button(self, wx.ID_ANY, "Collect")

        buttons.Add(self.btn_enable, 1, wx.ALIGN_CENTER)
        buttons.Add(self.btn_collect, 1, wx.ALIGN_CENTER)

        lbl_server = wx.StaticText(self, wx.ID_ANY, "Scope Server")
        self.txt_server = wx.TextCtrl(self, wx.ID_ANY, self.scope.server)
        self.btn_connect = wx.Button(self, wx.ID_ANY, "Connect")
        input_host.Add(lbl_server, 0, wx.ALIGN_CENTER | wx.RIGHT, 5)
        input_host.Add(self.txt_server, 1, wx.EXPAND)
        input_host.Add(self.btn_connect, 0, wx.EXPAND)

        self.sizer.Add(self.status, 0, wx.EXPAND | wx.ALL, 5)
        self.sizer.Add(input_host, 0,
                       wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT, 5)

        self.choice_t_cpu = wx.Choice(self, wx.ID_ANY)
        self._add_field("Target CPU", self.choice_t_cpu, small=True)

        self.choice_s_cpu = wx.Choice(self, wx.ID_ANY)
        self._add_field("Scope CPU", self.choice_s_cpu, small=True)

        self.command = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_RIGHT)
        self._add_field("Target Command", self.command)
        self.command.Bind(wx.EVT_TEXT, self.onchange_capture_settings)

        self.ta = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_RIGHT)
        self._add_field("Target App", self.ta)
        self.ta.Bind(wx.EVT_TEXT, self.onchange_capture_settings)

        self.target_cbuf = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_RIGHT)
        self._add_field("Target Trigger Buffer", self.target_cbuf)
        self.target_cbuf.Bind(wx.EVT_TEXT, self.onchange_capture_settings)

        self.msamples = IntCtrl(self, wx.ID_ANY, style=wx.TE_RIGHT)
        self._add_field("Max # Samples", self.msamples, small=True)
        self.msamples.Bind(wx.EVT_TEXT, self.onchange_capture_settings)

        self.delta = IntCtrl(self, wx.ID_ANY, style=wx.TE_RIGHT)
        self._add_field("Time Step", self.delta, small=True)
        self.delta.Bind(wx.EVT_TEXT, self.onchange_capture_settings)

        self.debug = wx.CheckBox(self, wx.ID_ANY)
        self._add_field("Debug TA Calls", self.debug, small=True)
        self.debug.Bind(wx.EVT_CHECKBOX, self.onchange_capture_settings)

        self.sizer.Add(buttons, 0, wx.ALL | wx.CENTER, 5)

        # Events
        self.btn_enable.Bind(wx.EVT_BUTTON, self.onclick_btn_enable)
        self.btn_connect.Bind(wx.EVT_BUTTON, self.onclick_btn_connect)
        self.choice_t_cpu.Bind(wx.EVT_CHOICE, self.onchoice_t_cpu)
        self.choice_s_cpu.Bind(wx.EVT_CHOICE, self.onchoice_s_cpu)
        self.btn_collect.Bind(wx.EVT_BUTTON, self.onclick_btn_collect)

        pub.subscribe(self.on_enable_changed, ScopeSource.ENABLED_CHANGED)
        pub.subscribe(self.on_connect_changed, ScopeSource.CONNECTED_CHANGED)
        pub.subscribe(self.on_capture_settings_changed,
                      ScopeSource.CAPTURE_SETTINGS_CHANGED)
        pub.subscribe(self.on_readiness_changed,
                      CaptureSource.READINESS_CHANGED)

        # Initial data
        self.draw_status()
        self.draw_capture_settings()
        self.draw_collect_btn()

    def _add_field(self, label, item, small=False):
        """Add a label/element pair to the UI"""
        szr = wx.BoxSizer(wx.HORIZONTAL)
        lbl = wx.StaticText(self, wx.ID_ANY, label)
        szr.Add(lbl, 1 if small else 0, wx.ALIGN_CENTER)
        szr.Add((15, 15), 0, wx.EXPAND)
        szr.Add(item, 0 if small else 1, wx.ALIGN_RIGHT)
        self.sizer.Add(szr, 0, wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT, 5)

    def onclick_btn_connect(self, evt):
        """Connect button has been clicked."""
        server = self.txt_server.GetValue()
        if self.scope.is_connected():
            self.scope.disconnect()
        self.scope.connect(server)

    def onclick_btn_enable(self, evt):
        """Enable button has been clicked."""
        if not self.scope.is_enabled():
            self.scope.enable()
        else:
            self.scope.disable()

    def onclick_btn_collect(self, evt):
        """Collect button has been clicked."""
        if self.env.get_active_dataset_name() is not None:
            self.env.collection_pipeline.run(1)

    def onchoice_t_cpu(self, evt):
        """The target CPU has changed."""
        sel = self.choice_t_cpu.GetSelection()
        if sel != wx.NOT_FOUND:
            self.scope.set_target_core(sel)

    def onchoice_s_cpu(self, evt):
        """The scope CPU has changed."""
        sel = self.choice_s_cpu.GetSelection()
        if sel != wx.NOT_FOUND:
            self.scope.set_scope_core(sel)

    def on_enable_changed(self):
        """The enable state of the scope has changed."""
        self.draw_status()

    def on_connect_changed(self):
        """The connection state of the scope has changed."""
        self.draw_status()
        self.draw_capture_settings()

    def on_capture_settings_changed(self):
        """The capture settings have changed."""
        self.draw_capture_settings()

    def on_readiness_changed(self):
        """The readiness of the scope has changed."""
        self.draw_collect_btn()

    def onchange_capture_settings(self, evt):
        """The user is trying to change the capture settings."""
        cmd = self.command.GetValue()
        ta = self.ta.GetValue()
        cbuf = self.target_cbuf.GetValue()
        msamp = self.msamples.GetValue()
        delta = self.delta.GetValue()
        debug = self.debug.GetValue()

        self.scope.set_capture_params(max_samples=msamp,
                                      time_delta=delta,
                                      ta_command=cmd,
                                      ta_name=ta,
                                      ta_cbuf=cbuf,
                                      debug=debug)

    def draw_status(self):
        """Update the state, buttons, and spinners based on current state"""
        enabled = self.scope.is_enabled()
        connected = self.scope.is_connected()

        if enabled and connected:
            scope_state = "Enabled"
        elif connected and not enabled:
            scope_state = "Connected and Disabled"
        else:
            scope_state = "Disconnected"

        btn_label = "Disable" if enabled else "Enable"
        scope_status = "Scope is %s" % scope_state

        self.status.SetLabel(scope_status)
        self.btn_connect.Enable(not enabled or not connected)
        self.txt_server.Enable(not enabled or not connected)
        self.btn_enable.SetLabel(btn_label)
        self.btn_enable.Enable(connected)

        ncores = self.scope.num_cores
        cores = ["Core %u" % i for i in range(ncores)]

        sel = self.choice_t_cpu.GetSelection()
        if sel == wx.NOT_FOUND:
            sel = self.scope.get_target_core()
        self.choice_t_cpu.SetItems(cores)
        if ncores > 0:
            self.choice_t_cpu.SetSelection(sel)
        self.choice_t_cpu.Enable(connected and not enabled)

        sel = self.choice_s_cpu.GetSelection()
        if sel == wx.NOT_FOUND:
            sel = self.scope.get_scope_core()
        self.choice_s_cpu.SetItems(cores)
        if ncores > 0:
            self.choice_s_cpu.SetSelection(sel)
        self.choice_s_cpu.Enable(connected and not enabled)

    def draw_capture_settings(self):
        """Draw the capture settings."""
        enabled = self.scope.is_enabled()
        connected = self.scope.is_connected()

        self.command.ChangeValue(self.scope.ta_command)
        self.ta.ChangeValue(self.scope.ta_name)
        self.target_cbuf.ChangeValue(self.scope.ta_cbuf)
        self.msamples.ChangeValue(self.scope.max_samples)
        self.delta.ChangeValue(self.scope.time_delta)
        self.debug.SetValue(self.scope.debug)

        self.command.Enable(connected)
        self.ta.Enable(connected)
        self.target_cbuf.Enable(connected)
        self.msamples.Enable(connected)
        self.delta.Enable(connected)
        self.debug.Enable(connected)

    def draw_collect_btn(self):
        """Enable or disable the collection button"""
        self.btn_collect.Enable(self.scope.is_ready())