In this section we'll demonstrate simple usage of the Python SDK to print the blinky toggeled LEDs in a cool way. Note that this example is targeted for the nRF52832 but you can also run if for the STM32F4 MCU.
Create a test
We'll use the blinky example from the nrf52832 samples zip We downloaded earlier on. Inside the /samples_nrf52832/pre_compiled/blinky folder, create a test.py file. Paste the following code into the test.py file and save it.
#!/usr/bin/python2 from jumper.vlab import Vlab import sys # this array holds the four LEDs' visualisation LEDs=["..","..","..",".."] # This function will be called back for each state change of a pin. # In this case, we use it to simulate the visible behaviour of the nRF52 DK: # the blinky example makes the DK's four LEDs bink. This function visualise these four leds def on_pin(number, level): # update the state of the simulated LEDs if level: LEDs[number-17] = "XX" else: LEDs[number-17] = ".." # show them in the terminal print " " + LEDs + " " + LEDs + " " + LEDs + " " + LEDs, sys.stdout.flush() print "\r", # set up the device simulation v = Vlab(working_directory=".", platform = "nrf52832") v.load("blinky.bin") # register the callback which visualises the LEDs v.on_pin_level_event(on_pin) # run the simulation v.start()
Run the test file:
Result should look like this
Alright! Let's drill down and see what just happened. First step is importing the Vlab package. If you installed Jumper (How to install section), this should work smoothly.
from jumper.vlab import Vlab
The next interesting place is defining the pin listener with a callback. It's like placing a logic analyzer on every GPIO pin of your MCU. Once this line is called:
v.on_pin_level_event(on_pin), the on_pin function will occure everytime a pin level changes. The on_pin callback will be called with
number (the GPIO pin number) and
def on_pin(number, level):
After the on_pin function ends, we're starting to setup the Vlab. First step is to set the working directy and the MCU to run your firmware on. In this example we're setting it to work out of the same folder as the python test we're writing. The workding directly setting is where the Virtual Lab will look for various configuration files for more elaborate scenarios. The MCU we chose is the nRF52832.
v = Vlab(working_directory=".", platform = "nrf52832")
Next, we'll load the firmware to the Virtual Lab:
After signing up for pin events (
v.on_pin_level_event(on_pin)), we'll just start the Virtual Lab!
And that's it! Running
python test.py will execute the script. Checkout the SDK section for more SDK functionality.