1
\$\begingroup\$

I have a Feather M0 basic paired with a OLED FeatherWing. I am making an instructions page but the code is quite repetitive. How can I make my code shorter?

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()
\$\endgroup\$
  • 1
    \$\begingroup\$ What is circuit-python and why do you think it deserves it's own tag? Can you tell us more about what your code is supposed to accomplish and whether it succesfully does so? \$\endgroup\$ – Mast Aug 7 at 18:29
  • \$\begingroup\$ With the Feather M0 you mean the protoboard with ATSAMD21 Cortex M0 microcontroller? \$\endgroup\$ – Mast Aug 7 at 18:30
  • \$\begingroup\$ The Feather M0 does use the ATSAMD21, it's the same as the Arduino Zero \$\endgroup\$ – Unsigned_Arduino Aug 9 at 23:43
  • \$\begingroup\$ @Mast, circuitpython is a subset of micropython maintained by Adafruit Industries. \$\endgroup\$ – Unsigned_Arduino Aug 9 at 23:47
0
\$\begingroup\$

I think here a small class would help to keep track of all things related to a 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

(Untested, since I obviously don't have your hardware lying around.)

With your initialization code can be shortened a bit:

names = ["A", "B", "C"]
pins = [board.D9, board.D6, board.D5]
buttons = {name: Button(name, pin) for name, pin in zip(names, pins)}

As for your printing to the oled, they always follow the same format, so just define a function for that:

def print_page(oled, strings):
    oled.fill(0)
    for i, s in enumerate(strings):
        oled.text(s, 0, i * 8, 1)
    oled.show()

After which your main code becomes:

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()

Note that I used triple-quoted strings so you can just write your message in plaintext, line-breaks included. You might want to outsource that part to another file and just load / import from that.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.