10. Automate common workflows#

If you find yourself repeating the same sequence of steps in pw_console over and over again, there are a few ways to automate those workflows.

Snippets#

Teams often have common commands or RPCs they need to run while debugging or developing a product. You can put these in snippets for easy sharing across your team. Try running a snippet now:

  1. If you don’t already have a pw_console instance connected to a Pico, connect one now:

    bazelisk run //apps/blinky:rp2040_console
    
    bazelisk run //apps/blinky:rp2350_console
    
  2. In pw_console click File then click Insert Repl Snippet.

    https://storage.googleapis.com/pigweed-media/sense/snippet_v1.png
  3. Select Echo RPC with your keyboard and then press Enter.

    Troubleshooting

    If clicking Echo RPC doesn’t work, try the keyboard-based workflow.

    You should see the Python Repl input prompt (bottom-left pane) get auto-populated with an echo command.

  4. Focus the Python Repl and then press Enter to execute the pre-populated echo command.

    Tip

    If you’re curious about how snippets are implemented, take a look at //.pw_console.yaml. Notice the snippets entries. Each of these is an automated workflow that can be run in pw_console.

  5. Press Ctrl+D twice to close pw_console.

The following video is a demonstration of snippets:

Python scripts#

For a long, complex workflow, you may prefer encapsulating everything into a script.

  1. Run the script:

    In Bazel Build Targets expand //tools, then right-click :example_script (py_binary), then select Run target.

    A terminal launches and executes the script contained in //tools/sense/example_script.py.

    Run the following command:

    $ bazelisk run //tools:example_script
    

Troubleshooting

If the script fails: make sure that you closed the pw_console instance from the last section before attempting this section.

You should see output similar to this:

20241221 08:20:18 INF Using serial port: /dev/ttyACM0
20241221 08:20:18 DBG Starting read process
20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.log.Logs.Listen, call_id=1)
20241221 08:20:18 DBG Using selector: EpollSelector
20241221 08:20:18 INF Calling Echo(msg="Hello")
20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2)
20241221 08:20:18 DBG PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2) received response: msg: "Hello"

20241221 08:20:18 INF PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2) completed: Status.OK
The status was Status.OK
The message was Hello
20241221 08:20:18 INF Calling Echo(msg="Goodbye!")
20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3)
20241221 08:20:18 DBG PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3) received response: msg: "Goodbye!"

20241221 08:20:18 INF PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3) completed: Status.OK
Status.OK: msg: "Goodbye!"

20241221 08:20:18 DBG Stopping read process

Towards the end of the output you can see the echoed message and the printed status information.

Tip

Take a look at //tools/sense/example_script.py if you’re curious about how this script is implemented.

Summary#

Gone are the days of ad hoc development workflows that some teammates benefit from and others don’t. With Pigweed, these common workflows become explicit, centralized, and shareable, and they’re checked in alongside the rest of the project’s code.

Next, head over to 11. Communicate with your Pico over the Web Serial API to try interacting with your Pico through a web app.