Source code for ui_utils

"""With no screen and no keypad, we have to make do with the buttons and LEDs.

The *ui_utils* module gives you the ability to enter data at program run-time.
This can reduce the number of times you have to edit and re-download your program.
This is especially handy if you are running your CodeBot untethered.

Attributes:
  UI_BUTTON_NONE (int): value used to represent NO BUTTON is pressed
  UI_BUTTON_0 (int) : value used to represent BTN-0 is pressed
  UI_BUTTON_1 (int) : value used to represent BTN-1 is pressed
  UI_BUTTON_BOTH (int) : value used to represent BOTH buttons are pressed

Treating "both buttons are pressed" as a special case gives us a virtual 3rd button.
"""

import botcore as cb
#import devbotcore as cb

import time

UI_BUTTON_NONE = 0x00
UI_BUTTON_0 = 0x01
UI_BUTTON_1 = 0x02
UI_BUTTON_BOTH = 0x03

[docs]def get_button(): """Returns which button is currently pressed, as one of the UI_BUTTON_xxx codes.""" result = UI_BUTTON_NONE if cb.buttons.is_pressed(0): result |= UI_BUTTON_0 if cb.buttons.is_pressed(1): result |= UI_BUTTON_1 if result == UI_BUTTON_NONE: return UI_BUTTON_NONE time.sleep(0.25) # TODO tune debounce result = 0 if cb.buttons.is_pressed(0): result |= UI_BUTTON_0 if cb.buttons.is_pressed(1): result |= UI_BUTTON_1 while cb.buttons.is_pressed(0) or cb.buttons.is_pressed(1): time.sleep(0.1) time.sleep(0.1) return result
[docs]def wait_for_button(requested_ui_button): """Wait until the specified button is pressed. Args: requested_ui_button (int): the UI_BUTTON_XXX combination to wait for """ while get_button() != requested_ui_button: pass
[docs]def tweak_parameter(initial_value): """Allows a value to be adjusted, or kept as-is: Press BTN-1 to raise the value. Press BTN-0 to lower the value. Press BOTH buttons at the same time to lock-in your choice. Flashes the final value on the LEDs so you can tell what you entered. Args: initial_value (int): the value to start at. """ value = initial_value cb.leds.user(value) done = False while not done: button = get_button() if button == UI_BUTTON_BOTH: done = True elif button == UI_BUTTON_1: if value < 255: value += 1 elif button == UI_BUTTON_0: if value > 0: value -= 1 cb.leds.user(value) count = 3 while count: count -= 1 cb.leds.user(0) time.sleep(0.5) cb.leds.user(value) time.sleep(0.5) cb.leds.user(0) time.sleep(0.5) return value