diff --git a/examples/bb8joystick/__init__.py b/examples/bb8joystick/__init__.py index f09f7d9..d742022 100644 --- a/examples/bb8joystick/__init__.py +++ b/examples/bb8joystick/__init__.py @@ -27,10 +27,10 @@ if __name__ == "__main__": try: - # joystick.on_color_sensor(set_bb_color) + #joystick.on_color_sensor(set_bb_color) joystick.on_external_motor(set_heading) print("All set up") - time.sleep(60) + time.sleep(600) finally: joystick.disconnect() bb8.disconnect() diff --git a/examples/bb8joystick/joystick.py b/examples/bb8joystick/joystick.py index 7747f66..9cbd7e9 100644 --- a/examples/bb8joystick/joystick.py +++ b/examples/bb8joystick/joystick.py @@ -1,18 +1,43 @@ +import logging +import time + from pylgbst.hub import MoveHub -from pylgbst.peripherals import VisionSensor, EncodedMotor +from pylgbst.peripherals import EncodedMotor class Joystick(object): def __init__(self): super(Joystick, self).__init__() self._hub = MoveHub() - self._sensor = [] + + self.button_pressed = False + + self._hub.motor_external.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1) + self._hub.motor_A.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1) + self._hub.motor_B.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1) + self._hub.button.subscribe(self._on_btn) def disconnect(self): self._hub.disconnect() - def on_color_sensor(self, callback): - self._hub.vision_sensor.subscribe(callback, VisionSensor.COLOR_RGB, granularity=5) + def _on_btn(self, state): + self.button_pressed = state - def on_external_motor(self, callback): - self._hub.motor_external.subscribe(callback, EncodedMotor.SENSOR_ANGLE, granularity=5) + def on_button(self, callback): + """ + Notifies about button state change. ``callback(state)`` gets single bool parameter + """ + + def wrapper(state): + if state in (0, 1): + logging.debug("Pressed button: %s", state) + callback(bool(state)) + + self._hub.button.subscribe(wrapper) + + +if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + stick = Joystick() + stick.on_button(lambda x: print("Button: %s" % x)) + time.sleep(100)