Module LegoRobot
[frames] | no frames]

Source Code for Module LegoRobot

  1  # LegoRobot.py 
  2   
  3  ''' 
  4   This software is part of the RaspiBrickPi library. 
  5   It is Open Source Free Software, so you may 
  6   - run the code for any purpose 
  7   - study how the code works and adapt it to your needs 
  8   - integrate all or parts of the code in your own programs 
  9   - redistribute copies of the code 
 10   - improve the code and release your improvements to the public 
 11   However the use of the code is entirely your responsibility. 
 12   ''' 
 13   
 14  from Tools import * 
 15  from OLED1306 import OLED1306 
 16  from Beeper import Beeper 
 17  from Button import Button 
 18  import SharedConstants 
 19  import brickpi3 
 20  import sys 
21 22 23 -class LegoRobot():
24 _oled = OLED1306() 25 _beeper = Beeper(SharedConstants.P_BUZZER) 26 _bp = brickpi3.BrickPi3() 27
28 - def __init__(self):
29 LegoRobot._oled.clear() 30 LegoRobot._beeper.start(40, 120, 1, True) 31 self._isBtnHit = False 32 button = Button(SharedConstants.P_BUTTON1) 33 button.addButtonListener(self._onButtonEvent)
34
35 - def exit(self):
36 LegoRobot._oled.clear() 37 LegoRobot._beeper.start(40, 120, 2, True) 38 # Redirect stdout to swallow output from reset_all() 39 from StringIO import StringIO 40 out = sys.stdout 41 sys.stdout = StringIO() 42 LegoRobot._bp.reset_all() 43 sys.stdout = out
44
45 - def getBrickPi(self):
46 ''' 47 Returns the instance of the BrickPi class. Any of its methods may be called. 48 ''' 49 return LegoRobot._bp
50 51 # for compatibility with BrickPi/BrickPi+
52 - def addParts(self):
53 pass
54
55 - def isEscapeHit(self):
56 ''' 57 Checks, if the left button was ever hit or hit since the last invocation. 58 @return: True, if the button was hit; otherwise False 59 ''' 60 self._isBtnHit 61 Tools.delay(1) 62 hit = self._isBtnHit 63 self._isBtnHit = False 64 return hit
65
66 - def _onButtonEvent(self, btn, event):
67 if event == SharedConstants.BUTTON_PRESSED: 68 self._isBtnHit = True
69 70 71 @staticmethod
72 - def setSoundVolume(volume):
73 ''' 74 Sets the sound volume. Value is kept when the program exits. 75 @param volume: the sound volume (0..100) 76 ''' 77 os.system("amixer sset PCM,0 " + str(volume)+ "% >/dev/null")
78 79 @staticmethod
80 - def playTone(frequency, duration):
81 ''' 82 Plays a single sine tone with given frequency and duration. 83 @param frequency: the frequency in Hz 84 @param duration: the duration in ms 85 ''' 86 os.system("speaker-test -t sine -f " + str(frequency) 87 + " >/dev/null & pid=$! ; sleep " + str(duration / 1000.0) + "s ; kill -9 $pid")
88 89 @staticmethod
90 - def getIPAddresses():
91 ''' 92 @return: List of all IP addresses of machine 93 ''' 94 p = Popen(["ifconfig"], stdout = PIPE) 95 ifc_resp = p.communicate() 96 patt = re.compile(r'inet\s*\w*\S*:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') 97 resp = patt.findall(ifc_resp[0]) 98 return resp
99 100 @staticmethod
101 - def initSound(soundFile, volume):
102 ''' 103 Prepares the given wav or mp3 sound file for playing with given volume (0..100). The sound 104 sound channel is opened and a background noise is emitted. 105 @param soundFile: the sound file in the local file system 106 @volume: the sound volume (0..100) 107 @returns: True, if successful; False if the sound system is not available or the sound file 108 cannot be loaded 109 ''' 110 try: 111 pygame.mixer.init() 112 except: 113 # print "Error while initializing sound system" 114 return False 115 try: 116 pygame.mixer.music.load(soundFile) 117 except: 118 pygame.mixer.quit() 119 # print "Error while loading sound file", soundFile 120 return False 121 try: 122 pygame.mixer.music.set_volume(volume / 100.0) 123 except: 124 return False 125 return True
126 127 @staticmethod
128 - def closeSound():
129 ''' 130 Stops any playing sound and closes the sound channel. 131 ''' 132 try: 133 pygame.mixer.stop() 134 pygame.mixer.quit() 135 except: 136 pass
137 138 @staticmethod
139 - def playSound():
140 ''' 141 Starts playing. 142 ''' 143 try: 144 pygame.mixer.music.play() 145 except: 146 pass
147 148 @staticmethod
149 - def fadeoutSound(time):
150 ''' 151 Decreases the volume slowly and stops playing. 152 @param time: the fade out time in ms 153 ''' 154 try: 155 pygame.mixer.music.fadeout(time) 156 except: 157 pass
158 159 @staticmethod
160 - def setSoundVolume(volume):
161 ''' 162 Sets the volume while the sound is playing. 163 @param volume: the sound volume (0..100) 164 ''' 165 try: 166 pygame.mixer.music.set_volume(volume / 100.0) 167 except: 168 pass
169 170 @staticmethod
171 - def stopSound():
172 ''' 173 Stops playing sound. 174 ''' 175 try: 176 pygame.mixer.music.stop() 177 except: 178 pass
179 180 @staticmethod
181 - def pauseSound():
182 ''' 183 Temporarily stops playing at current position. 184 ''' 185 try: 186 pygame.mixer.music.pause() 187 except: 188 pass
189 190 @staticmethod
191 - def resumeSound():
192 ''' 193 Resumes playing from stop position. 194 ''' 195 try: 196 pygame.mixer.music.unpause() 197 except: 198 pass
199 200 @staticmethod
201 - def rewindSound():
202 ''' 203 Resumes playing from the beginning. 204 ''' 205 try: 206 pygame.mixer.music.rewind() 207 except: 208 pass
209 210 @staticmethod
211 - def isSoundPlaying():
212 ''' 213 @return: True, if the sound is playing; otherwise False 214 ''' 215 try: 216 return pygame.mixer.music.get_busy() 217 except: 218 return False
219