ButtonEncoder.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:RaspberryPi-MPD-CharDisplay 作者: bill6300gp 项目源码 文件源码
def __init__(self, type=0, pull='UP', *vartuple):
    # ButtonEncoder(SWtype, pullLevel, PinSW)
    # ButtonEncoder(SWtype, pullLevel, PinSW, PinA, PinB)
    # ** SWtype: 1=Button, 2=Incremental Encoder
    #    [Usage] ButtonEncoder(1, pullLevel, PinSW)
    #            ButtonEncoder(2, pullLevel, [PinSW, PinA, PinB])
    #            ButtonEncoder(2, pullLevel, PinSW, PinA, PinB)
    # ** pull: Internal pull up/down resistor: UP, DOWN >> default: UP
    # ** Pin suggestion : 04(Pin-07), 17(Pin-11), 27(Pin-13), 22(Pin-15), 05(Pin-29), 06(Pin-31), 13(Pin-33), 26(Pin-37),
    #    BCM no.(Pin no.) 23(Pin-16), 24(Pin-18), 25(Pin-22), 12(Pin-32)
    #                     * Pin=0 to skip GPIO setup, but avoid setting PinA=0 and PinB=0 when SWtype is Encoder!
    if type==1 or type==2:
      self.__SWtype=type

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      if self.__SWtype==1 and (len(vartuple)==1 and isinstance(vartuple[0], int)):
        self.__PinSW=vartuple[0]
        if pull.upper().find('UP')>=0:
          GPIO.setup(self.__PinSW, GPIO.IN, pull_up_down=GPIO.PUD_UP)
          self.__Nlevel=True
        if pull.upper().find('DOWN')>=0:
          GPIO.setup(self.__PinSW, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
          self.__Nlevel=False
      elif self.__SWtype==2:
        if len(vartuple)==1 and (isinstance(vartuple[0], list) and len(vartuple[0])==3 and isinstance(vartuple[0][0], int) and isinstance(vartuple[0][1], int) and isinstance(vartuple[0][2], int)):
          self.__PinSW=vartuple[0][0]
          self.__PinA =vartuple[0][1]
          self.__PinB =vartuple[0][2]
        elif len(vartuple)==3 and (isinstance(vartuple[0], int) and isinstance(vartuple[1], int) and isinstance(vartuple[2], int)): 
          self.__PinSW=vartuple[0]
          self.__PinA =vartuple[1]
          self.__PinB =vartuple[2]
        else:
          self.__SWtype=0
          return None

        if pull.upper().find('UP')>=0:
          if self.__PinSW!=0:
            GPIO.setup(self.__PinSW, GPIO.IN, pull_up_down=GPIO.PUD_UP)
          if self.__PinA !=0:
            GPIO.setup(self.__PinA , GPIO.IN, pull_up_down=GPIO.PUD_UP)
          if self.__PinB !=0:
            GPIO.setup(self.__PinB , GPIO.IN, pull_up_down=GPIO.PUD_UP)
          self.__Nlevel=True
        if pull.upper().find('DOWN')>=0:
          if self.__PinSW!=0:
            GPIO.setup(self.__PinSW, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
          if self.__PinA !=0:
            GPIO.setup(self.__PinA , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
          if self.__PinB !=0:
            GPIO.setup(self.__PinB , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
          self.__Nlevel=False
      else:
        self.__SWtype=0
        return None
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号