I will give this a try and let you know.
Thank you for the help!
pete
I will give this a try and let you know.
Thank you for the help!
pete
No problem. Looks like I’m repeating myself lol I was just about to link to the full python file and noticed I had already posted it. And mentioned why I split it up into two blocks. The two small blocks of code in my latter post should be all you need, just take out the sense.clear and led shim stuff.
I tried to merge your shutdown code into mine and I’m getting some strange behavior:
def shutdown():
global x
img = Image.open("/home/pi/badge/off.png")
inky_display.set_image(img)
inky_display.show()
print ("button pressed")
time.sleep(30)
x = 0
The extra time.sleep(30) is there to give the PHAT time enough to change the image.
If I put the next block of your code immediately following that, I get an error message from Python that says “NameError: name ‘x’ is not defined”
if x == 0:
img = Image.new(" ", (inky_display.WIDTH, inky_display.HEIGHT))
draw = ImageDraw.Draw(img)
raise SystemExit
time.sleep(30)
If I put it at the end of the file, it never gets to that code block because there’s a call to the swap procedure right before it. And if I put it before the swap(img_name)
call, making that line the last line of code, I get the NameError again.
What am I doing wrong?
pete
Yeah you have to set it initially to something.
I have an x = 2 # shutdown variable
line in my original code before the while true loop.
Set it to anything but zero and it does nothing until you change it to Zero later on. Sorry about that.
Should I declare the global variable then before I get to the shutdown
procedure declaration (i.e. remove the global x
declaration from the first line of shutdown()
) and then set a value for it — say, up top where I’m setting all the image file names?
You need the x = 2 line above the while true statement. That sets the variable x to 2. That will get rid of the variable not defined error.
Then you either do this
def Shutdown(channel):
(your other stuff you want done on shutdown here)
os.system("sudo shutdown now -P")
time.sleep(30)
Or if that doesn’t work you do this above the while true.
def Shutdown(channel):
global x
x = 0
and this after the while true.
if x == (0):
(your other stuff you want done on shutdown here)
os.system("sudo shutdown now -P")
time.sleep(30)
This is what I am working with right now. This code will loop and swap the images, but when it hits the shutdown
procedure, it never seems to enter the If x == 0
section of the code and just continues to swap to the next image:
# Badge code
# Import libraries
from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
import time
# Shutdown libraries
from gpiozero import Button
from signal import pause
import os, sys
# Instantiate inkyPHAT
inky_display = InkyPHAT("red")
inky_display.set_border(inky_display.WHITE)
# Set image file variables
power = "/home/pi/badge/power.png"
name1 = "/home/pi/badge/name1.png"
name2 = "/home/pi/badge/name2.png"
name3 = "/home/pi/badge/name3.png"
pur = "/home/pi/badge/pur.png"
trade = "/home/pi/badge/trade.png"
img_name = ""
x = 2
# Set variables for shutdown
offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 21
holdTime = int(sys.argv[2]) if len(sys.argv) >= 3 else 4
# the function called to shut down the RPi
def shutdown():
img = Image.open("/home/pi/badge/off.png")
inky_display.set_image(img)
inky_display.show()
print ("button pressed")
time.sleep(15)
x = 0
if x == 0:
img = Image.new(" ", (inky_display.WIDTH, inky_display.HEIGHT))
draw = ImageDraw.Draw(img)
raise SystemExit
time.sleep(30)
print("exiting")
# Procedure to swap pictures every minute
def swap(img_name):
if img_name == power:
img_name = name1
elif img_name == name1:
img_name = pur
elif img_name == pur:
img_name = name2
elif img_name == name2:
img_name = trade
elif img_name == trade:
img_name = name3
else:
img_name = power
print(img_name)
img = Image.open(img_name)
inky_display.set_image(img)
inky_display.show()
time.sleep(42) # pause the code for 42 seconds
swap(img_name)
# Handle the shutdown switch
btn = Button(offGPIO, hold_time=holdTime)
btn.when_held = shutdown
# Call the procedure
swap(img_name)
Here’s another variation I tried. This one loads the first image to the inkyPHAT, and then exits all on it’s own when it runs.
# Badge code
# Import libraries
from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne
import time
# Shutdown libraries
from gpiozero import Button
from signal import pause
import os, sys
font = ImageFont.truetype(FredokaOne,22)
# Instantiate inkyPHAT
inky_display = InkyPHAT("red")
inky_display.set_border(inky_display.WHITE)
# Set image file variables
power = "/home/pi/badge/power.png"
name1 = "/home/pi/badge/name1.png"
name2 = "/home/pi/badge/name2.png"
name3 = "/home/pi/badge/name3.png"
pur = "/home/pi/badge/pur.png"
trade = "/home/pi/badge/trade.png"
off = "/home/pi/badge/off.png"
global img_name = ""
# Set variables for shutdown
offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 21
holdTime = int(sys.argv[2]) if len(sys.argv) >= 3 else 4
# the function called to shut down the RPi
def shutdown():
print("shutdown called")
img_name = off
# Procedure to swap pictures every minute
def swap(img_name):
print("swap called")
if img_name == power:
img_name = name1
elif img_name == name1:
img_name = pur
elif img_name == pur:
img_name = name2
elif img_name == name2:
img_name = trade
elif img_name == trade:
img_name = name3
elif img_name == off:
print ("button pressed")
print("shutting down")
else:
img_name = power
print("loading:",img_name)
img = Image.open(img_name)
inky_display.set_image(img)
inky_display.show()
time.sleep(42) # pause the code for 42 seconds
def main():
print("img:",img_name)
if img_name == off:
img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
message = "exiting"
print(message)
w, h = font.getsize(message)
x = (inky_display.WIDTH / 2) - (w / 2)
y = (inky_display.HEIGHT / 2) - (h / 2)
draw.text((x,y), message, inky_display.BLACK, font)
inky_display.set_img(img)
inky_display.show
print("stop")
raise SystemExit
time.sleep(30)
elif img_name != off:
print("next image")
swap(img_name)
# Handle the shutdown switch
btn = Button(offGPIO, hold_time=holdTime)
btn.when_held = shutdown
main()
I promise, I really was a good VB programmer for over a decade. I feel so very, very, very lost. I just don’t understand what I’m doing wrong, and I really do appreciate all your patience and your help.
pete
I did next to no programming until I bought my first Pi. Then self taught myself python.
I’m no expert in python but one thing I notice is there is no while True:
section in your code?
I believe that is where the main code, the part that repeats over and over is supposed to go.
What is above the while true just gets run the once, all you setting up variables and def’s etc go there.
Have a look at what I posted way back at the beginning of this thread.
I finally got it working the way I wanted – or at least close enough that it’ll do! Here’s what I came up with:
# Badge code
# Import libraries
from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne
import time
# Shutdown libraries
from gpiozero import Button
from signal import pause
import os, sys
# Instantiate inkyPHAT
inky_display = InkyPHAT("red")
inky_display.set_border(inky_display.WHITE)
# Set image file variables
power = "/home/pi/badge/power.png"
name1 = "/home/pi/badge/name1.png"
name2 = "/home/pi/badge/name2.png"
name3 = "/home/pi/badge/name3.png"
pur = "/home/pi/badge/pur.png"
trade = "/home/pi/badge/trade.png"
off = "/home/pi/badge/blank.png"
global img_name
img_name = ""
# Set variables for shutdown
offGPIO = int(sys.argv[1]) if len(sys.argv) >= 2 else 21
holdTime = int(sys.argv[2]) if len(sys.argv) >= 3 else 4
# the function called to shut down the RPi
def shutdown():
img_name = off
img = Image.open(img_name)
inky_display.set_image(img)
inky_display.show()
os.system("sudo poweroff")
time.sleep(5)
# Handle the shutdown switch
btn = Button(offGPIO, hold_time=holdTime)
btn.when_held = shutdown
# Main loop
while img_name != off:
if img_name == power:
img_name = name1
elif img_name == name1:
img_name = pur
elif img_name == pur:
img_name = name2
elif img_name == name2:
img_name = trade
elif img_name == trade:
img_name = name3
elif img_name == off:
print("shutdown called")
else:
img_name = power
if img_name != off:
img = Image.open(img_name)
inky_display.set_image(img)
inky_display.show()
time.sleep(20) # pause the code for 20 seconds
Thank you again for all your help!
Nice to hear you have it working the way you want. I’m sure some of my python code would have those in the know scratching thier heads, lol. But like you say, it works and does what I want it to do. =)
Don’t kid yourself! Your code was very useful.
Thank you again!
pete