Skip to content
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

Device for a Neopixel strip, controlled from RPi #948

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

approxima
Copy link

An alternative approach to control Adafruit Neopixel strips from RPI. Using "adafruit-circuitpython-neopixel" library.

…m RPi

Uses "adafruit-circuitpython-neopixel" library
Added "adafruit-circuitpython-neopixel" dependency
Copy link
Contributor

coderabbitai bot commented May 11, 2024

Walkthrough

The update enhances support for RPi NEOPIXEL STRIP WS2812 LEDs by introducing the RPI_NEOPIXEL class in the rpi_neopixel.py file. This class leverages the Adafruit-circuitpython-neopixel library to handle device operations like initialization, activation, deactivation, and data flushing, simplifying LED strip control.

Changes

File Summary of Changes
ledfx/.../rpi_neopixel.py Introduced RPI_NEOPIXEL class with methods for device management and data control.
ledfx/.../rpi_neopixel.py Added a static method CONFIG_SCHEMA and methods for initialization, activation, deactivation, and data flushing to the RPI_NEOPIXEL class.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Out of diff range and nitpick comments (1)
ledfx/devices/rpi_neopixel.py (1)

24-42: Consider cleaning up or clarifying the purpose of the commented-out code for better maintainability.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between cbbd451 and 17d345a.
Files ignored due to path filters (1)
  • pyproject.toml is excluded by !**/*.toml
Files selected for processing (1)
  • ledfx/devices/rpi_neopixel.py (1 hunks)
Additional Context Used
Ruff (2)
ledfx/devices/rpi_neopixel.py (2)

2-2: enum.Enum imported but unused


4-4: numpy imported but unused

Additional comments not posted (3)
ledfx/devices/rpi_neopixel.py (3)

44-46: Initialization method correctly calls the superclass initializer.


62-63: Deactivation method correctly delegates to the superclass.


65-69: Data flushing method is implemented correctly for the Neopixel library.

@@ -0,0 +1,69 @@
import logging
from enum import Enum
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import: Enum.

- from enum import Enum

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from enum import Enum

Comment on lines 47 to 60
def activate(self):
try:
import neopixel #Adafruit-circuitpython-neopixel library
import board
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18, #Raspberry Pi GPIO PIN 18, hardcoded
self.pixel_count, #Number of pixels
)
super().activate()
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider making the GPIO pin configurable to enhance flexibility.

- board.D18, #Raspberry Pi GPIO PIN 18, hardcoded
+ board.D18 if 'gpio_pin' not in self.config else self.config['gpio_pin'], # Configurable GPIO pin

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def activate(self):
try:
import neopixel #Adafruit-circuitpython-neopixel library
import board
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18, #Raspberry Pi GPIO PIN 18, hardcoded
self.pixel_count, #Number of pixels
)
super().activate()
def activate(self):
try:
import neopixel #Adafruit-circuitpython-neopixel library
import board
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18 if 'gpio_pin' not in self.config else self.config['gpio_pin'], # Configurable GPIO pin
self.pixel_count, #Number of pixels
)
super().activate()

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 17d345a and 350ae46.
Files selected for processing (1)
  • ledfx/devices/rpi_neopixel.py (1 hunks)
Additional Context Used
Ruff (2)
ledfx/devices/rpi_neopixel.py (2)

2-2: enum.Enum imported but unused


4-4: numpy imported but unused

Additional comments not posted (3)
ledfx/devices/rpi_neopixel.py (3)

43-45: Initialization method is correctly implemented.


61-62: Deactivation method is correctly implemented.


64-70: Data flushing method is correctly implemented.

Comment on lines +23 to +41
@staticmethod
@property
def CONFIG_SCHEMA():
return vol.Schema(
{
vol.Required(
"pixel_count",
description="Number of individual pixels",
default=144,
): vol.All(int, vol.Range(min=1)),
# vol.Required( #Uncomment to test this list in the frontend. PIN is hardcoded later anyway...
# "gpio_pin",
# description="Raspberry Pi GPIO pin your LEDs are connected to",
# ): vol.In(list([17, 18, 19])), # [18, 17, 19] Frontend doesn't allow to create a device if the first element in the list is selected
# vol.Required(
# "color_order", description="Color order", default="RGB"
# ): vol.In(list(COLOR_ORDERS.keys())),
}
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider including additional configuration options for GPIO pin and color order to enhance flexibility.

Would you like me to help implement these features or should I open a GitHub issue to track this enhancement?

Comment on lines 46 to 70
def activate(self):
try:
import board
import neopixel # Adafruit-circuitpython-neopixel library
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18, # Raspberry Pi GPIO PIN 18, hardcoded
self.pixel_count, # Number of pixels
)
super().activate()

def deactivate(self):
super().deactivate()

def flush(self, data):
"""Flush LED data to the strip"""
pixelData = [
tuple(led) for led in data.tolist()
] # Convert each row to a tuple. In Neopixel library, colors for each pixel are stored as tuples.
self.strip[:] = pixelData[:]
self.strip.show()
Copy link
Contributor

Choose a reason for hiding this comment

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

Error handling in activation is robust. Consider making the GPIO pin configurable to enhance flexibility.

- board.D18, #Raspberry Pi GPIO PIN 18, hardcoded
+ board.D18 if 'gpio_pin' not in self.config else self.config['gpio_pin'], # Configurable GPIO pin

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def activate(self):
try:
import board
import neopixel # Adafruit-circuitpython-neopixel library
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18, # Raspberry Pi GPIO PIN 18, hardcoded
self.pixel_count, # Number of pixels
)
super().activate()
def deactivate(self):
super().deactivate()
def flush(self, data):
"""Flush LED data to the strip"""
pixelData = [
tuple(led) for led in data.tolist()
] # Convert each row to a tuple. In Neopixel library, colors for each pixel are stored as tuples.
self.strip[:] = pixelData[:]
self.strip.show()
def activate(self):
try:
import board
import neopixel # Adafruit-circuitpython-neopixel library
except ImportError:
_LOGGER.warning(
"Unable to load Neopixel module - are you on a Raspberry Pi?"
)
self.deactivate()
self.strip = neopixel.NeoPixel(
board.D18 if 'gpio_pin' not in self.config else self.config['gpio_pin'], # Configurable GPIO pin
self.pixel_count, # Number of pixels
)
super().activate()
def deactivate(self):
super().deactivate()
def flush(self, data):
"""Flush LED data to the strip"""
pixelData = [
tuple(led) for led in data.tolist()
] # Convert each row to a tuple. In Neopixel library, colors for each pixel are stored as tuples.
self.strip[:] = pixelData[:]
self.strip.show()

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 350ae46 and c655181.
Files selected for processing (1)
  • ledfx/devices/rpi_neopixel.py (1 hunks)
Additional Context Used
Ruff (1)
ledfx/devices/rpi_neopixel.py (1)

2-2: enum.Enum imported but unused

Additional comments not posted (6)
ledfx/devices/rpi_neopixel.py (6)

2-2: Remove unused import: Enum.


23-41: Consider making the GPIO pin and color order configurable to enhance flexibility.


43-45: Initialization method looks good.


46-60: Consider making the GPIO pin configurable to enhance flexibility.


61-62: Deactivation method is correctly implemented.


64-71: Data flushing method is efficiently implemented.

@approxima approxima marked this pull request as draft May 11, 2024 16:51
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.

None yet

1 participant