本文整理汇总了Python中Timer.Timer.addRepeatingTask方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.addRepeatingTask方法的具体用法?Python Timer.addRepeatingTask怎么用?Python Timer.addRepeatingTask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Timer.Timer
的用法示例。
在下文中一共展示了Timer.addRepeatingTask方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dict
# 需要导入模块: from Timer import Timer [as 别名]
# 或者: from Timer.Timer import addRepeatingTask [as 别名]
class MpdGpio:
''' Constants. Don't change! '''
STATE_STARTUP = 0;
STATE_NORMAL = 1;
mpd = None
screen = None
timer = None
status = dict()
observers = []
timer = None
# Constructor
def __init__(self):
# Set state
self.status['state'] = self.STATE_STARTUP
# Setup MPD client
self.mpd = MPDClient()
self.mpd.timeout = 10 # network timeout in seconds (floats allowed), default: None
self.mpd.idletimeout = None
# Setup GPIO
self.GPIO = GPIO
self.GPIO.setwarnings(False)
self.GPIO.setmode(GPIO.BOARD)
# Add button callbacks
GPIO.setup(PIN_PAUZE_PLAY, GPIO.IN)
GPIO.setup(PIN_VOLUME_UP, GPIO.IN)
GPIO.setup(PIN_VOLUME_DOWN, GPIO.IN)
GPIO.add_event_detect(PIN_PAUZE_PLAY, GPIO.RISING, callback=self.pauseOrPlay, bouncetime=BUTTON_BOUNCETIME)
GPIO.add_event_detect(PIN_VOLUME_UP, GPIO.RISING, callback=self.volumeUp, bouncetime=BUTTON_BOUNCETIME)
GPIO.add_event_detect(PIN_VOLUME_DOWN, GPIO.RISING, callback=self.volumeDown, bouncetime=BUTTON_BOUNCETIME)
# Start LCD
self.screen = Screen(self)
self.screen.onStatusChange()
print(self.getIpAddress())
# Start timer
self.timer = Timer()
self.timer.addRepeatingTask(POLLING_INTERVAL, self.updateMpdStatus)
#self.timer.addRepeatingTask(SCREEN_UPDATE_INTERVAL, self.screen.tick)
self.timer.start()
# Getters
''' Get the GPIO interface '''
def getGPIO(self):
return self.GPIO
def getStatus(self):
return self.status
''' Get the current IP address or None if it doesn't have one '''
def getIpAddress(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
return s.getsockname()[0]
except Error as e:
return None
# Methods
''' Add a state observer to the list of observers. The observer had to have an onStatusChange() callback '''
def addObserver(self, observer):
self.observers.append(observer)
''' Notify the observers that the status has changed '''
def notifyObservers(self):
for observer in self.observers:
observer.onStatusChange()
def updateMpdStatus(self):
done = False
while not done:
try:
self.status['mpd'] = self.mpd.status()
self.status['song'] = self.mpd.currentsong()
if self.status['state'] == self.STATE_STARTUP:
self.status['state'] = self.STATE_NORMAL
self.notifyObservers()
done = True
#print(str(self.status))
#print(str(self.song))
except ConnectionError as e:
print(str(e))
self.mpdConnect()
def mpdConnect(self):
self.mpd.connect(MPD_ADDRESS, MPD_PORT)
def pauseOrPlay(self, channel):
if 'state' in self.status['mpd']:
if self.status['mpd']['state'] == 'play':
try:
self.mpd.pause(1)
self.status['mpd']['state'] = 'pause'
print('Pause')
#.........这里部分代码省略.........