def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10, reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False): super().__init__(min_val, max_val, reverse, range_mode, half_step) if pull_up == True: self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP) self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP) else: self._pin_clk = Pin(pin_num_clk, Pin.IN) self._pin_dt = Pin(pin_num_dt, Pin.IN) self._pin_clk_irq = ExtInt(pin_num_clk, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._process_rotary_pins) self._pin_dt_irq = ExtInt(pin_num_dt, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._process_rotary_pins) # turn on 3.3V output to power the rotary encoder (pyboard D only) if 'PYBD' in os.uname().machine: Pin('EN_3V3').value(1)
def init(timer_id=2, nc_pin='Y3', gnd_pin='Y4', vcc_pin='Y1', data_pin='Y2'): global nc global gnd global vcc global data global micros global timer # Leave the pin unconnected if nc_pin is not None: nc = Pin(nc_pin) nc.init(Pin.OUT_OD) nc.high() # Make the pin work as GND if gnd_pin is not None: gnd = Pin(gnd_pin) gnd.init(Pin.OUT_PP) gnd.low() # Make the pin work as power supply if vcc_pin is not None: vcc = Pin(vcc_pin) vcc.init(Pin.OUT_PP) vcc.high() # Configure the pid for data communication data = Pin(data_pin) # Save the ID of the timer we are going to use timer = timer_id # setup the 1uS timer micros = pyb.Timer(timer, prescaler=83, period=0x3fffffff) # 1MHz ~ 1uS # Prepare interrupt handler ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, None) ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, edge)
class Encoder: """This class control WSR-08834 encoder. One of the pyboard pin is used to count pulses (via an interrupt)""" def __init__(self, robot, int_pin): self._robot = robot #reference to robot an encoder belongs into (is used to stop its motors) self._int_pin = int_pin #pyboard pin used to count encoder pulses self._cnt_pulses = 0 #counts pulses from encoder self._stop_pulses = 0 #keeps destination number of pulses to stop motor when reached self._last_pulses = 0 #number of counted pulses during last check self._ext_int = ExtInt( int_pin, ExtInt.IRQ_RISING, Pin.PULL_NONE, self.__int_handler) #setup pin to trigger interrupt def __int_handler(self, line): #encoder interrupt handler self._cnt_pulses += 1 if self._cnt_pulses >= self._stop_pulses: #when destination number of pulses reached stop robot's motor self._ext_int.disable() self._robot.stop() def set_stop_pulses( self, stop_pulses): #setup destination number of pulses for the move self._cnt_pulses = 0 self._last_pulses = 0 self._stop_pulses = stop_pulses self._ext_int.enable() def get_stop_pulses(self): return self._stop_pulses def get_current_pulses(self): return self._cnt_pulses def force_stop_cnt(self): # change counters so movement end is forced to be reported by is_stopped method self._cnt_pulses = self._stop_pulses def reset(self): self._cnt_pulses = 0 self._stop_pulses = 0 self._last_pulses = 0 def is_stopped( self ): #check if movement ended i.e. if number of desired pulses reached if self._cnt_pulses >= self._stop_pulses: return True else: return False def is_moving(self): #check if uPyBot moving return not self.is_stopped() def is_changing(self): #check if number of pulses changed since last check changing = False if self._cnt_pulses != self._last_pulses: changing = True self._last_pulses = self._cnt_pulses return changing
def __init__(self, robot, int_pin): self._robot = robot #reference to robot an encoder belongs into (is used to stop its motors) self._int_pin = int_pin #pyboard pin used to count encoder pulses self._cnt_pulses = 0 #counts pulses from encoder self._stop_pulses = 0 #keeps destination number of pulses to stop motor when reached self._last_pulses = 0 #number of counted pulses during last check self._ext_int = ExtInt( int_pin, ExtInt.IRQ_RISING, Pin.PULL_NONE, self.__int_handler) #setup pin to trigger interrupt
def __init__(self, X=True) : if X : self.pwmpin = Pin('X6', Pin.OUT_PP) self.pwmtim = Timer(2, freq=5000) self.pwm = self.pwmtim.channel(1, mode=Timer.PWM, pin=self.pwmpin) self.pwm.pulse_width(0) self.dir = Pin('X2', Pin.OUT_PP) self.dir.off() # O = forward, 1 = reverse self.sleep = Pin('X3', Pin.OUT_PP) self.sleep.value(0) # 0 = sleep, 1 = active self.enca = Pin('X4', Pin.IN, Pin.PULL_UP) self.encb = Pin('X5', Pin.IN, Pin.PULL_UP) else : self.pwmpin = Pin('Y1', Pin.OUT_PP) self.pwmtim = Timer(8, freq=5000) self.pwm = self.pwmtim.channel(1, mode=Timer.PWM, pin=self.pwmpin) self.pwm.pulse_width(0) self.dir = Pin('Y2', Pin.OUT_PP) self.dir.off() # O = forward, 1 = reverse self.sleep = Pin('Y3', Pin.OUT_PP) self.sleep.value(0) # 0 = sleep, 1 = active self.enca = Pin('Y4', Pin.IN, Pin.PULL_UP) self.encb = Pin('Y5', Pin.IN, Pin.PULL_UP) self.pwmscale = (self.pwmtim.period() + 1) // 10000 # scale factot for permyriad(10000 as it allows to get more distinct points and avoid divisions) power self.count_a = 0 # counter for impulses on the A output of the encoder self.target_a = 0 # target value for the A counter (for controlled rotation) self.count_b = 0 # counter for impulses on the B output of the encoder self.time_a = 0 # last time we got an impulse on the A output of the encoder self.time_b = 0 # last time we got an impulse on the B output of the encoder self.elapsed_a_b = 0 # time elapsed between an impulse on A and an impulse on B self.dirsensed = 0 # direction sensed through the phase of the A and B outputs self.rpm = 0 # current speed in rotations per second self.rpm_last_a = 0 # value of the A counter when we last computed the rpms self.cruise_rpm = 0 # target value for the rpms self.walking = False # Boolean that indicates if the robot is walking or stationary self.desiredDir = True# Boolean that indecates the desired direction of the motor for move function self._control = PID() # PID control for the rotation of the wheels self._control.setTunings(KP, KI, KD); self._control.setSampleTime(1); self._control.setOutputLimits(-10000, 10000) self._control_distance = PID() # PID control for the cascade of the distance self._control_distance.setTunings(KP_DISTANCE, KI_DISTANCE, KD_DISTANCE); self._control_distance.setSampleTime(1); self._control_distance.setOutputLimits(-MAXIMUM_VELOCITY, MAXIMUM_VELOCITY) ExtInt(self.enca, ExtInt.IRQ_RISING, Pin.PULL_UP, self.enca_handler) ExtInt(self.encb, ExtInt.IRQ_RISING, Pin.PULL_UP, self.encb_handler) if RomiMotor.rpmtimer is None : # create only one shared timer for all instances RomiMotor.rpmtimer = Timer(4) RomiMotor.rpmtimer.init(freq=100, callback=RomiMotor.class_rpm_handler) RomiMotor.rpm_handlers.append(self.rpm_handler) # register the handler for this instance
def __init__(self): self.tmp = self.time = 0 self.cnt = 0 self.fr = 0 self.trig = Pin('X12', Pin.OUT_PP, Pin.PULL_NONE) echoR = Pin('X1', Pin.IN, Pin.PULL_NONE) echoF = Pin('X2', Pin.IN, Pin.PULL_NONE) self.micros = pyb.Timer(5, prescaler=83, period=0x3fffffff) self.timer = Timer(2, freq=1000) self.timer.period(3600) self.timer.prescaler(1375) self.timer.callback(lambda e: self.run_trig()) extR = ExtInt(echoR, ExtInt.IRQ_RISING, Pin.PULL_NONE, self.start_count) extF = ExtInt(echoF, ExtInt.IRQ_FALLING, Pin.PULL_NONE, self.read_dist)
class RotaryIRQ(Rotary): def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10, reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False, invert=False): super().__init__(min_val, max_val, reverse, range_mode, half_step, invert) if pull_up == True: self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP) self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP) else: self._pin_clk = Pin(pin_num_clk, Pin.IN) self._pin_dt = Pin(pin_num_dt, Pin.IN) self._pin_clk_irq = ExtInt(pin_num_clk, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._process_rotary_pins) self._pin_dt_irq = ExtInt(pin_num_dt, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._process_rotary_pins) # turn on 3.3V output to power the rotary encoder (pyboard D only) if 'PYBD' in os.uname().machine: Pin('EN_3V3').value(1) def _enable_clk_irq(self): self._pin_clk_irq.enable() def _enable_dt_irq(self): self._pin_dt_irq.enable() def _disable_clk_irq(self): self._pin_clk_irq.disable() def _disable_dt_irq(self): self._pin_dt_irq.disable() def _hal_get_clk_value(self): return self._pin_clk.value() def _hal_get_dt_value(self): return self._pin_dt.value() def _hal_enable_irq(self): self._enable_clk_irq() self._enable_dt_irq() def _hal_disable_irq(self): self._disable_clk_irq() self._disable_dt_irq() def _hal_close(self): self._hal_disable_irq()
def __init__(self, pin, callback): self._ev_start = Event() self._callback = callback self._times = array('i', (0 for _ in range(self.edgecount + 1))) # 1 for overrun ExtInt(pin, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._cb_pin) self._reset() loop = asyncio.get_event_loop() loop.create_task(self._run())
def init_encoder(self): ExtInt(Pin.cpu.A1, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=lambda t: self.callbackR(t)) ExtInt(Pin.cpu.A0, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=lambda t: self.callbackR(t)) ExtInt(Pin.cpu.B8, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=lambda t: self.callbackL(t)) ExtInt(Pin.cpu.C7, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=lambda t: self.callbackL(t))
def dinit_encoder(self): ExtInt(Pin.cpu.A1, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=None) ExtInt(Pin.cpu.A0, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=None) ExtInt(Pin.cpu.B8, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=None) ExtInt(Pin.cpu.C7, ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, callback=None)
def enable(self, callback=None, trigger=ExtInt.IRQ_FALLING, initial_check=False, **arguments): if self._interrupt: if not self._calling_callback: self._interrupt.enable() elif callback: self._callback = callback if arguments: self._callback_arguments = arguments print("Creating interrupt on pin", self.pin, "on", trigger) self._interrupt = ExtInt( self.pin, trigger, Pin.PULL_NONE, self.interrupt_callback ) print() else: raise NotImplementedError("No callback defined") self._enabled = True
def __init__(self, X=True): if X: self.pwmpin = Pin('X1', Pin.OUT_PP) self.pwmtim = Timer(2, freq=5000) self.pwm = self.pwmtim.channel(1, mode=Timer.PWM, pin=self.pwmpin) self.pwm.pulse_width(0) self.dir = Pin('X2', Pin.OUT_PP) self.dir.off() # O = forward, 1 = reverse self.sleep = Pin('X3', Pin.OUT_PP) self.sleep.value(0) # 0 = sleep, 1 = active self.enca = Pin('X4', Pin.IN, Pin.PULL_UP) self.encb = Pin('X5', Pin.IN, Pin.PULL_UP) else: self.pwmpin = Pin('Y1', Pin.OUT_PP) self.pwmtim = Timer(8, freq=5000) self.pwm = self.pwmtim.channel(1, mode=Timer.PWM, pin=self.pwmpin) self.pwm.pulse_width(0) self.dir = Pin('Y2', Pin.OUT_PP) self.dir.off() # O = forward, 1 = reverse self.sleep = Pin('Y3', Pin.OUT_PP) self.sleep.value(0) # 0 = sleep, 1 = active self.enca = Pin('Y4', Pin.IN, Pin.PULL_UP) self.encb = Pin('Y5', Pin.IN, Pin.PULL_UP) self.pwmscale = (self.pwmtim.period() + 1) // 100 # scale factor for percent power self.count_a = 0 # counter for impulses on the A output of the encoder self.target_a = 0 # target value for the A counter (for controlled rotation) self.count_b = 0 # counter for impulses on the B output of the encoder self.time_a = 0 # last time we got an impulse on the A output of the encoder self.time_b = 0 # last time we got an impulse on the B output of the encoder self.elapsed_a_b = 0 # time elapsed between an impulse on A and an impulse on B self.dirsensed = 0 # direction sensed through the phase of the A and B outputs self.rpm = 0 # current speed in rotations per second self.rpm_last_a = 0 # value of the A counter when we last computed the rpms self.cruise_rpm = 0 # target value for the rpms ExtInt(self.enca, ExtInt.IRQ_RISING, Pin.PULL_UP, self.enca_handler) ExtInt(self.encb, ExtInt.IRQ_RISING, Pin.PULL_UP, self.encb_handler) if RomiMotor.rpmtimer is None: # create only one shared timer for all instances RomiMotor.rpmtimer = Timer(4) RomiMotor.rpmtimer.init(freq=4, callback=RomiMotor.class_rpm_handler) RomiMotor.rpm_handlers.append( self.rpm_handler) # register the handler for this instance
def __init__(self, led): self.led = led self.i2c = I2C(2, I2C.MASTER) self.buf1 = bytearray(2) self.buf2 = bytearray(2) self.buf1 = self.i2c.mem_read(2, 0x26, 0) self.buf2 = self.i2c.mem_read(2, 0x27, 0) self.intr1 = 0 self.intr2 = 0 self.counter = 0 self.left_down = Pin('X12', Pin.IN, Pin.PULL_UP) self.left_enter = Pin('Y12', Pin.IN, Pin.PULL_UP) self.camera = Pin('X11', Pin.IN, Pin.PULL_UP) self.u6 = UART(6, baudrate=9600, read_buf_len=4096) print(self.buf1) print(self.buf2) print("hello world") ExtInt('X19', ExtInt.IRQ_FALLING, Pin.PULL_UP, self.intr1_cb) ExtInt('X20', ExtInt.IRQ_FALLING, Pin.PULL_UP, self.intr2_cb)
def init(timer_id=2, data_pin='Y9', the_dhttype='DHT11'): global data global micros global timer global dhttype if (the_dhttype == 'DHT11'): dhttype = 0 else: dhttype = 1 # Configure the pid for data communication data = Pin(data_pin) # Save the ID of the timer we are going to use timer = timer_id # setup the 1uS timer micros = pyb.Timer(timer, prescaler=83, period=0x3fffffff) # 1MHz ~ 1uS # Prepare interrupt handler ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, None) ExtInt(data, ExtInt.IRQ_FALLING, Pin.PULL_UP, edge)
def __init__(self, trigger_pin, return_pin, sos=0.034029): self.return_pin = return_pin trigger_pin.init(mode=Pin.OUT) self.trigger_pin = trigger_pin self.sos = sos self.start_micros = None self.elapsed = None self.bogus = False self.interrupt = ExtInt(return_pin, ExtInt.IRQ_RISING_FALLING, Pin.PULL_DOWN, self.IRQ)
def __init__(self, rackleds, rackbuttons, buzz, pin, color, id): self.rackleds = rackleds self.rackbuttons = rackbuttons #rack auquel appartient le bouton self.buzzer = buzz # buzzer du jeux self.buttonPin = pin # pin sur la PYBStick26 self.color = color # couleur du bouton (associé à la led de la même couleur) self.id = id # rang dans le rack: 0 à 3 self.button = Pin(pin, Pin.IN, Pin.PULL_UP) # pin reliée au bouton en pull_up self.extint = ExtInt(pin, ExtInt.IRQ_FALLING, Pin.PULL_UP, self.callback)
class Button(object): def __init__(self): self.extint = ExtInt(Pin('ON/OFF'), ExtInt.IRQ_FALLING, Pin.PULL_UP, self.__callback__) self._status = False def __callback__(self, line): #print('1, button __callback__', line) self.extint.disable() delay(500) self._status = False if self._status else True self.extint.enable() def reset(self): irq_state = disable_irq() self._status = False enable_irq(irq_state) def on(self): #print('switch status=', self._status) return True if self._status else False
def main(): ## X1,X2路PWM波分别作为激励和采样保持开关信号,非晶丝串联150ohm电阻 ## 激励电流25-30mA tim = Timer(2, freq=500000) #500Khz ch1 = tim.channel(1, Timer.PWM, pin=pyb.Pin('X1', pyb.Pin.OUT_PP)) ch2 = tim.channel(2, Timer.PWM, pin=pyb.Pin('X2', pyb.Pin.OUT_PP)) ch1.pulse_width_percent(20) ch2.pulse_width_percent(30) ## 2路光耦,Y1 启动与关闭,Y2同步 ext_sts1 = extState() ext_sts2 = extState() ExtInt(pyb.Pin('Y1'), ExtInt.IRQ_FALLING, Pin.PULL_NONE, ext_sts1.callback) ExtInt(pyb.Pin('Y2'), ExtInt.IRQ_FALLING, Pin.PULL_NONE, ext_sts2.callback) ## 创建文件 file_name = new_file() data_file = open(file_name, 'w') ## uart1, Tx -> X9 Rx -> X10 uart = UART(1, 57600) while not ext_sts1.new_event: # 等待启动信号,要求采集客户端先启动 buf = uart.read(128) ext_sts1.clear() pyb.LED(1).on() pyb.LED(2).on() while not ext_sts1.new_event: # 等待结束信号 if ext_sts2.new_event: # 写同步信号 ext_sts2.clear() data_file.write('\n-- new block ---\n') pyb.LED(2).toggle() buf = uart.read(128) data_file.write(buf) data_file.close() pyb.LED(1).off() pyb.LED(2).off()
def setup(cls): # Watch the SD card-detect signal line... but very noisy # - this is called a few seconds after system startup from pyb import Pin, ExtInt def card_change(_): # Careful: these can come fast and furious! cls.last_change = utime.ticks_ms() cls.last_change = utime.ticks_ms() cls.irq = ExtInt(Pin('SD_SW'), ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, card_change)
def __init__(self, pin, callback, extended, *args): # Optional args for callback self._ev_start = Event() self._callback = callback self._extended = extended self._addr = 0 self.block_time = 80 if extended else 73 # Allow for some tx tolerance (?) self._args = args self._times = array('i', (0 for _ in range(_EDGECOUNT + 1))) # +1 for overrun if platform == 'pyboard': ExtInt(pin, ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, self._cb_pin) else: pin.irq(handler = self._cb_pin, trigger = (Pin.IRQ_FALLING | Pin.IRQ_RISING), hard = True) self._edge = 0 self._ev_start.clear() loop = asyncio.get_event_loop() loop.create_task(self._run())
class InterruptHandler(): def __init__(self, pin_interrupt, callback=None, **arguments): self._enabled = False self._calling_callback = False self.pin = pin_interrupt self._callback = None self._callback_arguments = {} self._allocation_scheduled_callback = self.scheduled_callback self._interrupt = None self.interrupt_time = utime.ticks_ms() if callback: self.enable(callback, arguments) def enable(self, callback=None, trigger=ExtInt.IRQ_FALLING, initial_check=False, **arguments): if self._interrupt: if not self._calling_callback: self._interrupt.enable() elif callback: self._callback = callback if arguments: self._callback_arguments = arguments print("Creating interrupt on pin", self.pin, "on", trigger) self._interrupt = ExtInt( self.pin, trigger, Pin.PULL_NONE, self.interrupt_callback ) print() else: raise NotImplementedError("No callback defined") self._enabled = True def disable(self): self._enabled = False self._interrupt.disable() def time_since_interrupt(self): return utime.ticks_diff(utime.ticks_ms(), self.interrupt_time) def interrupt_callback(self, line=None): self.interrupt_time = utime.ticks_ms() if self._calling_callback: return if self._interrupt: self._interrupt.disable() self._calling_callback = True micropython.schedule(self._allocation_scheduled_callback, line) def scheduled_callback(self, line): self._callback_arguments["line"] = line self._callback(**self._callback_arguments) self._calling_callback = False if self._interrupt and self._enabled: self._interrupt.enable()
def setup(cls): # Watch the SD card-detect signal line... but very noisy # - this is called a few seconds after system startup from pyb import Pin, ExtInt def card_change(_): # Careful: these can come fast and furious! cls.last_change = utime.ticks_ms() cls.last_change = utime.ticks_ms() cls.irq = ExtInt(Pin('SD_SW'), ExtInt.IRQ_RISING_FALLING, Pin.PULL_UP, card_change) # mark 2+ boards have a light for SD activity. from machine import Pin cls.active_led = Pin('SD_ACTIVE', Pin.OUT)
cb_timestamp_index += 1 if cb_timestamp_index > GPIO_CB_TS_ARRAY_LEN: # should not happen raise BoundsException('CB timestamp array bounds exceeded') cb_timestamp_index = 0 cb_timestamp = array.array('L', 0 for x in range(GPIO_CB_TS_ARRAY_LEN)) cb_timestamp_jitter = array.array('L', 0 for x in range(GPIO_CB_TS_ARRAY_LEN)) hist = array.array('L', 0 for x in range(GPIO_HIST_ARRAY_LEN)) # Configure GPIO output pin (PyBoard v1.0). p_out = Pin('X3', mode=Pin.OUT_PP, pull=Pin.PULL_DOWN) # Configure GPIO input pin to trigger an interrupt (PyBoard v1.0). ExtInt(Pin('X4'), ExtInt.IRQ_RISING, Pin.PULL_DOWN, gpio_callback) while True: # Wait for callback timestamp array to fill up in ISR. while cb_timestamp_index < GPIO_CB_TS_ARRAY_LEN: sleep_us(100) # Start of critical section. irq_state = pyb.disable_irq() cb_captured_time_span = cb_timestamp[ GPIO_CB_TS_ARRAY_LEN - 1] - cb_timestamp[GPIO_CB_IGNORED_TIMESTAMPS] # Protect against ticks_us wrap-around. if cb_captured_time_span > 0: time_per_bin = float(cb_captured_time_span) / (
''' 1.5 Servo control ''' from pyb import Servo s1.Servo(1) s1.angle(45) s1.angle(-60, 1500) # move to -60 degrees in 1500ms s1.speed(50) # for continuous rotation servos ''' 1.6 External interrupts ''' from pyb import Pin, ExtInt callback = lambda e: print("intr") ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback) ''' 1.7 Timers ''' from pyb import Timer tim = Timer(1, freq=1000) time.counter() # get counter value time.freq(0.5) # 0.5Hz time.callback(lambda t: pyb.LED(1).toggle()) ''' 1.8 PWM ''' from pyb import Pin, Timer p = Pin('X1')
## Enable wakeup from an active-high edge on PA0 def imucallback(line): global evtime evtime = pyb.millis() return imu.read(0x3) def imudebug(line): tilt = imucallback(line) if (tilt & 0x80) != 0: print("Shake detected!") if (tilt & 0x20) != 0: print("Tap detected!") exti = ExtInt('MMA_INT', Pin.IRQ_RISING, Pin.PULL_NONE, imucallback if not settings.debug else imudebug) vbus = Pin('USB_VBUS', Pin.IN) ## Check for low power states, or do nothing. def trysuspend(): global evtime ## Do nothing if sleep is disabled timeout = settings.sleeptimeout if not timeout: return False ## Detect motion via the accelerometer dx = imu.x() - xyz[0] dy = imu.y() - xyz[1]
''' 实验名称:外部中断 版本:v1.0 日期:2020.12 作者:01Studio 社区:www.01studio.org ''' from pyb import Pin, ExtInt, LED #定义回调函数,需要将ext中断号传递进来 def fun1(ext): LED(4).toggle() ext = ExtInt(Pin('A0'), ExtInt.IRQ_FALLING, Pin.PULL_UP, fun1) #下降沿触发,打开上拉电阻
''' 实验名称:外部中断 版本:v1.0 日期:2019.9 作者:01Studio 社区:www.01studio.org ''' from pyb import Pin,ExtInt,LED callback = lambda e: LED(3).toggle() #下降沿触发,打开上拉电阻 ext = ExtInt(Pin('P9'), ExtInt.IRQ_FALLING, Pin.PULL_UP, callback)
uart = UART(4, 9600) #UART for ESP communication uart.init(9600, bits=8, parity=None, stop=1, read_buf_len=64) dacA.write(init_dacA) dacB.write(init_dacB) motor_relay_L.high() motor_relay_R.high() motor1_switch.high() motor2_switch.high() motor_L_brake.high() motor_R_brake.high() setSpeedR(0) setSpeedL(0) Switch().callback(lambda: forward(2000)) tim.callback(speedCorrection) ExtInt('Y1', ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, cfreq_R) #hall sensors as inperrupts ExtInt('Y2', ExtInt.IRQ_RISING_FALLING, Pin.PULL_NONE, cfreq_L) ExtInt(Pin('Y3'), ExtInt.IRQ_RISING, Pin.PULL_DOWN, brake_intr) # external intrupt to have sudden brake pyb.Timer(4, freq=33.33).callback(isr_speed_timer) ############################################################################################################# # LOOP # ############################################################################################################# while True: if (brake_status and status): motor1_switch.high() motor2_switch.high() status = False if (uart.any()): uart_input_text = uart.read()
t_pressed = 0 low = int(ord(i2c.mem_read(1, 0x38, regAddressXLow))) cord = (low & 0xff) high = int(ord(i2c.mem_read(1, 0x38, regAddressXHigh))) tempcord = (high & 0x0f) << 8 cord = cord | tempcord #print(cord) Y = cord low = int(ord(i2c.mem_read(1, 0x38, regAddressYLow))) cord = (low & 0xff) high = int(ord(i2c.mem_read(1, 0x38, regAddressYHigh))) tempcord = (high & 0x0f) << 8 cord = cord | tempcord #print(cord) X = cord #print(X,Y); lcd.pixels(X, Y, 0xffffff) def TS_Interrupt(): global t_pressed t_pressed = 1 callback = lambda e: TS_Interrupt() extint = ExtInt(Pin.board.LCD_INT, ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback) lcd = LCD(0)
walklight[1].high() delay(500) # Stop=green, walk=red walklight[1].low() walklight[0].high() delay(500) # Give the pedestrian a chance to see it stoplight[0].low() stoplight[2].high() # Create callback for the button def button_pressed(line): cur_value = button.value() active = 0 while (active < 50): if button.value() != cur_value: active += 1 else: active = 0 delay(1) print("") if active: cycle_lights() else: print("False press") # Create an interrupt for the button e = ExtInt('X1', ExtInt.IRQ_FALLING, Pin.PULL_UP, button_pressed)