The following examples demonstrate some of the capabilities of the libsoc_zero library. Please note that all examples are written assuming Python 3. Examples may work under Python 2 (but they may not!)
Turn an LED on and off repeatedly
from libsoc_zero.GPIO import LED
from time import sleep
led = LED('GPIO-E')
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Detect if the button has been pressed
from libsoc_zero.GPIO import Button
from time import sleep
btn = Button('GPIO-G')
while True:
sleep(0.25)
if btn.is_pressed():
print('Button is pressed!')
else:
print('Button is not pressed!')
Wait for button to be pressed and then switch LED on (or off)
from libsoc_zero.GPIO import Button
from libsoc_zero.GPIO import LED
from time import sleep
btn = Button('GPIO-G')
led = LED('GPIO-E')
while True:
btn.wait_for_press()
if led.is_lit:
led.off()
else:
led.off()
sleep(1)
Detect when the sensor is touched
from libsoc_zero.GPIO import Button
touch = Button('GPIO-C')
while True:
if touch.is_pressed():
print("Button is pressed")
else:
print("Button is not pressed")
Don’t finish program until sensor is touched
from libsoc_zero.GPIO import Button
touch = Button('GPIO-C')
print('Waiting for press event...')
touch.wait_for_press()
print('Button was pressed!')
Detect when the sensor has been tilted
from libsoc_zero.GPIO import Tilt
tilt = Tilt('GPIO-G')
tilt.wait_for_tilt()
print('Tilt happened')