Skip to content

Commit 7c53f78

Browse files
authored
Merge pull request #237 from pimoroni/patch/examples-4colour
Examples: updated for compatibility with 4 colour display
2 parents e9e25c0 + c124a8d commit 7c53f78

File tree

6 files changed

+84
-19
lines changed

6 files changed

+84
-19
lines changed

examples/phat/calendar-phat.py

100755100644
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@
2727
except TypeError:
2828
raise TypeError("You need to update the Inky library to >= v1.1.0")
2929

30+
flip = inky_display.eeprom.display_variant == 23
31+
BLACK = inky_display.BLACK if not flip else inky_display.WHITE
32+
WHITE = inky_display.WHITE if not flip else inky_display.BLACK
3033

3134
if inky_display.resolution not in ((212, 104), (250, 122)):
3235
w, h = inky_display.resolution
3336
raise RuntimeError("This example does not support {}x{}".format(w, h))
3437

35-
inky_display.set_border(inky_display.BLACK)
38+
inky_display.set_border(BLACK)
3639

3740
# Uncomment the following if you want to rotate the display 180 degrees
3841
# inky_display.h_flip = True
3942
# inky_display.v_flip = True
4043

4144

42-
def create_mask(source, mask=(inky_display.WHITE, inky_display.BLACK, inky_display.RED)):
45+
def create_mask(source, mask=(WHITE, BLACK, inky_display.RED)):
4346
"""Create a transparency mask.
4447
4548
Takes a paletized source image and converts it into a mask
@@ -88,7 +91,7 @@ def print_number(position, number, colour):
8891

8992
# Load our sprite sheet and prepare a mask
9093
text = Image.open(os.path.join(PATH, "resources/calendar.png"))
91-
text_mask = create_mask(text, [inky_display.WHITE])
94+
text_mask = create_mask(text, [WHITE])
9295

9396
# Note: The mask determines which pixels from our sprite sheet we want
9497
# to actually use when calling img.paste().
@@ -116,7 +119,7 @@ def print_number(position, number, colour):
116119
cal_y = 2
117120

118121
# Paint out a black rectangle onto which we'll draw our canvas
119-
draw.rectangle((cal_x, cal_y, cal_x + cal_w - 1, cal_y + cal_h - 1), fill=inky_display.BLACK, outline=inky_display.WHITE)
122+
draw.rectangle((cal_x, cal_y, cal_x + cal_w - 1, cal_y + cal_h - 1), fill=BLACK, outline=WHITE)
120123

121124
# The starting position of the months in our spritesheet
122125
months_x = 2
@@ -145,10 +148,10 @@ def print_number(position, number, colour):
145148
monthyear_x = 28
146149

147150
# Paste in the month name we grabbed from our sprite sheet
148-
img.paste(inky_display.WHITE, (monthyear_x, cal_y + 4), month_mask)
151+
img.paste(WHITE, (monthyear_x, cal_y + 4), month_mask)
149152

150153
# Print the year right below the month
151-
print_number((monthyear_x, cal_y + 5 + col_h), now.year, inky_display.WHITE)
154+
print_number((monthyear_x, cal_y + 5 + col_h), now.year, WHITE)
152155

153156
# Draw the vertical lines which separate the columns
154157
# and also draw the day names into the table header
@@ -162,7 +165,7 @@ def print_number(position, number, colour):
162165
# Crop the relevant day name from our text image
163166
crop_region = (crop_x, 0, crop_x + 16, 9)
164167
day_mask = text_mask.crop(crop_region)
165-
img.paste(inky_display.WHITE, (o_x + 4, cal_y + 2), day_mask)
168+
img.paste(WHITE, (o_x + 4, cal_y + 2), day_mask)
166169

167170
# Offset to the right side of the column and draw the vertical line
168171
o_x += col_w + 1
@@ -187,13 +190,13 @@ def print_number(position, number, colour):
187190
# Draw in the day name.
188191
# If it's the current day, invert the calendar background and text
189192
if (day.day, day.month) == (now.day, now.month):
190-
draw.rectangle((x, y, x + col_w - 1, y + col_h - 1), fill=inky_display.WHITE)
191-
print_number((x + 3, y + 3), day.day, inky_display.BLACK)
193+
draw.rectangle((x, y, x + col_w - 1, y + col_h - 1), fill=WHITE)
194+
print_number((x + 3, y + 3), day.day, BLACK)
192195

193196
# If it's any other day, paint in as white if it's in the current month
194197
# and red if it's in the previous or next month
195198
else:
196-
print_number((x + 3, y + 3), day.day, inky_display.WHITE if day.month == now.month else inky_display.RED)
199+
print_number((x + 3, y + 3), day.day, WHITE if day.month == now.month else inky_display.RED)
197200

198201
# Display the completed calendar on Inky pHAT
199202
inky_display.set_image(img)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This basic example shows how to draw shapes and text on Inky pHAT using PIL.
2+
3+
from font_fredoka_one import FredokaOne
4+
from PIL import Image, ImageDraw, ImageFont
5+
6+
from inky.auto import auto
7+
8+
inky_display = auto(ask_user=True, verbose=True)
9+
10+
WIDTH, HEIGHT = inky_display.width, inky_display.height
11+
12+
# Create new PIL image with a white background
13+
image = Image.new("P", (WIDTH, HEIGHT), inky_display.WHITE)
14+
draw = ImageDraw.Draw(image)
15+
16+
font = ImageFont.truetype(FredokaOne, 18)
17+
18+
# draw some shapes
19+
draw.rectangle((100, 0, 170, 50), fill=inky_display.YELLOW) # Rectangle
20+
draw.ellipse((90, 90, 180, 180), fill=inky_display.RED) # Circle (ellipse)
21+
22+
23+
# draw some text
24+
draw.text((80, 60), "Hello, World!", inky_display.BLACK, font)
25+
26+
inky_display.set_image(image)
27+
inky_display.show()

examples/phat/weather-phat.py

100755100644
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353

5454
inky_display.set_border(inky_display.BLACK)
5555

56+
flip = inky_display.eeprom.display_variant == 23
57+
BLACK = inky_display.BLACK if not flip else inky_display.WHITE
58+
WHITE = inky_display.WHITE if not flip else inky_display.BLACK
59+
5660
# Details to customise your weather display
5761

5862
CITY = "Sheffield"
@@ -83,7 +87,7 @@ def get_weather(address):
8387
return weather
8488

8589

86-
def create_mask(source, mask=(inky_display.WHITE, inky_display.BLACK, inky_display.RED)):
90+
def create_mask(source, mask=(WHITE, BLACK, inky_display.RED)):
8791
"""Create a transparency mask.
8892
8993
Takes a paletized source image and converts it into a mask
@@ -165,13 +169,13 @@ def create_mask(source, mask=(inky_display.WHITE, inky_display.BLACK, inky_displ
165169
# Write text with weather values to the canvas
166170
datetime = time.strftime("%d/%m %H:%M")
167171

168-
draw.text((41, 12), datetime, inky_display.WHITE, font=font)
172+
draw.text((41, 12), datetime, WHITE, font=font)
169173

170-
draw.text((72, 34), "T", inky_display.WHITE, font=font)
171-
draw.text((92, 34), "{}°C".format(temperature), inky_display.WHITE if temperature < WARNING_TEMP else inky_display.RED, font=font)
174+
draw.text((72, 34), "T", WHITE, font=font)
175+
draw.text((92, 34), "{}°C".format(temperature), WHITE if temperature < WARNING_TEMP else inky_display.RED, font=font)
172176

173-
draw.text((72, 58), "W", inky_display.WHITE, font=font)
174-
draw.text((92, 58), "{}kmh".format(windspeed), inky_display.WHITE, font=font)
177+
draw.text((72, 58), "W", WHITE, font=font)
178+
draw.text((92, 58), "{}kmh".format(windspeed), WHITE, font=font)
175179

176180
# Draw the current weather icon over the backdrop
177181
if weather_icon is not None:

examples/what/dither-image-what.py

100755100644
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@
5353

5454
img = img.crop((x0, y0, x1, y1))
5555

56-
# Convert the image to use a white / black / red colour palette
56+
# Convert the image to use a white / black / red/yellow colour palette
5757

5858
pal_img = Image.new("P", (1, 1))
59-
pal_img.putpalette((255, 255, 255, 0, 0, 0, 255, 0, 0) + (0, 0, 0) * 252)
59+
if inky_display.colour == "red/yellow":
60+
pal_img.putpalette((0, 0, 0, 255, 255, 255, 255, 255, 0, 255, 0, 0) + (0, 0, 0) * 251)
61+
else:
62+
pal_img.putpalette((255, 255, 255, 0, 0, 0, 255, 0, 0) + (0, 0, 0) * 252)
6063

6164
img = img.convert("RGB").quantize(palette=pal_img)
6265

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This basic example shows how to draw shapes and text on Inky wHAT using PIL.
2+
3+
from font_fredoka_one import FredokaOne
4+
from PIL import Image, ImageDraw, ImageFont
5+
6+
from inky.auto import auto
7+
8+
inky_display = auto(ask_user=True, verbose=True)
9+
10+
WIDTH, HEIGHT = inky_display.width, inky_display.height
11+
12+
# Create new PIL image with a white background
13+
image = Image.new("P", (WIDTH, HEIGHT), inky_display.WHITE)
14+
draw = ImageDraw.Draw(image)
15+
16+
font = ImageFont.truetype(FredokaOne, 28)
17+
18+
# draw some shapes
19+
draw.rectangle((0, 0, 200, 200), fill=inky_display.YELLOW) # Rectangle
20+
draw.ellipse((200, 200, 400, 400), fill=inky_display.RED) # Circle (ellipse)
21+
22+
# draw some text
23+
draw.text((120, 130), "Hello, World!", inky_display.BLACK, font)
24+
25+
inky_display.set_image(image)
26+
inky_display.show()

examples/what/quotes-what.py

100755100644
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
inky_display.set_border(inky_display.WHITE)
3333
# inky_display.set_rotation(180)
3434

35+
3536
def getsize(font, text):
3637
_, _, right, bottom = font.getbbox(text)
3738
return (right, bottom)
3839

40+
3941
# This function will take a quote as a string, a width to fit
4042
# it into, and a font (one that's been loaded) and then reflow
4143
# that quote with newlines to fit into the space required.
@@ -65,7 +67,7 @@ def reflow_quote(quote, width, font):
6567

6668
# Create a new canvas to draw on
6769

68-
img = Image.new("P", (WIDTH, HEIGHT))
70+
img = Image.new("P", (WIDTH, HEIGHT), inky_display.WHITE)
6971
draw = ImageDraw.Draw(img)
7072

7173
# Load the fonts

0 commit comments

Comments
 (0)