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

Source Code for Module raspibrick

  1  # raspibrick.py 
  2   
  3  ''' 
  4   This software is part of the raspibrick module. 
  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  import SharedConstants 
 15  from RobotInstance import RobotInstance 
 16  from Robot import Robot 
 17  from Robot import MyRobot 
 18  from Motor import Motor 
 19  from ServoMotor import ServoMotor 
 20  from Gear import Gear 
 21  from Led import Led 
 22  from InfraredSensor import InfraredSensor 
 23  from UltrasonicSensor import UltrasonicSensor 
 24  from LightSensor import LightSensor 
 25  from RobotContext import RobotContext 
 26  from Beeper import Beeper 
 27  from soundplayer import SoundPlayer 
 28  from OLED1306 import OLED1306 
 29  from Obstacle import Obstacle 
 30  from Target import Target 
 31  from Torch import Torch 
 32  from Shadow import Shadow 
 33  from Display import Display 
 34  from DgTell import DgTell 
 35  from DgTell1 import DgTell1 
 36  from Disp4tronix import Disp4tronix 
 37  from tcpcom import * 
 38  from Camera import Camera 
 39  from PCF8591 import ADC 
 40  from Tools import * 
 41  import tty 
 42  import termios 
 43  import sys 
 44   
 45  # globals 
 46  MOTOR_LEFT = SharedConstants.MOTOR_LEFT 
 47  MOTOR_RIGHT = SharedConstants.MOTOR_RIGHT 
 48  IR_CENTER = SharedConstants.IR_CENTER 
 49  IR_LEFT = SharedConstants.IR_LEFT 
 50  IR_RIGHT = SharedConstants.IR_RIGHT 
 51  IR_LINE_LEFT = SharedConstants.IR_LINE_LEFT 
 52  IR_LINE_RIGHT = SharedConstants.IR_LINE_RIGHT 
 53  LED_FRONT = SharedConstants.LED_FRONT 
 54  LED_LEFT = SharedConstants.LED_LEFT 
 55  LED_REAR = SharedConstants.LED_REAR 
 56  LED_RIGHT = SharedConstants.LED_RIGHT 
 57  LS_FRONT_LEFT = SharedConstants.LS_FRONT_LEFT 
 58  LS_FRONT_RIGHT = SharedConstants.LS_FRONT_RIGHT 
 59  LS_REAR_LEFT = SharedConstants.LS_REAR_LEFT 
 60  LS_REAR_RIGHT = SharedConstants.LS_REAR_RIGHT 
 61  BUTTON_LONGPRESSED = SharedConstants.BUTTON_LONGPRESSED 
 62  BUTTON_PRESSED = SharedConstants.BUTTON_PRESSED 
 63  BUTTON_RELEASED = SharedConstants.BUTTON_RELEASED 
 64  BUTTON_CLICKED = SharedConstants.BUTTON_CLICKED 
 65  BUTTON_DOUBLECLICKED = SharedConstants.BUTTON_DOUBLECLICKED 
 66   
 67   
68 -def getKey():
69 ''' 70 Waits for a key stroke. 71 A key stroke may generate 1, 2, 3 or 4 characters, 72 e.g. cursor up: x,y,z with ord(x) = 27, ord(y) = 91, ord(z) = 65 73 @return: one character (string) of key sequence 74 ''' 75 fd = sys.stdin.fileno() 76 old_settings = termios.tcgetattr(fd) 77 try: 78 tty.setraw(sys.stdin.fileno()) 79 ch = sys.stdin.read(1) 80 finally: 81 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 82 if ord(ch) == 0x03: # Ctrl-C 83 raise KeyboardInterrupt 84 return ch
85
86 -def readKey():
87 ''' 88 Single character input. Read cursor keys. 89 @return: one character (string) for normal keys, special value for cursor keys: 90 # 16:Up, 17:Down, 18:Right, 19:Left 91 ''' 92 ch1 = getKey() 93 if ord(ch1) == 126: # ~ used for special key, ignore 94 return readKey() 95 if ord(ch1) != 0x1B: # not special key 96 return ch1 97 ch2 = getKey() 98 if ord(ch2) != 0x5B: # not [ 99 return ch1 100 ch3 = getKey() 101 if ord(ch3) != 0x31: 102 print "third", ord(ch3) 103 return chr(0x10 + ord(ch3) - 65)
104 105
106 -def isButtonHit():
107 robot = RobotInstance.getRobot() 108 if robot != None: 109 return robot.isButtonHit() 110 return False
111
112 -def isEnterHit():
113 robot = RobotInstance.getRobot() 114 if robot != None: 115 return robot.isEnterHit() 116 return False
117
118 -def isEscapeHit():
119 robot = RobotInstance.getRobot() 120 if robot != None: 121 return robot.isEscapeHit() 122 return False
123
124 -def isUpHit():
125 robot = RobotInstance.getRobot() 126 if robot != None: 127 return robot.isUpHit() 128 return False
129
130 -def isDownHit():
131 robot = RobotInstance.getRobot() 132 if robot != None: 133 return robot.isDownHit() 134 return False
135
136 -def isLeftHit():
137 robot = RobotInstance.getRobot() 138 if robot != None: 139 return robot.isLeftHit() 140 return False
141
142 -def isRightHit():
143 robot = RobotInstance.getRobot() 144 if robot != None: 145 return robot.isRightHit() 146 return False
147