Python pyb.LED Examples

The following are 30 code examples of pyb.LED(). 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: accelleds.py    From micropython-stm-lib with MIT License 7 votes vote down vote up
def led_angle():
    # make LED objects
    l1 = pyb.LED(1)
    l2 = pyb.LED(2)
    accel = STAccel()

    while True:
        # get x-axis
        x = accel.x() * 50

        # turn on LEDs depending on angle
        if x < -10:
            l2.on()
            l1.off()
        elif x > 10:
            l1.on()
            l2.off()
        else:
            l1.off()
            l2.off()

        # delay so that loop runs at at 1/50ms = 20Hz
        pyb.delay(50) 
Example #2
Source File: art1.py    From micropython-async with MIT License 6 votes vote down vote up
def cb(data, addr, led):
    if addr == 0x40:  # Adapt for your remote
        if data == 1:  # Button 1. Adapt for your remote/buttons
            print('LED on')
            if platform == 'pyboard':
                led.on()
            else:
                led(0)
        elif data == 2:
            print('LED off')
            if platform == 'pyboard':
                led.off()
            else:
                led(1)
        elif data < REPEAT:
            print('Bad IR data')
    else:
        print('Incorrect remote') 
Example #3
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 #4
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 #5
Source File: astests.py    From micropython-async with MIT License 6 votes vote down vote up
def test_btncb():
    s = '''
press toggles red
release toggles green
double click toggles yellow
long press toggles blue
'''
    print('Test of pushbutton executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (red,))
    pb.release_func(toggle, (green,))
    pb.double_func(toggle, (yellow,))
    pb.long_func(toggle, (blue,))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer()) 
Example #6
Source File: astests.py    From micropython-async with MIT License 6 votes vote down vote up
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())

# Test for the switch class with a callback 
Example #7
Source File: heartbeat.py    From micropython-async with MIT License 6 votes vote down vote up
def heartbeat(tms):
    if platform == 'pyboard':  # V1.x or D series
        from pyb import LED
        led = LED(1)
    elif platform == 'esp8266':
        from machine import Pin
        led = Pin(2, Pin.OUT, value=1)
    elif platform == 'linux':
        return  # No LED
    else:
        raise OSError('Unsupported platform.')
    while True:
        if platform == 'pyboard':
            led.toggle()
        elif platform == 'esp8266':
            led(not led())
        await asyncio.sleep_ms(tms) 
Example #8
Source File: switches.py    From micropython-async with MIT License 6 votes vote down vote up
def test_swcb():
    s = '''
close toggles red
open toggles green
'''
    print('Test of switch executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red,))
    sw.open_func(toggle, (green,))
    run()

# Test for the Pushbutton class (coroutines)
# Pass True to test suppress 
Example #9
Source File: switches.py    From micropython-async with MIT License 6 votes vote down vote up
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    run()

# Test for the switch class with a callback 
Example #10
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 #11
Source File: as_rwGPS_time.py    From micropython-async with MIT License 6 votes vote down vote up
def shutdown():
    global gps
    # Normally UART is already at BAUDRATE. But if last session didn't restore
    # factory baudrate we can restore connectivity in the subsequent stuck
    # session with ctrl-c.
    uart.init(BAUDRATE)
    await asyncio.sleep(0.5)
    await gps.command(FULL_COLD_START)
    print('Factory reset')
    gps.close()  # Stop ISR
    #print('Restoring default baudrate (9600).')
    #await gps.baudrate(9600)
    #uart.init(9600)
    #gps.close()  # Stop ISR
    #print('Restoring default 1s update rate.')
    #await asyncio.sleep(0.5)
    #await gps.update_interval(1000)  # 1s update rate 
    #print('Restoring satellite data.')
    #await gps.command(as_rwGPS.DEFAULT_SENTENCES)  # Restore satellite data

# Setup for tests. Red LED toggles on fix, blue on PPS interrupt. 
Example #12
Source File: art1.py    From micropython-async with MIT License 6 votes vote down vote up
def test():
    print('Test for IR receiver. Assumes NEC protocol. Turn LED on or off.')
    if platform == 'pyboard':
        p = Pin('X3', Pin.IN)
        led = LED(2)
    elif platform == 'esp8266':
        freq(160000000)
        p = Pin(13, Pin.IN)
        led = Pin(2, Pin.OUT)
        led(1)
    elif ESP32:
        p = Pin(23, Pin.IN)
        led = Pin(21, Pin.OUT)  # LED with 220Ω series resistor between 3.3V and pin 21
        led(1)
    ir = NEC_IR(p, cb, True, led)  # Assume extended address mode r/c
    loop = asyncio.get_event_loop()
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()  # Still need ctrl-d because of interrupt vector 
Example #13
Source File: art1.py    From micropython-async with MIT License 6 votes vote down vote up
def test():
    print('Test for IR receiver. Assumes NEC protocol. Turn LED on or off.')
    if platform == 'pyboard':
        p = Pin('X3', Pin.IN)
        led = LED(2)
    elif platform == 'esp8266':
        freq(160000000)
        p = Pin(13, Pin.IN)
        led = Pin(2, Pin.OUT)
        led(1)
    elif ESP32:
        p = Pin(23, Pin.IN)
        led = Pin(21, Pin.OUT)  # LED with 220Ω series resistor between 3.3V and pin 21
        led(1)
    ir = NEC_IR(p, cb, True, led)  # Assume extended address mode r/c
    loop = asyncio.get_event_loop()
    loop.run_forever() 
Example #14
Source File: art1.py    From micropython-async with MIT License 6 votes vote down vote up
def cb(data, addr, led):
    if addr == 0x40:  # Adapt for your remote
        if data == 1:  # Button 1. Adapt for your remote/buttons
            print('LED on')
            if platform == 'pyboard':
                led.on()
            else:
                led(0)
        elif data == 2:
            print('LED off')
            if platform == 'pyboard':
                led.off()
            else:
                led(1)
        elif data < REPEAT:
            print('Bad IR data')
    else:
        print('Incorrect remote') 
Example #15
Source File: switches.py    From micropython-samples with MIT License 6 votes vote down vote up
def test_btncb():
    s = '''
press toggles red
release toggles green
double click toggles yellow
long press toggles blue
'''
    print('Test of pushbutton executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (red,))
    pb.release_func(toggle, (green,))
    pb.double_func(toggle, (yellow,))
    pb.long_func(toggle, (blue,))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer()) 
Example #16
Source File: switches.py    From micropython-samples with MIT License 6 votes vote down vote up
def test_swcb():
    s = '''
close toggles red
open toggles green
'''
    print('Test of switch executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red,))
    sw.open_func(toggle, (green,))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())

# Test for the Pushbutton class (coroutines)
# Pass True to test suppress 
Example #17
Source File: switches.py    From micropython-samples with MIT License 6 votes vote down vote up
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())

# Test for the switch class with a callback 
Example #18
Source File: asnano_sync.py    From micropython-nano-gui with MIT License 6 votes vote down vote up
def main():
    print('Press Pyboard usr button to stop test.')
    # Asynchronously flash Pyboard LED's. Because we can.
    leds = [asyncio.create_task(flash(1, 200)), asyncio.create_task(flash(2, 233))]
    # Task for each meter and GUI LED
    mtasks =[MyMeter(2, 'left').task, MyMeter(50, 'right').task, MyMeter(98, 'bass').task]
    k = Killer()
    while True:
        if await k.wait(800):  # Switch was pressed
            break
        refresh(ssd)
    for task in mtasks + leds:
        task.cancel()
    await asyncio.sleep_ms(0)
    ssd.fill(0)  # Clear display at end.
    refresh(ssd) 
Example #19
Source File: heartbeat_irq.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self):
        self.tick = 0
        self.led = pyb.LED(1)
        tim = pyb.Timer(0)
        tim.init(prescaler=128, period=37500) # 10 Hz
        tim.callback(self.heartbeat_cb) 
Example #20
Source File: heartbeat_fade_irq.py    From upy-examples with MIT License 5 votes vote down vote up
def __init__(self):
        self.tick = 0
        self.led = pyb.LED(4)
        tim = pyb.Timer(4)
        tim.init(freq=100)
        tim.callback(self.heartbeat_cb) 
Example #21
Source File: midiplay.py    From micropython-stm-lib with MIT License 5 votes vote down vote up
def main():
    # Initialize UART for MIDI
    uart = UART(2, baudrate=31250)
    midi = MidiOut(uart)
    button1 = Switch()
    button2 = Pin('PC0', Pin.IN, Pin.PULL_UP)
    button3 = Pin('PC1', Pin.IN, Pin.PULL_UP)
    led1 = LED(1)
    led2 = LED(2)
    led3 = LED(3)
    tune = program = 0

    # send a PROGRAM CHANGE to set instrument to #0 (Grand Piano)
    midi.program_change(program)

    while True:
        if button1():
            # When button 1 is pressed, play the current tune
            play(midi, TUNES[TUNENAMES[tune]], led1)
            led1.off()
        if not button2():
            # When button 2 is pressed, select the next of the tunes
            led2.on()
            tune = (tune+1) % len(TUNENAMES)
            delay(BLINK_DELAY)
            led2.off()
        if not button3():
            # When button 3 is pressed, change to next program (instrument)
            led3.on()
            program = (program+1) % len(PROGRAMS)
            midi.program_change(PROGRAMS[program])
            delay(BLINK_DELAY)
            led3.off()

        delay(200) 
Example #22
Source File: asnano.py    From micropython-nano-gui with MIT License 5 votes vote down vote up
def meter(n, x, text, t):
    print('Meter {} test.'.format(n))
    m = Meter(wri, 5, x, divisions = 4, ptcolor=YELLOW,
              label=text, style=Meter.BAR, legends=('0.0', '0.5', '1.0'))
    l = LED(wri, ssd.height - 16 - wri.height, x, bdcolor=YELLOW, label ='over')
    while True:
        v = int.from_bytes(uos.urandom(3),'little')/16777216
        m.value(v, color(v))
        l.color(color(v))
        l.text(txt(v), fgcolor=color(v))
        refresh(ssd)
        await asyncio.sleep_ms(t) 
Example #23
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)
    blue = pyb.LED(4)
    uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
    sreader = asyncio.StreamReader(uart)
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=us_cb, pps_cb_args=(tick, blue)) 
Example #24
Source File: as_GPS_time.py    From micropython-async with MIT License 5 votes vote down vote up
def setup():
    red = pyb.LED(1)
    blue = pyb.LED(4)
    uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
    sreader = asyncio.StreamReader(uart)
    pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
    return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
                             fix_cb=lambda *_: red.toggle(),
                             pps_cb=lambda *_: blue.toggle())

# Test terminator: task sets the passed event after the passed time. 
Example #25
Source File: sr_init.py    From micropython-async with MIT License 5 votes vote down vote up
def heartbeat():
    led = LED(1)
    while True:
        await asyncio.sleep_ms(500)
        led.toggle() 
Example #26
Source File: asnano.py    From micropython-nano-gui with MIT License 5 votes vote down vote up
def flash(n, t):
    led = pyb.LED(n)
    while True:
        led.toggle()
        await asyncio.sleep_ms(t) 
Example #27
Source File: astests.py    From micropython-async with MIT License 5 votes vote down vote up
def test_btn(suppress=False, lf=True, df=True):
    s = '''
press pulses red
release pulses green
double click pulses yellow
long press pulses blue
'''
    print('Test of pushbutton scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin, suppress)
    pb.press_func(pulse, (red, 1000))
    pb.release_func(pulse, (green, 1000))
    if df:
        print('Doubleclick enabled')
        pb.double_func(pulse, (yellow, 1000))
    if lf:
        print('Long press enabled')
        pb.long_func(pulse, (blue, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())

# Test for the Pushbutton class (callbacks) 
Example #28
Source File: asnano_sync.py    From micropython-nano-gui with MIT License 5 votes vote down vote up
def __init__(self, x, text):
        CWriter.set_textpos(ssd, 0, 0)  # In case previous tests have altered it
        wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False)
        wri.set_clip(True, True, False)
        super().__init__(wri, 5, x, divisions = 4, ptcolor=YELLOW, label=text,
                         style=Meter.BAR, legends=('0.0', '0.5', '1.0'))
        self.led = LED(wri, ssd.height - 16 - wri.height, x, bdcolor=YELLOW, label ='over')
        self.task = asyncio.create_task(self._run()) 
Example #29
Source File: astests.py    From micropython-async with MIT License 5 votes vote down vote up
def pulse(led, ms):
    led.on()
    await asyncio.sleep_ms(ms)
    led.off()

# Toggle an LED (callback) 
Example #30
Source File: aledflash.py    From micropython-async with MIT License 5 votes vote down vote up
def test(duration):
    loop = asyncio.get_event_loop()
    duration = int(duration)
    if duration > 0:
        print("Flash LED's for {:3d} seconds".format(duration))
    leds = [pyb.LED(x) for x in range(1,5)]  # Initialise all four on board LED's
    for x, led in enumerate(leds):           # Create a coroutine for each LED
        t = int((0.2 + x/2) * 1000)
        loop.create_task(toggle(leds[x], t))
    loop.run_until_complete(killer(duration))
    loop.close()