diff --git a/README.rst b/README.rst index 92d857a..69faa2c 100644 --- a/README.rst +++ b/README.rst @@ -28,11 +28,20 @@ This is easily achieved by downloading Usage Example ============= +Normally this library is used with the +`Adafruit_CircuitPython_BluefruitConnect +`_ +library +(``adafruit_bluefruit_connect``). The included examples use that library. +Below is a simple standalone example. + .. code-block:: python from adafruit_bluefruit_connect.color_packet import ColorPacket from adafruit_bluefruit_connect.gyro_packet import GyroPacket + # [uart setup omitted] + color_packet = ColorPacket((70,75,80)) gyro_packet = GyroPacket.from_bytes(packet_buf) uart.write(gyro_packet.to_bytes()) diff --git a/adafruit_bluefruit_connect/button_packet.py b/adafruit_bluefruit_connect/button_packet.py index cead0ff..db181ad 100644 --- a/adafruit_bluefruit_connect/button_packet.py +++ b/adafruit_bluefruit_connect/button_packet.py @@ -35,7 +35,25 @@ from .packet import Packet class ButtonPacket(Packet): - """A packet containing a button name and its state""" + """A packet containing a button name and its state.""" + + BUTTON_1 = '1' + """Code for Button 1 on the Bluefruit LE Connect app Control Pad screen.""" + BUTTON_2 = '2' + """Button 2.""" + BUTTON_3 = '3' + """Button 3.""" + BUTTON_4 = '4' + """Button 4.""" + #pylint: disable= invalid-name + UP = '5' + """Up Button.""" + DOWN = '6' + """Down Button.""" + LEFT = '7' + """Left Button.""" + RIGHT = '8' + """Right Button.""" _FMT_PARSE = ' Accelerometer to send data from the device's +# accelerometer. See how it matches what this prints. + +from adafruit_ble.uart import UARTServer +from adafruit_bluefruit_connect.packet import Packet +# Only the packet classes that are imported will be known to Packet. +from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket + +uart_server = UARTServer() + +while True: + # Advertise when not connected. + uart_server.start_advertising() + while not uart_server.connected: + pass + + while uart_server.connected: + packet = Packet.from_stream(uart_server) + if isinstance(packet, AccelerometerPacket): + print(packet.x, packet.y, packet.z) diff --git a/examples/bluefruit_connect_crickit_test.py b/examples/bluefruit_connect_crickit_test.py deleted file mode 100644 index e0dfadc..0000000 --- a/examples/bluefruit_connect_crickit_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# Demo using Bluefruit Connect app, a pan-tilt kit (two servos), -# and a 24-NeoPixel ring. - -from adafruit_crickit import crickit -from adafruit_ble.uart import UARTServer - -from adafruit_bluefruit_connect.packet import Packet -# Only the packet classes that are imported will be known to Packet. -from adafruit_bluefruit_connect.color_packet import ColorPacket -from adafruit_bluefruit_connect.button_packet import ButtonPacket - -crickit.init_neopixel(24) - -uart_server = UARTServer() - -advertising_now = False -tilt = 0 -rotate = 0 -crickit.servo_1.angle = rotate -crickit.servo_2.angle = tilt - -while True: - - if not uart_server.connected: - if not advertising_now: - uart_server.start_advertising() - advertising_now = True - continue - - # Connected, so no longer advertising - advertising_now = False - - packet = Packet.from_stream(uart_server) - if isinstance(packet, ColorPacket): - crickit.neopixel.fill(packet.color) - elif isinstance(packet, ButtonPacket): - if packet.pressed: - if packet.button == '5': - tilt = min(170, tilt + 1) - elif packet.button == '6': - tilt = max(0, tilt - 1) - elif packet.button == '7': - rotate = min(170, rotate + 1) - elif packet.button == '8': - rotate = max(0, rotate - 1) - - crickit.servo_1.angle = rotate - crickit.servo_2.angle = tilt diff --git a/examples/bluefruit_connect_simpletest.py b/examples/bluefruit_connect_simpletest.py index 698042e..436d76d 100644 --- a/examples/bluefruit_connect_simpletest.py +++ b/examples/bluefruit_connect_simpletest.py @@ -1,4 +1,6 @@ -# Print out the color data from a ColorPacket. +# Print out the color data from ColorPackets. +# To use, start this program, and start the Adafruit Bluefruit LE Connect app. +# Connect, and then select colors on the Controller->Color Picker screen. from adafruit_ble.uart import UARTServer from adafruit_bluefruit_connect.packet import Packet @@ -7,18 +9,13 @@ uart_server = UARTServer() -advertising_now = False - while True: - if not uart_server.connected: - if not advertising_now: - uart_server.start_advertising() - advertising_now = True - continue - - # Connected, so no longer advertising - advertising_now = False + # Advertise when not connected. + uart_server.start_advertising() + while not uart_server.connected: + pass - packet = Packet.from_stream(uart_server) - if isinstance(packet, ColorPacket): - print(packet.color) + while uart_server.connected: + packet = Packet.from_stream(uart_server) + if isinstance(packet, ColorPacket): + print(packet.color)