Module Display
[hide private]
[frames] | no frames]

Source Code for Module Display

  1  # Display.py 
  2  # Remote mode 
  3  ''' 
  4  The 7 segments have the following binary values 
  5             1 
  6             - 
  7       32 |     |2 
  8   
  9            64 
 10             - 
 11       16 |     |4 
 12             - 
 13             8 
 14   
 15  The decimal points use value 128 with digit 1, 2 or 3 
 16  ''' 
 17   
 18  from RobotInstance import RobotInstance 
 19   
 20   
 21  # ------------------------   Class Display  ------------------------------------------- 
22 -class Display():
23 ''' 24 Abstraction of the 4 digit 7-segment display attached to the I2C port. 25 If no display is found, all methods return immediately. 26 '''
27 - def __init__(self):
28 ''' 29 Creates a display instance either from class Display4tronix or DisplayDidel. 30 Because the 4tronix display is multiplexed (one digit shown after 31 the other, a display thread is used to display all 4 digits in a rapid succession. 32 ''' 33 self.device = "display" 34 robot = RobotInstance.getRobot() 35 if robot == None: # deferred registering, because Robot not yet created 36 RobotInstance._partsToRegister.append(self) 37 else: 38 self._setup(robot)
39
40 - def _setup(self, robot):
41 robot.sendCommand(self.device + ".create") 42 self.robot = robot
43
44 - def clear(self):
45 ''' 46 Turns all digits off. Stops a running display thread. 47 ''' 48 self._checkRobot() 49 self.robot.sendCommand(self.device + ".clear")
50
51 - def showText(self, text, pos = 0, dp = [0, 0, 0, 0]):
52 ''' 53 Displays 4 characters of the given text. The text is considered to be prefixed and postfixed by spaces 54 and the 4 character window is selected by the text pointer pos that determines the character displayed at the 55 leftmost digit, e.g. (_: empty): 56 showText("AbCdEF") -> AbCd 57 showText("AbCdEF", 1) -> bCdE 58 showText("AbCdEF", -1) ->_AbC 59 showText("AbCdEF", 4) -> EF__ 60 To display a character and its decimal point, use the dp parameter [most right dp,...,most left dp] and 61 set the decimal point values to 1. Because the 4tronix display is multiplexed (one digit shown after 62 the other, a display thread is started now to display all 4 digits in a rapid succession 63 (if it is not yet started). 64 @param text: the text to display (list, tuple, string or integer) 65 @param pos: the start value of the text pointer (character index positioned a leftmost digit) 66 @param dp: a list with four 1 or 0, if the decimal point is shown or not 67 @return: True, if successful; False, if the display is not available, 68 text or dp has illegal type or one of the characters can't be displayed 69 ''' 70 self._checkRobot() 71 if not (type(text) == int or type(text) == list or type(text) == str): 72 return 73 if type(text) == int: 74 text = str(text) 75 d = [0] * 4 76 for i in range(min(4, len(dp))): 77 d[i] = dp[i] 78 79 self.robot.sendCommand(self.device + ".showText." + text + "." + str(pos) + "." + \ 80 str(d[0]) + ", " + str(d[1]) + ", " + str(d[2]) + ", " + str(d[3]))
81
82 - def scrollToLeft(self):
83 ''' 84 Scrolls the scrollable text one step to the left. 85 @return: the number of characters remaining at the right 86 ''' 87 self._checkRobot() 88 return int(self.robot.sendCommand(self.device + ".scrollToLeft"))
89
90 - def scrollToRight(self):
91 ''' 92 Scrolls the scrollable text one step to the left. 93 @return: the number of characters remaining at the left 94 ''' 95 self._checkRobot() 96 return int(self.robot.sendCommand(self.device + ".scrollToRight"))
97
98 - def setToStart(self):
99 ''' 100 Shows the scrollable text at the start position. 101 ''' 102 self._checkRobot() 103 self.robot.sendCommand(self.device + ".setToStart")
104
105 - def showTicker(self, text, count = 1, speed = 2, blocking = False):
106 ''' 107 Shows a ticker text that scroll to left until the last 4 characters are displayed. 108 @param text: the text to display, if short than 4 characters, scrolling is disabled 109 @param count: the number of repetitions (default: 1). For count == 0, infinite duration, 110 stopped by calling stopTicker() 111 @param speed: the speed number of scrolling operations per sec (default: 2) 112 @param blocking: if True, the method blocks until the ticker has finished; otherwise 113 it returns immediately (default: False) 114 ''' 115 self._checkRobot() 116 if blocking: 117 param = "1" 118 else: 119 param = "0" 120 self.robot.sendCommand(self.device + ".showTicker." + text + "." + \ 121 str(count) + "." + str(speed) + "." + param)
122
123 - def stopTicker(self):
124 ''' 125 Stops a running ticker. 126 The method blocks until the ticker thread is finished and isTickerAlive() returns False. 127 ''' 128 self._checkRobot() 129 self.robot.sendCommand(self.device + ".stopTicker")
130
131 - def isTickerAlive(self):
132 ''' 133 @return: True, if the ticker is displaying; otherwise False 134 ''' 135 self._checkRobot() 136 rc = self.robot.sendCommand(self.device + ".isTickerAlive") 137 if rc == "0": 138 return False 139 return True
140
141 - def showBlinker(self, text, dp = [0, 0, 0, 0], count = 3, speed = 1, blocking = False):
142 ''' 143 Shows a blinking text for the given number of times and blinking speed. 144 @param text: the text to display, if short than 4 characters, scrolling is disabled 145 @param count: the number of repetitions (default: 3). For count = 0, infinite duration, 146 may be stopped by calling stopBlinker(). 147 @param speed: the speed number of blinking operations per sec (default: 1) 148 @param blocking: if True, the method blocks until the blinker has finished; otherwise 149 it returns immediately (default: False) 150 ''' 151 self._checkRobot() 152 d = [0] * 4 153 for i in range(min(4, len(dp))): 154 d[i] = dp[i] 155 if blocking: 156 param = "1" 157 else: 158 param = "0" 159 self.robot.sendCommand(self.device + ".showBlinker." + text + "." + \ 160 str(d[0]) + ", " + str(d[1]) + ", " + str(d[2]) + ", " + str(d[3]) + "." + \ 161 str(count) + "." + str(speed) + "." + param)
162
163 - def stopBlinker(self):
164 ''' 165 Stops a running blinker. 166 The method blocks until the blinker thread is finished and isBlinkerAlive() returns False. 167 ''' 168 self._checkRobot() 169 self.robot.sendCommand(self.device + ".stopBlinker")
170
171 - def isBlinkerAlive(self):
172 ''' 173 @return: True, if the blinker is displaying; otherwise False 174 ''' 175 self._checkRobot() 176 rc = self.robot.sendCommand(self.device + ".isBlinkerAlive") 177 if rc == "0": 178 return False 179 return True
180
181 - def isAvailable(self):
182 ''' 183 @return: True, if the display is detetectd on the I2C interface; otherwise False 184 ''' 185 self._checkRobot() 186 rc = self.robot.sendCommand(self.device + ".isAvailable") 187 if rc == "0": 188 return False 189 return True
190
191 - def _checkRobot(self):
192 if RobotInstance.getRobot() == None: 193 raise Exception("Create Robot instance first")
194