Skip to content

Conversation

@nnja
Copy link
Contributor

@nnja nnja commented Jun 14, 2020

The current color picker example is problematic when displaying animations on the pixel strip.

As soon as the app connects to the device, the animation noticeably slows down.

Checking if the UARTService is in waiting before attempting to read from the stream fixes this issue.

Also renamed uart_service to uart_server for consistency.

Examples below.

  1. Running the code below will display the animation normally until the app connects. Once the app is connected to the device, the animation slows down.
# CircuitPython NeoPixel Color Picker Example
# Animation slows down when device is connected to from app

import board
import neopixel
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket

from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.color import RED

ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)

pixels = neopixel.NeoPixel(board.D12, 5, brightness=0.1)
blink = Blink(pixels, speed=0.5, color=RED)

while True:
    # Advertise when not connected.
    ble.start_advertising(advertisement)
    while not ble.connected:
        blink.animate()
    ble.stop_advertising()

    while ble.connected:
        blink.animate()
        packet = Packet.from_stream(uart_service)
        if isinstance(packet, ColorPacket):
            print(packet.color)
            blink = Blink(pixels, speed=0.5, color=packet.color)
  1. This behavior is fixed when checking for uart_service.in_waiting like in the updated example.
# CircuitPython NeoPixel Color Picker Example
# Animation doesn't slow down when bluetooth connected

import board
import neopixel
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket

from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.color import RED

ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)

pixels = neopixel.NeoPixel(board.D12, 5, brightness=0.1)
blink = Blink(pixels, speed=0.5, color=RED)

while True:
    # Advertise when not connected.
    ble.start_advertising(advertisement)
    while not ble.connected:
        blink.animate()
    ble.stop_advertising()

    while ble.connected:
        blink.animate()
        if uart_service.in_waiting:
            packet = Packet.from_stream(uart_service)
            if isinstance(packet, ColorPacket):
                print(packet.color)
                blink = Blink(pixels, speed=0.5, color=packet.color)

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Good fix; thank you!

@dhalbert dhalbert merged commit 3585131 into adafruit:master Jun 14, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 16, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_BME280 to 2.4.2 from 2.4.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_BME280#38 from JanHBade/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 6.1.5 from 6.1.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#90 from nnja/fix_color_picker_example_for_animation
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