Button Shim and Inky navigation

Greetings all

I am very new to Python and I am still getting to grips with it - I am at a point where I have collected a whole load of phats and hats and I want to start combining them together for projects I have in mind. Right now I really want to get my inkyphat to display a picture when I press one of the buttons on my button shim. I have managed to create suitable artwork for the inky but I am unsure how to write the code to achieve this can anyone help?

Many thanks for any help I did try searching the forums but I failed to find anything that seemed relevant to what I am trying to achieve. If I consider my goal in blocks it would be along the lines of:

-Program starts and Pi displays an image and waits for button press

-user navigates through selection of choices posed by an image displayed on inky

-each choice advances the user through an image based narrative

First place I’d go is here,

And check for conflicts with pins or i2c addresses etc.

This
https://pinout.xyz/phatstack

will do it for you, even if you don’t own or use a phat Stack.

Keep in mind the refresh rate on the inky on the slow side. It takes a while to update etc. 5 seconds or more I believe.

Once you do the installs for both, have a look at the examples for both.
I would check out the keyboard one for the button shim


Not sure which one fits for the inky

Thanks, I looked at the stack compatibility page but button shim isn’t listed only the touch phat.

Button shim pinout is here, https://pinout.xyz/pinout/button_shim#
I2C address (0x3f) Its linked to from the product page.
Which inky do you have?

I have the inkyphat display.

Inkyphat pinout is here https://pinout.xyz/pinout/inky_phat#
I don’t see any conflicts.

I don’t own either of them so your a bit on your own as far as coding it in python.
Unless somebody else replies.

No I appreciate any help people are kind enough to give! I have found some code online which seems somewhat similar to what I am trying to do thanks for checking with the pinout guide for me.

I’m working with the black and white version of the inky but I ordered a bunch of stuff which includes some of the red versions so I’m keen to work on the problems I’m having with the code.

If your clip and pasting code try to copy it right into an editor like say idle, that should keep the syntax intact. That will cut down on errors. And there is a check module option in the Run menu options in idle.
That will flag a lot of errors like incorrect indents etc. Resist the urge to save it as a text file, you want to save it as a .py file.

Here is the mess of things I’ve made so far…

#!/usr/bin/env python
import time
import subprocess
import signal
from PIL import Image
import sys
import buttonshim
import inky

print(""“Test buttons
“””)

Button A

@buttonshim.on_release(buttonshim.BUTTON_A)
def button_a(button, pressed):
inkyphat.colour = BLACK
inkyphat.set_border(inky.BLACK)
inkyphat.set_colour(‘black’)
inkyphat.set_rotation(180)
inkyphat.set_image(Image.open("/home/pi/Pimoroni/inky/examples/img/img1.png"))
inkyphat.show()

Button B

@buttonshim.on_release(buttonshim.BUTTON_B)
def button_b(button, pressed):
inkyphat.colour = BLACK
inkyphat.set_border(inky.BLACK)
inkyphat.set_colour(‘black’)
inkyphat.set_rotation(180)
inkyphat.set_image(Image.open("/home/pi/Pimoroni/inky/examples/img/img2.png"))
inkyphat.show()

signal.pause()

If you past your code between 3 ``` and 3 more ` it will be in code tags and keep the indents etc.

code

On my keyboard its the key just under the esc key with a ~ and a ` on it.

An update - My code is buggy but I figured out how to achieve what I was trying to do so far I have pasted the code below. It starts by opening a completely white image and then waits for a button press which will load up whatever image is stored.

#!/usr/bin/env python

import time
import subprocess
import signal
from PIL import Image
import sys
import buttonshim
from inky import InkyPHAT

print(""“It can only fail.
“””)

inky_display = InkyPHAT(“red”)
cooldown = time.time() - 2.5
lastbutton = “”
#inky_display.colour = ‘black’
#inky_display.set_border(1)

#inky_display.rectangle((0,0, 212,104), fill=1, outline=0)

def flash(length, col):
if col == 1: #red
buttonshim.set_pixel(255,0,0)
else: # green
buttonshim.set_pixel(0,255,0)
time.sleep(length)

def display(image,button):
global cooldown
global lastbutton
if lastbutton == button:
print(“go easy”)
return
elif time.time() - cooldown < 2.5:
print(“Just no”)
return
else:
flash(0,1)
inky_display.set_image(Image.open(image))
inky_display.show()
cooldown = time.time()
lastbutton = button
flash(2.5, 2)

display("/home/pi/Pimoroni/inky/examples/img/imgwht.png", “0”)

# Button A 

@buttonshim.on_release(buttonshim.BUTTON_A)
def button_a(button, pressed):
display("/home/pi/Pimoroni/inky/examples/img/img1.png", “a”)

Button B

@buttonshim.on_release(buttonshim.BUTTON_B)
def button_b(button, pressed):
display("/home/pi/Pimoroni/inky/examples/img/img2.png", “b”)
# Button C
@buttonshim.on_release(buttonshim.BUTTON_C)
def button_b(button, pressed):
display("/home/pi/Pimoroni/inky/examples/img/img3.png", “b”)
# Button D
@buttonshim.on_release(buttonshim.BUTTON_D)
def button_b(button, pressed):
display("/home/pi/Pimoroni/inky/examples/img/img4.png", “b”)
# Button E
@buttonshim.on_release(buttonshim.BUTTON_E)
def button_b(button, pressed):
display("/home/pi/Pimoroni/inky/examples/img/img5.png", “b”)
signal.pause()

If it errors just Google it to death and you’ll eventually figure out what’s not working. And or post the error message here. ;)