Skip to content

Instantly share code, notes, and snippets.

@RaneWallin
Last active February 2, 2024 14:03
Show Gist options
  • Save RaneWallin/fd73ddbffdabea23358f722adb9f4075 to your computer and use it in GitHub Desktop.
Save RaneWallin/fd73ddbffdabea23358f722adb9f4075 to your computer and use it in GitHub Desktop.
Complete demo project for the Waveshare 2.7" HAT on Raspberry Pi
import sys # import sys
sys.path.insert(1, "./lib") # add the lib folder to sys so python can find the libraries
import epd2in7b # import the display drivers
from PIL import Image,ImageDraw,ImageFont # import the image libraries
import time
from gpiozero import Button # import the Button control from gpiozero
btn1 = Button(5) # assign each button to a variable
btn2 = Button(6) # by passing in the pin number
btn3 = Button(13) # associated with the button
btn4 = Button(19) #
epd = epd2in7b.EPD() # get the display object and assing to epd
epd.init() # initialize the display
print("Clear...") # print message to console (not display) for debugging
epd.Clear(0xFF) # clear the display
# Print a message to the screen
# @params string
def printToDisplay(string):
# Drawing on the Horizontal image. We must create an image object for both the black layer
# and the red layer, even if we are only printing to one layer
HBlackImage = Image.new('1', (epd2in7b.EPD_HEIGHT, epd2in7b.EPD_WIDTH), 255) # 298*126
HRedImage = Image.new('1', (epd2in7b.EPD_HEIGHT, epd2in7b.EPD_WIDTH), 255) # 298*126
# create a draw object and the font object we will use for the display
draw = ImageDraw.Draw(HBlackImage)
font = ImageFont.truetype('/usr/share/fonts/truetype/google/Bangers-Regular.ttf', 30)
# draw the text to the display. First argument is starting location of the text in pixels
draw.text((25, 65), string, font = font, fill = 0)
# Add the images to the display. Both the black and red layers need to be passed in, even
# if we did not add anything to one of them
epd.display(epd.getbuffer(HBlackImage), epd.getbuffer(HRedImage))
# Handle button presses
# param Button (passed from when_pressed)
def handleBtnPress(btn):
# get the button pin number
pinNum = btn.pin.number
# python hack for a switch statement. The number represents the pin number and
# the value is the message we will print
switcher = {
5: "Hello, World!",
6: "This is my first \nRPi project.",
13: "Hope you liked it.",
19: "Goodbye"
}
# get the string based on the passed in button and send it to printToDisplay()
msg = switcher.get(btn.pin.number, "Error")
printToDisplay(msg)
# tell the button what to do when pressed
btn1.when_pressed = handleBtnPress
btn2.when_pressed = handleBtnPress
btn3.when_pressed = handleBtnPress
btn4.when_pressed = handleBtnPress
@gudongfeng
Copy link

gudongfeng commented May 2, 2020

I've encountered one issue with this code. I need to append the following code to pause the program, otherwise, it will just stop the execution and not listening to any button click.

from signal import pause
pause()

@emilyboda
Copy link

@gudongfeng I'm also having this issue. Where do you insert the pause() command? I've put it just after the epd.Clear() but then nothing happens when I press the buttons, it pauses indefinitely.

@gudongfeng
Copy link

@emilyboda Can you try to append it at the end of the file?

@chedstrom
Copy link

I really appreciate you putting together this little demo for beginners, especially the use of the buttons. It wasn't working for me directly as written from your dev.to site, kept getting the "no parent package" error, but I was able to get it to work by borrowing WaveShare's latest code in their demo programs, and saved my program in the same folder as the waveshare test files for now.

For anyone else having this issue, i got around it by using the first few lines of code from the waveshare demo files. Since I have the 2.7in black only HAT, i just swapped out everything above Try: in the epd_2in7_test.py file and then used Rane's code below that. If you also have the 2.7 grayscale only display, make sure to reference the correct epd in the code (epd2in7, not epd2in7b), and to change HBlackImage to HImage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment