我有一个羽毛M0的基本和一个有机发光二极管FeatherWing。我正在制作一个指令页,但是代码是相当重复的。如何使我的代码更短?
import board
from busio import I2C
from adafruit_ssd1306 import SSD1306_I2C
from time import sleep
from digitalio import DigitalInOut, Direction, Pull
i2c = I2C(board.SCL, board.SDA)
oled = SSD1306_I2C(128, 32, i2c)
button_A = DigitalInOut(board.D9)
button_B = DigitalInOut(board.D6)
button_C = DigitalInOut(board.D5)
button_A.direction = Direction.INPUT
button_B.direction = Direction.INPUT
button_C.direction = Direction.INPUT
button_A.pull = Pull.UP
button_B.pull = Pull.UP
button_C.pull = Pull.UP
def check_buttons():
if button_A.value is False:
return 'A'
elif button_B.value is False:
return'B'
elif button_C.value is False:
return 'C'
else:
return None
def wait_for_A():
sleep(0.5)
while check_buttons() is not 'A':
pass
oled.fill(0)
oled.text('Adafruit Feather', 0, 0, 1)
oled.text('Program selector', 0, 8, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions:', 0, 0, 1)
oled.text('Use A and C to move', 0, 8, 1)
oled.text('up and down.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('Press B to select a', 0, 8, 1)
oled.text('program.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('At anytime you can', 0, 8, 1)
oled.text('press reset to go...', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('... back to the main', 0, 8, 1)
oled.text('screen.', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('The programs will', 0, 8, 1)
oled.text('have there own...', 0, 16, 1)
oled.text('Press A to continue', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.text('Instructions (cont.):', 0, 0, 1)
oled.text('... instructions.', 0, 8, 1)
oled.text('Press A to continue', 0, 16, 1)
oled.text('to menu', 0, 24, 1)
oled.show()
wait_for_A()
oled.fill(0)
oled.show()发布于 2019-08-08 17:39:33
我认为在这里,一个小类将有助于跟踪与Button相关的所有内容:
class Button(DigitalInOut):
def __init__(self, name, pin, direction=Direction.INPUT, pull=Pull.UP):
self.name = name
super().__init__(pin)
self.direction = direction
self.pull = pull
def wait_for_press(self):
while self.value:
pass(未经测试,因为我显然没有你的硬件。)
对于初始化代码,可以缩短一点:
names = ["A", "B", "C"]
pins = [board.D9, board.D6, board.D5]
buttons = {name: Button(name, pin) for name, pin in zip(names, pins)}至于打印到oled,它们总是遵循相同的格式,所以只需定义一个函数:
def print_page(oled, strings):
oled.fill(0)
for i, s in enumerate(strings):
oled.text(s, 0, i * 8, 1)
oled.show()在此之后,您的主要代码变成:
messages = [
"""
Adafruit Feather
Program selector
Press A to continue
""",
"""
Instructions:
Use A and C to move
up and down
Press A to continue
""",
"""
Instructions (cont.):
Press B to select a
program.
Press A to continue
""",
...
]
for message in messages:
print_page(oled, message.splitlines()[1:])
buttons["A"].wait_for_press()
oled.fill(0)
oled.show()请注意,我使用了三引号字符串,这样您就可以用纯文本(包括换行)写消息了。您可能希望将该部分外包给另一个文件,然后从该文件加载/导入。
https://codereview.stackexchange.com/questions/225734
复制相似问题