-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons_test.py
121 lines (96 loc) · 3.87 KB
/
buttons_test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# This test file is to test the LCD screen and button functionalities.
# run this script to verify that all components work correctly.
# SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries
# SPDX-License-Identifier: MIT
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
from PIL import Image, ImageDraw
import adafruit_ssd1306
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# Create the SSD1306 OLED class.
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
# Input pins:
button_A = DigitalInOut(board.D5)
button_A.direction = Direction.INPUT
button_A.pull = Pull.UP
button_B = DigitalInOut(board.D6)
button_B.direction = Direction.INPUT
button_B.pull = Pull.UP
button_L = DigitalInOut(board.D27)
button_L.direction = Direction.INPUT
button_L.pull = Pull.UP
button_R = DigitalInOut(board.D23)
button_R.direction = Direction.INPUT
button_R.pull = Pull.UP
button_U = DigitalInOut(board.D17)
button_U.direction = Direction.INPUT
button_U.pull = Pull.UP
button_D = DigitalInOut(board.D22)
button_D.direction = Direction.INPUT
button_D.pull = Pull.UP
button_C = DigitalInOut(board.D4)
button_C.direction = Direction.INPUT
button_C.pull = Pull.UP
# Clear display.
disp.fill(0)
disp.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
# test something
width = disp.width
height = disp.height
image = Image.new("1", (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
while True:
if button_U.value: # button is released
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=0) # Up
else: # button is pressed:
draw.polygon([(20, 20), (30, 2), (40, 20)], outline=255, fill=1) # Up filled
print('Up')
if button_L.value: # button is released
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=0) # left
else: # button is pressed:
draw.polygon([(0, 30), (18, 21), (18, 41)], outline=255, fill=1) # left filled
print('Left')
if button_R.value: # button is released
draw.polygon([(60, 30), (42, 21), (42, 41)], outline=255, fill=0) # right
else: # button is pressed:
draw.polygon(
[(60, 30), (42, 21), (42, 41)], outline=255, fill=1
) # right filled
print('right')
if button_D.value: # button is released
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=0) # down
else: # button is pressed:
draw.polygon([(30, 60), (40, 42), (20, 42)], outline=255, fill=1) # down filled
print('down')
if button_C.value: # button is released
draw.rectangle((20, 22, 40, 40), outline=255, fill=0) # center
else: # button is pressed:
draw.rectangle((20, 22, 40, 40), outline=255, fill=1) # center filled
print('button center')
if button_A.value: # button is released
draw.ellipse((70, 40, 90, 60), outline=255, fill=0) # A button
else: # button is pressed:
draw.ellipse((70, 40, 90, 60), outline=255, fill=1) # A button filled
print('button A center')
if button_B.value: # button is released
draw.ellipse((100, 20, 120, 40), outline=255, fill=0) # B button
else: # button is pressed:
draw.ellipse((100, 20, 120, 40), outline=255, fill=1) # B button filled
print('button B center')
if not button_A.value and not button_B.value and not button_C.value:
catImage = Image.open("happycat_oled_64.ppm").convert("1")
disp.image(catImage)
else:
# Display image.
disp.image(image)
disp.show()