Skip to content

Robust packet reading #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2019
Merged

Conversation

dhalbert
Copy link
Contributor

  • Read characters until a start-of-packet character is read.
  • Fix incorrect packet length for LocationPacket.

This fixes some but not all issues with adafruit/circuitpython#1568.

@dhalbert dhalbert requested a review from kattni February 20, 2019 22:44
@dhalbert
Copy link
Contributor Author

More comprehensive test program:

from adafruit_ble.uart import UARTServer
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket
from adafruit_bluefruit_connect.gyro_packet import GyroPacket
from adafruit_bluefruit_connect.location_packet import LocationPacket
from adafruit_bluefruit_connect.magnetometer_packet import MagnetometerPacket
from adafruit_bluefruit_connect.quaternion_packet import QuaternionPacket


uart_server = UARTServer(buffer_size=256)

while True:
    uart_server.start_advertising()
    while not uart_server.connected:
        pass

    # Now we're connected

    while uart_server.connected:
        if uart_server.in_waiting:
            packet = Packet.from_stream(uart_server)
            if isinstance(packet, AccelerometerPacket):
                print("Acceleration:", packet.x, packet.y, packet.z)
            elif isinstance(packet, ButtonPacket):
                print("Button", packet.button, packet.pressed)
            elif isinstance(packet, ColorPacket):
                print("Color:", packet.color)
            elif isinstance(packet, GyroPacket):
                print("Gyro:", packet.x, packet.y, packet.z)
            elif isinstance(packet, LocationPacket):
                print("Location:", packet.latitude, packet.longitude, packet.altitude)
            elif isinstance(packet, MagnetometerPacket):
                print("Magnetometer:", packet.x, packet.y, packet.z)
            elif isinstance(packet, QuaternionPacket):
                print("Quaternion:", packet.x, packet.y, packet.z, packet.w)

    # If we got here, we lost the connection. Go up to the top and start
    # advertising again and waiting for a connection.

Copy link
Contributor

@kattni kattni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Location is working now, and I was able to run the updated demo for 20 minutes. At that point it stopped, however, I was able to interact with the serial console/REPL still, so it didn't crash the board, the phone simply stopped sending data. Thank you for the update!

Travis is unhappy. I'll merge it once that's sorted!

@kattni kattni merged commit 086398d into adafruit:master Feb 21, 2019
@dhalbert dhalbert deleted the robust-packet-reading branch February 21, 2019 17:58
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants