Bus
#struct Bus| Name | Type | Description |
|---|---|---|
id | int = 0 |
Standard library · Hardware
The root of the hardware packages, with the version of the hardware API.
import hwhw is the root of the hardware packages. It holds one constant, API_LEVEL, the version of the hardware API. To tell a real board from the simulated one a normal computer gets, ask board.Simulated. The packages hw.board, hw.gpio, hw.i2c, hw.uart, hw.ds1307, hw.ds18b20, hw.dht, hw.spi, hw.hcsr04, hw.pir, hw.wifi and hw.ble sit beside it.
The packages under hw (hw.gpio, hw.i2c, hw.uart and the device drivers) talk to the pins through hw.board. On a freestanding build that layer calls the board runtime; on a normal computer it is a simulation kept in memory, so the same source compiles and runs in both places. DelayUs only advances the simulated clock on the computer, so a blink loop finishes at once.
import hw
import hw.board
import hw.gpio
func blink(led: gpio.Pin, times: int):
repeat times:
gpio.Write(led, gpio.HIGH)
board.DelayUs(500000 as u64)
gpio.Write(led, gpio.LOW)
board.DelayUs(500000 as u64)
end
end
func main:
led := gpio.Open(13, gpio.OUTPUT)
blink(led, 2)
println hw.API_LEVEL, gpio.Read(led)
end1 0
Use board.Simulated to skip things that make sense only on a real board, such as waiting for a person to press a button, and to feed the simulation inputs in tests: board.SimSetPin, board.SimSetPulse and board.SimInject do nothing on hardware.
import hw.board
import hw.pir
func main:
if not board.Simulated():
println "real board"
ret
end
sensor := pir.Open(7)
println pir.Motion(sensor)
board.SimSetPin(7, 1)
println pir.Motion(sensor)
endfalse true
struct Bus| Name | Type | Description |
|---|---|---|
id | int = 0 |
struct Sensor| Name | Type | Description |
|---|---|---|
pin | int = 0 | |
kind | int = 22 |
struct Reading| Name | Type | Description |
|---|---|---|
status | int = 1 | |
humidity | f64 = -1000 | |
temperature | f64 = -1000 |
struct Sensor| Name | Type | Description |
|---|---|---|
trigger | int = 0 | |
echo | int = 0 |
struct Port| Name | Type | Description |
|---|---|---|
number | int = 0 | |
baud | int = 115200 | |
data_bits | int = 8 | |
parity | int = 0 | |
stop_bits | int = 1 |
enum State| Name | Type | Description |
|---|---|---|
Disconnected | | |
Connecting | | |
Connected | | |
Failed | |
enum Security| Name | Type | Description |
|---|---|---|
Open | | |
Wep | | |
Wpa | | |
Wpa2 | | |
Wpa3 | |
struct Network| Name | Type | Description |
|---|---|---|
ssid | str = "" | |
bssid | str = "" | |
rssi | int = 0 | |
channel | int = 0 | |
security | Security |
struct Socket| Name | Type | Description |
|---|---|---|
id | int = -1 | |
ok | bool = false |
struct Sensor| Name | Type | Description |
|---|---|---|
pin | int = 0 | |
rom | u64 = 0 | |
addressed | bool = false |
struct Port| Name | Type | Description |
|---|---|---|
path | str = "" | |
handle | i64 = -1 | |
ok | bool = false | |
baud | int = 0 |
struct Pin| Name | Type | Description |
|---|---|---|
number | int = 0 | |
mode | int = 0 |
struct Bus| Name | Type | Description |
|---|---|---|
sck | int = 0 | |
mosi | int = 0 | |
miso | int = 0 | |
cs | int = -1 | |
mode | int = 0 | |
order | int = 0 | |
half_us | int = 0 |
struct Sensor| Name | Type | Description |
|---|---|---|
pin | int = 0 | |
active_high | bool = true |
struct Device| Name | Type | Description |
|---|---|---|
name | str = "" | |
address | str = "" | |
rssi | int = 0 |
struct Service| Name | Type | Description |
|---|---|---|
handle | int = -1 | |
uuid | str = "" | |
ok | bool = false |
struct Characteristic| Name | Type | Description |
|---|---|---|
handle | int = -1 | |
uuid | str = "" | |
props | int = 0 | |
ok | bool = false |
struct Event| Name | Type | Description |
|---|---|---|
handle | int = -1 | |
uuid | str = "" | |
value | Vector<u8> | |
ok | bool = false |
struct Reading| Name | Type | Description |
|---|---|---|
year | int = 2000 | |
month | int = 1 | |
day | int = 1 | |
weekday | int = 1 | |
hour | int = 0 | |
minute | int = 0 | |
second | int = 0 |
struct Device| Name | Type | Description |
|---|---|---|
bus | i2c.Bus |
const MAX_PINS: i32 = 64const DHT11: i32 = 11const DHT22: i32 = 22const READ_OK: i32 = 0const READ_TIMEOUT: i32 = 1const READ_CHECKSUM: i32 = 2const ERROR_VALUE: f64 = -1000const MAX_ECHO_US: i32 = 38000const PARITY_NONE: i32 = 0const PARITY_EVEN: i32 = 1const PARITY_ODD: i32 = 2const API_LEVEL: i32 = 1The version of the hardware API, 1 for now. It grows when the meaning of an existing function changes, so a program can refuse a package it was not written for.
import hw
func main:
println "hardware API level", hw.API_LEVEL
endhardware API level 1
const max_scan: i32 = 32const state_disconnected: i32 = 0const state_connecting: i32 = 1const state_connected: i32 = 2const state_failed: i32 = 3const security_open: i32 = 0const security_wep: i32 = 1const security_wpa: i32 = 2const security_wpa2: i32 = 3const security_wpa3: i32 = 4const FAMILY: i32 = 40const ERROR_C: f64 = -1000const PARITY_NONE: i32 = 0const PARITY_EVEN: i32 = 1const PARITY_ODD: i32 = 2const INPUT: i32 = 0const OUTPUT: i32 = 1const INPUT_PULLUP: i32 = 2const LOW: i32 = 0const HIGH: i32 = 1const MSB_FIRST: i32 = 0const LSB_FIRST: i32 = 1const max_scan: i32 = 32const max_value: i32 = 512const prop_read: i32 = 1const prop_write: i32 = 2const prop_notify: i32 = 4const ADDRESS: i32 = 104func Simulated(): boolfunc GpioMode(pin: int, mode: int)func GpioWrite(pin: int, level: int)func GpioRead(pin: int): intfunc Micros(): u64func DelayUs(us: u64)func PulseIn(pin: int, level: int, timeout_us: u64): u64func SimSetPulse(pin: int, width_us: u64)func I2cWrite(bus: int, addr: int, reg: int, value: int): intfunc I2cRead(bus: int, addr: int, reg: int): intfunc UartConfig(port: int, baud: int)func UartWrite(port: int, value: int)func UartRead(port: int): intfunc UartPeek(port: int): boolfunc SimInject(port: int, value: int)func SimTaken(port: int): intfunc SimSetPin(pin: int, level: int)func SimOneWire(pin: int, serial: u64)func SimTemperature(milli_celsius: int)func SimDht(pin: int, kind: int, humidity_tenths: int, temperature_tenths: int)func SimDhtBadChecksum(bad: bool)func SimLink(source: int, reader: int)func WifiCall(op: int, a: int, b: int, c: int): intfunc WifiCallStr(op: int, s: str, t: str, a: int): intfunc BleCall(op: int, a: int, b: int, c: int): intfunc BleCallStr(op: int, s: str, a: int, b: int): intfunc Open(id: int): Busfunc WriteReg(b: Bus, addr: int, reg: int, value: int): boolfunc ReadReg(b: Bus, addr: int, reg: int): intfunc Present(b: Bus, addr: int): boolfunc ReadBlock(b: Bus, addr: int, reg: int, n: int): Vector<int>func WriteBlock(b: Bus, addr: int, reg: int, data: Vector<int>): boolfunc Open(pin: int, kind: int): Sensorfunc Read(s: Sensor): Readingfunc Valid(r: Reading): boolfunc Status(r: Reading): intfunc Humidity(r: Reading): f64func Temperature(r: Reading): f64func ReadHumidity(s: Sensor): f64func ReadTemperature(s: Sensor): f64func Open(trigger: int, echo: int): Sensorfunc EchoMicros(s: Sensor): u64func DistanceMm(s: Sensor): intfunc DistanceCm(s: Sensor): f64func Open(number: int, baud: int): Portfunc WriteByte(p: Port, value: int)func WriteStr(p: Port, text: str)func ReadByte(p: Port): intfunc Available(p: Port): boolfunc ReadLine(p: Port): strfunc Init(): boolfunc Ready(): boolfunc Scan(): Vector<Network>func Connect(ssid: str, password: str, timeout_ms: int): boolfunc Disconnect()func Status(): Statefunc IsConnected(): boolfunc Ip(): strfunc Rssi(): intfunc Mac(): strfunc StartAccessPoint(ssid: str, password: str, channel: int): boolfunc TcpOpen(host: str, port: int): Socketfunc TcpSend(s: Socket, data: str): boolfunc TcpReceive(s: Socket, timeout_ms: int): strfunc TcpClose(s: Socket)func Ssid(n: Network): strfunc Bssid(n: Network): strfunc Signal(n: Network): intfunc Channel(n: Network): intfunc SecurityOf(n: Network): Securityfunc SecurityCode(n: Network): intfunc StateCode(): intfunc SocketOk(s: Socket): boolfunc SocketId(s: Socket): intfunc SimReset()func SimAddNetwork(ssid: str, bssid: str, rssi: int, channel: int, security: int)func SimSetPassword(ssid: str, password: str)func SimSetIp(ip: str)func SimSetDelay(ms: int)func SimDrop()func SimSetReply(host: str, port: int, data: str)func SimSent(s: Socket): strfunc SimAccessPoint(): strfunc Open(pin: int): Sensorfunc OpenRom(pin: int, rom: u64): Sensorfunc Crc8(data: Vector<int>): intfunc Detected(s: Sensor): boolfunc ReadRom(s: Sensor): u64func Scratchpad(s: Sensor): Vector<int>func Resolution(s: Sensor): intfunc SetResolution(s: Sensor, bits: int): boolfunc Start(s: Sensor): boolfunc Ready(s: Sensor): boolfunc WaitReady(s: Sensor, timeout_ms: int): boolfunc RawToCelsius(raw: int): f64func LastCelsius(s: Sensor): f64func ReadCelsius(s: Sensor): f64func IsOpen(p: Port): boolfunc Supported(): boolfunc Valid(baud: int, data_bits: int, parity: int, stop_bits: int): boolfunc OpenFormat(path: str, baud: int, data_bits: int, parity: int, stop_bits: int): Portfunc Open(path: str, baud: int): Portfunc Close(p &: Port)func WriteBytes(p: Port, data: void*, n: int): boolfunc Write(p: Port, text: str): boolfunc WriteByte(p: Port, value: int): boolfunc ReadByte(p: Port, timeout_ms: int): intfunc ReadLine(p: Port, timeout_ms: int): strfunc Flush(p: Port)func Open(number: int, mode: int): Pinfunc SetMode(p &: Pin, mode: int)func Write(p: Pin, level: int)func Read(p: Pin): intfunc Toggle(p: Pin)func IsHigh(p: Pin): boolfunc Open(sck: int, mosi: int, miso: int, cs: int, mode: int): Busfunc SetSpeed(b &: Bus, half_us: int)func SetOrder(b &: Bus, order: int)func Mode(b: Bus): intfunc Select(b: Bus)func Deselect(b: Bus)func Transfer(b: Bus, value: int): intfunc TransferBuffer(b: Bus, data: Vector<int>): Vector<int>func Exchange(b: Bus, data: Vector<int>): Vector<int>func Open(pin: int): Sensorfunc OpenInverted(pin: int): Sensorfunc Motion(s: Sensor): boolfunc WaitMotion(s: Sensor, timeout_ms: int): boolfunc ValidUuid(uuid: str): boolfunc Init(name: str): boolfunc Ready(): boolfunc Advertise(enable: bool): boolfunc Advertising(): boolfunc Scan(timeout_ms: int): Vector<Device>func AddService(uuid: str): Servicefunc AddCharacteristic(service: Service, uuid: str, props: int, value: Vector<u8>): Characteristicfunc SetValue(c: Characteristic, value: Vector<u8>): boolfunc Value(c: Characteristic): Vector<u8>func Notify(c: Characteristic): boolfunc Poll(): boolfunc Take(): Eventfunc Connect(address: str): boolfunc Disconnect()func IsConnected(): boolfunc Read(uuid: str): Vector<u8>func Write(uuid: str, value: Vector<u8>): boolfunc DeviceName(d: Device): strfunc DeviceAddress(d: Device): strfunc DeviceRssi(d: Device): intfunc EventUuid(e: Event): strfunc EventValue(e: Event): Vector<u8>func EventOk(e: Event): boolfunc ServiceOk(s: Service): boolfunc CharacteristicOk(c: Characteristic): boolfunc SimReset()func SimAddBleDevice(name: str, address: str, rssi: int)func SimSetRemoteValue(address: str, uuid: str, value: Vector<u8>)func SimRemoteWritten(address: str, uuid: str): Vector<u8>func SimInjectWrite(c: Characteristic, value: Vector<u8>): boolfunc SimSetClient(connected: bool)func SimNotified(): intfunc Make(year: int, month: int, day: int, weekday: int, hour: int, minute: int, second: int): Readingfunc Format(r: Reading): strfunc Open(bus: i2c.Bus): Devicefunc Detected(d: Device): boolfunc Running(d: Device): boolfunc Set(d: Device, r: Reading): boolfunc Halt(d: Device): boolfunc Now(d: Device): Reading