1.. _showcase-sense-tutorial-automate: 2 3============================= 410. Automate common workflows 5============================= 6If you find yourself repeating the same sequence of steps in 7``pw_console`` over and over again, there are a few ways to automate 8those workflows. 9 10.. _showcase-sense-tutorial-automate-snippets: 11 12-------- 13Snippets 14-------- 15Teams often have common commands or RPCs they need to run while 16debugging or developing a product. You can put these in snippets 17for easy sharing across your team. Try running a snippet now: 18 19#. If you don't already have a ``pw_console`` instance connected to 20 a Pico, connect one now: 21 22 .. tab-set:: 23 24 .. tab-item:: Pico 1 & 1W (RP2040) 25 26 .. code-block:: console 27 28 bazelisk run //apps/blinky:rp2040_console 29 30 .. tab-item:: Pico 2 & 2W (RP2350) 31 32 .. code-block:: console 33 34 bazelisk run //apps/blinky:rp2350_console 35 36#. In ``pw_console`` click **File** then click **Insert Repl Snippet**. 37 38 .. figure:: https://storage.googleapis.com/pigweed-media/sense/snippet_v1.png 39 40#. Select **Echo RPC** with your keyboard and then press :kbd:`Enter`. 41 42 .. admonition:: Troubleshooting 43 44 If clicking **Echo RPC** doesn't work, try the keyboard-based 45 workflow. 46 47 You should see the **Python Repl** input prompt (bottom-left pane) get auto-populated 48 with an echo command. 49 50#. Focus the **Python Repl** and then press :kbd:`Enter` to execute 51 the pre-populated echo command. 52 53 .. tip:: 54 55 If you're curious about how snippets are implemented, take a look 56 at ``//.pw_console.yaml``. Notice the ``snippets`` entries. Each 57 of these is an automated workflow that can be run in ``pw_console``. 58 59#. Press :kbd:`Ctrl+D` twice to close ``pw_console``. 60 61The following video is a demonstration of snippets: 62 63.. raw:: html 64 65 <video preload="metadata" style="width: 100%; height: auto;" controls> 66 <source type="video/webm" 67 src="https://storage.googleapis.com/pigweed-media/airmaranth/snippets.webm#t=14.0"/> 68 </video> 69 70.. _showcase-sense-tutorial-automate-scripts: 71 72-------------- 73Python scripts 74-------------- 75For a long, complex workflow, you may prefer encapsulating 76everything into a script. 77 78#. Run the script: 79 80 .. tab-set:: 81 82 .. tab-item:: VS Code 83 :sync: vsc 84 85 In **Bazel Build Targets** expand **//tools**, then 86 right-click **:example_script (py_binary)**, then select 87 **Run target**. 88 89 A terminal launches and executes the script contained in 90 ``//tools/sense/example_script.py``. 91 92 .. tab-item:: CLI 93 :sync: cli 94 95 Run the following command: 96 97 .. code-block:: console 98 99 $ bazelisk run //tools:example_script 100 101.. admonition:: Troubleshooting 102 103 If the script fails: make sure that you closed the ``pw_console`` 104 instance from the last section before attempting this section. 105 106You should see output similar to this: 107 108.. code-block:: console 109 110 20241221 08:20:18 INF Using serial port: /dev/ttyACM0 111 20241221 08:20:18 DBG Starting read process 112 20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.log.Logs.Listen, call_id=1) 113 20241221 08:20:18 DBG Using selector: EpollSelector 114 20241221 08:20:18 INF Calling Echo(msg="Hello") 115 20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2) 116 20241221 08:20:18 DBG PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2) received response: msg: "Hello" 117 118 20241221 08:20:18 INF PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=2) completed: Status.OK 119 The status was Status.OK 120 The message was Hello 121 20241221 08:20:18 INF Calling Echo(msg="Goodbye!") 122 20241221 08:20:18 DBG Starting PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3) 123 20241221 08:20:18 DBG PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3) received response: msg: "Goodbye!" 124 125 20241221 08:20:18 INF PendingRpc(channel=1, method=pw.rpc.EchoService.Echo, call_id=3) completed: Status.OK 126 Status.OK: msg: "Goodbye!" 127 128 20241221 08:20:18 DBG Stopping read process 129 130Towards the end of the output you can see the echoed message and the 131printed status information. 132 133.. tip:: 134 135 Take a look at ``//tools/sense/example_script.py`` if you're 136 curious about how this script is implemented. 137 138.. _showcase-sense-tutorial-automate-summary: 139 140------- 141Summary 142------- 143Gone are the days of ad hoc development workflows that some 144teammates benefit from and others don't. With Pigweed, these 145common workflows become explicit, centralized, and shareable, 146and they're checked in alongside the rest of the project's 147code. 148 149Next, head over to :ref:`showcase-sense-tutorial-webapp` to try 150interacting with your Pico through a web app. 151