class KeypadDisplay(object): def __init__(self, lcd_address, button_pins=[17, 18, 27, 22, 23, 24, 25], led_pins=[16, 20, 21]): """Initializes the KeypadDisplay object button_pins: A list of 7 GPIO pins used for button inputs, order is A, B, L, U, D, R, C pin Usage ---|----------- 17 | Button A 18 | Button B 27 | Button Left 22 | Button Up 23 | Button Down 24 | Button right 25 | Button C led_pins: A list of 3 GPIO pins to use for led outputs, order is R, G, B pin Usage ---|---------- 16 | Red LED 20 | Yellow LED 21 | Green LED """ self.button_a = Button(button_pins[0]) self.button_b = Button(button_pins[1]) self.button_left = Button(button_pins[2]) self.button_up = Button(button_pins[3]) self.button_down = Button(button_pins[4]) self.button_right = Button(button_pins[5]) self.button_c = Button(button_pins[6]) self.led_red = LED(led_pins[0]) self.led_yellow = LED(led_pins[1]) self.led_green = LED(led_pins[2]) self.lcd = LCD(I2CPCF8574Interface(lcd_address), num_rows=4, num_cols=20) def set_led(self, value) -> Leds: """Sets the LED's according to the passed value""" self.led_red.off() self.led_yellow.off() self.led_green.off() if value in [Leds.RED, Leds.ALL]: self.led_red.on() if value in [Leds.YELLOW, Leds.ALL]: self.led_yellow.on() if value in [Leds.GREEN, Leds.ALL]: self.led_green.on() def print(self, message): """Clears the LCD and display the message""" self.lcd.clear() self.lcd.print(message)
from lcd.lcd import CursorMode import board # Talk to the LCD at I2C address 0x27. # The number of rows and columns defaults to 4x20, so those # arguments could be omitted in this case. lcd = LCD(I2CPCF8574Interface(0x27), num_rows=2, num_cols=16) # 0x27 or 0x3F button = DigitalInOut(board.D2) button.direction = Direction.INPUT button.pull = Pull.UP button_state = None last_state = None value = 0 lcd.print(" CircuitPython LCD ") time.sleep(2) lcd.clear() lcd.set_cursor_pos(0, 0) lcd.print("ButtonPresses:") # Make the cursor visible as a line. lcd.set_cursor_mode(CursorMode.LINE) while True: if button.value: print("not pressed") else: value = value + 1 print(value) button_state = None lcd.set_cursor_pos(1, 0) lcd.print(str(value)) time.sleep(0.2)
# The number of rows and columns defaults to 4x20, so those # arguments could be omitted in this case. lcd = LCD(I2CPCF8574Interface(0x27), num_rows=2, num_cols=16) # 0x27 or 0x3F button = DigitalInOut(board.D2) button.direction = Direction.INPUT button.pull = Pull.UP button_state = None last_state = None value = 0 lcd.print(" CircuitPython LCD ") # opening screen time.sleep(2) lcd.clear() # clears screen lcd.set_cursor_pos(0, 0) lcd.print("ButtonPresses:") # prints this # Make the cursor visible as a line. lcd.set_cursor_mode(CursorMode.LINE) while True: if button.value: print("not pressed") # prints that when button is not pressed # for some reason the button value is backwards so I just made my code accomodate that else: value = value + 1 # when button is pressed add 1 to value print(value) # prints the value in serial
button.pull = digitalio.Pull.UP SwitchState = digitalio.DigitalInOut(board.D3) SwitchState.direction = digitalio.Direction.INPUT SwitchState.pull = digitalio.Pull.UP lcd = LCD(I2CPCF8574Interface(0x27), num_rows=2, num_cols=16) counter = 0 oldSwitchState = 0 newSwitchState = 1 adder = 1 while True: lcd.clear() # clears LCD lcd.set_cursor_pos(1, 0) # sets position of LCD text lcd.print("pressed:") lcd.set_cursor_pos(1, 9) lcd.print(str(counter)) # prints the string number if button.value: print("0") time.sleep(0.05) oldSwitchState = 0 elif oldSwitchState == 0 and newSwitchState == 1: if SwitchState.value: # When the switch is on this makes the counter go up adder = 1
class ShortcutBox: """Convert switch presses to keyboard and mouse shortcuts.""" def __init__(self, filename="shortcuts.txt"): self.display = LCD(I2CPCF8574Interface(0x27)) self.display.clear() self.keyboard = Keyboard() self.mouse = Mouse() self.consumer_control = ConsumerControl() self.display.print( "Ready! Choose a\nshortcut and press\nthe do-it switch.") self.switch_ins = tuple( digitalio.DigitalInOut(pin) for pin in SWITCH_PINS) for switch_in in self.switch_ins: switch_in.switch_to_input(pull=digitalio.Pull.UP) try: # Shortcuts is a dict mapping a switch number to a list of Shortcuts. self.shortcuts = Shortcut.read_shortcuts(filename) except BadShortcut as ex: self.fatal_error(*ex.args) if not self.shortcuts: self.fatal_error("No shortcuts defined!") self.current_switch = None # Keep track of the current shortcut for each switch. Start at the zero-th ones. # Skip any switches with no shortcuts. self.current_shortcut_index = {} for switch in self.shortcuts: self.current_shortcut_index[switch] = 0 def run(self): """Start shortcut processing. Run forever.""" while True: for switch, switch_in in enumerate(self.switch_ins): if not switch_in.value: # If switch is pressed, it's pulled low. Debounce by waiting for bounce time. time.sleep(BOUNCE_SECS) if switch == DO_IT_SWITCH: if self.current_switch: self.execute(self.current_shortcut) self.display.print(" *") else: self.display.clear() self.display.print("No shortcut\nchosen yet.") else: if switch == self.current_switch: # Advance to next shortcut if this not the first press on this switch. self.next_shortcut(switch) else: # Change to a new switch. Don't advance that switch's shortcuts yet. self.current_switch = switch self.display_shortcut(self.current_shortcut, switch) # Wait for switch to be released. Debounce on the way up, too. while not switch_in.value: pass time.sleep(BOUNCE_SECS) @property def current_shortcut(self): """The last shortcut designated by a switch press, or None if we're on a switch with no shortcuts.""" switch = self.current_switch if switch in self.shortcuts: return self.shortcuts[switch][self.current_shortcut_index[switch]] else: return None def execute(self, shortcut): """Execute the given shortcut, if it's not None.""" if shortcut: shortcut.execute(self.keyboard, self.mouse, self.consumer_control) def next_shortcut(self, switch): """Move on to the next shortcut for the given switch. Skip switches with no shortcuts.""" if switch in self.current_shortcut_index: self.current_shortcut_index[switch] = ( (self.current_shortcut_index[switch] + 1) % len(self.shortcuts[switch])) def display_shortcut(self, shortcut, switch): self.display.clear() self.display.print( str(switch), ' ', shortcut.label if shortcut else "No shortcuts\non this switch!") def fatal_error(self, *strings): """Display and error and hang until reset.""" self.display.clear() self.display.print(' '.join(strings)) # Hang until user presses the reset button. while True: pass