本文整理汇总了Python中driver.Driver.addListener方法的典型用法代码示例。如果您正苦于以下问题:Python Driver.addListener方法的具体用法?Python Driver.addListener怎么用?Python Driver.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类driver.Driver
的用法示例。
在下文中一共展示了Driver.addListener方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from driver import Driver [as 别名]
# 或者: from driver.Driver import addListener [as 别名]
class ElevatorWrapper:
topFloor = 3
botFloor = 0
def __init__(self):
self.elevator = Driver()
self.direction = 0
self.lastFloor = self.getFloor() if self.getFloor() != None else -1
self.destination = 0
self.externalFloorListener = None
self.stop = self.elevator.stop
self.addListener = self.elevator.addListener
for i, floor in enumerate(INPUT.SENSORS):
self.elevator.addListener(floor, partial(self.floorListener, i))
self.addListener(INPUT.STOP, self.stopstruction)
self.addListener(INPUT.OBSTRUCTION, self.stopstruction)
def moveToFloor(self, floor):
if self.elevator.readChannel(INPUT.OBSTRUCTION):
print "Obstruction"
return
if floor > self.topFloor:
print "Too much floor: %i > %i , WAT" % (floor, self.topFloor)
return
print "Moving to floor", floor
if floor > self.lastFloor:
print "Going up"
self.elevator.move(OUTPUT.MOTOR_UP)
self.direction = 1
elif floor < self.lastFloor:
print "Going down"
self.elevator.move(OUTPUT.MOTOR_DOWN)
self.direction = 0
else:
print "Open doors"
self.destination = floor
def floorListener(self, floor, en, to): # TODO: en, to
print "At floor", floor
if floor == self.destination and self.floorListener == None:
print "At destination"
self.elevator.stop()
elif floor >= self.topFloor and self.direction == 1:
print "Stop it! (top)", floor, self.topFloor, self.direction
self.elevator.stop()
elif floor <= self.botFloor and self.direction == 0:
print "Stop it! (bot)", floor, self.botFloor, self.direction
self.elevator.stop()
"""if self.lastFloor < floor: self.direction = 1
elif self.lastFloor > floor: self.direction = 0
else: print "derp" """
self.lastFloor = floor;
print type(floor)
self.elevator.setFloorIndicator(floor)
self.externalFloorListener(floor)
def buttonListener(self, where, what, floor, en, to): #TODO en, to
print where, what, floor
if where == "in":
self.moveToFloor(floor)
elif where == "out":
if what == "up":
self.moveToFloor(floor)
elif what == "down":
self.moveToFloor(floor)
else:
raise ValueError("Invalid \"where\"")
def stopstruction(self, en, to): # TODO: en, to
self.elevator.stop()
if self.lastFloor % 1.0 != 0.5 and self.getFloor() == None: self.lastFloor = self.lastFloor + (0.5 if self.direction else -0.5)
print "Stopped, now at", self.lastFloor
def isObstructed(self):
return self.elevator.readChannel(INPUT.OBSTRUCTION)
def down(self):
self.elevator.move(OUTPUT.MOTOR_DOWN)
sleep(2.0)
self.elevator.stop()
def getFloor(self):
return self.elevator.getCurrentFloor()
def addButtonListener(self, listener):
self.addListener(INPUT.FLOOR_UP1, partial(listener, "out", "up", 0))
self.addListener(INPUT.FLOOR_UP2, partial(listener, "out", "up", 1))
self.addListener(INPUT.FLOOR_UP3, partial(listener, "out", "up", 2))
self.addListener(INPUT.FLOOR_DOWN2, partial(listener, "out", "down", 1))
#.........这里部分代码省略.........