def __init__(self, device, command, qt_widget, initial_state='undefined'): """ :param device: :type device: str :param command: :type command: str :param qt_widget: :type qt_widget: :param initial_state: :type initial_state: str """ super(ILI3DSPlugin_QSM, self).__init__() self._widget = qt_widget # self._id = generate_id(device, command) # self._qsm = QStateMachine() # self._states = {} # self._add_states() self._add_transitions() # # -- Initial State self._qsm.setInitialState(self._get_state(initial_state)) # - Launch QSM self._qsm.start()
def __init__(self, touchpad, parent=None): QStateMachine.__init__(self, parent) self.touchpad = touchpad self._touchpad_wrapper = TouchpadQtWrapper(self.touchpad, self) # setup monitoring objects self._monitors = {"mouses": MouseDevicesManager(self), "keyboard": create_keyboard_monitor(self)} self._enabled_monitors = set() # setup the states: self.states = {} for name, touchpad_off in self._STATE_NAMES.iteritems(): state = QState(self) state.setObjectName(name) state.assignProperty(self._touchpad_wrapper, "off", touchpad_off) self.states[name] = state # setup the initial state self.setInitialState(self.states["on"]) # setup the transitions self.transitions = defaultdict(list) # mouse management transitions for state in ("on", "temporarily_off"): self._add_transition(state, "off", self._monitors["mouses"].firstMousePlugged) self._add_transition("off", "on", self._monitors["mouses"].lastMouseUnplugged) # keyboard management transitions self._add_transition("on", "temporarily_off", self._monitors["keyboard"].typingStarted) self._add_transition("temporarily_off", "on", self._monitors["keyboard"].typingStopped) # start monitors self.initialState().entered.connect(self._start_stop_monitors) # stop monitors if the state machine is stopped self.stopped.connect(self._stop_all_monitors)
def __init__(self, touchpad, parent=None): QStateMachine.__init__(self, parent) self.touchpad = touchpad self._touchpad_wrapper = TouchpadQtWrapper(self.touchpad, self) # setup monitoring objects self._monitors = {'mouses': MouseDevicesManager(self), 'keyboard': create_keyboard_monitor(self)} self._enabled_monitors = set() # setup the states: self.states = {} for name, touchpad_off in self._STATE_NAMES.iteritems(): state = QState(self) state.setObjectName(name) state.assignProperty(self._touchpad_wrapper, 'off', touchpad_off) self.states[name] = state # setup the initial state self.setInitialState(self.states['on']) # setup the transitions self.transitions = defaultdict(list) # mouse management transitions for state in ('on', 'temporarily_off'): self._add_transition( state, 'off', self._monitors['mouses'].firstMousePlugged) self._add_transition( 'off', 'on', self._monitors['mouses'].lastMouseUnplugged) # keyboard management transitions self._add_transition('on', 'temporarily_off', self._monitors['keyboard'].typingStarted) self._add_transition('temporarily_off', 'on', self._monitors['keyboard'].typingStopped) # start monitors self.initialState().entered.connect(self._start_stop_monitors) # stop monitors if the state machine is stopped self.stopped.connect(self._stop_all_monitors)
def __init__(self, argv): # init the widget QApplication.__init__(self, argv) # set up the state machine self._machine = QStateMachine() # Start with a top-level serial state self._exp_state = Serial( new_timeline=True) #QState(QState.ExclusiveStates) self._current_parent = self._exp_state # Set as initial state of state machine self._machine.addState(self._exp_state) self._machine.setInitialState(self._exp_state) # set up timer self._last_time = now() self._new_time = now()
def __init__(self,argv): # init the widget QApplication.__init__(self,argv) # set up the state machine self._machine = QStateMachine() # Start with a top-level serial state self._exp_state = Serial(new_timeline=True) #QState(QState.ExclusiveStates) self._current_parent = self._exp_state # Set as initial state of state machine self._machine.addState(self._exp_state) self._machine.setInitialState(self._exp_state) # set up timer self._last_time = now() self._new_time = now()
class Experiment(QApplication): def __init__(self,argv): # init the widget QApplication.__init__(self,argv) # set up the state machine self._machine = QStateMachine() # Start with a top-level serial state self._exp_state = Serial(new_timeline=True) #QState(QState.ExclusiveStates) self._current_parent = self._exp_state # Set as initial state of state machine self._machine.addState(self._exp_state) self._machine.setInitialState(self._exp_state) # set up timer self._last_time = now() self._new_time = now() def _add_transition_if_needed(self, state): # default to stored parent parent = state.parentState() if parent == 0: parent = self._current_parent children = parent.children() # if serial if parent.childMode() == QState.ExclusiveStates: # if no previous states, must set as initial state if len(children) > 1: # add transition to previons prev = children[-2] prev.addTransition(prev.finished, state) elif len(children) == 1 and parent.initialState() is None: # set the new state as the initial parent.setInitialState(state) def get_event_time(self): """ Return the time range (time, range) in which we are sure the latest set of events occurred. """ return (self._last_time,self._new_time-self._last_time) def run(self): """ Run the experiment made up of the State Machine. """ # make sure toplevel _exp_state has initial state if necessary if len(self._exp_state.children())>0 and \ self._exp_state.initialState() is None: self._exp_state.setInitialState(exp._exp_state.children()[0]) # have it quit when done exp._add_transition_if_needed(QuitState(parent=self._exp_state)) # add the final state exp._add_transition_if_needed(QFinalState(parent=self._exp_state)) # start the state machine self._machine.start() # enter event loop self._stopped = False self.wait_until() #self.exec_() def wait_until(self, end_time = None): """ Wait until the desired time, or infinitely if no end is specified. """ if end_time is None: # no end time, so just loop while not self._stopped: self._new_time = now() self.processEvents() self._last_time = self._new_time QThread.usleep(100) else: while self._new_time < end_time and not self._stopped: self._new_time = now() self.processEvents() self._last_time = self._new_time QThread.usleep(100) def stop(self): self._stopped = True
class ILI3DSPlugin_QSM(QObject): """ """ def __init__(self, device, command, qt_widget, initial_state='undefined'): """ :param device: :type device: str :param command: :type command: str :param qt_widget: :type qt_widget: :param initial_state: :type initial_state: str """ super(ILI3DSPlugin_QSM, self).__init__() self._widget = qt_widget # self._id = generate_id(device, command) # self._qsm = QStateMachine() # self._states = {} # self._add_states() self._add_transitions() # # -- Initial State self._qsm.setInitialState(self._get_state(initial_state)) # - Launch QSM self._qsm.start() @property def _iter_strs_from_signals(self): """ :return: """ list_str_signals_transitions = map( lambda s: 'self.' + s, map( itemgetter(0), filter(lambda t: 'qsm_signals_' in t[0], LI3DSPlugin_QSM.__dict__.iteritems()))) for str_signal_transition in list_str_signals_transitions: state_start_name, state_end_name = str_signal_transition.split( 'self._qsm_signals_')[1].split('_to_') yield str_signal_transition, state_start_name, state_end_name def _add_states(self): """ :return: """ states_names = set() for _, state_start_name, state_end_name in self._iter_strs_from_signals: states_names.add(state_start_name) states_names.add(state_end_name) for state_name in states_names: self._add_state(state_name) def _add_transitions(self): """ """ # Add transitions from static declaration of list of signals used by QSM transitions. # it's a bit tricky but i can't see a simple solution to do this ... for str_signal_transition, state_start_name, state_end_name in self._iter_strs_from_signals: state_start, state_end = self._get_states( (state_start_name, state_end_name)) state_start.addTransition(eval(str_signal_transition), state_end) def _get_states(self, *args): """ :param args: :return: >>> qsm = LI3DSPlugin_QSM('arduino', 'flash', None) >>> map(type, qsm._get_states(('undefined', 'on', 'off'))) [<class 'PyQt4.QtCore.QState'>, <class 'PyQt4.QtCore.QState'>, <class 'PyQt4.QtCore.QState'>] """ return map(self._get_state, *args) def _get_transition(self, state_start_name, state_end_name): """ :param state_start_name: :type state_end_name: str :param state_end_name: :type state_end_name: str :return: :rtype: pyqtSignal # >>> qsm = LI3DSPlugin_QSM('arduino', 'flash', None) # >>> type(qsm._get_transition('undefined', 'off')) # <type 'PyQt4.QtCore.pyqtSignal'> """ return self.__class__.__dict__.get('_qsm_signals_%s_to_%s' % (state_start_name, state_end_name)) def _action_widget_textEdit(self, state_name): """ :return: """ try: widget_textEdit = self._widget.get( generate_id('textEdit', self._id)) self._get_state(state_name).assignProperty(widget_textEdit, 'text', state_name) except AttributeError as e: print_err('Unexpected error: %s - error: %s', sys.exc_info()[0], e) def _add_state(self, state_name, state_action=_action_widget_textEdit): """ :param state_name: :param state_action: :return: """ id_state = self._generate_id_for_state(state_name) state = self._states[id_state] = QState() state.setObjectName(id_state) # state_action(self, state_name) # Add State to Machine self._qsm.addState(state) def _get_state(self, state_name): """ :param state_name: :return: """ return self._states[self._generate_id_for_state(state_name)] def _generate_id_for_state(self, state_name): """ :param state_name: :return: >>> qsm = LI3DSPlugin_QSM('arduino', 'flash', None) >>> qsm._generate_id_for_state('undefined') 'arduino_flash_undefined' """ return generate_id(self._id, state_name)
class Experiment(QApplication): def __init__(self, argv): # init the widget QApplication.__init__(self, argv) # set up the state machine self._machine = QStateMachine() # Start with a top-level serial state self._exp_state = Serial( new_timeline=True) #QState(QState.ExclusiveStates) self._current_parent = self._exp_state # Set as initial state of state machine self._machine.addState(self._exp_state) self._machine.setInitialState(self._exp_state) # set up timer self._last_time = now() self._new_time = now() def _add_transition_if_needed(self, state): # default to stored parent parent = state.parentState() if parent == 0: parent = self._current_parent children = parent.children() # if serial if parent.childMode() == QState.ExclusiveStates: # if no previous states, must set as initial state if len(children) > 1: # add transition to previons prev = children[-2] prev.addTransition(prev.finished, state) elif len(children) == 1 and parent.initialState() is None: # set the new state as the initial parent.setInitialState(state) def get_event_time(self): """ Return the time range (time, range) in which we are sure the latest set of events occurred. """ return (self._last_time, self._new_time - self._last_time) def run(self): """ Run the experiment made up of the State Machine. """ # make sure toplevel _exp_state has initial state if necessary if len(self._exp_state.children())>0 and \ self._exp_state.initialState() is None: self._exp_state.setInitialState(exp._exp_state.children()[0]) # have it quit when done exp._add_transition_if_needed(QuitState(parent=self._exp_state)) # add the final state exp._add_transition_if_needed(QFinalState(parent=self._exp_state)) # start the state machine self._machine.start() # enter event loop self._stopped = False self.wait_until() #self.exec_() def wait_until(self, end_time=None): """ Wait until the desired time, or infinitely if no end is specified. """ if end_time is None: # no end time, so just loop while not self._stopped: self._new_time = now() self.processEvents() self._last_time = self._new_time QThread.usleep(100) else: while self._new_time < end_time and not self._stopped: self._new_time = now() self.processEvents() self._last_time = self._new_time QThread.usleep(100) def stop(self): self._stopped = True