Python pyb.Pin() Examples

The following are 30 code examples of pyb.Pin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyb , or try the search function .
Example #1
Source File: qr.py    From specter-diy with MIT License 7 votes vote down vote up
def __init__(self, path, trigger=None, uart="YA", baudrate=9600):
        super().__init__(path)
        self.button = "Scan QR code"

        if simulator:
            self.EOL = b"\r\n"
        else:
            self.EOL = b"\r"

        self.data = b""
        self.uart_bus = uart
        self.uart = pyb.UART(uart, baudrate, read_buf_len=2048)
        self.trigger = None
        self.is_configured = False
        if trigger is not None or simulator:
            self.trigger = pyb.Pin(trigger, pyb.Pin.OUT)
            self.trigger.on()
            self.is_configured = True
        self.scanning = False 
Example #2
Source File: epd.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def _power_off(self):                       # turn of power and all signals
        self.Pin_PANEL_ON.low()
#        self._SPI_send(b'\x00\x00')
        self.spi.deinit()
        self.Pin_SCK.init(mode = pyb.Pin.OUT_PP)
        self.Pin_SCK.low()
        self.Pin_MOSI.init(mode = pyb.Pin.OUT_PP)
        self.Pin_MOSI.low()
        self.Pin_BORDER.low()
        # ensure SPI MOSI and CLOCK are Low before CS Low
        self.Pin_RESET.low()
        self.Pin_EPD_CS.low()
        # pulse discharge pin
        self.Pin_DISCHARGE.high()
        pyb.delay(150)
        self.Pin_DISCHARGE.low()

# One frame of data is the number of lines * rows. For example:
# The 2.7” frame of data is 176 lines * 264 dots. 
Example #3
Source File: hd44780.py    From micropython-stm-lib with MIT License 6 votes vote down vote up
def __init__(self, width=16, lines=2, pins=None, init=True):
        """Initialize instance and dictionary of output pin objects."""
        # Initialize dict of pin objects
        self.pins = {name: pyb.Pin(pin, PIN_MODE) for name, pin in
                     zip(PIN_NAMES, pins if pins else self._default_pins)}
        # Maximum characters per line
        self.width = width
        # Number of display rows
        self.lines = lines
        # State of display on/off, underscore & blinking cursor control
        self._displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
        # State of text flow direction and auto-scrolling
        self._displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT

        if init:
            self.init() 
Example #4
Source File: epdpart.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def _power_off(self):                       # turn of power and all signals
        self.Pin_RESET.low()
        self.Pin_PANEL_ON.low()
        self.Pin_BORDER.low()
        self.spi.deinit()
        self.Pin_SCK.init(mode = pyb.Pin.OUT_PP)
        self.Pin_SCK.low()
        self.Pin_MOSI.init(mode = pyb.Pin.OUT_PP)
        self.Pin_MOSI.low()
        # ensure SPI MOSI and CLOCK are Low before CS Low
        self.Pin_EPD_CS.low()
        # pulse discharge pin
        self.Pin_DISCHARGE.high()
        pyb.delay(150)
        self.Pin_DISCHARGE.low()

# USER INTERFACE
# clear_screen() calls clear_data() and, if show, EPD_clear()
# showdata() called from show() 
Example #5
Source File: as_rwGPS_time.py    From micropython-async with MIT License 6 votes vote down vote up
def us_setup(tick):
    global uart, gps  # For shutdown
    red = pyb.LED(1)
    blue = pyb.LED(4)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    gps = as_tGPS.GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=us_cb, pps_cb_args=(tick, blue))
    gps.FULL_CHECK = False
    await asyncio.sleep(2)
    await gps.baudrate(BAUDRATE)
    uart.init(BAUDRATE)
    await asyncio.sleep(1)
    await gps.enable(gsa=0, gsv=0)  # Disable satellite data
    await gps.update_interval(UPDATE_INTERVAL)
    pstr = 'Baudrate {} update interval {}ms satellite messages disabled.'
    print(pstr.format(BAUDRATE, UPDATE_INTERVAL)) 
Example #6
Source File: as_rwGPS_time.py    From micropython-async with MIT License 6 votes vote down vote up
def setup():
    global uart, gps  # For shutdown
    red = pyb.LED(1)
    blue = pyb.LED(4)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    gps = as_tGPS.GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=lambda *_: blue.toggle())
    gps.FULL_CHECK = False
    await asyncio.sleep(2)
    await gps.baudrate(BAUDRATE)
    uart.init(BAUDRATE)
    await asyncio.sleep(1)
    await gps.enable(gsa=0, gsv=0)  # Disable satellite data
    await gps.update_interval(UPDATE_INTERVAL)
    pstr = 'Baudrate {} update interval {}ms satellite messages disabled.'
    print(pstr.format(BAUDRATE, UPDATE_INTERVAL))
    return gps

# Test terminator: task sets the passed event after the passed time. 
Example #7
Source File: as_rwGPS_time.py    From micropython-async with MIT License 6 votes vote down vote up
def us_setup(tick):
    global uart, gps  # For shutdown
    red = pyb.LED(1)
    blue = pyb.LED(4)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    gps = GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=us_cb, pps_cb_args=(tick, blue))
    gps.FULL_CHECK = False
    await asyncio.sleep(2)
    await gps.baudrate(BAUDRATE)
    uart.init(BAUDRATE)
    await asyncio.sleep(1)
    await gps.enable(gsa=0, gsv=0)  # Disable satellite data
    await gps.update_interval(UPDATE_INTERVAL)
    pstr = 'Baudrate {} update interval {}ms satellite messages disabled.'
    print(pstr.format(BAUDRATE, UPDATE_INTERVAL)) 
Example #8
Source File: as_rwGPS_time.py    From micropython-async with MIT License 6 votes vote down vote up
def setup():
    global uart, gps  # For shutdown
    red = pyb.LED(1)
    blue = pyb.LED(4)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    gps = GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=lambda *_: blue.toggle())
    gps.FULL_CHECK = False
    await asyncio.sleep(2)
    await gps.baudrate(BAUDRATE)
    uart.init(BAUDRATE)
    await asyncio.sleep(1)
    await gps.enable(gsa=0, gsv=0)  # Disable satellite data
    await gps.update_interval(UPDATE_INTERVAL)
    pstr = 'Baudrate {} update interval {}ms satellite messages disabled.'
    print(pstr.format(BAUDRATE, UPDATE_INTERVAL))
    return gps

# Test terminator: task sets the passed event after the passed time. 
Example #9
Source File: qr.py    From specter-diy with MIT License 6 votes vote down vote up
def init(self):
        if self.is_configured:
            return
        # if failed to configure - probably a different scanner
        # in this case fallback to PIN trigger mode FIXME
        self.clean_uart()
        self.is_configured = self.configure()
        if self.is_configured:
            print("Scanner: automatic mode")
            return

        # Try one more time with different baudrate
        self.uart = pyb.UART(self.uart_bus, 115200, read_buf_len=2048)
        self.clean_uart()
        self.is_configured = self.configure()
        if self.is_configured:
            print("Scanner: automatic mode")
            return

        # PIN trigger mode
        self.uart = pyb.UART(self.uart_bus, 9600, read_buf_len=2048)
        self.trigger = pyb.Pin(QRSCANNER_TRIGGER, pyb.Pin.OUT)
        self.trigger.on()
        self.is_configured = True
        print("Scanner: Pin trigger mode") 
Example #10
Source File: pin_test.py    From upy-examples with MIT License 5 votes vote down vote up
def MyMapper(pin_name):
   if pin_name == "bar":
       return pyb.Pin.cpu.A15   # YELLOW 
Example #11
Source File: pin_test_teensy.py    From upy-examples with MIT License 5 votes vote down vote up
def test():
    test_pin('C5')

    MyMapperDict = {'Foo' : pyb.Pin.board.D13 }   # GREEN 
    pyb.Pin.dict(MyMapperDict)

    test_pin('Foo')

    pyb.Pin.mapper(MyMapper)

    test_pin('bar')
    test_pin('D13') 
Example #12
Source File: Tach.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self, timer_num, channel_num, pin_name, pulses_per_rev=2):
        self.timestamp = [0] * Tachometer.NUM_SAMPLES
        self.num_samples = -1
        self.delta_time = 0
        self.idx = 0
        self.pulses_per_rev = pulses_per_rev
        self.pulse_detected = False

        # Setup the timer to run at 1MHz
        # We assume that we're running on a 32-bit timer).
        if timer_num != 2 and timer_num != 5:
            raise ValueError("Tachometer needs a 32-bit timer")
        self.timer = pyb.Timer(timer_num)
        print("Initializing timer")
        self.timer.init(prescaler=(int(self.timer.source_freq() / 1000000) - 1),
                        period=0x1fffffff)

        self.pin = pyb.Pin(pin_name)

        print("Initializing channel")
        self.channel = self.timer.channel(channel_num, pyb.Timer.IC,
                                          pin=self.pin,
                                          polarity=pyb.Timer.RISING)
        self.channel.callback(self.channel_callback)
        print("self.channel =", self.channel)
        print("Initialization done") 
Example #13
Source File: Tach.py    From upy-examples with MIT License 5 votes vote down vote up
def test():
    """Test program - assumes X2 is jumpered to X1."""
    micropython.alloc_emergency_exception_buf(100)

    print("Starting tach")
    tach = Tachometer(timer_num=2, channel_num=1, pin_name='X1')

    print("Starting pulses")
    t5 = pyb.Timer(5, freq=4)
    oc_pin = pyb.Pin.board.X2
    oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin)

    for freq in range(0, 600, 100):
        if freq == 0:
            freq = 1
        else:
            t5.freq(freq * 4)   # x 2 for toggle, x2 for 2 pulses_per_rev
        pyb.delay(1000)
        print("RPM =",  tach.rpm(), "Freq =", freq, " as RPM =", freq * 60)

    # stop the pulses
    print("Stopping pulses")
    oc_pin.init(pyb.Pin.OUT_PP)
    # wait for 1.5 seconds
    pyb.delay(1500)
    print("RPM =",  tach.rpm())
    print("RPM =",  tach.rpm())

    print("Starting pulses again")
    # start the pulses up again
    oc = t5.channel(2, pyb.Timer.OC_TOGGLE, pin=oc_pin)
    pyb.delay(2000)
    print("RPM =",  tach.rpm())
    print("RPM =",  tach.rpm()) 
Example #14
Source File: pins.py    From upy-examples with MIT License 5 votes vote down vote up
def pins():
    mode_str = { pyb.Pin.IN     : 'IN',
                 pyb.Pin.OUT_PP : 'OUT_PP',
                 pyb.Pin.OUT_OD : 'OUT_OD',
                 pyb.Pin.AF_PP  : 'AF_PP',
                 pyb.Pin.AF_OD  : 'AF_OD',
                 pyb.Pin.ANALOG : 'ANALOG' }
    pull_str = { pyb.Pin.PULL_NONE : '',
                 pyb.Pin.PULL_UP   : 'PULL_UP',
                 pyb.Pin.PULL_DOWN : 'PULL_DOWN' }
    width = [0, 0, 0, 0]
    rows = []
    for pin_entry in pins_af.PINS_AF:
        row = []
        pin_name = pin_entry[0]
        pin = pyb.Pin(pin_name)
        pin_mode = pin.mode()
        row.append(pin_name)
        row.append(mode_str[pin_mode])
        row.append(pull_str[pin.pull()])
        if pin_mode == pyb.Pin.AF_PP or pin_mode == pyb.Pin.AF_OD:
            pin_af = pin.af()
            for af_entry in pin_entry[1:]:
                if pin_af == af_entry[0]:
                    af_str = '%d: %s' % (pin_af, af_entry[1])
                    break
            else:
                af_str = '%d' % pin_af
        else:
            af_str = ''
        row.append(af_str)
        for col in range(len(width)):
            width[col] = max(width[col], len(row[col]))
        rows.append(row)
    for row in rows:
        for col in range(len(width)):
            print('%-*s ' % (width[col], row[col]), end='')
        print('') 
Example #15
Source File: pins.py    From upy-examples with MIT License 5 votes vote down vote up
def pins():
    mode_str = { pyb.Pin.IN     : 'IN',
                 pyb.Pin.OUT_PP : 'OUT_PP',
                 pyb.Pin.OUT_OD : 'OUT_OD',
                 pyb.Pin.AF_PP  : 'AF_PP',
                 pyb.Pin.AF_OD  : 'AF_OD',
                 pyb.Pin.ANALOG : 'ANALOG' }
    pull_str = { pyb.Pin.PULL_NONE : '',
                 pyb.Pin.PULL_UP   : 'PULL_UP',
                 pyb.Pin.PULL_DOWN : 'PULL_DOWN' }
    width = [0, 0, 0, 0]
    rows = []
    for pin_entry in pins_af.PINS_AF:
        row = []
        pin_name = pin_entry[0]
        pin = pyb.Pin(pin_name)
        pin_mode = pin.mode()
        row.append(pin_name)
        row.append(mode_str[pin_mode])
        row.append(pull_str[pin.pull()])
        if pin_mode == pyb.Pin.AF_PP or pin_mode == pyb.Pin.AF_OD:
            pin_af = pin.af()
            for af_entry in pin_entry[1:]:
                if pin_af == af_entry[0]:
                    af_str = '%d: %s' % (pin_af, af_entry[1])
                    break
            else:
                af_str = '%d' % pin_af
        else:
            af_str = ''
        row.append(af_str)
        for col in range(len(width)):
            width[col] = max(width[col], len(row[col]))
        rows.append(row)
    for row in rows:
        for col in range(len(width)):
            print('%-*s ' % (width[col], row[col]), end='')
        print('') 
Example #16
Source File: boot-msc.py    From upy-examples with MIT License 5 votes vote down vote up
def pins():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin))) 
Example #17
Source File: pin_test.py    From upy-examples with MIT License 5 votes vote down vote up
def test_pin(pin_name):
    pin = pyb.Pin(pin_name, pyb.Pin.OUT_PP)
    pin.high()
    pyb.delay(250)
    pin.low()
    pyb.delay(250) 
Example #18
Source File: pin_test_teensy.py    From upy-examples with MIT License 5 votes vote down vote up
def test_pin(pin_name):
    pin = pyb.Pin(pin_name, pyb.Pin.OUT_PP)
    pin.high()
    pyb.delay(250)
    pin.low()
    pyb.delay(250) 
Example #19
Source File: pin_test.py    From upy-examples with MIT License 5 votes vote down vote up
def test():
    test_pin('A13')    # RED

    MyMapperDict = {'Foo' : pyb.Pin.cpu.A14 }   # GREEN 
    pyb.Pin.dict(MyMapperDict)

    test_pin('Foo')

    pyb.Pin.mapper(MyMapper)

    test_pin('bar')
    test_pin('LED_BLUE') 
Example #20
Source File: boot-hid.py    From upy-examples with MIT License 5 votes vote down vote up
def pins():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin))) 
Example #21
Source File: boot-hid.py    From upy-examples with MIT License 5 votes vote down vote up
def af():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin.af_list()))) 
Example #22
Source File: boot-cdc-only.py    From upy-examples with MIT License 5 votes vote down vote up
def af():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin.af_list()))) 
Example #23
Source File: boot.py    From upy-examples with MIT License 5 votes vote down vote up
def pins():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin))) 
Example #24
Source File: boot.py    From upy-examples with MIT License 5 votes vote down vote up
def af():
    for pin_name in dir(pyb.Pin.board):
        pin = pyb.Pin(pin_name)
        print('{:10s} {:s}'.format(pin_name, str(pin.af_list()))) 
Example #25
Source File: rc.py    From flight_controller with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, index):
    timer = timers[rc_pins_timers[index]]
    self.pin = Pin(rc_pins[index])
    self.channel = timer.channel(rc_pins_channels[index],
                                 Timer.IC,
                                 pin=self.pin,
                                 polarity=Timer.BOTH)
    self.channel.callback(self.callback) 
Example #26
Source File: esc.py    From flight_controller with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, index):
    self.timer = Timer(esc_pins_timers[index], prescaler=83, period=19999)
    self.channel = self.timer.channel(esc_pins_channels[index],
                                      Timer.PWM,
                                      pin=Pin(esc_pins[index]))
    self.trim = esc_trim[index] 
Example #27
Source File: ultrasonic.py    From MicroPython-Examples with MIT License 5 votes vote down vote up
def __init__(self, tPin, ePin):
        self.triggerPin = tPin
        self.echoPin = ePin

        # Init trigger pin (out)
        self.trigger = pyb.Pin(self.triggerPin)
        self.trigger.init(pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
        self.trigger.low()

        # Init echo pin (in)
        self.echo = pyb.Pin(self.echoPin)
        self.echo.init(pyb.Pin.IN, pyb.Pin.PULL_NONE) 
Example #28
Source File: as_GPS_time.py    From micropython-async with MIT License 5 votes vote down vote up
def us_setup(tick):
    red = pyb.LED(1)
    yellow = pyb.LED(3)
    uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
    sreader = asyncio.StreamReader(uart)
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    return GPS_Timer(sreader, pps_pin, local_offset=1,
                     fix_cb=lambda *_: red.toggle(),
                     pps_cb=us_cb, pps_cb_args=(tick, yellow)) 
Example #29
Source File: rtc_time.py    From micropython-async with MIT License 5 votes vote down vote up
def low_power_pins():
            pins = [
                # user IO pins
                'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15',
                'B0', 'B1', 'B3', 'B4', 'B5', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12', 'B13',
                'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6',
                'D0', 'D3', 'D8', 'D9',
                'E0', 'E1', 'E12', 'E14', 'E15',
                'F1', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F13', 'F14', 'F15',
                'H2', 'H3', 'H5', 'H6', 'H7', 'H8',
                'I0', 'I1',

                # internal pins
                'D1', 'D14', 'D15',
                'F0', 'F12',
                'G0', 'G1', 'G2', 'G3', 'G4', 'G5', #'G6',
                'H4', 'H9', 'H10', 'H11', 'H12', 'H13', 'H14', 'H15',
                'I2', 'I3',
            ]
            pins_led = ['F3', 'F4', 'F5',]
            pins_sdmmc = ['D6', 'D7', 'G9', 'G10', 'G11', 'G12']
            pins_wlan = ['D2', 'D4', 'I7', 'I8', 'I9', 'I11']
            pins_bt = ['D5', 'D10', 'E3', 'E4', 'E5', 'E6', 'G8', 'G13', 'G14', 'G15', 'I4', 'I5', 'I6', 'I10']
            pins_qspi1 = ['B2', 'B6', 'D11', 'D12', 'D13', 'E2']
            pins_qspi2 = ['E7', 'E8', 'E9', 'E10', 'E11', 'E13']
            if disable_pins:
                for p in pins:
                    pyb.Pin(p, pyb.Pin.IN, pyb.Pin.PULL_DOWN)
            if disable_3v3:
                pyb.Pin('EN_3V3', pyb.Pin.IN, None)
            if disable_leds:
                for p in pins_led:
                    pyb.Pin(p, pyb.Pin.IN, pyb.Pin.PULL_UP) 
Example #30
Source File: touch_bytecode.py    From micropython-tft-gui with MIT License 5 votes vote down vote up
def __init__(self, controller = "XPT2046", asyn = False, *, confidence = 5, margin = 50, delay = 10, calibration = None):
        if PCB_VERSION == 1:
            self.pin_clock = pyb.Pin("Y8", pyb.Pin.OUT_PP)
            self.pin_clock.value(0)
            self.pin_d_out = pyb.Pin("Y7", pyb.Pin.OUT_PP)
            self.pin_d_in  = pyb.Pin("Y6", pyb.Pin.IN)
            self.pin_irq   = pyb.Pin("Y5", pyb.Pin.IN)
        else:
            self.pin_clock = pyb.Pin("X11", pyb.Pin.OUT_PP)
            self.pin_clock.value(0)
            self.pin_d_out = pyb.Pin("X12", pyb.Pin.OUT_PP)
            self.pin_d_in  = pyb.Pin("Y1", pyb.Pin.IN)
            self.pin_irq   = pyb.Pin("Y2", pyb.Pin.IN)
# set default values
        self.ready = False
        self.touched = False
        self.x = 0
        self.y = 0
        self.buf_length = 0
        cal = TOUCH.DEFAULT_CAL if calibration is None else calibration
        self.asynchronous = False
        self.touch_parameter(confidence, margin, delay, cal)
        if asyn:
            self.asynchronous = True
            asyncio.create_task(self._main_thread())

# set parameters for get_touch()
# res: Resolution in bits of the returned values, default = 10
# confidence: confidence level - number of consecutive touches with a margin smaller than the given level
#       which the function will sample until it accepts it as a valid touch
# margin: Difference from mean centre at which touches are considered at the same position 
# delay: Delay between samples in ms.
#