Help! Unicorn hat doesn't turn off on shutdown!

Hello all, first post here.

I ordered a Unicorn hat the other day and it finally came in today (Sooooooo much excitement).
I installed all the required stuff and downloaded the examples.

I then added an entry into crontab:
@reboot sudo python /home/pi/Pimoroni/unicornhat/examples/rainbow.py &

This works great and starts the script on boot, the only problem is that the Unicorn Hat stays powered after shutting down the system and acts as if you had just canceled out of the script.

Any help would be greatly appreciated!

That’s the expected behaviour. The HAT’s still receiving power, so the LEDs remain lit. The only way round this would be to add a try/except inside the script that clears the Unicorn HAT just before the script exits, but I’m not sure of the best way to do that when it’s running as a background process.

@gadgetoid or @RogueM might be able to suggest a solution…?

one suggestion would be to have a script specifically to clear the display, calling it on poweroff using cron… that ought to work?

Does cron have an @poweroff function?

First off thank you all for your replies.

I dont believe that Cron has an @shutdown command, if it dies though thay would be super awesome.

I thought it did, but apparently not. Then upstart might be the answer (or init script if on wheezy). I don’t think there is any other way, but @gadgetoid might be able to suggest some other solution.

Wheezy is a version of Linux, correct? If so I believe I am on Jessie right now, running v4.0.2(the .2 might be wrong) of RetroPie.

Im unsure what Upstart is but I can look into it.

Do you know what happens to the script on shutdown? Would there be a way to potentially listen for the shutdown signal in the python script and then clear out the colors?(And maybe clean up GPIO if this isnt handled elsewhere)

I am unsure whether it’s possible to handle anywhere else… for that matter I’m not even sure if it’s as simple as kicking in another script, I’d have to try.

Retropie 4.x is based on Jessie yes, but using an init script should still ‘work’ if you want to run something at shutdown, it just seems overkill.

I willing to try whatever :)

I wont be able to do anything until around 7PM EST though ( Currently at work :/ )

So after adding a momentary switch to power on and power off my pi, I decided to go ahead and add a fan, which also retains power after shutdown.

It would seem I need a way to turn off the power to the GPIO pins when the pi is shutdown, does anyone know if this is possible?

I think the only way is to cut power to the Pi. This is why we designed Picade HAT to back-power the Pi, so that we could cut power at the HAT and the Pi would be completely switched off.

This thread has a number of suggestions -> http://raspberrypi.stackexchange.com/questions/5372/circuit-to-safely-power-down-pi

An update!

For now I was able to turn it off in the script (until I decide whether I want to use a transistor or switch to cut the power to both the unicorn hat and fan, but the fan isnt in this build as I am awaiting my taller case)
I had to change unicorn.AUTO to unicorn.HAT because when I hooked it up I decided to use three wires rather than placing it on the Pi and for some reason it only lit half of the unicorn hat after doing so.

I also reverted my crontab back to its original state and added the script into rc.local.

#!/usr/bin/env python

import colorsys
import math
import time
import signal
import sys

import unicornhat as unicorn

def Shutdown(Code, Frame):
    unicorn.off()
    sys.exit(0)

signal.signal(signal.SIGTERM, Shutdown)

unicorn.set_layout(unicorn.HAT)
unicorn.rotation(0)
unicorn.brightness(0.5)
width,height=unicorn.get_shape()

i = 0.0
offset = 30
try:
	while True:
		i = i + 0.3
		for y in range(height):
			for x in range(width):
				r = 0#x * 32
				g = 0#y * 32
				xy = x + y / 4
				r = (math.cos((x+i)/2.0) + math.cos((y+i)/2.0)) * 64.0 + 128.0
				g = (math.sin((x+i)/1.5) + math.sin((y+i)/2.0)) * 64.0 + 128.0
				b = (math.sin((x+i)/2.0) + math.cos((y+i)/1.5)) * 64.0 + 128.0
				r = max(0, min(255, r + offset))
				g = max(0, min(255, g + offset))
				b = max(0, min(255, b + offset))
				unicorn.set_pixel(x,y,int(r),int(g),int(b))
		unicorn.show()
		time.sleep(0.01)
except KeyboardInterrupt:
	unicorn.off()

yes, unless the EEPROM can be read (physical pin 27-28 are connected) then the library will assume you have a pHAT, so if select wiring you do indeed have to override unicornhat.AUTO

for future reference, if you ever want to run a script on shutdown that clears your Unicorn, on a systemd-based system such as Raspbian Jessie (including latest Retropie which is based on it), you can do it as follows:

create a script (its name does not matter, say clear-unicorn) with something like this:

#! /bin/sh

case "$1" in
    halt) sudo python -c 'import unicornhat; unicornhat.clear()';;
    poweroff) sudo python -c 'import unicornhat; unicornhat.clear()';;
    reboot) sudo python -c 'import unicornhat; unicornhat.clear()';;
esac

don’t forget to chmod +x to make it executable, and then drop it in /lib/systemd/system-shutdown/

… from there on, every time the system goes for a poweroff (halt or reboot, though which options trigger the clear are down to you) the Unicorn will go off.

1 Like

Oh thats great information, thanks! I think i’ll keep it in the script for now (less to edit later if I want to make it a mood light or something when the script shutsdown)