Skip to content

Commit 1c21428

Browse files
committed
belinted
1 parent b2a4c9e commit 1c21428

File tree

5 files changed

+54
-44
lines changed

5 files changed

+54
-44
lines changed

examples/bonnet_buttons.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
2121

22+
from PIL import Image, ImageDraw
2223

23-
import time
2424
import board
2525
import busio
2626
from digitalio import DigitalInOut, Direction, Pull
2727
import adafruit_ssd1306
28-
from PIL import Image, ImageDraw, ImageFont
2928

3029
# Create the I2C interface.
3130
i2c = busio.I2C(board.SCL, board.SDA)
@@ -36,18 +35,31 @@
3635

3736
# Input pins:
3837
button_A = DigitalInOut(board.D5)
38+
button_A.direction = Direction.INPUT
3939
button_A.pull = Pull.UP
40+
4041
button_B = DigitalInOut(board.D6)
42+
button_B.direction = Direction.INPUT
4143
button_B.pull = Pull.UP
44+
4245
button_L = DigitalInOut(board.D27)
46+
button_L.direction = Direction.INPUT
4347
button_L.pull = Pull.UP
48+
4449
button_R = DigitalInOut(board.D23)
50+
button_R.direction = Direction.INPUT
4551
button_R.pull = Pull.UP
52+
4653
button_U = DigitalInOut(board.D17)
54+
button_U.direction = Direction.INPUT
4755
button_U.pull = Pull.UP
56+
4857
button_D = DigitalInOut(board.D22)
58+
button_D.direction = Direction.INPUT
4959
button_D.pull = Pull.UP
60+
5061
button_C = DigitalInOut(board.D4)
62+
button_C.direction = Direction.INPUT
5163
button_C.pull = Pull.UP
5264

5365

@@ -65,7 +77,7 @@
6577
draw = ImageDraw.Draw(image)
6678

6779
# Draw a black filled box to clear the image.
68-
draw.rectangle((0,0,width,height), outline=0, fill=0)
80+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
6981

7082

7183
while True:
@@ -90,19 +102,19 @@
90102
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) #down filled
91103

92104
if button_C.value: # button is released
93-
draw.rectangle((20, 22,40,40), outline=255, fill=0) #center
105+
draw.rectangle((20, 22, 40, 40), outline=255, fill=0) #center
94106
else: # button is pressed:
95-
draw.rectangle((20, 22,40,40), outline=255, fill=1) #center filled
107+
draw.rectangle((20, 22, 40, 40), outline=255, fill=1) #center filled
96108

97109
if button_A.value: # button is released
98-
draw.ellipse((70,40,90,60), outline=255, fill=0) #A button
110+
draw.ellipse((70, 40, 90, 60), outline=255, fill=0) #A button
99111
else: # button is pressed:
100-
draw.ellipse((70,40,90,60), outline=255, fill=1) #A button filled
112+
draw.ellipse((70, 40, 90, 60), outline=255, fill=1) #A button filled
101113

102114
if button_B.value: # button is released
103-
draw.ellipse((100,20,120,40), outline=255, fill=0) #B button
115+
draw.ellipse((100, 20, 120, 40), outline=255, fill=0) #B button
104116
else: # button is pressed:
105-
draw.ellipse((100,20,120,40), outline=255, fill=1) #B button filled
117+
draw.ellipse((100, 20, 120, 40), outline=255, fill=1) #B button filled
106118

107119
if not button_A.value and not button_B.value and not button_C.value:
108120
catImage = Image.open('happycat_oled_64.ppm').convert('1')

examples/pillow_animate.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
21+
2122
import math
2223
import time
24+
from PIL import Image, ImageDraw, ImageFont
2325

2426
from board import SCL, SDA
2527
import busio
2628
import adafruit_ssd1306
2729

28-
from PIL import Image, ImageDraw, ImageFont
29-
3030
# Create the I2C interface.
3131
i2c = busio.I2C(SCL, SDA)
3232

3333
# Create the SSD1306 OLED class.
34-
# The first two parameters are the pixel width and pixel height. Change these
35-
# to the right size for your display!
34+
# The first two parameters are the pixel width and pixel height.
35+
# Change these to the right size for your display!
3636
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
3737

3838
# Note you can change the I2C address, or add a reset pin:
@@ -53,15 +53,17 @@
5353
# Load default font.
5454
font = ImageFont.load_default()
5555

56-
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as this python script!
56+
# Alternatively load a TTF font. Make sure the .ttf font file is in the
57+
# same directory as this python script!
5758
# Some nice fonts to try: http://www.dafont.com/bitmap.php
5859
# font = ImageFont.truetype('Minecraftia.ttf', 8)
5960

6061
# Create drawing object.
6162
draw = ImageDraw.Draw(image)
6263

6364
# Define text and get total width.
64-
text = 'SSD1306 ORGANIC LED DISPLAY. THIS IS AN OLD SCHOOL DEMO SCROLLER!! GREETZ TO: LADYADA & THE ADAFRUIT CREW, TRIXTER, FUTURE CREW, AND FARBRAUSCH'
65+
text = 'SSD1306 ORGANIC LED DISPLAY. THIS IS AN OLD SCHOOL DEMO SCROLLER!!'+\
66+
'GREETZ TO: LADYADA & THE ADAFRUIT CREW, TRIXTER, FUTURE CREW, AND FARBRAUSCH'
6567
maxwidth, unused = draw.textsize(text, font=font)
6668

6769
# Set animation and sine wave parameters.
@@ -75,7 +77,7 @@
7577
pos = startpos
7678
while True:
7779
# Clear image buffer by drawing a black filled box.
78-
draw.rectangle((0,0,width,height), outline=0, fill=0)
80+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
7981
# Enumerate characters and draw them offset vertically based on a sine wave.
8082
x = pos
8183
for i, c in enumerate(text):

examples/pillow_images.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
21-
import time
2221

23-
import time
22+
from PIL import Image
2423

2524
from board import SCL, SDA
2625
import busio
2726
import adafruit_ssd1306
2827

29-
from PIL import Image, ImageDraw, ImageFont
30-
31-
import subprocess
32-
3328
# Create the I2C interface.
3429
i2c = busio.I2C(SCL, SDA)
3530

examples/pillow_shapes.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
21-
import time
21+
22+
from PIL import Image, ImageDraw, ImageFont
2223

2324
from board import SCL, SDA
2425
import busio
2526
import adafruit_ssd1306
2627

27-
from PIL import Image, ImageDraw, ImageFont
28-
29-
3028
# Create the I2C interface.
3129
i2c = busio.I2C(SCL, SDA)
3230

@@ -52,7 +50,7 @@
5250
draw = ImageDraw.Draw(image)
5351

5452
# Draw a black filled box to clear the image.
55-
draw.rectangle((0,0,width,height), outline=0, fill=0)
53+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
5654

5755
# Draw some shapes.
5856
# First define some constants to allow easy resizing of shapes.
@@ -63,7 +61,7 @@
6361
# Move left to right keeping track of the current x position for drawing shapes.
6462
x = padding
6563
# Draw an ellipse.
66-
draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
64+
draw.ellipse((x, top, x+shape_width, bottom), outline=255, fill=0)
6765
x += shape_width+padding
6866
# Draw a rectangle.
6967
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
@@ -79,12 +77,13 @@
7977
# Load default font.
8078
font = ImageFont.load_default()
8179

82-
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
80+
# Alternatively load a TTF font. Make sure the .ttf font file is in the
81+
# same directory as the python script!
8382
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
8483
#font = ImageFont.truetype('Minecraftia.ttf', 8)
8584

8685
# Write two lines of text.
87-
draw.text((x, top), 'Hello', font=font, fill=255)
86+
draw.text((x, top), 'Hello', font=font, fill=255)
8887
draw.text((x, top+20), 'World!', font=font, fill=255)
8988

9089
# Display image.

examples/stats.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
# not support PIL/pillow (python imaging library)!
2626

2727
import time
28+
import subprocess
29+
from PIL import Image, ImageDraw, ImageFont
2830

2931
from board import SCL, SDA
3032
import busio
3133
import adafruit_ssd1306
3234

33-
from PIL import Image, ImageDraw, ImageFont
3435

35-
import subprocess
3636

3737
# Create the I2C interface.
3838
i2c = busio.I2C(SCL, SDA)
@@ -56,7 +56,7 @@
5656
draw = ImageDraw.Draw(image)
5757

5858
# Draw a black filled box to clear the image.
59-
draw.rectangle((0,0,width,height), outline=0, fill=0)
59+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
6060

6161
# Draw some shapes.
6262
# First define some constants to allow easy resizing of shapes.
@@ -70,31 +70,33 @@
7070
# Load default font.
7171
font = ImageFont.load_default()
7272

73-
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
73+
# Alternatively load a TTF font. Make sure the .ttf font file is in the
74+
# same directory as the python script!
7475
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
7576
#font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)
7677

7778
while True:
7879

7980
# Draw a black filled box to clear the image.
80-
draw.rectangle((0,0,width,height), outline=0, fill=0)
81+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
8182

82-
# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
83+
# Shell scripts for system monitoring from here:
84+
# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
8385
cmd = "hostname -I | cut -d\' \' -f1"
84-
IP = subprocess.check_output(cmd, shell = True ).decode("utf-8")
86+
IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
8587
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
86-
CPU = subprocess.check_output(cmd, shell = True ).decode("utf-8")
88+
CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
8789
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'"
88-
MemUsage = subprocess.check_output(cmd, shell = True ).decode("utf-8")
90+
MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
8991
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'"
90-
Disk = subprocess.check_output(cmd, shell = True ).decode("utf-8")
92+
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
9193

9294
# Write four lines of text.
9395

94-
draw.text((x, top+0), "IP: " + str(IP), font=font, fill=255)
95-
draw.text((x, top+8), str(CPU), font=font, fill=255)
96-
draw.text((x, top+16), str(MemUsage), font=font, fill=255)
97-
draw.text((x, top+25), str(Disk), font=font, fill=255)
96+
draw.text((x, top+0), "IP: "+IP, font=font, fill=255)
97+
draw.text((x, top+8), CPU, font=font, fill=255)
98+
draw.text((x, top+16), MemUsage, font=font, fill=255)
99+
draw.text((x, top+25), Disk, font=font, fill=255)
98100

99101
# Display image.
100102
disp.image(image)

0 commit comments

Comments
 (0)