#!/usr/bin/python # -*- coding:utf-8 -*- import sys import os imgdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'img') libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib') fontdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'font') if os.path.exists(libdir): sys.path.append(libdir) from waveshare_epd import epd2in7 import time from PIL import Image,ImageDraw,ImageFont import traceback import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) epd = epd2in7.EPD() ## init and Clear epd.init() epd.Clear(0xFF) font24 = ImageFont.truetype(os.path.join(fontdir, 'Font.ttc'), 24) font18 = ImageFont.truetype(os.path.join(fontdir, 'Font.ttc'), 18) font35 = ImageFont.truetype(os.path.join(fontdir, 'Font.ttc'), 35) key1 = 5 key2 = 6 key3 = 13 key4 = 19 GPIO.setup(key1, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(key2, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(key3, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(key4, GPIO.IN, pull_up_down=GPIO.PUD_UP) def updateDisplay(string): Himage = Image.open(os.path.join(imgdir, 'plm2.bmp')) epd.display(epd.getbuffer(Himage)) time.sleep(4) Himage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame draw = ImageDraw.Draw(Himage) draw.line((5, 5, 259, 5), fill = 0) draw.text((10, 0), 'hello world', font = font24, fill = 0) draw.text((20, 50), string, font = font24, fill = 0) print('update display') epd.display(epd.getbuffer(Himage)) time.sleep(5) def main(): while True: key1state = GPIO.input(key1) key2state = GPIO.input(key2) key3state = GPIO.input(key3) key4state = GPIO.input(key4) if key1state == False: print('Key1 Pressed') time.sleep(0.2) if key2state == False: print('Key2 Pressed') time.sleep(0.2) if key3state == False: print('Key3 Pressed') time.sleep(0.2) epd2in7.epdconfig.module_exit() exit() if key4state == False: print('Key4 Pressed') updateDisplay('Key4 pressed') time.sleep(0.2) if __name__ == '__main__': main()