-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_input_partial_update.py
84 lines (70 loc) · 2.2 KB
/
text_input_partial_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
import os
import logging
from waveshare_epd import epd4in2_V2
from PIL import Image,ImageDraw,ImageFont
import keyboard
import time
import signal
#initialize some vars
logging.basicConfig(level=logging.INFO)
font18 = ImageFont.truetype('Font.ttc', 13)
text = " "
chars_per_line = 45
lines_on_screen = 25
current_line = 0
def init_display():
#initialize and clear display
epd = epd4in2_V2.EPD()
epd.init()
epd.Clear()
return epd
def init_image(epd):
draw_image = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
draw = ImageDraw.Draw(draw_image)
return draw,draw_image
# Not currently working. This method is displaying some of the CLI for some reason.
def get_input_text(e):
global text
if e.name == 'enter':
logging.info("\nKey Pressed:" + e.name)
e.name += '\n'
elif e.name == 'backspace':
logging.info("\nKey Pressed:" + e.name)
text = text[:-1]
elif e.name == 'space':
text += ' '
elif len(e.name) == 1: # Check if the key is a character
logging.info("\nKey Pressed:" + e.name)
text += e.name
time.sleep(.05)
def partial_update_text(draw, draw_image,text, epd):
logging.info("draw text")
draw.rectangle((0, 0, 400, 300), fill = 255)
draw.text((0, 0), text, font = font18, fill=0)
epd.display_Partial(epd.getbuffer(draw_image))
def full_update_text(draw, draw_image,text, epd):
logging.info("full update")
draw.rectangle((0, 0, 400, 300), fill = 255)
draw.text((0, 0), text, font = font18, fill=0)
epd.display(epd.getbuffer(draw_image))
def cleanup(epd):
# Cleanup and sleep
epd.init()
epd.Clear()
epd.sleep()
# start keyboard listener and callback to get_input_text method
epd = init_display() #initialize the display one time.
draw, draw_image = init_image(epd)
keyboard.on_press(get_input_text, suppress=True) #handles keyboard input
try:
while True:
time.sleep(.1)
partial_update_text(draw, draw_image, text, epd)
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
epd4in2_V2.epdconfig.module_exit(cleanup=True)
cleanup(epd)
exit()