Standard library · Hardware

hw.gpio

Digital pins: open as input, output or input with pull-up, then write, read and toggle.

import hw.gpio

hw.gpio is the everyday way to use a digital pin. Open sets a mode and returns a Pin; Write, Read, Toggle and IsHigh work on it. The pin number must be below 64. On a computer the pin lives in the simulated board, so writing a pin and reading it back works, and board.SimSetPin plays the part of a button or a sensor.

Blinking a pin

Open the pin as OUTPUT and flip it with Toggle. IsHigh answers with a bool instead of a 0 or 1.

hw-gpio-guide-blink.salam
Try it
import hw.gpio

func main:
    led := gpio.Open(13, gpio.OUTPUT)
    repeat 4 in i:
        gpio.Toggle(led)
        println i, gpio.IsHigh(led)
    end
end
Output
0 true
1 false
2 true
3 false

Reading a button

A button wired to ground needs the pull-up: open the pin as INPUT_PULLUP, or switch an open pin with SetMode. It reads HIGH at rest and LOW when pressed.

hw-gpio-guide-button.salam
Try it
import hw.board
import hw.gpio

func main:
    mut button := gpio.Open(2, gpio.INPUT)
    gpio.SetMode(button, gpio.INPUT_PULLUP)
    println "pressed:", gpio.Read(button) == gpio.LOW
    board.SimSetPin(2, 0)
    println "pressed:", gpio.Read(button) == gpio.LOW
end
Output
pressed: false
pressed: true

Types

Pin

#
struct Pin

A digital pin and the mode it was last given.

NameTypeDescription
numberint = 0The pin number, 0 to 63.
modeint = 0The mode: INPUT, OUTPUT or INPUT_PULLUP.
hw-gpio-Pin.salam
Try it
import hw.gpio

func main:
    mut led := gpio.Open(13, gpio.OUTPUT)
    gpio.Write(led, gpio.HIGH)
    println gpio.Read(led), gpio.IsHigh(led)
    gpio.Toggle(led)
    println gpio.Read(led), gpio.IsHigh(led)
    gpio.SetMode(led, gpio.INPUT_PULLUP)
    println gpio.IsHigh(led), gpio.INPUT, gpio.LOW
end
Output
1 true
0 false
true 0 0

Constants and variables

INPUT

#
const INPUT: i32 = 0

Pin mode: input, floating.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

OUTPUT

#
const OUTPUT: i32 = 1

Pin mode: output, driven by the program.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

INPUT_PULLUP

#
const INPUT_PULLUP: i32 = 2

Pin mode: input with the pull-up resistor on, so an open pin reads high.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

LOW

#
const LOW: i32 = 0

The low level, 0.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

HIGH

#
const HIGH: i32 = 1

The high level, 1.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

Functions

Open

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

Sets the mode of a pin and returns a Pin for it.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

SetMode

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

Changes the mode of an open pin. The pin is passed by reference so its mode field follows.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

Write

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

Drives the pin high or low. Any non-zero level is high.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

Read

#
func Read(p: Pin): int

Reads the pin: 1 for high, 0 for low.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

Toggle

#
func Toggle(p: Pin)

Flips the pin from high to low or from low to high.

The example for this item is shown with the ones above: hw-gpio-Pin.salam

IsHigh

#
func IsHigh(p: Pin): bool

True when the pin reads high.

The example for this item is shown with the ones above: hw-gpio-Pin.salam