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


Python Pin.init方法代码示例

本文整理汇总了Python中pyb.Pin.init方法的典型用法代码示例。如果您正苦于以下问题:Python Pin.init方法的具体用法?Python Pin.init怎么用?Python Pin.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyb.Pin的用法示例。


在下文中一共展示了Pin.init方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: declare_channel

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
 def declare_channel(self, channel, direction):
     try:
         self.available_pins.index(channel)
         if self._find_channel(channel):
             return ;
         pin = Pin(channel)
         if direction:
             pin.init(Pin.OUT_PP)
         else:
             pin.init(Pin.IN, Pin.PULL_UP)
         self.channels += [pin]
     except:
         pass
开发者ID:SolitonNew,项目名称:pyhome,代码行数:15,代码来源:drivers.py

示例2: Pin

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
pin = Pin(pin_map[0], mode=Pin.OPEN_DRAIN, pull=Pin.PULL_UP)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_DOWN)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_NONE)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.MED_POWER)
pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
pin = Pin(pin_map[0], mode=Pin.OUT, drive=pin.LOW_POWER)
pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_DOWN)
pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP)
pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP)
test_pin_af() # try the entire af range on all pins

# test pin init and printing
pin = Pin(pin_map[0])
pin.init(mode=Pin.IN)
print(pin)
pin.init(Pin.IN, Pin.PULL_DOWN)
print(pin)
pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
print(pin)
pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
print(pin)

# test value in OUT mode
pin = Pin(pin_map[0], mode=Pin.OUT)
pin.value(0)
pin.toggle() # test toggle
print(pin())
pin.toggle() # test toggle again
print(pin())
开发者ID:noahchense,项目名称:micropython,代码行数:33,代码来源:pin.py

示例3: Pin

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
from pyb import Pin

p = Pin('X1')
print(p)
print(p.name())
print(p.pin())
print(p.port())

p = Pin('X1', Pin.IN, Pin.PULL_UP)
#p = Pin('X1', Pin.IN, pull=Pin.PULL_UP)
print(p.value())

p.init(p.IN, p.PULL_DOWN)
#p.init(p.IN, pull=p.PULL_DOWN)
print(p.value())

p.init(p.OUT_PP)
p.low()
print(p.value())
p.high()
print(p.value())
p.value(0)
print(p.value())
p.value(1)
print(p.value())
p.value(False)
print(p.value())
p.value(True)
print(p.value())
开发者ID:AruGit,项目名称:micropython,代码行数:31,代码来源:pin.py

示例4: Encoder

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
class Encoder():
    """
    Abstracts a quadrature encoder to give a tick count.
    The count is a signed integer starting at zero. It wraps the count from the
    attached timer to give a seamless and continuous tick count. Overflows of
    the internal timer counter register should be adequatly handled. If
    overflows or weird behaviour occurs around the overflow points, try
    increasing Encoder.HYSTERESIS.
    Note: Only works on pin pairs 'X1' & 'X2', 'X9' & 'X10', or 'Y1', 'Y2'. The
    timer will be automatically selected. Both Timer 2 and 5 work for 'X1' &
    'X2', but Timer 5 is preferred because Timer 2 is used for LED PWM but both
    can be used by changing the values of Encoder.AF_MAP.
    """
    # Constant for decoding in single line mode
    SINGLE_MODE = Timer.ENC_A
    # Constant for decoding in quad mode
    DUAL_MODE = Timer.ENC_AB
    # Maps alternate pin function descriptions to the required timer number
    TIMER_MAP = {'AF1_TIM2': 2, 'AF2_TIM4': 4, 'AF2_TIM5': 5, 'AF3_TIM8': 8}
    # Maps pin names to the alternate function to use
    AF_MAP = {'X1' : 'AF2_TIM5', 'X2': 'AF2_TIM5', 'X9': 'AF2_TIM4',
              'X10': 'AF2_TIM4', 'Y1': 'AF3_TIM8', 'Y2': 'AF3_TIM8'}
    # Defines the pin pairs that must be used
    PIN_PAIRS = [['X1','X9','Y1'],['X2','X10','Y2']]
    # Hysteresis value to overflow detection
    HYSTERESIS = 12 # One full rotation of encoder

    def __init__(self, pinA, pinB, mode = DUAL_MODE):
        """
        Instantiate an Encoder object.
        Initalises the Pins, Timer and TimerChannel for use as a quadrature
        decoder. Registers an overflow callback to elegantly handle counter
        register overflows.
        pinA: Any valid value taken by pyb.Pin constructor
        pinB: Any valid value taken by pyb.Pin constructor
        mode: Mode to use for decoding (Encoder.SINGLE_MODE or
                Encoder.DUAL_MODE)
        raises: Any exception thrown by pyb.Pin, pyb.Timer, pyb.Timer.channel
                or Exception if pins are not compatible pairs
        """
        self._chA = Pin(pinA)
        self._chB = Pin(pinB)
        self._ticks = 0

        # Check pins are compatible
        self._checkPins(pinA, pinB)

        # init pins for alternate encoder function
        af = self.AF_MAP[self._chA.names()[1]]
        channel = self.TIMER_MAP[af]
        af = getattr(Pin, af)
        self._chA.init(Pin.AF_PP, pull = Pin.PULL_NONE, af = af)
        self._chB.init(Pin.AF_PP, pull = Pin.PULL_NONE, af = af)
        # init timer
        self._timer = Timer(channel, prescaler = 0, period = 100000)
        # init encoder mode
        # self._channel = self._timer.channel(1, mode)
        self._timer.channel(1, mode)
        # setup overflow callback
        self._timer.callback(self._overflow)
        # init count register to middle of count
        self._timer.counter(self._timer.period()//2)
        self._lastRead = self._timer.counter()

    def _checkPins(self, pinA, pinB):
        """
        Check that two pins can be used for a decoding and are on the same
        timer.
        """
        try:
            if pinA in self.PIN_PAIRS[0]:
                if self.PIN_PAIRS[0].index(pinA) != self.PIN_PAIRS[1].index(pinB):
                    raise Exception()
            elif pinA in self.PIN_PAIRS[1]:
                if self.PIN_PAIRS[0].index(pinB) != self.PIN_PAIRS[1].index(pinA):
                    raise Exception()
            else:
                raise Exception()
        except:
            raise Exception(pinA + ' & ' + pinB + ' are not on the same Timer')

    def ticks(self, ticks = None):
        """
        Get or set the current tick count.
        Ticks is a signed integer.
        """
        if ticks is not None: # set ticks to desired value
            self._ticks = ticks
        else: # retrieve latest count and update internals
            count = self._timer.counter()
            self._ticks = self._ticks + (count - self._lastRead)
            self._lastRead = count
        return self._ticks

    def _overflow(self, timer):
        """
        Timer overflow callback to gracefully handle overflow events. If
        weird things are occurring, try increasing the HYSTERESIS value.
        """
        count = timer.counter()
#.........这里部分代码省略.........
开发者ID:jradtilbrook,项目名称:DUObot,代码行数:103,代码来源:encoder.py

示例5: Pin

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
from pyb import Pin

p = Pin('X1', Pin.IN)
print(p)
print(p.name())
print(p.pin())
print(p.port())

p = Pin('X1', Pin.IN, Pin.PULL_UP)
p = Pin('X1', Pin.IN, pull=Pin.PULL_UP)
p = Pin('X1', mode=Pin.IN, pull=Pin.PULL_UP)
print(p)
print(p.value())

p.init(p.IN, p.PULL_DOWN)
p.init(p.IN, pull=p.PULL_DOWN)
p.init(mode=p.IN, pull=p.PULL_DOWN)
print(p)
print(p.value())

p.init(p.OUT_PP)
p.low()
print(p.value())
p.high()
print(p.value())
p.value(0)
print(p.value())
p.value(1)
print(p.value())
p.value(False)
print(p.value())
开发者ID:19emtuck,项目名称:micropython,代码行数:33,代码来源:pin.py

示例6: RS485

# 需要导入模块: from pyb import Pin [as 别名]
# 或者: from pyb.Pin import init [as 别名]
class RS485(object):   
    def __init__(self, uart_num, pin_rw, dev_id):
        self.error = []
        self.uart = UART(uart_num)
        self.uart.init(57600, bits=8, parity=0, timeout=10, read_buf_len=64)
        self.pin_rw = Pin(pin_rw)
        self.pin_rw.init(Pin.OUT_PP)
        self.pin_rw.value(0)
        self.dev_id = dev_id
        
        self.file_parts = 0
        self.file_parts_i = 1
        self.file_is_open = False

    def check_lan(self):
        res = []
        uart = self.uart
        try:
            
            buf = uart.readall()            
            if buf:
                buf = buf.decode("utf-8")
                LED(2).toggle()
                for pack in buf.split(chr(0x0)):
                    if pack:
                        try:
                            data = False
                            data = loads(pack)
                            if len(data) > 0 and data[0] == self.dev_id:
                                if data[2][0] == "SET_CONFIG_FILE":
                                    res = [data]
                                    if data[2][2] == False:
                                        self.file_parts = data[2][1]
                                        self.file_parts_i = 1
                                        self._write_config(True, '')
                                        self.file_is_open = True
                                    else:
                                        if self.file_is_open:
                                            if self.file_parts_i == data[2][1]:
                                                self._write_config(False, data[2][2])
                                                if self.file_parts_i == self.file_parts:
                                                    self.file_is_open = False
                                                self.file_parts_i += 1
                                            else:
                                                res = [[self.dev_id, 3]]
                                                self.error += ["Error 3  %s" % (data)]
                                                self.file_is_open = False
                                                break
                                        else:
                                            res = [[self.dev_id, 3]]
                                            self.error += ["Error 4 DATA: %s" % (data)]
                                            break
                                else:
                                    self.file_is_open = False
                                    res = [data]
                        except Exception as e:
                            res = [[self.dev_id, 3]]
                            if data:
                                self.error += ["Error 1 {}".format(e.args) + " DATA:  %s" % (data)]
                            else:
                                self.error += ["Error 1 {}".format(e.args) + " PACK:  %s" % (pack)]
                            LED(4).on()
        except Exception as e:
            res = [[self.dev_id, 3]]
            self.error += ["Error 2 {}".format(e.args)]
            LED(4).on()
        return res

    def send_pack(self, pack_type, pack_data):
        pin_rw = self.pin_rw.value
        uart = self.uart
        pin_rw(1)
        try:
            buf = [self.dev_id, pack_type, pack_data]
            data = dumps(buf).encode("utf-8")
            data += bytearray([0x0])
            uart.write(data)
        except:
            #print("Возникла ошибка при отправке пакета")
            #print(data)
            LED(3).on()
        pin_rw(0)

    def _write_config(self, is_start, data):
        if is_start:
            f = open("config.py", "w")
        else:
            f = open("config.py", "a")
        try:
            f.write(data)
        except:
            pass
        f.close()
开发者ID:SolitonNew,项目名称:pyhome,代码行数:95,代码来源:rs485.py


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