aboutsummaryrefslogtreecommitdiff
path: root/epaperisslow.py
blob: ce6f0773c63790adfcf61d161d2554216487baed (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#---------------------------------------------------------------epaperisslow.py
# playing around with 2.13" HD mono pi hat.
#
# screen drawing takes 9-10 seconds per update.

from netifaces import interfaces, ifaddresses, AF_INET

def ip4_addresses():
    ip_list = []
    for interface in interfaces():
        for link in ifaddresses(interface)[AF_INET]:
            ip_list.append(link['addr'])
    return ip_list

from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M  ")

import time
import busio
import board
from digitalio import DigitalInOut, Direction

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from adafruit_epd.epd import Adafruit_EPD
from adafruit_epd.ssd1675b import Adafruit_SSD1675B  # pylint: disable=unused-import
#from adafruit_epd.ssd1675 import Adafruit_SSD1675  # pylint: disable=unused-import
import math
import random


# create two buttons
switch1 = DigitalInOut(board.D6)
switch2 = DigitalInOut(board.D5)
switch1.direction = Direction.INPUT
switch2.direction = Direction.INPUT

# create the spi device and pins we will need
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
ecs = DigitalInOut(board.CE0)
dc = DigitalInOut(board.D22)
rst = DigitalInOut(board.D27)
busy = DigitalInOut(board.D17)

# give them all to our driver
display = Adafruit_SSD1675B(
    122,
    250,
    spi,  # 2.13" HD mono display (rev B)
    cs_pin=ecs,
    dc_pin=dc,
    sramcs_pin=None,
    rst_pin=rst,
    busy_pin=busy,
)
display.rotation = 1

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = display.width
height = display.height
image = Image.new("RGB", (width, height))
splashimage = Image.new("RGB", (width, height))

WHITE = (0xFF, 0xFF, 0xFF)
BLACK = (0x00, 0x00, 0x00)
print("clearing display")
# clear the buffer
display.fill(Adafruit_EPD.WHITE)
# clear it out
display.display()

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
splash = ImageDraw.Draw(splashimage)
# empty it
draw.rectangle((0, 0, width, height), fill=WHITE)
splash.rectangle((0, 0, width, height), fill=BLACK)
print("drawing box")

# Draw an outline box
draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 5
shape_width = 30
top = padding
bottom = height - padding
# Move left to right keeping track of the current x position for drawing shapes.
x = padding

# Load default font.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 72)

draw.text((x, top + 30), current_time, font=font, fill=BLACK)
splash.text((x, top + 30), current_time, font=font, fill=WHITE)

print("entering the loops")

display.image(splashimage)
display.display()

old_ticks = time.monotonic() - 50.0
image2display=splashimage

while True:
    
    ticks = time.monotonic()
    
    if not switch1.value:
        print("Switch 1")
        image2display=image
        display.image(image2display)
        display.display()
        while not switch1.value:
            time.sleep(0.01)
            
    if not switch2.value:
        print("Switch 2")
        image2display=splashimage
        display.image(image2display)
        display.display()
        while not switch2.value:
            time.sleep(0.01)
            
    # print( ticks - old_ticks)
    
    if(( ticks  - old_ticks) >= 50.0):
        current_time=datetime.now().strftime("%H:%M  ")
        print("updating "+current_time)
        draw.rectangle((0, 0, width, height), fill=WHITE)
        splash.rectangle((0, 0, width, height), fill=BLACK)
        draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE)
        draw.text((padding,top+30),current_time,font=font, fill=BLACK)
        splash.text((x,top+30),current_time, font=font, fill=WHITE)
        display.image(image2display)
        display.display()
        old_ticks=time.monotonic()