Controlling the Gateway¶
The below assumes you have sucessfully connected to your gateway:
from lager import lager
gateway = lager.Lager()
GPIO Example¶
import time
# Blink an LED connected to GPIO 0
gateway.gpio.write(0, HIGH)
time.sleep(0.5)
gateway.gpio.write(0, LOW);
time.sleep(0.5)
SPI Example¶
SPI Modes:
Mode |
POL |
PHA |
0 |
0 |
0 |
1 |
0 |
1 |
2 |
1 |
0 |
3 |
1 |
1 |
# Start an SPI connection
# mode = 0
# speed = 50,000 bits per second
gateway.spi.connect(0, 50000)
# Write a single 0x00 byte
gateway.spi.write(b'\x00')
# Write multiple bytes
bytes = b'\xc0\x01'
gateway.spi.write(bytes)
# Read 10 bytes from SPI
count, rx_bytes = gateway.spi.read(10)
print(f"Received {count} bytes: {rx_bytes}")
# Transfer bytes
count, rx_bytes = gateway.spi.xfer(b'\xc0\x01')
print(f"Received {count} bytes: {rx_bytes}")
I2C Example¶
# Open an I2C connection to device address 0x53
gateway.i2c.connect(0x53)
# Read a single byte from device
byte = gateway.i2c.read_byte()
# Read a single byte from register 3
byte = gateway.i2c.read_byte_data(3)
# Read 10 bytes from the raw device
count, rx_bytes = gateway.i2c.read_device(10)
# Read 8 bytes from register 3
# Note: count may be between 1 and 32
count, rx_bytes = gateway.i2c.read_i2c_block_data(3, 8)
# Read a single 16 bit word from register 3
rx_word = gateway.i2c.read_word_data(3)
# Write up to 32 bytes to register 3
gateway.i2c.write_block_data(3, b'\x00\x01\x02')
# Write a single byte to the device
gateway.i2c.write_byte(0x23)
# Write a single byte to register 3
gateway.i2c.write_byte_data(3, 0x23)
# Write data to the raw device
gateway.i2c.write_device(b'\x00\x01\x02')
# Write up to 32 byets to register 3
gateway.i2c.write_i2c_block_data(3, b'\x00\x01\x02')
# Write a single bit to the device
gateway.i2c.write_quick(1)
# Write a single 16 bit word to register 3
gateway.i2c.write_word_data(3, 0xc001)
Serial/UART Example¶
Serial access is provided by the pyserial
module:
import serial
with serial.Serial('/dev/ttyS0', 9600, timeout=1) as ser:
x = ser.read() # Read one byte
s = ser.read(10) # Read up to 10 bytes (timeout)
ser.write(b'hello') # Write a string