本文整理汇总了Python中machine.Pin.irq方法的典型用法代码示例。如果您正苦于以下问题:Python Pin.irq方法的具体用法?Python Pin.irq怎么用?Python Pin.irq使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类machine.Pin
的用法示例。
在下文中一共展示了Pin.irq方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
class Button:
def __init__(self, pin):
from machine import Pin
self.pin = Pin(pin, Pin.IN)
def get_presses(self, delay = 1):
last_time, last_state, presses = time.time(), 0, 0
while time.time() < last_time + delay:
time.sleep_ms(50)
if last_state == 0 and self.pin.value() == 1:
last_state = 1
if last_state == 1 and self.pin.value() == 0:
last_state, presses = 0, presses + 1
return presses
def is_pressed(self):
return self.pin.value() == 0
def was_pressed(self):
last_state = self.pin.value()
time.sleep_ms(15)
if last_state == 1 and self.pin.value() == 0:
return True
return False
def irq(self, handler, trigger):
self.pin.irq(handler = handler, trigger = trigger)
示例2: activate
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
def activate(state):
global inPin
if(state):
inPin = Pin(12, Pin.IN, Pin.PULL_UP)
inPin.irq(trigger=Pin.IRQ_RISING, handler=timeUS)
# Disables the internal pull-up, therefore preventing any further RISING_EDGE case.
else:
inPin = Pin(12, Pin.IN)
示例3: __init__
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
class PulseCounter:
def __init__(self, pin, value=0): # value is in cents
self.pin = Pin(pin, mode=Pin.IN)
self.value = value
self.count = 0
self.total = 0
self.int = self.pin.irq(handler=self._count, trigger=Pin.IRQ_FALLING, priority=7)
def _count(self, pin):
self.count += 1
self.total += self.value
示例4: __init__
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
class PulseCounter:
def __init__(self, pin, pull, trigger, debounce_ms):
self._pin = Pin(pin, mode=Pin.IN, pull=pull)
self._debounce_ms = debounce_ms
self._last_count_ms = time.ticks_ms()
self._irq = self._pin.irq(trigger=trigger, handler=self._handler)
self.counter = 0
def _handler(self, pin):
time_ms = time.ticks_ms()
if (time_ms - self._last_count_ms > self._debounce_ms):
self.counter += 1
self._last_count_ms = time_ms
示例5: Trigger_Monitor
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
class Trigger_Monitor(object):
'''
Is there a way to change the callback?
'''
def __init__(self):
# OUTPUT/INPUT
self.pins = ['GP16', 'GP13']
self.outputPin = Pin(self.pins[0], mode=Pin.OUT, value=1)
self.inputPin = Pin(self.pins[1], mode=Pin.IN, pull=Pin.PULL_UP)
self.triggerCount = 0
self._triggerType_ = Pin.IRQ_RISING
self.inputIRQ = self.inputPin.irq(trigger=self._triggerType_, handler=self.pinHandler)
self.irqState = True
def toggleInput(self, time_ms = 5):
self.outputPin.toggle()
time.sleep_ms(time_ms)
def pinHandler(self, pin_o):
# global self.pin_irq_count_trigger
# global self.pin_irq_count_total
# self._triggerType_
# if self._triggerType_ & self.inputIRQ.flags():
# self.pin_irq_count_trigger += 1
self.triggerCount += 1
def getTriggerCount(self):
print("Trigger Count: ", self.triggerCount)
def resetTriggerCount(self):
self.triggerCount = 0
def disableIRQ(self):
self.irqState = machine.disable_irq() # Start of critical section
def reEnableIRQ(self):
machine.enable_irq(True)
self.irqState=True
示例6: callback
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
from machine import Pin
def callback(p):
mypin.value(mypin.value() ^ 1)
btn = Pin(5, Pin.IN, Pin.PULL_UP)
mypin = Pin(4, Pin.OUT)
btn.irq(trigger=Pin.IRQ_FALLING, handler=callback)
while 1:
pass
示例7: print
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
print("debounced", p, b[0])
return
b[0] = c.sock.send(quieter)
print("change pin", p, b[0])
def callback_play_pause(p):
if b[0]:
print("debounced", p, b[0])
return
b[0] = c.sock.send(play_pause)
print("change pin", p, b[0])
p0 = Pin(0, Pin.IN, Pin.PULL_UP)
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
p13 = Pin(13, Pin.IN, Pin.PULL_UP)
p0.irq(trigger=Pin.IRQ_RISING, handler=callback_louder)
p2.irq(trigger=Pin.IRQ_RISING, handler=callback_quieter)
p13.irq(trigger=Pin.IRQ_RISING, handler=callback_play_pause)
def wrap(text,lim):
lines = []
pos = 0
line = []
for word in text.split():
if pos + len(word) < lim + 1:
line.append(word)
pos+= len(word) + 1
else:
lines.append(' '.join(line))
line = [word]
pos = len(word)
示例8: Pin
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
from machine import Pin
import time
led = Pin("GP16", Pin.OUT)
led.toggle()
def log(msg):
print(msg)
def btnPressed(pin):
led.toggle()
time.sleep_us(100)
btn = Pin("GP17", Pin.IN, Pin.PULL_UP)
btn.irq(trigger=Pin.IRQ_FALLING, handler=btnPressed)
def home():
global wifi
if wifi.mode() != WLAN.STA:
wifi.mode(WLAN.STA)
wifi.connect(config.HOME_SSID, auth=(WLAN.WPA, config.HOME_PASSWORD))
def tc():
global t, p
import theta
t = theta.Theta()
p = t.connect()
if not p:
print("Connect failed!")
else:
示例9: Pin
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
pin0 = Pin(pins[0], mode=Pin.OUT, value=1)
pin1 = Pin(pins[1], mode=Pin.IN, pull=Pin.PULL_UP)
def pin_handler (pin_o):
global pin_irq_count_trigger
global pin_irq_count_total
global _trigger
if _trigger & pin1_irq.flags():
pin_irq_count_trigger += 1
pin_irq_count_total += 1
pin_irq_count_trigger = 0
pin_irq_count_total = 0
_trigger = Pin.IRQ_FALLING
pin1_irq = pin1.irq(trigger=_trigger, handler=pin_handler)
for i in range (0, 10):
pin0.toggle()
time.sleep_ms(5)
print(pin_irq_count_trigger == 5)
print(pin_irq_count_total == 5)
pin_irq_count_trigger = 0
pin_irq_count_total = 0
_trigger = Pin.IRQ_RISING
pin1_irq = pin1.irq(trigger=_trigger, handler=pin_handler)
for i in range (0, 200):
pin0.toggle()
time.sleep_ms(5)
print(pin_irq_count_trigger == 100)
print(pin_irq_count_total == 100)
示例10: button_handler
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
# Complete project details at https://RandomNerdTutorials.com
from machine import Pin
import machine, neopixel, time
# define interrupt handling functions
def button_handler(pin):
global button_pressed
button_pressed = pin
# configure pushbuttons as interrupts
button1 = Pin(15, Pin.IN)
button1.irq(trigger=Pin.IRQ_RISING, handler=button_handler)
button2 = Pin(14, Pin.IN)
button2.irq(trigger=Pin.IRQ_RISING, handler=button_handler)
button3 = Pin(12, Pin.IN)
button3.irq(trigger=Pin.IRQ_RISING, handler=button_handler)
button4 = Pin(13, Pin.IN)
button4.irq(trigger=Pin.IRQ_RISING, handler=button_handler)
button_pressed = button1
# LED strip configuration
# number of pixels
n = 48
# strip control gpio
p = 5
np = neopixel.NeoPixel(machine.Pin(p), n)
# FUNCTIONS FOR LIGHTING EFFECTS
# bounce
示例11: EPD
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
class EPD(object):
MAX_READ = 45
SW_NORMAL_PROCESSING = 0x9000
EP_FRAMEBUFFER_SLOT_OVERRUN = 0x6a84 # too much data fed in
EP_SW_INVALID_LE = 0x6c00 # Wrong expected length
EP_SW_INSTRUCTION_NOT_SUPPORTED = 0x6d00 # bad instr
EP_SW_WRONG_PARAMETERS_P1P2 = 0x6a00
EP_SW_WRONG_LENGTH = 0x6700
DEFAULT_SLOT=0 # always the *oldest*, should wear-level then I think
def __init__(self, debug=False, baud=100000):
# From datasheet
# Bit rate – up to 12 MHz1
# ▪ Polarity – CPOL = 1; clock transition high-to-low on the leading edge and low-to-high on the
# trailing edge
# ▪ Phase – CPHA = 1; setup on the leading edge and sample on the trailing edge
# ▪ Bit order – MSB first
# ▪ Chip select polarity – active low
self.spi = SPI(0)
try:
self.spi.init(mode=SPI.MASTER, baudrate=baud, bits=8,
polarity=1, phase=1, firstbit=SPI.MSB,
pins=('GP31', 'GP16', 'GP30')) # CLK, MOSI, MISO
except AttributeError:
self.spi.init(baudrate=baud, bits=8,
polarity=1, phase=1, firstbit=SPI.MSB,
pins=('GP31', 'GP16', 'GP30')) # CLK, MOSI, MISO
# These are all active low!
self.tc_en_bar = Pin('GP4', mode=Pin.OUT)
self.disable()
self.tc_busy_bar = Pin('GP5', mode=Pin.IN)
self.tc_busy_bar.irq(trigger=Pin.IRQ_RISING) # Wake up when it changes
self.tc_cs_bar = Pin('GP17', mode=Pin.ALT, alt=7)
self.debug = debug
def enable(self):
self.tc_en_bar.value(0) # Power up
time.sleep_ms(5)
while self.tc_busy_bar() == 0:
machine.idle() # will it wake up here?
# /tc_busy goes high during startup, low during init, then high when not busy
def disable(self):
self.tc_en_bar.value(1) # Off
def send_command(self, ins, p1, p2, data=None, expected=None):
# These command variables are always sent
cmd = struct.pack('3B', ins, p1, p2)
# Looks like data is only sent with the length (Lc)
if data:
assert len(data) <= 251 # Thus speaks the datasheet
cmd += struct.pack('B', len(data))
cmd += data
# Expected data is either not present at all, 0 for null-terminated, or a number for fixed
if expected is not None:
cmd += struct.pack('B', expected)
if self.debug:
print("Sending: " + hexlify(cmd).decode())
self.spi.write(cmd)
# Wait for a little while
time.sleep_us(15) # This should take at most 14.5us
while self.tc_busy_bar() == 0:
machine.idle()
# Request a response
if expected is not None:
if expected > 0:
result_bytes = self.spi.read(2 + expected)
else:
result_bytes = self.spi.read(EPD.MAX_READ)
strlen = result_bytes.find(b'\x00')
result_bytes = result_bytes[:strlen] + result_bytes[strlen+1:strlen+3]
else:
result_bytes = self.spi.read(2)
if self.debug:
print("Received: " + hexlify(result_bytes).decode())
(result,) = struct.unpack_from('>H', result_bytes[-2:])
if result != EPD.SW_NORMAL_PROCESSING:
raise ValueError("Bad result code: 0x%x" % result)
return result_bytes[:-2]
@staticmethod
def calculate_checksum(data, skip=16):
"""
Initial checksum value is 0x6363
#.........这里部分代码省略.........
示例12: callback_louder
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
def callback_louder(p):
b[0] = s.send(louder)
print("change pin", p, b[0])
def callback_quieter(p):
b[0] = s.send(quieter)
print("change pin", p, b[0])
b = bytearray(2)
quieter = umqtt.mtpPublish('sonos/ct', '{"action":"quieter"}')
louder = umqtt.mtpPublish('sonos/ct', '{"action":"louder"}')
s = socket.socket()
p0 = Pin(0, Pin.IN, Pin.PULL_UP)
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
p0.irq(trigger=Pin.IRQ_RISING, handler=callback_louder)
p2.irq(trigger=Pin.IRQ_RISING, handler=callback_quieter)
def run():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(ssid, pw)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
#s = socket.socket()
s.connect((host, 1883))
s.send(umqtt.mtpConnect("somename"))
示例13: handle_interrupt
# 需要导入模块: from machine import Pin [as 别名]
# 或者: from machine.Pin import irq [as 别名]
# Complete project details at https://RandomNerdTutorials.com
from machine import Pin
from time import sleep
motion = False
def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
led = Pin(12, Pin.OUT)
pir = Pin(14, Pin.IN)
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True:
if motion:
print('Motion detected! Interrupt caused by:', interrupt_pin)
led.value(1)
sleep(20)
led.value(0)
print('Motion stopped!')
motion = False