mirror of
https://github.com/apirrone/Open_Duck_Mini_Runtime.git
synced 2025-09-03 03:33:54 +00:00
right eye is green for some reason?
This commit is contained in:
parent
7e3ab7f147
commit
ebf88f92d7
4 changed files with 107 additions and 32 deletions
|
@ -2,7 +2,7 @@ import random
|
|||
import time
|
||||
from threading import Thread, Event
|
||||
|
||||
from .led_controller import get_controller
|
||||
from open_duck_mini_runtime.led_controller import get_controller
|
||||
|
||||
|
||||
class Eyes:
|
||||
|
|
|
@ -2,46 +2,41 @@
|
|||
Shared controller for a single 3-LED NeoPixel strip used by eyes (2 LEDs) and projector (1 LED).
|
||||
|
||||
Design:
|
||||
- Index 0: left eye
|
||||
- Index 0: projector
|
||||
- Index 1: right eye
|
||||
- Index 2: projector
|
||||
|
||||
This module lazily imports hardware libraries to avoid import errors on non-hardware hosts.
|
||||
- Index 2: left eye
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from threading import Lock
|
||||
from typing import Tuple, Optional
|
||||
|
||||
# Direct hardware imports (we assume we're running on-device)
|
||||
import board
|
||||
import neopixel
|
||||
|
||||
# Pin and pixel configuration
|
||||
PIXEL_PIN = board.D10
|
||||
NUM_PIXELS = 3
|
||||
|
||||
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
|
||||
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
|
||||
ORDER = neopixel.RGBW
|
||||
|
||||
# Brightness and a single shared NeoPixel instance
|
||||
BRIGHTNESS = 1
|
||||
pixels = neopixel.NeoPixel(
|
||||
PIXEL_PIN, NUM_PIXELS, brightness=BRIGHTNESS, auto_write=False, pixel_order=ORDER
|
||||
)
|
||||
|
||||
class LedController:
|
||||
def __init__(
|
||||
self,
|
||||
data_pin_name: str = "D23", # default GPIO for NeoPixel data
|
||||
num_pixels: int = 3,
|
||||
brightness: float = 0.3,
|
||||
) -> None:
|
||||
def __init__(self) -> None:
|
||||
self._lock = Lock()
|
||||
self._pixels = None # type: ignore
|
||||
self._deinited = False
|
||||
|
||||
# Lazy import to prevent issues on dev machines/CI without hardware
|
||||
# Importing here avoids name collision with our package module names.
|
||||
import importlib
|
||||
|
||||
board = importlib.import_module("board")
|
||||
neopixel = importlib.import_module("neopixel")
|
||||
|
||||
data_pin = getattr(board, data_pin_name)
|
||||
|
||||
# Initialize NeoPixel strip
|
||||
self._pixels = neopixel.NeoPixel(
|
||||
data_pin,
|
||||
num_pixels,
|
||||
brightness=brightness,
|
||||
auto_write=False,
|
||||
pixel_order=getattr(neopixel, "GRB", None) or getattr(neopixel, "RGB"),
|
||||
)
|
||||
# Use the module-level NeoPixel configured above
|
||||
self._pixels = pixels
|
||||
|
||||
# Cache simple color tuples
|
||||
self.OFF = (0, 0, 0)
|
||||
|
@ -73,10 +68,10 @@ class LedController:
|
|||
if proj_color is None:
|
||||
proj_color = self.WHITE if self.projector_on else self.OFF
|
||||
|
||||
# Assign indices: 0-left, 1-right, 2-projector
|
||||
self._pixels[0] = left_color
|
||||
# Assign indices: 2-left, 1-right, 0-projector
|
||||
self._pixels[0] = proj_color
|
||||
self._pixels[1] = right_color
|
||||
self._pixels[2] = proj_color
|
||||
self._pixels[2] = left_color
|
||||
self._pixels.show()
|
||||
|
||||
# Eyes API
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import time
|
||||
|
||||
from .led_controller import get_controller
|
||||
from open_duck_mini_runtime.led_controller import get_controller
|
||||
|
||||
|
||||
class Projector:
|
||||
|
|
80
tests/light_test.py
Normal file
80
tests/light_test.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
# Simple test for NeoPixels on Raspberry Pi
|
||||
import time
|
||||
|
||||
import board
|
||||
|
||||
import neopixel
|
||||
|
||||
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
|
||||
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
|
||||
pixel_pin = board.D10
|
||||
|
||||
# The number of NeoPixels
|
||||
num_pixels = 3
|
||||
|
||||
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
|
||||
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
|
||||
ORDER = neopixel.GRB
|
||||
|
||||
pixels = neopixel.NeoPixel(
|
||||
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
|
||||
)
|
||||
|
||||
|
||||
def wheel(pos):
|
||||
# Input a value 0 to 255 to get a color value.
|
||||
# The colours are a transition r - g - b - back to r.
|
||||
if pos < 0 or pos > 255:
|
||||
r = g = b = 0
|
||||
elif pos < 85:
|
||||
r = int(pos * 3)
|
||||
g = int(255 - pos * 3)
|
||||
b = 0
|
||||
elif pos < 170:
|
||||
pos -= 85
|
||||
r = int(255 - pos * 3)
|
||||
g = 0
|
||||
b = int(pos * 3)
|
||||
else:
|
||||
pos -= 170
|
||||
r = 0
|
||||
g = int(pos * 3)
|
||||
b = int(255 - pos * 3)
|
||||
return (r, g, b) if ORDER in {neopixel.RGB, neopixel.GRB} else (r, g, b, 0)
|
||||
|
||||
|
||||
def rainbow_cycle(wait):
|
||||
for j in range(255):
|
||||
for i in range(num_pixels):
|
||||
pixel_index = (i * 256 // num_pixels) + j
|
||||
pixels[i] = wheel(pixel_index & 255)
|
||||
pixels.show()
|
||||
time.sleep(wait)
|
||||
|
||||
|
||||
while True:
|
||||
# Comment this line out if you have RGBW/GRBW NeoPixels
|
||||
pixels.fill((255, 0, 0))
|
||||
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
||||
# pixels.fill((255, 0, 0, 0))
|
||||
pixels.show()
|
||||
time.sleep(1)
|
||||
|
||||
# Comment this line out if you have RGBW/GRBW NeoPixels
|
||||
pixels.fill((0, 255, 0))
|
||||
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
||||
# pixels.fill((0, 255, 0, 0))
|
||||
pixels.show()
|
||||
time.sleep(1)
|
||||
|
||||
# Comment this line out if you have RGBW/GRBW NeoPixels
|
||||
pixels.fill((0, 0, 255))
|
||||
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
||||
# pixels.fill((0, 0, 255, 0))
|
||||
pixels.show()
|
||||
time.sleep(1)
|
||||
|
||||
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step
|
Loading…
Reference in a new issue