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. In pw_console click File.

  2. Click Insert Repl Snippet.

  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 get auto-populated with an echo command.

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

  5. If your 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.

  6. 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 the workflow into its own 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 close the pw_console from the last section before attempting this section.

You should see output similar to this:

# ...
The status was Status.OK
The message was Hello
20240717 18:26:25 INF Calling Echo(msg="Goodbye!")
20240717 18:26:25 DBG Starting PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3)
20240717 18:26:25 DBG PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) finished with status Status.OK
20240717 18:26:25 DBG PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) received response: msg: "Goodbye!"

20240717 18:26:25 INF PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) completed: Status.OK
Status.OK: msg: "Goodbye!"

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

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.