This is actually a more complex problem than it outwardly appears. This is because, while the WS2812 LEDs on Unicorn HAT and Unicorn pHAT are “smart” and will remember the last data you sent to them, they’re also fairly forgiving about what they interpret as data.
Basically- if you’re not actively sending them a signal telling them to “show X” they might suddenly decide to interpret some random environmental noise as “show Y”, which isn’t fun!
Anyway, if you’re willing to overlook this detail you can fudge it by using adding:
unicorn.atexit._exithandlers = 
It looks like I need to add this as a feature into Unicorn HAT, although because it has no onboard driver chip to maintain the last state you sent (causing the problem documented above) I’ve so far avoided doing so.
The correct way to accomplish this, would be to have the Python script running continuously, and then use some sort of interface (a fifo, pipe or otherwise) to communicate with that script and tell it to turn the lights on/off.
You could use
SIGUSR2 to do this, or perhaps
SIGCONT. Off the top of my head:
import unicornhat as unicorn
def turn_on(signum, stack):
for x in range(8):
unicorn.set_pixel(x, 1, 255, 0, 255)
def turn_off(signum, stack):
Save this as
unincornsignal.py and run with:
sudo unicornsignal.py & to send it to the background.
You should see a line appear after this that says something like:
The latter number is the script’s PID, substitute this in the below commands.
sudo kill -12 PID to turn Unicorn HAT off
sudo kill -10 PID to turn it on.
You could make a Python script to run these commands for you:
parser = argparse.ArgumentParser()
parser.add_argument("-pid", "--pid", type=int, help="pid of Unicorn Server")
parser.add_argument("-sw", "--show", action="store_true", help="turn on the led")
parser.add_argument("-st", "--stop", action="store_true", help="stop the led")
args = parser.parse_args()
And call with:
pi@raspberrypi:~ $ sudo python unicornremote.py --pid 464 --stop
pi@raspberrypi:~ $ sudo python unicornremote.py --pid 464 --show
And you could even go so far as to have
unicornsignal.py save its pid to an external file, and have
unicornremote.py read that in automatically.
Then set up a systemd unit to run your
unicornsignal.py in the background:
Description=My UnicornHAT Server
Which you would save as
Then reload systemd:
sudo systemctl daemon-reload
And start your new service:
sudo systemctl start unicornserver
You can also get its status:
sudo systemctl status unicornserver
And stop it:
sudo systemctl stop unicornserver