Skip to content

Summit/blink/solution #248

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

Merged
merged 3 commits into from
Mar 14, 2025

Conversation

bessman
Copy link
Collaborator

@bessman bessman commented Mar 14, 2025

This includes a solution for the PSLab Development 101 workshop at FOSSASIA Summit 2025.

It shall not be merged into main.

Summary by Sourcery

Implements a blink functionality for the PSLab device's onboard RGB LED, including a Python implementation and a firmware implementation. Refactors the connection and RGB LED control logic to use a new exchange method for sending commands and data.

New Features:

  • Adds a blink function to control the onboard RGB LED with a specified color and period.
  • Implements a firmware-based blink function for the RGB LED.

Copy link

sourcery-ai bot commented Mar 14, 2025

Reviewer's Guide by Sourcery

This pull request introduces a blink functionality for the PSLab device's onboard RGB LED, including a Python implementation and a firmware implementation. It refactors the connection and rgb_led methods to use the new exchange method.

Sequence diagram for get_version method

sequenceDiagram
  participant S as ScienceLab
  participant C as Connection
  S->>C: get_version()
  activate C
  C->>C: exchange(COMMON + GET_VERSION)
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: version
  deactivate C
Loading

Sequence diagram for get_firmware_version method

sequenceDiagram
  participant S as ScienceLab
  participant C as Connection
  S->>C: get_firmware_version()
  activate C
  C->>C: exchange(COMMON + GET_FW_VERSION)
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: FirmwareVersion(major, minor, patch)
  deactivate C
Loading

Sequence diagram for rgb_led method

sequenceDiagram
  participant S as ScienceLab
  participant C as Connection
  S->>S: rgb_led(colors, output, order)
  S->>C: exchange(cmd, args)
  activate C
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: 
  deactivate C
Loading

Sequence diagram for blink_c method with period

sequenceDiagram
  participant S as ScienceLab
  participant C as Connection
  S->>S: blink_c(psl, color, period)
  S->>C: exchange(cmd, args)
  activate C
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: 
  deactivate C
Loading

Sequence diagram for blink_c method without period

sequenceDiagram
  participant S as ScienceLab
  participant C as Connection
  S->>S: blink_c(psl, color, period)
  S->>C: exchange(cmd)
  activate C
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: 
  deactivate C
  S->>S: rgb_led(color)
  S->>C: exchange(cmd, args)
  activate C
  C->>C: write(header + data)
  C->>C: read(CP.Header.size)
  C->>C: read(response_size)
  C-->>S: 
  deactivate C
Loading

File-Level Changes

Change Details Files
Introduces a new exchange method to the Connection class for sending commands and data to the PSLab device and receiving responses.
  • Adds the exchange method to handle command and data exchange with the device.
  • Implements error handling based on the status code received in the response header.
  • Raises an exception if the status code indicates an error.
  • Raises a TimeoutError if the length of the response is less than the expected size.
pslab/connection/connection.py
Refactors the get_version and get_firmware_version methods in the Connection class to use the new exchange method.
  • Updates get_version to use exchange for retrieving the version string.
  • Updates get_firmware_version to use exchange for retrieving firmware version information.
  • Removes the manual sending of command bytes and reading of response bytes.
  • Removes the version length variable.
pslab/connection/connection.py
Adds a blink function to control the onboard RGB LED.
  • Introduces a new blink function that takes a ScienceLab instance, a color tuple, and a blink period as input.
  • Implements the blink functionality by toggling the RGB LED on and off using the rgb_led method.
  • Uses time.time() to control the blink timing.
pslab/blink.py
Adds a blink_c function to control the onboard RGB LED using a firmware implementation.
  • Introduces a new blink_c function that takes a ScienceLab instance, a color tuple, and a blink period as input.
  • Implements the blink functionality by sending a command to the firmware with the color and period information.
  • If the period is zero, the function turns on the LED with the specified color and stops the blinking.
pslab/blink.py
Updates the rgb_led method in the ScienceLab class to use the new exchange method.
  • Updates rgb_led to use exchange for sending the RGB LED control command and data.
  • Removes the manual sending of command bytes and reading of acknowledgment bytes.
  • Adds a conditional to handle the onboard RGB led.
pslab/sciencelab.py
Updates the protocol definition.
  • Adds a struct definition for the header.
  • Removes the NRFL01 radio module definitions.
pslab/protocol.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bessman bessman marked this pull request as ready for review March 14, 2025 17:05
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bessman - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The new exchange method looks good, but consider adding a timeout parameter to handle cases where the device doesn't respond.
  • It looks like the rgb_led method is being modified to use the new exchange method, which is great. However, the conditional logic based on device version could be simplified for better readability.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

self.device.send_byte(pin)

self.device.get_ack()
cmd = CP.COMMON + CP.SET_RGB_COMMON
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Undefined variable 'pin' in rgb_led.

The code packs a 'pin' variable with CP.Byte.pack(pin) but it is never defined. Likely the intention is to extract the correct value from 'pins' (e.g. 'pin = pins[output]').

@bessman bessman merged commit 3be1fc6 into fossasia:summit/blink/solution Mar 14, 2025
2 checks passed
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.

1 participant