Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
type: boolean

env:
TOIT_VERSION: v2.0.0-alpha.148
TOIT_VERSION: v2.0.0-alpha.197
BUILD_DIR: public
HAS_PROTOBUF: false
RUN_TESTS: true
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
run: |
export TOIT_SDK=${{ steps.setup-toit.outputs.toit-install-dir }}/toit
cd tools
$TOIT_SDK/bin/toit.pkg install
$TOIT_SDK/bin/toit pkg install
./run_all.sh

ci:
Expand Down
23 changes: 11 additions & 12 deletions docs/language/sdk/display.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ pins and bus you are using to connect your display.
Example contents of `get_display.toit`
<!-- CODE FILENAME get_display.toit -->
```
import gpio
import i2c
import ssd1306 show *
import pixel-display show *

get-display -> PixelDisplay:
scl := gpio.Pin 4
sda := gpio.Pin 5
scl := 4
sda := 5
bus := i2c.Bus
--sda=sda
--scl=scl
--frequency=800_000
--sda=sda
--scl=scl
--frequency=800_000

devices := bus.scan
if not devices.contains Ssd1306.I2C-ADDRESS:
Expand Down Expand Up @@ -155,14 +154,14 @@ main:
display/PixelDisplay ::= get-display

sans-10 := Style
--font = SANS-10-BOLD
--color = BLACK
--align-right
--font = SANS-10-BOLD
--color = BLACK
--align-right

sans-24 := Style
--font = SANS-24-FONT
--color = BLACK
--align-center
--font = SANS-24-FONT
--color = BLACK
--align-center

[
Label --style=sans-10 --x=40 --y=30 --text="Hello from",
Expand Down
2 changes: 1 addition & 1 deletion docs/language/syntax.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,6 @@ in the first clause of a `for`, `if`, or `while` statement.
while data := reader.read:
// data was not null.

if parsed := (int.parse str --on-error=: null):
if parsed := (int.parse str --if-error=: null):
// parsed is not null and thus a valid number.
```
3 changes: 1 addition & 2 deletions docs/language/tasks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ The advantage of tasks in comparison with threads is that they take turns. To
take an oversimplified example:

```
import gpio
import gpio.adc as gpio

// The analog input is connected to pin 17.
ADC1 ::= gpio.Adc (gpio.Pin.in 17)
ADC1 ::= gpio.Adc 17

class VoltageStatus:
mv /int := 0
Expand Down
4 changes: 2 additions & 2 deletions docs/language/typeconversion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ main:
s7 /string := "$(%0.3f 1.2) $(%0.2f PI)"
print s7 // => "1.200 3.14"
//or
print (f.stringify 3)
print (PI.stringify 2)
print (f.to-string --precision=3)
print (PI.to-string --precision=2)

// Convert an integer to a floating point number
f2 /float := i.to-float
Expand Down
10 changes: 4 additions & 6 deletions docs/peripherals/drivers/bme280.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ A detailed description of adding sensors using the Qwiic cable can be found [her
First we're going to set up the I<sup>2</sup>C bus so we can connect the device:

```
import gpio
import i2c

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22
```

With the bus configured, we can specify how to address the BME280 device on the bus.
Expand Down Expand Up @@ -95,14 +94,13 @@ Program measuring temperature, relative humidity, and atmospheric pressure
with a BME280.
*/

import gpio
import i2c
import bme280

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22

device := bus.device bme280.I2C-ADDRESS-ALT

Expand Down
10 changes: 4 additions & 6 deletions docs/peripherals/drivers/sparkfun_joystick.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ The Joystick features an `ATtiny85` microcontroller with a custom firmware. As d
We use a simple I<sup>2</sup>C setup, with pin `21` for `SDA` (blue) and pin `22` for `SCL` (yellow).

```
import gpio
import i2c

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22
```

## Driver skeleton
Expand Down Expand Up @@ -264,15 +263,14 @@ class Joystick:
**`main.toit`**

```
import gpio
import i2c

import .qwiic-joystick

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22

device := bus.device Joystick.I2C-ADDRESS

Expand Down
6 changes: 2 additions & 4 deletions docs/peripherals/gpio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@ The ADC (analog-to-digital converter) can be used by importing `gpio.adc`:
For example:

```
import gpio
import gpio.adc show Adc

main:
adc := Adc (gpio.Pin 34)
adc := Adc 34
print adc.get
adc.close
```
Expand All @@ -172,11 +171,10 @@ pulse width.
Example:

```
import gpio
import gpio.pwm

main:
led := gpio.Pin 5
led := 5
generator := pwm.Pwm --frequency=400
channel := generator.start led
duty-percent := 0
Expand Down
16 changes: 7 additions & 9 deletions docs/peripherals/i2c.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
Toit exposes the peripheral through the [`i2c` library](https://libs.toit.io/i2c/library-summary).

```
import gpio
import i2c

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22
```

Each device must have a unique address. The address can be found in the datasheet for the selected peripheral - it's an 7-bit integer.

In case of the Bosch [BME280 sensor](https://cdn.sparkfun.com/assets/e/7/3/b/1/BME280_Datasheet.pdf), the address is `0x76`:

```
import gpio
import i2c

main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--sda=21
--scl=22

device := bus.device 0x76
```
Expand All @@ -36,9 +34,9 @@ The default frequency of the I<sup>2</sup>C bus is 400kHz in Toit. This can be c

```
i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
--frequency=100000
--sda=21
--scl=22
--frequency=100000
```

Remember that when changing the frequency, the associated pull-up resistors may have to be changed as well.
18 changes: 8 additions & 10 deletions docs/peripherals/spi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@
Toit exposes the peripheral through the [`spi` library](https://libs.toit.io/spi/library-summary).

```
import gpio
import spi

main:
bus := spi.Bus
--miso=gpio.Pin 12
--mosi=gpio.Pin 13
--clock=gpio.Pin 14
--miso=12
--mosi=13
--clock=14
```

When communicating with a device, a unique chip-select Pin is used to signal when the chip in question is addressed. In addition, it's important to not select a device frequency that exceeds the capabilities of device. The maximum frequency can be found in the datasheet for the selected peripheral.

In case of the Bosch BME280 sensor, the maximum frequency is 10MHz:

```
import gpio
import spi

main:
bus := spi.Bus
--miso=gpio.Pin 12
--mosi=gpio.Pin 13
--clock=gpio.Pin 14
--miso=12
--mosi=13
--clock=14

device := bus.device
--cs=gpio.Pin 15
--frequency=10_000_000
--cs=15
--frequency=10_000_000
```
14 changes: 7 additions & 7 deletions docs/tutorials/ble/advertising.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ main:
adapter := ble.Adapter
peripheral := adapter.peripheral

data := ble.AdvertisementData
data := ble.Advertisement

peripheral.start-advertise data
sleep --ms=10000
Expand All @@ -27,7 +27,7 @@ main:
adapter := ble.Adapter
peripheral := adapter.peripheral

data := ble.AdvertisementData
data := ble.Advertisement
--name="Toit device"

peripheral.start-advertise data
Expand All @@ -50,9 +50,9 @@ main:
adapter := ble.Adapter
peripheral := adapter.peripheral

data := ble.AdvertisementData
data := ble.Advertisement
--name="Toit device"
--service-classes=[BATTERY-SERVICE]
--services=[BATTERY-SERVICE]

peripheral.start-advertise data
sleep --ms=10000
Expand Down Expand Up @@ -82,10 +82,10 @@ main:
adapter := ble.Adapter
peripheral := adapter.peripheral

data := ble.AdvertisementData
data := ble.Advertisement
--name="Toit device"
--service-classes=[BATTERY-SERVICE]
--manufacturer-data=#[0xFF, 0xFF, 't', 'o', 'i', 't']
--services=[BATTERY-SERVICE]
--manufacturer-specific=#['t', 'o', 'i', 't']

peripheral.start-advertise data
sleep --ms=1000000
Expand Down
22 changes: 11 additions & 11 deletions docs/tutorials/ble/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services implemented by the remote device as well as the service characteristics

## Connecting to a remote device

In order for a BLE device to establish a connection to a remote device, the device must know the address. In most cases, the BLE device needs to discover the address which is done by scanning remote devices. The following example shows how to scan for the first remote device that implements a specific service.
In order for a BLE device to establish a connection to a remote device, the device must know its identifier. In most cases, the BLE device needs to discover the identifier by scanning remote devices. The following example shows how to scan for the first remote device that implements a specific service.

```toit
import ble
Expand All @@ -14,12 +14,12 @@ SCAN-DURATION ::= Duration --s=3

find-with-service central/ble.Central service/ble.BleUuid:
central.scan --duration=SCAN-DURATION: | device/ble.RemoteScannedDevice |
if device.data.service-classes.contains service:
return device.address
if device.data.services.contains service:
return device.identifier
throw "no device found"
```

With the address available, the connection can be established. In the following example, we use the `find-with-service` function to find a remote device with an advertised battery service and establish a connection to that device. Note that the device might also support services that are not explicitly advertised.
With the identifier available, the connection can be established. In the following example, we use the `find-with-service` function to find a remote device with an advertised battery service and establish a connection to that device. Note that the device might also support services that are not explicitly advertised.

```toit
// The battery service is defined in the Bluetooth specification.
Expand All @@ -32,8 +32,8 @@ main:
adapter := ble.Adapter
central := adapter.central

address := find-with-service central BATTERY-SERVICE
remote-device := central.connect address
identifier := find-with-service central BATTERY-SERVICE
remote-device := central.connect identifier
```

## Reading service characteristics
Expand All @@ -50,16 +50,16 @@ SCAN-DURATION ::= Duration --s=3

find-with-service central/ble.Central service/ble.BleUuid:
central.scan --duration=SCAN-DURATION: | device/ble.RemoteScannedDevice |
if device.data.service-classes.contains service:
return device.address
if device.data.services.contains service:
return device.identifier
throw "no device found"

main:
adapter := ble.Adapter
central := adapter.central

address := find-with-service central BATTERY-SERVICE
remote-device := central.connect address
identifier := find-with-service central BATTERY-SERVICE
remote-device := central.connect identifier
// Discover the battery service.
services := remote-device.discover-services [BATTERY-SERVICE]
battery-service/ble.RemoteService := services.first
Expand All @@ -72,5 +72,5 @@ main:
value := battery-level-characteristic.read
battery-level := value[0]

print "Battery level of $address: $battery-level%"
print "Battery level of $identifier: $battery-level%"
```
Loading