Skip to content

Commit b2a4c9e

Browse files
committed
all the linux/raspi examples
1 parent 95fe1ce commit b2a4c9e

File tree

7 files changed

+477
-0
lines changed

7 files changed

+477
-0
lines changed

examples/bonnet_buttons.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright (c) 2017 Adafruit Industries
2+
# Author: James DeVito
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
23+
import time
24+
import board
25+
import busio
26+
from digitalio import DigitalInOut, Direction, Pull
27+
import adafruit_ssd1306
28+
from PIL import Image, ImageDraw, ImageFont
29+
30+
# Create the I2C interface.
31+
i2c = busio.I2C(board.SCL, board.SDA)
32+
# Create the SSD1306 OLED class.
33+
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
34+
35+
36+
37+
# Input pins:
38+
button_A = DigitalInOut(board.D5)
39+
button_A.pull = Pull.UP
40+
button_B = DigitalInOut(board.D6)
41+
button_B.pull = Pull.UP
42+
button_L = DigitalInOut(board.D27)
43+
button_L.pull = Pull.UP
44+
button_R = DigitalInOut(board.D23)
45+
button_R.pull = Pull.UP
46+
button_U = DigitalInOut(board.D17)
47+
button_U.pull = Pull.UP
48+
button_D = DigitalInOut(board.D22)
49+
button_D.pull = Pull.UP
50+
button_C = DigitalInOut(board.D4)
51+
button_C.pull = Pull.UP
52+
53+
54+
# Clear display.
55+
disp.fill(0)
56+
disp.show()
57+
58+
# Create blank image for drawing.
59+
# Make sure to create image with mode '1' for 1-bit color.
60+
width = disp.width
61+
height = disp.height
62+
image = Image.new('1', (width, height))
63+
64+
# Get drawing object to draw on image.
65+
draw = ImageDraw.Draw(image)
66+
67+
# Draw a black filled box to clear the image.
68+
draw.rectangle((0,0,width,height), outline=0, fill=0)
69+
70+
71+
while True:
72+
if button_U.value: # button is released
73+
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0) #Up
74+
else: # button is pressed:
75+
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1) #Up filled
76+
77+
if button_L.value: # button is released
78+
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0) #left
79+
else: # button is pressed:
80+
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1) #left filled
81+
82+
if button_R.value: # button is released
83+
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) #right
84+
else: # button is pressed:
85+
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=1) #right filled
86+
87+
if button_D.value: # button is released
88+
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) #down
89+
else: # button is pressed:
90+
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) #down filled
91+
92+
if button_C.value: # button is released
93+
draw.rectangle((20, 22,40,40), outline=255, fill=0) #center
94+
else: # button is pressed:
95+
draw.rectangle((20, 22,40,40), outline=255, fill=1) #center filled
96+
97+
if button_A.value: # button is released
98+
draw.ellipse((70,40,90,60), outline=255, fill=0) #A button
99+
else: # button is pressed:
100+
draw.ellipse((70,40,90,60), outline=255, fill=1) #A button filled
101+
102+
if button_B.value: # button is released
103+
draw.ellipse((100,20,120,40), outline=255, fill=0) #B button
104+
else: # button is pressed:
105+
draw.ellipse((100,20,120,40), outline=255, fill=1) #B button filled
106+
107+
if not button_A.value and not button_B.value and not button_C.value:
108+
catImage = Image.open('happycat_oled_64.ppm').convert('1')
109+
disp.image(catImage)
110+
else:
111+
# Display image.
112+
disp.image(image)
113+
114+
disp.show()

examples/happycat_oled_32.ppm

Whitespace-only changes.

examples/happycat_oled_64.ppm

24.1 KB
Binary file not shown.

examples/pillow_animate.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
import math
22+
import time
23+
24+
from board import SCL, SDA
25+
import busio
26+
import adafruit_ssd1306
27+
28+
from PIL import Image, ImageDraw, ImageFont
29+
30+
# Create the I2C interface.
31+
i2c = busio.I2C(SCL, SDA)
32+
33+
# 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!
36+
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
37+
38+
# Note you can change the I2C address, or add a reset pin:
39+
#disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin)
40+
41+
# Get display width and height.
42+
width = disp.width
43+
height = disp.height
44+
45+
# Clear display.
46+
disp.fill(0)
47+
disp.show()
48+
49+
# Create image buffer.
50+
# Make sure to create image with mode '1' for 1-bit color.
51+
image = Image.new('1', (width, height))
52+
53+
# Load default font.
54+
font = ImageFont.load_default()
55+
56+
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as this python script!
57+
# Some nice fonts to try: http://www.dafont.com/bitmap.php
58+
# font = ImageFont.truetype('Minecraftia.ttf', 8)
59+
60+
# Create drawing object.
61+
draw = ImageDraw.Draw(image)
62+
63+
# 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+
maxwidth, unused = draw.textsize(text, font=font)
66+
67+
# Set animation and sine wave parameters.
68+
amplitude = height/4
69+
offset = height/2 - 4
70+
velocity = -2
71+
startpos = width
72+
73+
# Animate text moving in sine wave.
74+
print('Press Ctrl-C to quit.')
75+
pos = startpos
76+
while True:
77+
# Clear image buffer by drawing a black filled box.
78+
draw.rectangle((0,0,width,height), outline=0, fill=0)
79+
# Enumerate characters and draw them offset vertically based on a sine wave.
80+
x = pos
81+
for i, c in enumerate(text):
82+
# Stop drawing if off the right side of screen.
83+
if x > width:
84+
break
85+
# Calculate width but skip drawing if off the left side of screen.
86+
if x < -10:
87+
char_width, char_height = draw.textsize(c, font=font)
88+
x += char_width
89+
continue
90+
# Calculate offset from sine wave.
91+
y = offset+math.floor(amplitude*math.sin(x/float(width)*2.0*math.pi))
92+
# Draw text.
93+
draw.text((x, y), c, font=font, fill=255)
94+
# Increment x position based on chacacter width.
95+
char_width, char_height = draw.textsize(c, font=font)
96+
x += char_width
97+
98+
# Draw the image buffer.
99+
disp.image(image)
100+
disp.show()
101+
102+
# Move position for next frame.
103+
pos += velocity
104+
# Start over if text has scrolled completely off left side of screen.
105+
if pos < -maxwidth:
106+
pos = startpos
107+
108+
# Pause briefly before drawing next frame.
109+
time.sleep(0.05)

examples/pillow_images.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
import time
22+
23+
import time
24+
25+
from board import SCL, SDA
26+
import busio
27+
import adafruit_ssd1306
28+
29+
from PIL import Image, ImageDraw, ImageFont
30+
31+
import subprocess
32+
33+
# Create the I2C interface.
34+
i2c = busio.I2C(SCL, SDA)
35+
36+
# Create the SSD1306 OLED class.
37+
# The first two parameters are the pixel width and pixel height. Change these
38+
# to the right size for your display!
39+
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
40+
41+
# Note you can change the I2C address, or add a reset pin:
42+
#disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin)
43+
44+
# Clear display.
45+
disp.fill(0)
46+
disp.show()
47+
48+
49+
# Load image based on OLED display height. Note that image is converted to 1 bit color.
50+
if disp.height == 64:
51+
image = Image.open('happycat_oled_64.ppm').convert('1')
52+
else:
53+
image = Image.open('happycat_oled_32.ppm').convert('1')
54+
55+
# Alternatively load a different format image, resize it, and convert to 1 bit color.
56+
#image = Image.open('happycat.png').resize((disp.width, disp.height), Image.ANTIALIAS).convert('1')
57+
58+
# Display image.
59+
disp.image(image)
60+
disp.show()

examples/pillow_shapes.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
import time
22+
23+
from board import SCL, SDA
24+
import busio
25+
import adafruit_ssd1306
26+
27+
from PIL import Image, ImageDraw, ImageFont
28+
29+
30+
# Create the I2C interface.
31+
i2c = busio.I2C(SCL, SDA)
32+
33+
# 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!
36+
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
37+
38+
# Note you can change the I2C address, or add a reset pin:
39+
#disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin)
40+
41+
# Clear display.
42+
disp.fill(0)
43+
disp.show()
44+
45+
# Create blank image for drawing.
46+
# Make sure to create image with mode '1' for 1-bit color.
47+
width = disp.width
48+
height = disp.height
49+
image = Image.new('1', (width, height))
50+
51+
# Get drawing object to draw on image.
52+
draw = ImageDraw.Draw(image)
53+
54+
# Draw a black filled box to clear the image.
55+
draw.rectangle((0,0,width,height), outline=0, fill=0)
56+
57+
# Draw some shapes.
58+
# First define some constants to allow easy resizing of shapes.
59+
padding = 2
60+
shape_width = 20
61+
top = padding
62+
bottom = height-padding
63+
# Move left to right keeping track of the current x position for drawing shapes.
64+
x = padding
65+
# Draw an ellipse.
66+
draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
67+
x += shape_width+padding
68+
# Draw a rectangle.
69+
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
70+
x += shape_width+padding
71+
# Draw a triangle.
72+
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
73+
x += shape_width+padding
74+
# Draw an X.
75+
draw.line((x, bottom, x+shape_width, top), fill=255)
76+
draw.line((x, top, x+shape_width, bottom), fill=255)
77+
x += shape_width+padding
78+
79+
# Load default font.
80+
font = ImageFont.load_default()
81+
82+
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
83+
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
84+
#font = ImageFont.truetype('Minecraftia.ttf', 8)
85+
86+
# Write two lines of text.
87+
draw.text((x, top), 'Hello', font=font, fill=255)
88+
draw.text((x, top+20), 'World!', font=font, fill=255)
89+
90+
# Display image.
91+
disp.image(image)
92+
disp.show()

0 commit comments

Comments
 (0)