1
1
# -*- coding: utf-8 -*-
2
+ """
3
+ lantz.ui.loop
4
+ ~~~~~~~~~~~~~
5
+
6
+ A Loop backend and frontend.
7
+
8
+ :copyright: 2014 by Lantz Authors, see AUTHORS for more details.
9
+ :license: BSD, see LICENSE for more details.
10
+ """
2
11
3
12
import time
4
13
import math
@@ -16,6 +25,24 @@ class StopMode(IntEnum):
16
25
17
26
18
27
class Loop (Backend ):
28
+ """The Loop backend allows you to execute task periodically.
29
+
30
+ Usage:
31
+
32
+ from lantz.ui.blocks import Loop, LoopUi
33
+
34
+ def measure(counter, iterations, overrun):
35
+ print(counter, iterations, overrun)
36
+ data = osci.measure()
37
+ print(data)
38
+
39
+ app = Loop()
40
+
41
+ app.body = measure
42
+
43
+ start_gui_app(app, LoopUi)
44
+
45
+ """
19
46
20
47
#: Signal emitted before starting a new iteration
21
48
#: Parameters: loop counter, iterations, overrun
@@ -25,16 +52,38 @@ class Loop(Backend):
25
52
#: The parameter is used to inform if the loop was canceled.
26
53
loop_done = QtCore .Signal (bool )
27
54
55
+ #: The function to be called. It requires three parameters.
56
+ #: counter - the iteration number
57
+ #: iterations - total number of iterations
58
+ #: overrun - a boolean indicating if the time required for the operation
59
+ #: is longer than the interval.
60
+ #: :type: (int, int, bool) -> None
61
+ body = None
62
+
28
63
def __init__ (self , ** kwargs ):
29
64
super ().__init__ (** kwargs )
30
- self .body = None
31
65
self ._active = False
32
66
self ._internal_func = None
33
67
34
68
def stop (self ):
69
+ """Request the scanning to be stop.
70
+ Will stop when the current iteration is finished.
71
+ """
35
72
self ._active = False
36
73
37
74
def start (self , body , interval = 0 , iterations = 0 , timeout = 0 ):
75
+ """Request the scanning to be started.
76
+
77
+ :param body: function to be called at each iteration.
78
+ If None, the class body will be used.
79
+ :param interval: interval between starts of the iteration.
80
+ If the body takes too long, the iteration will
81
+ be as fast as possible and the overrun flag will be True
82
+ :param iterations: number of iterations
83
+ :param timeout: total time in seconds that the scanning will take.
84
+ If overdue, the scanning will be stopped.
85
+ If 0, there is no timeout.
86
+ """
38
87
self ._active = True
39
88
body = body or self .body
40
89
@@ -48,6 +97,7 @@ def internal(counter, overrun=False, schedule=QtCore.QTimer.singleShot):
48
97
body (counter , iterations , overrun )
49
98
50
99
if iterations and counter + 1 == iterations :
100
+ self ._active = False
51
101
self .loop_done .emit (False )
52
102
return
53
103
elif not self ._active :
@@ -65,12 +115,18 @@ def internal(counter, overrun=False, schedule=QtCore.QTimer.singleShot):
65
115
66
116
67
117
class LoopUi (Frontend ):
118
+ """The Loop frontend provides a GUI for the Rich Backend
119
+ """
68
120
69
121
gui = 'loop.ui'
70
122
71
123
auto_connect = False
72
124
125
+ #: Signal emitted when a start is requested.
126
+ #: The parameters are None, interval, iterations, duration
73
127
request_start = QtCore .Signal (object , object , object , object )
128
+
129
+ #: Signal emitted when a stop is requested.
74
130
request_stop = QtCore .Signal ()
75
131
76
132
def connect_backend (self ):
0 commit comments