Skip to content

Begin update to new protocol #247

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 2 commits into from
Mar 14, 2025
Merged

Begin update to new protocol #247

merged 2 commits into from
Mar 14, 2025

Conversation

bessman
Copy link
Collaborator

@bessman bessman commented Mar 4, 2025

This PR updates pslab-python to use the new protocol currently being worked on here: fossasia/pslab-firmware#180

The new protocol has two major changes from the old one:

  1. Every command is preceded by an eight-byte header, instead of the previous two-byte command code. The first two bytes in the header is the command code, the next two bytes are the size of the input data. The last four bytes in the header are reserved.
  2. Every command is acknowledged with a response header, which has the same size and format as the command header, except it carries a status code in place of the command code.

This flowchart summarizes the new protocol:

protocol-ng

Summary by Sourcery

Updates the communication protocol to use a new header-based format for commands and responses. This change involves modifying the structure of data transmission and reception, including the introduction of a header containing command codes, data sizes, and status codes. The update also includes changes to how version information is retrieved from the device.

New Features:

  • Introduces a new exchange method in the Connection class for sending commands and receiving responses with the new protocol header format.

Enhancements:

  • Replaces individual send_byte calls with a new exchange method for sending commands and receiving responses, streamlining the communication process.
  • Updates the rgb_led function to use the new exchange method and simplifies the logic for setting RGB LED colors.
  • Removes the check for SPI config support in the _set_gain method, simplifying the gain setting process.
  • Updates get_version and get_firmware_version to use the new exchange method.
  • Removes unused NRF constants in protocol.py

Copy link

sourcery-ai bot commented Mar 4, 2025

Reviewer's Guide by Sourcery

This PR updates the communication protocol to use a new header-based format for commands and responses. It introduces a new exchange method in Connection to handle the new protocol, and updates the get_version, get_firmware_version, rgb_led, and _set_gain methods to use the new exchange method. It also adds a struct for packing and unpacking the header.

Sequence diagram for get_version with new protocol

sequenceDiagram
  participant SL as ScienceLab
  participant C as Connection

  SL->>C: get_version()
  C->>C: exchange(COMMON + GET_VERSION)
  C->>C: pack header (cmd, len(data))
  C->>C: write(header + data)
  C->>C: read(header.size)
  C->>C: unpack header (status, response_size)
  alt status != 0
    C-->>SL: raise Exception(status)
  else
    C->>C: read(response_size)
    C-->>SL: version
  end
  SL-->>SL: version.rstrip(b"\x00").decode("utf-8")
  SL-->>SL: return version
Loading

Sequence diagram for get_firmware_version with new protocol

sequenceDiagram
  participant SL as ScienceLab
  participant C as Connection

  SL->>C: get_firmware_version()
  C->>C: exchange(COMMON + GET_FW_VERSION)
  C->>C: pack header (cmd, len(data))
  C->>C: write(header + data)
  C->>C: read(header.size)
  C->>C: unpack header (status, response_size)
  alt status != 0
    C-->>SL: raise Exception(status)
  else
    C->>C: read(response_size)
    C-->>SL: major, minor, patch
  end
  SL-->>SL: return FirmwareVersion(major, minor, patch)
Loading

Updated class diagram for Connection

classDiagram
  class Connection {
    +write(data: bytes) int
    +exchange(cmd: bytes, data: bytes) bytes
    +get_byte() int
    +get_version() str
    +get_firmware_version() FirmwareVersion
  }
  class FirmwareVersion {
    +major: int
    +minor: int
    +patch: int
  }
  Connection -- FirmwareVersion: returns
Loading

Updated class diagram for ScienceLab

classDiagram
  class ScienceLab {
    -device: ConnectionHandler
    +version: str
    +logic_analyzer: LogicAnalyzer
    +oscilloscope: Oscilloscope
    +waveform_generator: WaveformGenerator
    +datalogger: Datalogger
    +multimeter: Multimeter
    +power_source: PowerSource
    +i2c: I2C
    +spi: SPIMaster
    +cap_meter: CapacitanceMeter
    +res_meter: ResistanceMeter
    +freq_counter: FrequencyCounter
    +rgb_led(colors: List, output: str, order: str)
    -_read_program_address(address: int)
  }
  ScienceLab -- ConnectionHandler: has a
  ScienceLab -- LogicAnalyzer: has a
  ScienceLab -- Oscilloscope: has a
  ScienceLab -- WaveformGenerator: has a
  ScienceLab -- Datalogger: has a
  ScienceLab -- Multimeter: has a
  ScienceLab -- PowerSource: has a
  ScienceLab -- I2C: has a
  ScienceLab -- SPIMaster: has a
  ScienceLab -- CapacitanceMeter: has a
  ScienceLab -- ResistanceMeter: has a
  ScienceLab -- FrequencyCounter: has a
  note for ScienceLab "version attribute added in constructor"
Loading

File-Level Changes

Change Details Files
Introduces a new exchange method to handle communication with the device using the new header-based protocol.
  • Implements the exchange method to construct and send the header and data, then receive the status and response.
  • Raises an exception if the status code indicates an error.
  • Raises a TimeoutError if the response size is less than expected.
pslab/connection/connection.py
Updates get_version and get_firmware_version methods to use the new exchange method for communication.
  • Replaces the calls to send_byte and read with a single call to exchange in get_version.
  • Removes the explicit version length and simplifies the version retrieval process.
  • Replaces the calls to send_byte and get_byte with a single call to exchange in get_firmware_version.
pslab/connection/connection.py
Updates the rgb_led method to use the new exchange method.
  • Constructs the command and arguments for setting the RGB LED.
  • Uses the exchange method to send the command and receive the response.
  • Removes the calls to send_byte and get_ack.
pslab/sciencelab.py
Removes the calls to send_byte and get_ack in the _set_gain method.
  • Removes the calls to _check_spi_config and spi.set_parameters.
  • Removes the calls to send_byte and get_ack.
pslab/instrument/oscilloscope.py
Adds a struct for packing and unpacking the header.
  • Adds a struct named Header.
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 changed the base branch from main to develop March 14, 2025 17:00
@bessman bessman marked this pull request as ready for review March 14, 2025 17:01
@bessman
Copy link
Collaborator Author

bessman commented Mar 14, 2025

I need this for FOSSASIA Summit workshop, so I'm merging it into a new 'develop' branch.

@bessman bessman merged commit c35f7a6 into fossasia:develop Mar 14, 2025
2 checks passed
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:

  • Consider adding a timeout to the read calls in exchange to prevent indefinite blocking.
  • The removal of send_byte calls is good, but ensure all usages are replaced with the new exchange method.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues 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.


Parameters
----------
cmd : int
Copy link

Choose a reason for hiding this comment

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

suggestion: Align cmd parameter type in docstring and implementation.

The docstring currently indicates that cmd is an int, whereas the implementation expects a bytes object (and unpacks it using CP.ShortInt.unpack). Consider updating the docstring so that it accurately reflects the required type.

Suggested change
cmd : int
cmd : bytes

Comment on lines +93 to +94
if status:
raise Exception(status)
Copy link

Choose a reason for hiding this comment

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

suggestion: Raise a more descriptive exception on status error.

Instead of raising a generic Exception with the status code, consider raising a custom exception or adding a more descriptive error message. This could aid in debugging and handling errors in client code.

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