Buttons are simple, it's a switch that when pressed, makes an electrical connection. Right? Riiiiiight?
Well it's 2023 and feature creep has been going on for a while, and the Sparkfun Qwiic Green LED Button is no exception... because nothing says "Press Me" like a board with a dedicated ATTiny MCU to handle button presses and LED strobing effects and send them over an I2C Bus.
In all seriousness though, this is quite a fun little device because it saves you the hassle of doing your own LED timing and having to handle debounce etc.
Setting it up is really easy:
On the BeaglePlay, you'll just want to also install my fork of the Qwiic I2C Library which just specifies I2C Bus 5 as the one to use, since that's where it maps on the Qwiic conector here.
For more info on that, see my Qwiic OLED Tutorial
git clone https://github.com/virtualRadish/Qwiic_I2C_Py_LC
cd Qwiic_I2C_Py_LC/
sudo python setup.py install
sudo pip3 install sparkfun-qwiic-button
Now let's run some example code:from __future__ import print_function
import qwiic_button
import time
import sys
def run_example():
print("\nSparkFun Qwiic Button Example 1")
my_button = qwiic_button.QwiicButton()
if my_button.begin() == False:
print("\nThe Qwiic Button isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
print("\nButton ready!")
while True:
if my_button.is_button_pressed() == True:
print("\nThe button is pressed!")
else:
print("\nThe button is not pressed!")
time.sleep(0.02)
if __name__ == '__main__':
try:
run_example()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
Pressing the button and releasing it should show up on our terminal!
One of the fun bits of this simple button is that the ATTiny handles button strobing for us, let's see how we can get the LED to blink when we press it:
from __future__ import print_function
import qwiic_button
import time
import sys
brightness = 250 # The maximum brightness of the pulsing LED. Can be between 0 and 255
cycle_time = 1000 # The total time for the pulse to take. Set to a bigger number for a slower pulse or a smaller number for a faster pulse
off_time = 200 # The total time to stay off between pulses. Set to 0 to be pulsing continuously.
def run_example():
print("\nSparkFun Qwiic Button Example 3")
my_button = qwiic_button.QwiicButton()
if my_button.begin() == False:
print("\nThe Qwiic Button isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
print("\nButton ready!")
my_button.LED_off()
while True:
if my_button.is_button_pressed() == True:
print("\nThe button is pressed!")
my_button.LED_config(brightness, cycle_time, off_time)
else:
print("\nThe button is not pressed.")
my_button.LED_off()
time.sleep(0.02) # Let's not hammer too hard on the I2C bus
if __name__ == '__main__':
try:
run_example()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 3")
sys.exit(0)
It's one line to strobe an LED! Wow!
my_button.LED_config(brightness, cycle_time, off_time)
Well... anything you can do with a button really. How about executing a script?
In Python, you can use the Subprocess or OS modules to run arbitrary Bash commands from within your Python script. How cool!
Let's see how we can make the Button shutdown our BeaglePlay.
All it takes is two extra lines from our previous example:
import subprocess #This goes at the top of the file
and
subprocess.call(["shutdown", "-h", "now"]) #This calls the command between brackets
Now you can use your imagination, make that Button do anything you want, have it pull up a web page, run a bash script or anything you want that is trigger-able by a button.
Just make sure you run the python with "sudo" since only the root user is allowed to shutdown the system.
Bonus Tip:You can actually run the entire script in the background and have it work without constantly taking over your main terminal window by running it as a background process and sending it's usual output to the abyss.
sudo python Qwiic_I2C_Py_LC/shutDownLED.py >> /dev/null &
The script above is the same as what we were doing before, but the & character tells Bash to run it as a subprocess, and the >> /dev/null tells it to forward all outputs to /dev/null, aka the void.
Comments