Created
October 31, 2023 21:55
-
-
Save dglaude/26f3b2d389197fb34a2881faf56f31f6 to your computer and use it in GitHub Desktop.
Halloween Teddy Ruxpin: Monitor time of fly distance and power off USB-A port for 5 minutes when there is activity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2022 David Glaude (based on 2021 ladyada for Adafruit Industries) | |
# SPDX-License-Identifier: MIT | |
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries | |
# SPDX-FileCopyrightText: Copyright (c) 2021 Carter Nelson for Adafruit Industries | |
# | |
# SPDX-License-Identifier: Unlicense | |
# Simple demo of the VL53L1X distance sensor. | |
# Will print the sensed range/distance every second. | |
""" | |
This test will initialize the display using displayio and draw a solid white | |
background, a smaller black rectangle, and some white text. | |
Customized version of displayio_ssd1306_simpletest.py for 64x32 | |
""" | |
import time | |
import board | |
import displayio | |
import digitalio | |
import terminalio | |
import adafruit_vl53l1x | |
from adafruit_display_text import label | |
import adafruit_displayio_ssd1306 | |
led_or_power = digitalio.DigitalInOut(board.USB_HOST_5V_POWER) | |
led_or_power.direction = digitalio.Direction.OUTPUT | |
displayio.release_displays() | |
i2c = board.I2C() # uses board.SCL and board.SDA | |
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C) | |
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=64, height=32) | |
# Make the display context | |
splash = displayio.Group() | |
display.show(splash) | |
color_bitmap = displayio.Bitmap(64, 32, 1) | |
color_palette = displayio.Palette(1) | |
color_palette[0] = 0xFFFFFF # White | |
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) | |
splash.append(bg_sprite) | |
## Draw a smaller inner rectangle | |
inner_bitmap = displayio.Bitmap(62, 30, 1) | |
inner_palette = displayio.Palette(1) | |
inner_palette[0] = 0x000000 # Black | |
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1) | |
splash.append(inner_sprite) | |
TEXT1 = "" | |
text_area1 = label.Label(terminalio.FONT, text=TEXT1, color=0xFFFFFF, x=2, y=6) | |
splash.append(text_area1) | |
vl53 = adafruit_vl53l1x.VL53L1X(i2c) | |
# OPTIONAL: can set non-default values | |
vl53.distance_mode = 2 | |
vl53.timing_budget = 100 | |
vl53.start_ranging() | |
stay_in_loop=True | |
start_distance=300 | |
while stay_in_loop: | |
if vl53.data_ready: | |
start_distance=vl53.distance | |
text_area1.text=" {} cm".format(start_distance) | |
vl53.clear_interrupt() | |
stay_in_loop=False | |
print() | |
print(text_area1.text) | |
trigger_distance=start_distance*80/100 | |
TEXT2 = "" | |
text_area2 = label.Label(terminalio.FONT, text=TEXT2, color=0xFFFFFF, x=2, y=15) | |
splash.append(text_area2) | |
TEXT3 = "" | |
text_area3 = label.Label(terminalio.FONT, text=TEXT3, color=0xFFFFFF, x=2, y=24) | |
splash.append(text_area3) | |
loop=True | |
while True: | |
led_or_power.value = True | |
text_area3.text = "Ruxpin Off" | |
stay_in_loop=True | |
while stay_in_loop: | |
if vl53.data_ready: | |
new_distance=vl53.distance | |
text_area2.text=" {} cm".format(new_distance) | |
vl53.clear_interrupt() | |
if new_distance<trigger_distance: | |
stay_in_loop=False | |
time.sleep(1) | |
led_or_power.value = False | |
text_area3.text = "Ruxpin ON " | |
time.sleep(300) # 5 minutes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This must be used on a Feather RP2040 USB-Host connected to:
The Teddy Ruxpin need to be powered on and with battery inside.
When the Teddy Ruxpin detect a USB cable, it turn the eyes off and make the filesystem available.
So the idea is to put the Teddy Ruxping to sleep, and when movement is detected.