upytester
0.1

Contents:

  • Examples
    • Running Tests
    • Examples Contents
      • Basic
      • On Board Components
        • LED Blink
  • upytester
  • upyt
upytester
  • Docs »
  • Examples »
  • On Board Components »
  • LED Blink
  • View page source

LED Blink¶

In this example we’ll create a custom instruction on the pyboard to perform a specific task.

We’ll be re-creating the blink_led() with a blinking behaviour instead of just turning on for a set duration.

Bench Setup¶

The only physical requirement for this test is a pyboard connected via USB.

On Board Library¶

The point of on-board components is that they’re executing directly on the pyboard. This code is stored in a folder nested in the project, defined in the .upytester-bench.yml file:

bench:
    libraries:
        sd: 'benchlib-sd'

This library is synchronised onto the pyboard with:

upytester sync

If the upytester sync command finds the bench configuration file, it will mirror that directories contents onto the lib_bench folder on the SD card.

For this example, we’ve added an @instruction called custom_blinker:

import pyb

# upytester pyboard library
from upyt.cmd import instruction
import upyt.sched

@instruction
def custom_blinker(led=1, iterations=6):
    led = pyb.LED(led)

    def callback():
        led.toggle()
        iterations -= 1
        if iterations > 0:
            upyt.sched.loop.call_later_ms(100, callback)

    upyt.sched.loop.call_soon(callback)

Note that there is also a bench.py file. This is always imported just prior to the main scheduler loop starts (if it exists).

# import all libraries that define an @instruction
import customled

Importing the customled module ensures the @instruction decorator registers the method(s) it decorates as methods callable by the host.

Test Case¶

The test itself simply calls the @instruction registered method from the host pc.

class LEDTest(unittest.TestCase):
    def setUp(self):
        self.pyb_a = upytester.project.get_device('pyb_a')

    def tearDown(self):
        self.pyb_a.close()

    def test_custom_blinker(self):
        """
        Test the on-board LED instruction
        """
        self.pyb_a.custom_blinker(led=3)

If all of this has worked, you should see the yellow LED blink a few times. The test will actually complete before the blinking stops, because it’s running asynchronously on the board.

Next Previous

© Copyright 2019, Peter Boin

Built with Sphinx using a theme provided by Read the Docs.