-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathexample2-oled96-vgpio.py
102 lines (80 loc) · 3.23 KB
/
example2-oled96-vgpio.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# VIRTUAL-GPIO VERSION
# NOTE: You need to have PIL installed for your python at the hosting PC
from lib_oled96 import ssd1306
from time import sleep
from PIL import ImageFont, ImageDraw, Image
font = ImageFont.load_default()
import virtGPIO as GPIO # These are the only two variant lines !!
i2cbus = GPIO.I2C() #
oled = ssd1306(i2cbus)
draw = oled.canvas # "draw" onto this canvas, then call display() to send the canvas contents to the hardware.
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = oled.height - padding - 1
# Draw a rectangle of the same size of screen
draw.rectangle((0, 0, oled.width-1, oled.height-1), outline=1, fill=0)
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
# Draw an ellipse.
draw.ellipse((x, top, x+shape_width, bottom), outline=1, fill=0)
x += shape_width + padding
# Draw a filled rectangle.
draw.rectangle((x, top, x+shape_width, bottom), outline=1, fill=1)
x += shape_width + padding
# Draw a triangle.
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=1, fill=0)
x += shape_width+padding
# Draw an X.
draw.line((x, bottom, x+shape_width, top), fill=1)
draw.line((x, top, x+shape_width, bottom), fill=1)
#x += shape_width+padding
# Load default font.
font = ImageFont.load_default()
# Nah, second thoughts ... Alternatively load another TTF font.
font = ImageFont.truetype('FreeSerif.ttf', 15)
oled.display()
sleep(3)
# Write two lines of text.
draw.text((x, top), 'Hello', font=font, fill=1)
draw.text((x, top+40), 'World!', font=font, fill=1)
oled.display()
sleep(3)
draw.rectangle((0, 0, oled.width-1, oled.height-1), outline=255, fill=1)
oled.display()
sleep(3)
logo = Image.open('pi_logo.png')
draw.bitmap((32, 0), logo, fill=0)
oled.display()
sleep(3)
draw.rectangle((0, 0, oled.width-1, oled.height-1), outline=1, fill=0)
font = ImageFont.truetype('FreeSerifItalic.ttf', 57)
draw.text((18, 0), 'A5y', font=font, fill=1)
oled.display()
sleep(3)
draw.rectangle((0, 0, oled.width-1, oled.height-1), outline=1, fill=0)
font = ImageFont.truetype('FreeSans.ttf', 10)
draw.text((0, 0), 'Hello me very good mateys ...', font=font, fill=1)
draw.text((0, 10), 'Well now, what would you like', font=font, fill=1)
draw.text((0, 20), 'to be told this sunny Sunday?', font=font, fill=1)
draw.text((0, 30), 'Would a wild story amuse you?', font=font, fill=1)
draw.text((0, 40), 'This is a very long statement,', font=font, fill=1)
draw.text((0, 50), 'so believe it if you like.', font=font, fill=1)
oled.display()
sleep(3)
draw.rectangle((0, 0, oled.width-1, oled.height-1), outline=20, fill=0)
font = ImageFont.truetype('FreeSans.ttf', 14)
draw.text((0, 0), 'Hello me good mateys', font=font, fill=1)
draw.text((0, 15), 'What would you like', font=font, fill=1)
draw.text((0, 30), 'to be told this day?', font=font, fill=1)
draw.text((0, 45), 'This is a long story,', font=font, fill=1)
oled.display()
sleep(3)
oled.onoff(0) # kill the oled. RAM contents still there.
sleep(3)
oled.onoff(1) # Wake it up again. Display contents intact
sleep(3)
oled.cls() # Oled still on, but screen contents now blacked out