Skip to content

Using the Drawer

Kyle Combes edited this page Nov 22, 2017 · 1 revision

Initialization

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

from raspi.io_states.drawer_state import DrawerState

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

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

   self.drawer_state = DrawerState()

Updating the state

The DrawerState has one method you can call:

  • open() - opens the drawer

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 drawer. (In our case, that Arduino is simply known as ARDUINO2, because it does several things.)

To open the drawer, you would use the following code:

self.drawer_state.open()
self.update_io_state(ARDUINO2, self.drawer_state)

Responding to the drawer's height reading

When the player opens or closes the drawer, your event callback function (user_input_event_received, if you didn't rename it) is called. To check if the event was a door close, simply check event.id. Then check event.data to make sure it is not None (indicative of the drawer being open). Once you've verified it's not None, then you can use the value of the height in any way you choose.

For example, to display the height of the object on the e-ink display, you could use the following code:

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

   if event.id == EventType.DRAWER_STATE_CHANGE:
     # Drawer was opened or closed. We first need to check if it's closed before we can read the height of the object.
     if event.data is not None:  # There is a height reading, therefore the drawer is closed
        self.eink.set_text("The object is " + event.data + " cm tall")  # Display the height on the screen

Clone this wiki locally