Skip to content

fix: separate ADC source offsets from buffer destinations - #304

Open
YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/adc-buffer-offset
Open

YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/adc-buffer-offset

Conversation

@YashAnand69

@YashAnand69 YashAnand69 commented Oct 6, 2026 •

Copy link
Copy Markdown

Fixes #303.

fill_buffer(data, starting_position) currently uses the device destination as an index into the input list. For example, writing [7, 8, 9] at position 10 declares three samples but sends none because it slices data[10:13]. Multi-chunk writes also skip input values and can declare more payload words than they send.

Track the input offset from zero and add the destination only when sending the buffer address. Every chunk then contains its declared number of samples, in order. The firmware’s BUFFER_Fill handler reads exactly that declared count; no firmware or protocol change is needed.

Validation on macOS/Python 3.12:

  • Unmodified main: twelve nonzero-destination regressions fail; six zero-destination/empty-data controls pass.
  • All eighteen focused cases pass. Destination offsets 0/1/10/200 and lengths 3/128/129/260 cover single, exact-boundary, partial-next and multiple chunks. Tests record the real mixin’s command sequence through a mocked connection, including destination, declared count, every payload word and acknowledgment order; inputs stay unchanged.
  • Focused cases plus existing CLI/MQ135/Pico suites: 51 passed.
  • Black, Flake8, Bandit source, pydocstyle source/tests and diff checks pass. Wheel/sdist builds succeed. Sphinx HTML builds with seven existing warnings.

Physical-device integration was not performed. This restores the documented nonzero-destination behavior; existing zero-destination and empty writes are preserved. No dependency or signature change. AI tools assisted implementation and testing; I reviewed the full diff and checked the firmware’s payload-consumption loop.

Summary by Sourcery

Correct ADC buffer filling to keep source data offsets independent from device destinations.

Bug Fixes:

  • Fix ADC buffer writes so nonzero destination offsets no longer skip input samples or send mismatched chunk payloads.

Tests:

  • Add coverage for single- and multi-chunk writes across destination offsets and boundary lengths, including command ordering, payload integrity, and unchanged inputs.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected chunked buffer writes so data is sent from the start of the input while destination positions still respect the requested starting position.
  • Tests

    • Added coverage for different starting positions, chunk sizes, default positioning, and empty input.

@sourcery-ai

sourcery-ai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Fixes ADC buffer writes by tracking payload offsets independently from destination addresses, ensuring nonzero-destination and multi-chunk writes transmit all input samples in order; comprehensive mocked command-sequence tests cover offsets, boundaries, immutability, and empty writes.

Sequence diagram for corrected ADC buffer writes

sequenceDiagram
    participant Caller
    participant BufferMixin
    participant Device
    Caller->>BufferMixin: fill_buffer(data, starting_position)
    loop Each chunk up to 128 samples
        BufferMixin->>Device: send_byte(COMMON)
        BufferMixin->>Device: send_byte(FILL_BUFFER)
        BufferMixin->>Device: send_int(starting_position + offset)
        BufferMixin->>Device: send_int(samps)
        loop Each sample in data[offset:offset + samps]
            BufferMixin->>Device: send_int(value)
        end
        Device-->>BufferMixin: get_ack()
        BufferMixin->>BufferMixin: offset += samps
    end
Loading

File-Level Changes

Change Details Files
Separate source-data indexing from device-buffer destination addressing during chunked writes.
  • Initialize an input offset at zero for every write.
  • Compute each chunk’s destination as the requested starting position plus the input offset.
  • Slice and transmit payload values using the input offset while preserving declared chunk counts and acknowledgment sequencing.
pslab/instrument/buffer.py
Add regression coverage for destination offsets, chunk boundaries, and no-op behavior.
  • Exercise offsets 0, 1, 10, and 200 across single- and multi-chunk payload sizes.
  • Assert the complete mocked command sequence, including addresses, counts, payloads, and acknowledgments.
  • Verify input data is unchanged and empty writes emit no commands.
tests/test_buffer_fill.py

Assessment against linked issues

Issue Objective Addressed Explanation
#303 Ensure ADC buffer writes use a separate input-data offset and device destination, so all input values are transmitted in order when writing at a nonzero starting position. ✅
#303 Preserve correct chunk sizes, destination addresses, acknowledgment ordering, input-list immutability, and existing zero-destination/empty-data behavior without changing the firmware or protocol. ✅
#303 Add regression coverage for single- and multi-chunk writes across nonzero destinations and relevant data lengths. ✅

Possibly linked issues


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!

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

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c56c4865-28b6-416c-88a0-f6678af0f39c
📥 Commits

Reviewing files that changed from the base of the PR and between 08de0d0 and 3b744a1.

📒 Files selected for processing (2)
  • pslab/instrument/buffer.py
  • tests/test_buffer_fill.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

ADCBufferMixin.fill_buffer now uses a zero-based offset to slice input data and the requested starting position to set each device destination. Tests cover chunking, nonzero and default destinations, unchanged input data, and empty input.

Changes

ADC buffer writes

Layer / File(s) Summary
Separate data offsets from destination positions
pslab/instrument/buffer.py, tests/test_buffer_fill.py
fill_buffer slices each payload chunk by its offset in data and writes it at starting_position + offset. Tests check chunk sizes, destination positions, acknowledgments, unchanged input data, default position, and empty input.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 3b744

The change fixes the reported nonzero-offset truncation by writing input values in order to the requested destinations. No actionable merge-blocking risk is established; it appears ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: separating input offsets from ADC buffer destinations.
Linked Issues check ✅ Passed Issue #303 requires fill_buffer to separate input offsets from device destinations and preserve payload order, chunk counts, acknowledgments, and the input list. The PR summary reports that `pslab/i…
Out of Scope Changes check ✅ Passed The reported changes are limited to ADCBufferMixin.fill_buffer and tests/test_buffer_fill.py. The implementation fixes issue #303, and the tests verify that fix. No unrelated change is reported.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the offset calculation is wrong, samples could be written to the wrong positions in the device buffer and later reads could use those incorrect values. Reverting stops further incorrect writes, and the bounded buffer contents can be repaired by rewriting them.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
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.

ADC buffer writes skip input values at nonzero destinations

1 participant