Skip to content

Commit c9d2c5c

Browse files
authored
DOCS-3627: Shorten Hello World module guide (#4181)
1 parent f169df6 commit c9d2c5c

File tree

1 file changed

+14
-113
lines changed

1 file changed

+14
-113
lines changed

docs/operate/get-started/other-hardware/hello-world-module.md

Lines changed: 14 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ prev: "/operate/get-started/other-hardware/"
2222

2323
## What this guide covers
2424

25-
This guide will walk you through creating a {{< glossary_tooltip term_id="modular-resource" text="modular" >}} camera component that responds to API calls by returning a configured image.
25+
This guide walks you through creating a {{< glossary_tooltip term_id="modular-resource" text="modular" >}} camera component that returns a configured image.
2626
This guide also includes optional steps to create a modular sensor that returns random numbers, to demonstrate how you can include two modular resources within one {{< glossary_tooltip term_id="module" text="module" >}}.
27-
By the end of this guide, you will be able to create your own modular resources and package them into modules so you can use them on your machines.
27+
By the end, you will know how to create your own modular resources and package them into modules so you can use them on your machines.
2828

2929
{{% alert title="Note" color="note" %}}
3030

31-
This guide provides a basic example for learning purposes.
31+
This guide provides a basic learning example.
3232
For a more comprehensive guide including usage of cloud build tools for deployment across different platforms, see [Integrate other hardware](/operate/get-started/other-hardware/).
3333

3434
{{% /alert %}}
@@ -57,116 +57,17 @@ You can check by running `python3 --version` or `python --version` in your termi
5757

5858
{{< /expand >}}
5959

60-
## Create a test script
60+
## Decide what your module will do
6161

62-
The point of creating a module is to add functionality to your machine.
63-
For the purposes of this guide, you're going to make a module that does two things: It opens an image file from a configured path on your machine, and it returns a random number.
62+
The functionality you want to add to your machine determines the APIs you need to implement, so let's start by deciding what your module will do.
63+
For the purposes of this guide, you're going to make a module that does two things:
6464

65-
1. Find an image you'd like to display when your program runs.
66-
We used [this image of a computer with "hello world" on the screen](https://unsplash.com/photos/a-laptop-computer-sitting-on-top-of-a-wooden-desk-8q6e5hu3Ilc).
67-
Save the image to your computer.
68-
69-
1. Create a test script on your computer and copy the following code into it:
70-
71-
{{< tabs >}}
72-
{{% tab name="Python" %}}
73-
74-
```python {class="line-numbers linkable-line-numbers"}
75-
# test.py opens an image and prints a random number
76-
from PIL import Image
77-
import random
78-
79-
# TODO: Replace path with path to where you saved your photo
80-
photo = Image.open("/Users/jessamyt/Downloads/hello-world.jpg")
81-
82-
photo.show()
83-
84-
number = random.random()
85-
86-
print("Hello, World! The latest random number is ", number, ".")
87-
```
88-
89-
{{% /tab %}}
90-
{{% tab name="Go" %}}
91-
92-
```go {class="line-numbers linkable-line-numbers"}
93-
// test.go opens an image and prints a random number
94-
package main
95-
96-
import (
97-
"os"
98-
"os/exec"
99-
"fmt"
100-
"math/rand"
101-
)
102-
103-
func main() {
104-
105-
// TODO: Replace path string with path to where you saved your photo
106-
imagePath := "/Users/jessamyt/Downloads/hello-world.jpg"
107-
file, err := os.Open(imagePath)
108-
if err != nil {
109-
fmt.Println("Error opening image:", err)
110-
return
111-
}
112-
defer file.Close()
113-
114-
// "open" works on macOS.
115-
// For Linux, replace "open" with "xdg-open".
116-
err = exec.Command("open", imagePath).Start()
117-
if err != nil {
118-
fmt.Println("Error opening image viewer:", err)
119-
}
120-
121-
number := rand.Float64()
122-
fmt.Println("Hello, World! The latest random number is ", number, ".")
123-
}
124-
```
125-
126-
{{% /tab %}}
127-
{{< /tabs >}}
128-
129-
1. Replace the path in the script above with the path to where you saved your photo.
130-
Save the script.
131-
132-
1. Run the test script in your terminal:
133-
134-
{{< tabs >}}
135-
{{% tab name="Python" %}}
136-
137-
It's best practice to use a virtual environment for running Python scripts.
138-
You'll also need to install the dependency Pillow in the virtual environment before running the test script.
139-
140-
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
141-
python3 -m venv .venv
142-
source .venv/bin/activate
143-
pip install Pillow
144-
python3 test.py
145-
```
146-
147-
The image you saved should open on your screen, and a random number should print to your terminal.
148-
149-
In later steps, the module generator will create a new virtual environment with required dependencies, so you can deactivate the one you just ran the test script in:
150-
151-
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
152-
deactivate
153-
```
154-
155-
{{% /tab %}}
156-
{{% tab name="Go" %}}
157-
158-
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
159-
go run test.go
160-
```
161-
162-
The image you saved should open on your screen, and a random number should print to your terminal.
163-
164-
{{% /tab %}}
165-
{{< /tabs >}}
65+
- Opens an image file from a configured path on your machine
66+
- Returns a random number
16667

16768
## Choose an API to implement
16869

169-
Now it's time to decide which Viam [APIs](/dev/reference/apis/#component-apis) make sense for your module.
70+
Let's figure out which Viam [APIs](/dev/reference/apis/#component-apis) make sense for your module.
17071
You need a way to return an image, and you need a way to return a number.
17172

17273
If you look at the [camera API](/dev/reference/apis/components/camera/), you can see the `GetImage` method, which returns an image.
@@ -449,7 +350,7 @@ First, implement the camera API methods by editing the camera class definition:
449350
return pil_to_viam_image(img, CameraMimeType.JPEG)
450351
```
451352
452-
You can leave the rest of the functions not implemented, because this module is not meant to return a point cloud (`get_point_cloud()`), and does not need to return multiple images simultaneously (`get_images()`).
353+
Leave the rest of the functions not implemented, because this module is not meant to return a point cloud (`get_point_cloud()`), and does not need to return multiple images simultaneously (`get_images()`).
453354
454355
Save the file.
455356
@@ -579,7 +480,7 @@ First, implement the camera API methods by editing the camera class definition:
579480
580481
1. Delete the `SubscribeRTP` and `Unsubscribe` methods, since they are not applicable to this camera.
581482
582-
1. You can leave the rest of the functions not implemented, because this module is not meant to return a point cloud (`NextPointCloud`), and does not need to return multiple images simultaneously (`Images`).
483+
1. Leave the rest of the functions not implemented, because this module is not meant to return a point cloud (`NextPointCloud`), and does not need to return multiple images simultaneously (`Images`).
583484
584485
However, you do need to edit the return statements to return empty structs that match the API.
585486
Edit these methods so they look like this:
@@ -824,9 +725,9 @@ viam module upload --version 1.0.0 --platform any .
824725
{{% /tab %}}
825726
{{< /tabs >}}
826727
827-
Now, if you look at the [Viam Registry page](https://app.viam.com/registry) while logged into your account, you'll be able to find your private module listed.
828-
Because the module is now in the registry, you can configure the hello-sensor and hello-camera on your machines just as you would configure other components and services; there's no more need for local module configuration.
829-
The local module configuration is primarily for testing purposes.
728+
Now, if you look at the [Viam Registry page](https://app.viam.com/registry) while logged into your account, you can find your private module listed.
729+
With the module now in the registry, you can configure the hello-sensor and hello-camera on your machines just as you would configure other components and services.
730+
There's no more need for local module configuration; local modules are primarily used for testing.
830731
831732
{{<imgproc src="/how-tos/hello-config.png" resize="x1100" declaredimensions=true alt="The create a component menu open, searching for hello. The hello-camera and hello-sensor components are shown in the search results." style="max-width:500px" class="shadow aligncenter" >}}
832733

0 commit comments

Comments
 (0)