|
| 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 | +``` |
0 commit comments