From 5b027dabf681a110ca5e02c3601ec06d48e18303 Mon Sep 17 00:00:00 2001 From: Anne Barela <1911920+TheKitty@users.noreply.github.com> Date: Mon, 14 Feb 2022 09:39:07 -0500 Subject: [PATCH 1/2] Code for guide Exercise Buddy https://learn.adafruit.com/exercise-buddy/overview On behalf of Charlyn G --- Motion_Buddy/code.py | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Motion_Buddy/code.py diff --git a/Motion_Buddy/code.py b/Motion_Buddy/code.py new file mode 100644 index 000000000..15f4b209f --- /dev/null +++ b/Motion_Buddy/code.py @@ -0,0 +1,114 @@ +# SPDX-FileCopyrightText: 2022 Charlyn G for Adafruit Industries +# +# SPDX-License-Identifier: MIT +# +# Code for the Adafruit Learning System tutorial +# Exercise Buddy: Motion aware BLE media controller +# https://learn.adafruit.com/exercise-buddy/overview +# +import time +import board +import supervisor + +import neopixel +import adafruit_ble +import adafruit_lis3dh +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement +from adafruit_ble_apple_media import AppleMediaService, UnsupportedCommand + +# Initialize the accelerometer +i2c = board.I2C() +lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) + +# Initialize BLE radio +radio = adafruit_ble.BLERadio() +a = SolicitServicesAdvertisement() +a.solicited_services.append(AppleMediaService) +radio.start_advertising(a) + +# Neopixel indicator +pixel_pin = board.NEOPIXEL +pixel = neopixel.NeoPixel(pixel_pin, 1, brightness=0.5) +YELLOW = (200, 150, 0) +CYAN = (0, 100, 100) +PINK = (231, 84, 128) +pixel.fill(PINK) + +while not radio.connected: + pass + +print("connected") +pixel.fill(YELLOW) + +# Initialize variables +last_x = 0 +last_y = 0 +last_z = 0 +paused = True + +WAIT = 0.2 +WIGGLE_ROOM = 5 # Increase this for more jitter compensation. + + +def is_same_pos(last_position, current_position): + # Returns true if current_position is similar enough + # to last_position, within the specified wiggle room. + diff = abs(current_position - last_position) + print((diff,)) + return diff <= WIGGLE_ROOM + + +def not_enough_movement(x, y, z): + same_x = is_same_pos(last_x, x) + same_y = is_same_pos(last_y, y) + same_z = is_same_pos(last_z, z) + return same_x and same_y and same_z + + +while radio.connected: + + for connection in radio.connections: + if not connection.paired: + connection.pair() + print("paired") + pixel.fill(PINK) + time.sleep(1) + + if connection.paired: + pixel.fill(CYAN) + ams = connection[AppleMediaService] + print("app:", ams.player_name) + + try: + xf, yf, zf = lis3dh.acceleration + + if (not_enough_movement(xf, yf, zf)): + # Keep pausing. + print("pause!") + paused = True + ams.pause() + + else: + last_x = xf + last_y = yf + last_z = zf + + if paused: + print("play!") + paused = False + ams.play() + + except OSError: + supervisor.reload() + except UnsupportedCommand: + # This means that we tried to pause but there's + # probably nothing playing yet, so just wait a bit + # and try again. + pixel.fill(PINK) + time.sleep(10) + supervisor.reload() + + time.sleep(WAIT) + +print("disconnected") +pixel.fill(PINK) From d992ec2b6cb46d3aa63d867d0443159b16c2e041 Mon Sep 17 00:00:00 2001 From: Anne Barela <1911920+TheKitty@users.noreply.github.com> Date: Mon, 14 Feb 2022 09:44:41 -0500 Subject: [PATCH 2/2] Update code.py --- Motion_Buddy/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Motion_Buddy/code.py b/Motion_Buddy/code.py index 15f4b209f..1d5c51868 100644 --- a/Motion_Buddy/code.py +++ b/Motion_Buddy/code.py @@ -82,7 +82,7 @@ def not_enough_movement(x, y, z): try: xf, yf, zf = lis3dh.acceleration - if (not_enough_movement(xf, yf, zf)): + if not_enough_movement(xf, yf, zf): # Keep pausing. print("pause!") paused = True