The following section will demonstrate how to run, stop, pause and control the virtual device execution.
Create a test_blinky.py file next to the blinky.bin binary in the samples_nrf52832/pre_compiled/blinky folder (or where you built your code). Paste the following code onto the new test file.
#!/usr/bin/python2 from jumper.vlab import Vlab # set up the device simulation v = Vlab(working_directory=".", print_uart=True, platform="nrf52832") v.load("blinky.bin") def pins_listener(pin_number, pin_level): print("pin_number:", pin_number, " pin_level:", pin_level) v.run_for_ms(1000) v.on_pin_level_event(pins_listener) print('ran for one second') v.run_for_us(1000000) print('ran for one second') v.run_for_ns(1000000000) print('ran for one second') v.stop() print('Got here, so that means the test result should be OK')
Run your test script from the terminal:
Expected output should look like this:
Loading virtual device ........ Done ran for one second ('pin_number:', 20, ' pin_level:', 0) ('pin_number:', 17, ' pin_level:', 1) ran for one second ('pin_number:', 18, ' pin_level:', 1) ('pin_number:', 19, ' pin_level:', 1) ran for one second Got here, so that means the test result should be OK
So what just happened?
Instead of using v.start() which would have started the execution of the Virtual Lab, we used 3 run_for options:
- v.run_for_ms(1000) - run for 1000 ms (1 second)
- v.run_for_us(1000000) - run for 1000000 us (1 second)
- v.run_for_ns(1000000000) - run for 1000000000 ns (1 second)
This is useful for timing inputs into the device and testing timing related use case such as de-bouncer, communication and more.
How time is measured in the Virtual Lab
The Virtual Lab time is referenced to the main clock tick of the MCU. In the nRF52832 the main clock frequency is 64MHz, so a tick is 1/64MHz for example. The Virtual Lab is synced with tests written using the Python SDK.