Skip to content

Commit 2238d4d

Browse files
committed
Add initial Toit SDK info
SW-786
1 parent 5049fa7 commit 2238d4d

File tree

6 files changed

+222
-3
lines changed

6 files changed

+222
-3
lines changed

.vitepress/config.mts

+23
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export default withMermaid(defineConfig({
278278
},
279279
{
280280
text: 'Structure',
281+
collapsed: true,
281282
link: '/devices/api/structure',
282283
items: [
283284
{
@@ -300,11 +301,13 @@ export default withMermaid(defineConfig({
300301
},
301302
{
302303
text: 'Messages',
304+
collapsed: true,
303305
link: '/devices/api/messages',
304306
items: protocolMenuItems,
305307
},
306308
{
307309
text: 'Tools',
310+
collapsed: true,
308311
items: [
309312
{
310313
text: 'Generate',
@@ -320,6 +323,26 @@ export default withMermaid(defineConfig({
320323
},
321324
]
322325
},
326+
{
327+
text: 'SDKs',
328+
collapsed: true,
329+
items: [
330+
{
331+
text: 'Toit',
332+
link: '/devices/api/sdks/toit/',
333+
items: [
334+
{ text: 'Getting Started', link: '/devices/api/sdks/toit/getting-started' },
335+
{
336+
text: 'Examples',
337+
link: '/devices/api/sdks/toit/examples',
338+
items: [
339+
{ text: 'EInk Hello World', link: '/devices/api/sdks/toit/examples/eink' },
340+
]
341+
},
342+
],
343+
},
344+
]
345+
},
323346
]
324347
},
325348
],

devices/api/index.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ The protocol builds on top of existing Lightbug Protocols, and is designed to be
2222
- Easy to use and understand
2323
- Usable in a variety of settings, without complex dependencies
2424

25-
## Example
25+
## API Access
2626

27-
An example prefixed and minimal message might look as follows:
27+
API access depends on the device and may not be supported on all models.
28+
29+
Typically, it can be accessed via UART, I2C, or a UDP network connection.
30+
31+
For access details, please [contact our support team](https://lightbug.io/contact/).
32+
33+
Or you can get started with Toit quickly using the Lightbug provided SDK.
34+
35+
## Example Message
36+
37+
An example [prefixed](structure#prefix) and minimal message might look as follows:
2838

2939
| Format | Message |
3040
| ------ | --- |
3141
| Bytes | 76 66 3 11 0 1 0 0 0 0 0 75 190 |
3242
| Hex | `4c 42 03 0b 00 01 00 00 00 00 00 4b be` |
33-
| Hex (0x notation) | `0x4c 0x42 0x03 0x0b 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x4b 0xbe` |
3443

3544
<ProtocolBytes
3645
byteString="76 66 3 11 0 1 0 0 0 0 0 75 190"
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Eink Hello World
2+
3+
The latest version of the eink example can be [found on GitHub](https://github.com/lightbug-io/toit-lightbug/blob/main/examples/eink.toit).
4+
5+
6+
## Output
7+
8+
When the program is running, you should see some output through the `monitor` command that looks something like this:
9+
10+
```
11+
[lb-comms] INFO: Comms starting
12+
[lb-comms] INFO: Comms started
13+
💬 Sending text page to device
14+
💬 Text page sent
15+
Waiting on message latch
16+
✅ Text page ACKed
17+
Latch response: Message type: 5 length: 40 response-to: 3405447839
18+
```
19+
20+
## Device
21+
22+
The device EInk screen will update, displaying the text that was included in the message.
23+
24+
## Code
25+
26+
First, the required dependencies are [imported](https://docs.toit.io/language/imports).
27+
28+
This primarily includes things from the `lightbug` package, but also the [log library](https://libs.toit.io/log/library-summary) that is part of the toit SDK.
29+
30+
```toit
31+
import lightbug.devices as devices
32+
import lightbug.services as services
33+
import lightbug.messages as messages
34+
import log
35+
```
36+
37+
A `main` function is defined, which is the entry point of the toit program.
38+
39+
Here we start setting up the application, such as the log level to use, the device to use, and the communication service.
40+
41+
```toit
42+
main:
43+
// Setup the Toit logger with the INFO log level
44+
log.set-default (log.default.with-level log.INFO-LEVEL)
45+
46+
// This example is setup to work with the RH2 device
47+
device := devices.RtkHandheld2
48+
49+
// Setup the comms service, which allows communication with the Lightbug device
50+
comms := services.Comms --device=device
51+
```
52+
53+
We then send a single message to the device, which is a [text page](./../../../messages/10009-text-page.md).
54+
55+
The `send` function is used to send the message, and it can take a number of optional parameters that allow hooking into the message lifecycle.
56+
57+
```toit
58+
// Send a basic text page to the device to display
59+
// https://docs.lightbug.io/devices/api/tools/parse?bytes=03%2C57%2C00%2C19%2C27%2C02%2C00%2C05%2C01%2C01%2C01%2C04%2C7f%2C0e%2C60%2C9b%2C05%2C00%2C03%2C04%2C06%2C65%2C66%2C02%2Cd1%2C07%2C0b%2C48%2C65%2C6c%2C6c%2C6f%2C20%2C77%2C6f%2C72%2C6c%2C64%2C01%2C02%2C1f%2C57%2C65%2C6c%2C63%2C6f%2C6d%2C65%2C20%2C74%2C6f%2C20%2C79%2C6f%2C75%2C72%2C20%2C4c%2C69%2C67%2C68%2C74%2C62%2C75%2C67%2C20%2C64%2C65%2C76%2C69%2C63%2C65%2C0c%2C72%2C75%2C6e%2C6e%2C69%2C6e%2C67%2C20%2C54%2C6f%2C69%2C74%2Cb8%2C71
60+
latch := comms.send (messages.TextPage.to-msg
61+
--page-id=2001
62+
--page-title="Hello world"
63+
--line2="Welcome to your Lightbug device"
64+
--line3="running Toit"
65+
--redraw-type=2 // FullRedraw
66+
)
67+
--now=true
68+
--withLatch=true
69+
--preSend=(:: print "💬 Sending text page to device")
70+
--postSend=(:: print "💬 Text page sent")
71+
--onAck=(:: print "✅ Text page ACKed")
72+
--onNack=(:: print "❌ Text page NACKed")
73+
--onError=(:: print "❌ Text page error")
74+
```
75+
76+
The code then waits for the message to be responded to, printing the response to the console.
77+
78+
See [Latch](https://docs.toit.io/language/tasks/#latch) from the Toit [monitor library](https://libs.toit.io/monitor/library-summary).
79+
80+
```toit
81+
print "Waiting on message latch"
82+
response := latch.get
83+
// The value of the latch will be false on timeout or unknown error, or the msg object for the response
84+
// Such as https://docs.lightbug.io/devices/api/tools/parse?bytes=03%2C28%2C00%2C05%2C00%2C04%2C00%2C03%2C04%2C01%2C02%2C04%2C7f%2C0e%2C60%2C9b%2C01%2C00%2C04%2Cd9%2C16%2C00%2C00%2C08%2Cb3%2Cd5%2C9b%2C00%2C00%2C00%2C00%2C00%2C01%2C00%2C01%2C02%2C19%2C27%2Ca9%2C0c
85+
print "Latch response: $response"
86+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Examples
2+
3+
The lightbug pkg comes with a variety of examples that can be used to get started with the Lightbug device API.
4+
5+
These are found in the `examples` directory of the pkg, which itself is a standalone Toit package.
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
# Getting started
3+
4+
### 1. Get an ESP32 enabled device
5+
6+
ESP32s are being introduced on some Lightbug devices.
7+
8+
You'll need one of these devices to run Toit on a Lightbug device.
9+
10+
### 2. Install Jaguar
11+
12+
[Jaguar](https://github.com/toitlang/jaguar) is a small Toit application that runs on your ESP32 which comes with a command line tool that allows you to update and restart your ESP32 code written in Toit over WiFi, which is ideal for fast development.
13+
14+
Install Jaguar, [following step 2](https://docs.toit.io/getstarted/device/#2-install-jaguar) in the Toit getting started guide.
15+
16+
::: info
17+
We currently require `v1.50.3`+ of Jaguar, which bundles at least `v2.0.0-alpha.179` of Toit.
18+
:::
19+
20+
Once installed, you should be able to run the version command:
21+
22+
```sh
23+
jag version
24+
Version: v1.50.3
25+
SDK version: v2.0.0-alpha.179
26+
Build date: 2025-04-31T10:10:10Z
27+
```
28+
29+
### 3. Flash the ESP
30+
31+
Now it is time to connect your ESP enabled Lightbug device with a USB cable to your computer, and flash the ESP32 with the Jaguar firmware.
32+
33+
::: info
34+
All ESP enabled Lightbug devices currently have ESP32C6 chips, so `--chip esp32c6` is required for flashing.
35+
:::
36+
37+
Running `jag flash --chip esp32c6` will ask you for the serial port to use, and the WiFi credentials, but be aware that the tooling requires [permission to access your serial port](https://github.com/toitlang/jaguar#permission-to-access-serial-port).
38+
39+
It's possible to configure default WiFi credentials, see the [Toit guide](https://docs.toit.io/getstarted/device/#3-flash-your-device) for more info.
40+
41+
After flashing, your device boots up and starts the Toit virtual machine. The Jaguar service on the device starts a small HTTP server that listens for incoming requests. You can follow along by monitoring the serial output of the device:
42+
43+
```sh
44+
jag monitor
45+
```
46+
47+
### 4. Start a project
48+
49+
Create a new directory for your project, and run the following command to create a new Toit project:
50+
51+
```sh
52+
mkdir my-lightbug-project
53+
cd my-lightbug-project
54+
jag pkg init
55+
jag pkg install github.com/lightbug-io/toit-lightbug
56+
```
57+
58+
You can copy the example [eink based Hello World application](https://github.com/lightbug-io/toit-lightbug/blob/main/examples/eink.toit) for the RTK Handheld 2 device into your project with the following command:
59+
60+
```sh
61+
cp ./.packages/github.com/lightbug-io/toit-lightbug/0.7.0/examples/eink.toit ./main.toit
62+
```
63+
64+
### 5. Run the code
65+
66+
Use the `jag run` command to run the code on your device.
67+
68+
```sh
69+
jag run ./main.toit
70+
```
71+
72+
If your device can not be found automatically, you can specify the device IP address in the command line with the `--device` flag.
73+
74+
Once the code is running, you should see some output through the `monitor` command you ran earlier.
75+
76+
```
77+
[jaguar] INFO: program cf533602-d549-c9bd-34a3-3ae1093bda51 started
78+
[lb-comms] INFO: Comms starting
79+
[lb-comms] INFO: Comms started
80+
💬 Sending text page to device
81+
💬 Text page sent
82+
Waiting on message latch
83+
✅ Text page ACKed
84+
Latch response: Message type: 5 length: 40 response-to: 3405447839
85+
```
86+
87+
And you should see the device screen update

devices/api/sdks/toit/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Toit
2+
3+
[Toit](https://toitlang.org/) is a modern high-level open source object oriented language designed for microcontrollers and the Internet of Things.
4+
5+
[Jaguar](https://github.com/toitlang/jaguar) is a small Toit application that runs on your ESP32 which comes with a command line tool that allows you to update and restart your ESP32 code written in Toit over WiFi, which is ideal for fast development.
6+
7+
Lightbug provides a [Toit package](https://pkg.toit.io/package/github.com%2Flightbug-io%2Ftoit-lightbug) that includes a set of bindings to Lightbug devices and the [Lightbug device API](./../../).
8+
9+
Together, these tools allow you to quickly develop and deploy applications on Lightbug devices using Toit.

0 commit comments

Comments
 (0)