當前位置: 首頁>>代碼示例>>Python>>正文


Python Timer.freq方法代碼示例

本文整理匯總了Python中pyb.Timer.freq方法的典型用法代碼示例。如果您正苦於以下問題:Python Timer.freq方法的具體用法?Python Timer.freq怎麽用?Python Timer.freq使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyb.Timer的用法示例。


在下文中一共展示了Timer.freq方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: PWM

# 需要導入模塊: from pyb import Timer [as 別名]
# 或者: from pyb.Timer import freq [as 別名]
class PWM(object):
  """docstring for PWMM"""

  #Dict pin name: timer #, channel #.
  PinChannels = {
    'X1': [(2,1), (5,1)],
    'X2': [(2,2), (5,2)],
    'X3': [(2,3), (5,3), (9,1)],
    'X4': [(2,4), (5,4), (9,2)],
    'X6': [(2,1), (8,1)],
    'X7': [(13, 1)],
    'X8': [(1,1), (8,1), (14,1)],
    'X9': [(4,1)],
    'X10': [(4,2)],
    'Y1': [(8,1)],
    'Y2': [(8,2)],
    'Y3': [(4,3), (10,1)],
    'Y4': [(4,4), (11,1)],
    'Y6': [(1,1)],
    'Y7': [(1,2), (8,2), (12,1)],
    'Y8': [(1,3), (8,3), (12,2)],
    'Y9': [(2,3)],
    'Y10': [(2,4)],
    'Y11': [(1,2), (8,2)],
    'Y12': [(1,3), (8,3)]
  }

  class PWMException(Exception):
      def __init__( self, msg ) :
          self.msg = msg

  @staticmethod
  def timerandchannel( pinname, timernum ) :
    try:
      a = PWM.PinChannels[pinname]
      if timernum <= 0:
        return a[0]
      else:
        for v in a:
          if v[0] == timernum:
            return v
    except Exception as e :
      raise PWM.PWMException("Pin {} cannot be used for PWM".format(pinname))

    raise PWM.PWMException("Pin {} cannot use timer {}".format(pinname, timernum))

  def __init__( self, p, timernum, afreq = 100 ) :
    isname = type(p) == str
    pinname = p if isname else p.name()
    timernum, channel = PWM.timerandchannel(pinname, timernum)
    if isname:
      p = Pin(pinname)

    self._timer = Timer(timernum, freq = afreq)
    self._channel = self._timer.channel(channel, Timer.PWM, pin = p)

  @property
  def pulse_width( self ) : return self._channel.pulse_width()

  @pulse_width.setter
  def pulse_width( self, value ) : self._channel.pulse_width(value)

  @property
  def pulse_width_percent( self ) : return self._channel.pulse_width_percent()

  @pulse_width_percent.setter
  def pulse_width_percent( self, value ) : self._channel.pulse_width_percent(value)

  @property
  def freq( self ) : return self._timer.freq()

  @freq.setter
  def freq( self, value ) : self._timer.freq(value)

  def callback( self, value ) :
    self._channel.callback(value)
開發者ID:rolandvs,項目名稱:GuyCarverMicroPythonCode,代碼行數:78,代碼來源:PWM.py


注:本文中的pyb.Timer.freq方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。