Standard library · Hardware

hw

The root of the hardware packages, with the version of the hardware API.

import hw

hw 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.

One program for the board and the computer

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.

hw-guide-portable.salam
Try it
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)
end
Output
1 0

Telling the two apart

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.

hw-guide-simulated.salam
Try it
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)
end
Output
false
true

Types

Bus

#
struct Bus
NameTypeDescription
idint = 0

Sensor

#
struct Sensor
NameTypeDescription
pinint = 0
kindint = 22

Reading

#
struct Reading
NameTypeDescription
statusint = 1
humidityf64 = -1000
temperaturef64 = -1000

Sensor

#
struct Sensor
NameTypeDescription
triggerint = 0
echoint = 0

Port

#
struct Port
NameTypeDescription
numberint = 0
baudint = 115200
data_bitsint = 8
parityint = 0
stop_bitsint = 1

State

#
enum State
NameTypeDescription
Disconnected
Connecting
Connected
Failed

Security

#
enum Security
NameTypeDescription
Open
Wep
Wpa
Wpa2
Wpa3

Network

#
struct Network
NameTypeDescription
ssidstr = ""
bssidstr = ""
rssiint = 0
channelint = 0
securitySecurity

Socket

#
struct Socket
NameTypeDescription
idint = -1
okbool = false

Sensor

#
struct Sensor
NameTypeDescription
pinint = 0
romu64 = 0
addressedbool = false

Port

#
struct Port
NameTypeDescription
pathstr = ""
handlei64 = -1
okbool = false
baudint = 0

Pin

#
struct Pin
NameTypeDescription
numberint = 0
modeint = 0

Bus

#
struct Bus
NameTypeDescription
sckint = 0
mosiint = 0
misoint = 0
csint = -1
modeint = 0
orderint = 0
half_usint = 0

Sensor

#
struct Sensor
NameTypeDescription
pinint = 0
active_highbool = true

Device

#
struct Device
NameTypeDescription
namestr = ""
addressstr = ""
rssiint = 0

Service

#
struct Service
NameTypeDescription
handleint = -1
uuidstr = ""
okbool = false

Characteristic

#
struct Characteristic
NameTypeDescription
handleint = -1
uuidstr = ""
propsint = 0
okbool = false

Event

#
struct Event
NameTypeDescription
handleint = -1
uuidstr = ""
valueVector<u8>
okbool = false

Reading

#
struct Reading
NameTypeDescription
yearint = 2000
monthint = 1
dayint = 1
weekdayint = 1
hourint = 0
minuteint = 0
secondint = 0

Device

#
struct Device
NameTypeDescription
busi2c.Bus

Constants and variables

MAX_PINS

#
const MAX_PINS: i32 = 64

DHT11

#
const DHT11: i32 = 11

DHT22

#
const DHT22: i32 = 22

READ_OK

#
const READ_OK: i32 = 0

READ_TIMEOUT

#
const READ_TIMEOUT: i32 = 1

READ_CHECKSUM

#
const READ_CHECKSUM: i32 = 2

ERROR_VALUE

#
const ERROR_VALUE: f64 = -1000

MAX_ECHO_US

#
const MAX_ECHO_US: i32 = 38000

PARITY_NONE

#
const PARITY_NONE: i32 = 0

PARITY_EVEN

#
const PARITY_EVEN: i32 = 1

PARITY_ODD

#
const PARITY_ODD: i32 = 2

API_LEVEL

#
const API_LEVEL: i32 = 1

The 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.

hw-API_LEVEL.salam
Try it
import hw

func main:
    println "hardware API level", hw.API_LEVEL
end
Output
hardware API level 1

max_scan

#
const max_scan: i32 = 32

state_disconnected

#
const state_disconnected: i32 = 0

state_connecting

#
const state_connecting: i32 = 1

state_connected

#
const state_connected: i32 = 2

state_failed

#
const state_failed: i32 = 3

security_open

#
const security_open: i32 = 0

security_wep

#
const security_wep: i32 = 1

security_wpa

#
const security_wpa: i32 = 2

security_wpa2

#
const security_wpa2: i32 = 3

security_wpa3

#
const security_wpa3: i32 = 4

FAMILY

#
const FAMILY: i32 = 40

ERROR_C

#
const ERROR_C: f64 = -1000

PARITY_NONE

#
const PARITY_NONE: i32 = 0

PARITY_EVEN

#
const PARITY_EVEN: i32 = 1

PARITY_ODD

#
const PARITY_ODD: i32 = 2

INPUT

#
const INPUT: i32 = 0

OUTPUT

#
const OUTPUT: i32 = 1

INPUT_PULLUP

#
const INPUT_PULLUP: i32 = 2

LOW

#
const LOW: i32 = 0

HIGH

#
const HIGH: i32 = 1

MSB_FIRST

#
const MSB_FIRST: i32 = 0

LSB_FIRST

#
const LSB_FIRST: i32 = 1

max_scan

#
const max_scan: i32 = 32

max_value

#
const max_value: i32 = 512

prop_read

#
const prop_read: i32 = 1

prop_write

#
const prop_write: i32 = 2

prop_notify

#
const prop_notify: i32 = 4

ADDRESS

#
const ADDRESS: i32 = 104

Functions

Simulated

#
func Simulated(): bool

GpioMode

#
func GpioMode(pin: int, mode: int)

GpioWrite

#
func GpioWrite(pin: int, level: int)

GpioRead

#
func GpioRead(pin: int): int

Micros

#
func Micros(): u64

DelayUs

#
func DelayUs(us: u64)

PulseIn

#
func PulseIn(pin: int, level: int, timeout_us: u64): u64

SimSetPulse

#
func SimSetPulse(pin: int, width_us: u64)

I2cWrite

#
func I2cWrite(bus: int, addr: int, reg: int, value: int): int

I2cRead

#
func I2cRead(bus: int, addr: int, reg: int): int

UartConfig

#
func UartConfig(port: int, baud: int)

UartWrite

#
func UartWrite(port: int, value: int)

UartRead

#
func UartRead(port: int): int

UartPeek

#
func UartPeek(port: int): bool

SimInject

#
func SimInject(port: int, value: int)

SimTaken

#
func SimTaken(port: int): int

SimSetPin

#
func SimSetPin(pin: int, level: int)

SimOneWire

#
func SimOneWire(pin: int, serial: u64)

SimTemperature

#
func SimTemperature(milli_celsius: int)

SimDht

#
func SimDht(pin: int, kind: int, humidity_tenths: int, temperature_tenths: int)

SimDhtBadChecksum

#
func SimDhtBadChecksum(bad: bool)

SimLink

#
func SimLink(source: int, reader: int)

WifiCall

#
func WifiCall(op: int, a: int, b: int, c: int): int

WifiCallStr

#
func WifiCallStr(op: int, s: str, t: str, a: int): int

BleCall

#
func BleCall(op: int, a: int, b: int, c: int): int

BleCallStr

#
func BleCallStr(op: int, s: str, a: int, b: int): int

Open

#
func Open(id: int): Bus

WriteReg

#
func WriteReg(b: Bus, addr: int, reg: int, value: int): bool

ReadReg

#
func ReadReg(b: Bus, addr: int, reg: int): int

Present

#
func Present(b: Bus, addr: int): bool

ReadBlock

#
func ReadBlock(b: Bus, addr: int, reg: int, n: int): Vector<int>

WriteBlock

#
func WriteBlock(b: Bus, addr: int, reg: int, data: Vector<int>): bool

Open

#
func Open(pin: int, kind: int): Sensor

Read

#
func Read(s: Sensor): Reading

Valid

#
func Valid(r: Reading): bool

Status

#
func Status(r: Reading): int

Humidity

#
func Humidity(r: Reading): f64

Temperature

#
func Temperature(r: Reading): f64

ReadHumidity

#
func ReadHumidity(s: Sensor): f64

ReadTemperature

#
func ReadTemperature(s: Sensor): f64

Open

#
func Open(trigger: int, echo: int): Sensor

EchoMicros

#
func EchoMicros(s: Sensor): u64

DistanceMm

#
func DistanceMm(s: Sensor): int

DistanceCm

#
func DistanceCm(s: Sensor): f64

Open

#
func Open(number: int, baud: int): Port

WriteByte

#
func WriteByte(p: Port, value: int)

WriteStr

#
func WriteStr(p: Port, text: str)

ReadByte

#
func ReadByte(p: Port): int

Available

#
func Available(p: Port): bool

ReadLine

#
func ReadLine(p: Port): str

Init

#
func Init(): bool

Ready

#
func Ready(): bool

Scan

#
func Scan(): Vector<Network>

Connect

#
func Connect(ssid: str, password: str, timeout_ms: int): bool

Disconnect

#
func Disconnect()

Status

#
func Status(): State

IsConnected

#
func IsConnected(): bool

Ip

#
func Ip(): str

Rssi

#
func Rssi(): int

Mac

#
func Mac(): str

StartAccessPoint

#
func StartAccessPoint(ssid: str, password: str, channel: int): bool

TcpOpen

#
func TcpOpen(host: str, port: int): Socket

TcpSend

#
func TcpSend(s: Socket, data: str): bool

TcpReceive

#
func TcpReceive(s: Socket, timeout_ms: int): str

TcpClose

#
func TcpClose(s: Socket)

Ssid

#
func Ssid(n: Network): str

Bssid

#
func Bssid(n: Network): str

Signal

#
func Signal(n: Network): int

Channel

#
func Channel(n: Network): int

SecurityOf

#
func SecurityOf(n: Network): Security

SecurityCode

#
func SecurityCode(n: Network): int

StateCode

#
func StateCode(): int

SocketOk

#
func SocketOk(s: Socket): bool

SocketId

#
func SocketId(s: Socket): int

SimReset

#
func SimReset()

SimAddNetwork

#
func SimAddNetwork(ssid: str, bssid: str, rssi: int, channel: int, security: int)

SimSetPassword

#
func SimSetPassword(ssid: str, password: str)

SimSetIp

#
func SimSetIp(ip: str)

SimSetDelay

#
func SimSetDelay(ms: int)

SimDrop

#
func SimDrop()

SimSetReply

#
func SimSetReply(host: str, port: int, data: str)

SimSent

#
func SimSent(s: Socket): str

SimAccessPoint

#
func SimAccessPoint(): str

Open

#
func Open(pin: int): Sensor

OpenRom

#
func OpenRom(pin: int, rom: u64): Sensor

Crc8

#
func Crc8(data: Vector<int>): int

Detected

#
func Detected(s: Sensor): bool

ReadRom

#
func ReadRom(s: Sensor): u64

Scratchpad

#
func Scratchpad(s: Sensor): Vector<int>

Resolution

#
func Resolution(s: Sensor): int

SetResolution

#
func SetResolution(s: Sensor, bits: int): bool

Start

#
func Start(s: Sensor): bool

Ready

#
func Ready(s: Sensor): bool

WaitReady

#
func WaitReady(s: Sensor, timeout_ms: int): bool

RawToCelsius

#
func RawToCelsius(raw: int): f64

LastCelsius

#
func LastCelsius(s: Sensor): f64

ReadCelsius

#
func ReadCelsius(s: Sensor): f64

IsOpen

#
func IsOpen(p: Port): bool

Supported

#
func Supported(): bool

Valid

#
func Valid(baud: int, data_bits: int, parity: int, stop_bits: int): bool

OpenFormat

#
func OpenFormat(path: str, baud: int, data_bits: int, parity: int, stop_bits: int): Port

Open

#
func Open(path: str, baud: int): Port

Close

#
func Close(p &: Port)

WriteBytes

#
func WriteBytes(p: Port, data: void*, n: int): bool

Write

#
func Write(p: Port, text: str): bool

WriteByte

#
func WriteByte(p: Port, value: int): bool

ReadByte

#
func ReadByte(p: Port, timeout_ms: int): int

ReadLine

#
func ReadLine(p: Port, timeout_ms: int): str

Flush

#
func Flush(p: Port)

Open

#
func Open(number: int, mode: int): Pin

SetMode

#
func SetMode(p &: Pin, mode: int)

Write

#
func Write(p: Pin, level: int)

Read

#
func Read(p: Pin): int

Toggle

#
func Toggle(p: Pin)

IsHigh

#
func IsHigh(p: Pin): bool

Open

#
func Open(sck: int, mosi: int, miso: int, cs: int, mode: int): Bus

SetSpeed

#
func SetSpeed(b &: Bus, half_us: int)

SetOrder

#
func SetOrder(b &: Bus, order: int)

Mode

#
func Mode(b: Bus): int

Select

#
func Select(b: Bus)

Deselect

#
func Deselect(b: Bus)

Transfer

#
func Transfer(b: Bus, value: int): int

TransferBuffer

#
func TransferBuffer(b: Bus, data: Vector<int>): Vector<int>

Exchange

#
func Exchange(b: Bus, data: Vector<int>): Vector<int>

Open

#
func Open(pin: int): Sensor

OpenInverted

#
func OpenInverted(pin: int): Sensor

Motion

#
func Motion(s: Sensor): bool

WaitMotion

#
func WaitMotion(s: Sensor, timeout_ms: int): bool

ValidUuid

#
func ValidUuid(uuid: str): bool

Init

#
func Init(name: str): bool

Ready

#
func Ready(): bool

Advertise

#
func Advertise(enable: bool): bool

Advertising

#
func Advertising(): bool

Scan

#
func Scan(timeout_ms: int): Vector<Device>

AddService

#
func AddService(uuid: str): Service

AddCharacteristic

#
func AddCharacteristic(service: Service, uuid: str, props: int, value: Vector<u8>): Characteristic

SetValue

#
func SetValue(c: Characteristic, value: Vector<u8>): bool

Value

#
func Value(c: Characteristic): Vector<u8>

Notify

#
func Notify(c: Characteristic): bool

Poll

#
func Poll(): bool

Take

#
func Take(): Event

Connect

#
func Connect(address: str): bool

Disconnect

#
func Disconnect()

IsConnected

#
func IsConnected(): bool

Read

#
func Read(uuid: str): Vector<u8>

Write

#
func Write(uuid: str, value: Vector<u8>): bool

DeviceName

#
func DeviceName(d: Device): str

DeviceAddress

#
func DeviceAddress(d: Device): str

DeviceRssi

#
func DeviceRssi(d: Device): int

EventUuid

#
func EventUuid(e: Event): str

EventValue

#
func EventValue(e: Event): Vector<u8>

EventOk

#
func EventOk(e: Event): bool

ServiceOk

#
func ServiceOk(s: Service): bool

CharacteristicOk

#
func CharacteristicOk(c: Characteristic): bool

SimReset

#
func SimReset()

SimAddBleDevice

#
func SimAddBleDevice(name: str, address: str, rssi: int)

SimSetRemoteValue

#
func SimSetRemoteValue(address: str, uuid: str, value: Vector<u8>)

SimRemoteWritten

#
func SimRemoteWritten(address: str, uuid: str): Vector<u8>

SimInjectWrite

#
func SimInjectWrite(c: Characteristic, value: Vector<u8>): bool

SimSetClient

#
func SimSetClient(connected: bool)

SimNotified

#
func SimNotified(): int

Make

#
func Make(year: int, month: int, day: int, weekday: int, hour: int, minute: int, second: int): Reading

Format

#
func Format(r: Reading): str

Open

#
func Open(bus: i2c.Bus): Device

Detected

#
func Detected(d: Device): bool

Running

#
func Running(d: Device): bool

Set

#
func Set(d: Device, r: Reading): bool

Halt

#
func Halt(d: Device): bool

Now

#
func Now(d: Device): Reading