1
2
3 '''
4 Class that represents a 7-segment display attached to the I2C port.
5
6
7 This software is part of the raspibrick module.
8 It is Open Source Free Software, so you may
9 - run the code for any purpose
10 - study how the code works and adapt it to your needs
11 - integrate all or parts of the code in your own programs
12 - redistribute copies of the code777
13 - improve the code and release your improvements to the public
14 However the use of the code is entirely your responsibility.
15
16
17 The 7 segments have the following binary values
18 1
19 -
20 32 | |2
21
22 64
23 -
24 16 | |4
25 -
26 8
27
28 The decimal points use value 128 with digit 1, 2 or 3
29 '''
30
31 from Disp4tronix import Disp4tronix
32 from DgTell import DgTell
33 from DgTell1 import DgTell1
34 from RobotInstance import RobotInstance
35 from Tools import *
36
37
39 '''
40 Abstraction of the 4 digit 7-segment display attached to the I2C port.
41 If no display is found, all methods return immediately.
42 '''
43 _myInstance = None
44
46 '''
47 Creates a display instance either from class Display4tronix or DisplayDidel.
48 Because the 4tronix display is multiplexed (one digit shown after
49 the other, a display thread is used to display all 4 digits in a rapid succession.
50 '''
51 self._checkRobot()
52 robot = RobotInstance.getRobot()
53 self.text = ""
54 self.pos = 0
55 self.dp = [0, 0, 0, 0]
56 if robot.displayType == "4tronix":
57 Display._myInstance = Disp4tronix()
58 self.available = True
59 elif robot.displayType == "didel":
60 Display._myInstance = DgTell()
61 self.available = True
62 elif robot.displayType == "didel1":
63 Display._myInstance = DgTell1()
64 self.available = True
65 else:
66 self.available = False
67
68
78
79 - def showText(self, text, pos = 0, dp = [0, 0, 0, 0]):
80 '''
81 Displays 4 characters of the given text. The text is considered to be prefixed and postfixed by spaces
82 and the 4 character window is selected by the text pointer pos that determines the character displayed at the
83 leftmost digit, e.g. (_: empty):
84 showText("AbCdEF") -> AbCd
85 showText("AbCdEF", 1) -> bCdE
86 showText("AbCdEF", -1) ->_AbC
87 showText("AbCdEF", 4) -> EF__
88 Because the 4tronix display is multiplexed (one digit shown after the other),
89 a display thread is started now to display all 4 digits in a rapid succession (if it is not yet started).
90 The parameters are saved and compared to the values at the next invocation. If all are identical, the function returns
91 immediately.
92 @param text: the text to display (list, tuple, string or integer)
93 @param pos: the start value of the text pointer (character index positioned a leftmost digit)
94 @param dp: a list with one to four 1 or 0, if the decimal point is shown or not.
95 The decimal point selection depends on the attached display type. For the 4tronix display: the first element in list
96 corresponds to right dp, second element to center floor dp, the third element to center ceil dp. For the DgTell:
97 the first element in list corresponds to dp at second digit from the right, the second element to dp
98 at third digit from the right, the third element to dp at leftmost digit, the forth element to the dp at
99 rightmost digit. More than 4 elements are ignored
100 @return: True, if successful; False, if the display is not available,
101 text or dp has illegal type or one of the characters can't be displayed
102 '''
103 if not self.available:
104 return
105 if text == self.text and pos == self.pos and cmp(dp, self.dp) == 0:
106 return
107 self.text = text
108 self.pos = pos
109 self.dp = dp
110 self._checkRobot()
111 if type(pos) != int:
112 pos = 0
113 if type(dp) != list:
114 dp = [0, 0, 0, 0]
115 Display._myInstance.showText(text, pos, dp)
116
127
137
139 '''
140 Shows the scrollable text at the start position.
141 @return: 0, if successful; -1, if error
142 '''
143 if not self.available:
144 return
145 self._checkRobot()
146 self.text = ""
147 return Display._myInstance.setToStart()
148
149 - def showTicker(self, text, count = 1, speed = 2, blocking = False):
150 '''
151 Shows a ticker text that scroll to left until the last 4 characters are displayed.
152 @param text: the text to display, if short than 4 characters, scrolling is disabled
153 @param count: the number of repetitions (default: 1). For count = 0, infinite duration,
154 may be stopped by calling stopTicker().
155 @param speed: the speed number of scrolling operations per sec (default: 2)
156 @param blocking: if True, the method blocks until the ticker has finished; otherwise
157 it returns immediately (default: False)
158 '''
159 if not self.available:
160 return
161 self._checkRobot()
162 self.text = ""
163 Display._myInstance.showTicker(text, count, speed, blocking)
164
166 '''
167 Stops a running ticker.
168 The method blocks until the ticker thread is finished and isTickerAlive() returns False.
169 '''
170 if not self.available:
171 return
172 self._checkRobot()
173 Display._myInstance.stopTicker()
174
184
185 - def showBlinker(self, text, dp = [0, 0, 0, 0], count = 3, speed = 1, blocking = False):
186 '''
187 Shows a blinking text for the given number of times and blinking speed.
188 @param text: the text to display, if short than 4 characters, scrolling is disabled
189 @param count: the number of repetitions (default: 3). For count = 0, infinite duration,
190 may be stopped by calling stopBlinker().
191 @param speed: the speed number of blinking operations per sec (default: 1)
192 @param blocking: if True, the method blocks until the blinker has finished; otherwise
193 it returns immediately (default: False)
194 '''
195 if not self.available:
196 return
197 self._checkRobot()
198 self.text = ""
199 Display._myInstance.showBlinker(text, dp, count, speed, blocking)
200
202 '''
203 Stops a running blinker.
204 The method blocks until the blinker thread is finished and isBlinkerAlive() returns False.
205 '''
206 if not self.available:
207 return
208 self._checkRobot()
209 Display._myInstance.stopBlinker()
210
220
222 '''
223 Displays current version. Format X (three horz bars) + n.nn
224 '''
225 v = "X" + DgTell.VERSION.replace(".", "")
226 self.showText(v, pos = 0, dp = [0, 1])
227
229 '''
230 @return: True, if the display is detetectd on the I2C interface; otherwise False
231 '''
232 if not self.available:
233 return False
234 return self.available
235
239