当前位置: 首页>>代码示例>>Python>>正文


Python Timer.addRepeatingTask方法代码示例

本文整理汇总了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')
#.........这里部分代码省略.........
开发者ID:denniss17,项目名称:rPi-mpd-gpio,代码行数:103,代码来源:MpdGpio.py


注:本文中的Timer.Timer.addRepeatingTask方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。