Skip to content

Using the Numeric Keypad

Kyle Combes edited this page Nov 22, 2017 · 4 revisions

Initialization

To use the keypad, you'll need to first need to import the KeypadState class by adding the following line to the top of your Python file:

from raspi.io_states.keypad_state import KeypadState

Then you'll need to create a KeypadState object in your puzzle's __init__ method, like so:

def __init__(self, update_io_state, register_callback):
   # Your old code here

   self.keypad_state = KeypadState()

Updating the state

The KeypadState has one method you can call:

  • set_visible(is_visible) - pass in True to show the Trellis, or False to hide it

After you've updated the state object, you need to tell the BOCS to send the new state to the Arduino that is responsible for the start button. (In our case, that Arduino is simply known as ARDUINO1, because it does many things.)

To show the keypad, you would use the following code:

self.keypad_state.set_visible(True)
self.update_io_state(ARDUINO1, self.keypad_state)

Responding to button presses

Whenever the player presses a button on the keypad, your event callback function (user_input_event_received, if you didn't rename it) is called. To check if the event was a keypad button press, simply check event.id. Then look at event.data (a string encoding of the key that was pressed) to see which button was pressed.

def user_input_event_received(self, event):
   # Some other code here, perhaps

   if event.id == EventType.KEYPAD_BUTTON_PRESS:
     # Keypad button was pressed, so do something with the value of the key they pressed (how about they win if they pressed 5? Sure, why not?)
     if event.data == "5":  # User pressed the number 5, so they win!
        self.is_solved = True  # Mark the puzzle as solved, or something...
Clone this wiki locally