Controlling the Gateway
The below assumes you have sucessfully connected to your gateway:
from lager import lager
gateway = lager.Lager()
GPIO Example
Lager makes it easy to control your gateway GPIOs from Python.
Blink an LED:
import time
from lager import HIGH, LOW, OUTPUT
pin = 0
gateway.gpio.set_mode(pin, OUTPUT)
gateway.gpio.write(pin, HIGH)
time.sleep(0.5)
gateway.gpio.write(pin, LOW);
time.sleep(0.5)
Read an input:
from lager import INPUT
pin = 0
gateway.gpio.set_mode(pin, INPUT)
state = gateway.gpio.read(pin)
print(f"State: {state}")
ADC Example
Your Lager gateway has several ADC inputs at your disposal. You can access them like this:
# Read all ADC pins at once, return results in milivolts
results = gateway.adc.read(output='mv')
print(f"ADC Results: {results}")
# Read a specific ADC channel
channel = 2
result = gateway.adc.read(channel=channel)
print(f"ADC result for channel {channel}: {result}")
# Take several samples and return the average
# Note: average_count can be 4, 8, 16, or 32
channel = 2
result = gateway.adc.read(channel=channel, average_count=32)
print(f"ADC result for channel {channel}: {result}")
SPI Example
It's easy to read, write, and transfer data with the gateway's SPI port. Refer to the table below to make sure you're using the correct SPI mode for your device.
SPI Modes: (More information on SPI modes can be found here)
Mode |
POL |
PHA |
0 |
0 |
0 |
1 |
0 |
1 |
2 |
1 |
0 |
3 |
1 |
1 |
Example:
# Start an SPI connection
mode = 0
speed = 50000 # bits per second
gateway.spi.connect(mode, speed)
# 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
Here are some examples for how to interface with the I2C bus on your Lager gateway:
# 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 on your gateway is provided by the pyserial
module, which is already installed on your gateway.
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