xref: /aosp_15_r20/external/pigweed/docs/showcases/sense/tutorial/automate.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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#. In ``pw_console`` click **File**.
20
21   .. Add a screenshot here that makes it easier to find File menu
22
23#. Click **Insert Repl Snippet**.
24
25#. Select **Echo RPC** with your keyboard and then press :kbd:`Enter`.
26
27   .. admonition:: Troubleshooting
28
29      If clicking **Echo RPC** doesn't work, try the keyboard-based
30      workflow.
31
32   You should see the **Python Repl** input prompt get auto-populated
33   with an echo command.
34
35#. Focus the **Python Repl** and then press :kbd:`Enter` to execute
36   the pre-populated echo command.
37
38#. If your curious about how snippets are implemented, take a look
39   at ``//.pw_console.yaml``. Notice the ``snippets`` entries. Each
40   of these is an automated workflow that can be run in ``pw_console``.
41
42#. Press :kbd:`Ctrl+D` twice to close ``pw_console``.
43
44The following video is a demonstration of snippets:
45
46.. raw:: html
47
48   <video preload="metadata" style="width: 100%; height: auto;" controls>
49     <source type="video/webm"
50             src="https://storage.googleapis.com/pigweed-media/airmaranth/snippets.webm#t=14.0"/>
51   </video>
52
53.. _showcase-sense-tutorial-automate-scripts:
54
55--------------
56Python scripts
57--------------
58For a long, complex workflow, you may prefer encapsulating
59the workflow into its own script.
60
61#. Run the script:
62
63   .. tab-set::
64
65      .. tab-item:: VS Code
66         :sync: vsc
67
68         In **Bazel Build Targets** expand **//tools**, then
69         right-click **:example_script (py_binary)**, then select
70         **Run target**.
71
72         A terminal launches and executes the script contained in
73         ``//tools/sense/example_script.py``.
74
75      .. tab-item:: CLI
76         :sync: cli
77
78         Run the following command:
79
80         .. code-block:: console
81
82            $ bazelisk run //tools:example_script
83
84.. admonition:: Troubleshooting
85
86   If the script fails: make sure that you close the ``pw_console``
87   from the last section before attempting this section.
88
89You should see output similar to this:
90
91.. code-block:: console
92
93   # ...
94   The status was Status.OK
95   The message was Hello
96   20240717 18:26:25 INF Calling Echo(msg="Goodbye!")
97   20240717 18:26:25 DBG Starting PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3)
98   20240717 18:26:25 DBG PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) finished with status Status.OK
99   20240717 18:26:25 DBG PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) received response: msg: "Goodbye!"
100
101   20240717 18:26:25 INF PendingRpc(channel_id=1, service_id=352047186, method_id=2336689897, call_id=3) completed: Status.OK
102   Status.OK: msg: "Goodbye!"
103
104Towards the end of the output you can see the echoed message and the
105printed status information.
106
107Take a look at ``//tools/sense/example_script.py`` if you're
108curious about how this script is implemented.
109
110.. _showcase-sense-tutorial-automate-summary:
111
112-------
113Summary
114-------
115Gone are the days of ad hoc development workflows that some
116teammates benefit from and others don't. With Pigweed, these
117common workflows become explicit, centralized, and shareable,
118and they're checked in alongside the rest of the project's
119code.
120
121Next, head over to :ref:`showcase-sense-tutorial-webapp` to try
122interacting with your Pico through a web app.
123