Skip to content

fix: clarify and document CTMU temperature calibration logic#282

Open
saad-aids wants to merge 2 commits intofossasia:mainfrom
saad-aids:fix/ctmu-temperature-documentation
Open

fix: clarify and document CTMU temperature calibration logic#282
saad-aids wants to merge 2 commits intofossasia:mainfrom
saad-aids:fix/ctmu-temperature-documentation

Conversation

@saad-aids
Copy link
Copy Markdown

@saad-aids saad-aids commented Mar 28, 2026

  • Extract magic numbers into named module-level constants
  • Add _CTMU_CURRENT_RANGE_55UA constant for 0b11110 value
  • Add _TEMP_CALIB dict with documented calibration values
  • Expand temperature property docstring with full explanation

Fixes #272

Testing

  • Imported ScienceLab successfully with no errors
  • Behavior is identical to previous implementation
  • No functional changes, documentation and naming only

Summary by Sourcery

Document CTMU-based MCU temperature measurement and centralize its calibration constants.

Enhancements:

  • Introduce named constants for CTMU current range and temperature calibration values used in MCU temperature measurement.
  • Refine the ScienceLab.temperature property implementation to use shared calibration data instead of inlined magic numbers.
  • Expand the ScienceLab class and temperature property docstrings to describe the CTMU temperature measurement behavior and calibration.

- Extract magic numbers into named module-level constants
- Add _CTMU_CURRENT_RANGE_55UA constant for 0b11110 value
- Add _TEMP_CALIB dict with documented calibration values
- Expand temperature property docstring with full explanation

Fixes fossasia#272
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 28, 2026

Reviewer's Guide

Refactors the MCU temperature measurement to replace embedded magic numbers with named calibration constants and an explanatory docstring, while keeping behavior identical.

Class diagram for updated ScienceLab temperature calibration logic

classDiagram

class ScienceLab {
    +ConnectionHandler device
    +str firmware
    +LogicAnalyzer logic_analyzer
    +Oscilloscope oscilloscope
    +WaveformGenerator waveform_generator
    +PWMGenerator pwm_generator
    +Multimeter multimeter
    +PowerSupply power_supply
    +float temperature
    +__init__(ConnectionHandler device)
    +float temperature()
    -float _get_ctmu_voltage(int channel, int current_range, bool tgen)
}

class CTMUCalibration {
    <<module>>
    +int _CTMU_CURRENT_RANGE_55UA
    +dict _TEMP_CALIB
}

ScienceLab ..> CTMUCalibration : uses
Loading

File-Level Changes

Change Details Files
Replace hard-coded CTMU current range value with a named constant for temperature measurement.
  • Introduce a module-level _CTMU_CURRENT_RANGE_55UA constant representing the 0b11110 (55 µA) CTMU current range index.
  • Update temperature property to call _get_ctmu_voltage using the new named constant instead of the literal 0b11110.
pslab/sciencelab.py
Centralize and document CTMU temperature calibration data.
  • Introduce a module-level _TEMP_CALIB dict mapping CTMU current source indices to (offset_mV, slope_mV_per_degree_celsius) tuples.
  • Replace the inline if/elif chain with a lookup in _TEMP_CALIB using the current source index, followed by a generic temperature computation.
  • Preserve the previous calibration values exactly to avoid behavior changes.
pslab/sciencelab.py
Improve documentation of the MCU temperature measurement behavior.
  • Expand the temperature property docstring to explain CTMU usage, current range, channel, and calibration approach.
  • Clarify parameter meanings (e.g., current source index and tgen flag) at the call site to _get_ctmu_voltage via inline comments.
pslab/sciencelab.py

Assessment against linked issues

Issue Objective Addressed Explanation
#272 Centralize and clearly name CTMU temperature calibration parameters and related hardware-specific constants (current range, ADC configuration, etc.) instead of using inline magic numbers.
#272 Document the remaining constants and calibration behavior, including meaning and units of calibration parameters and CTMU configuration, so the temperature calibration logic is easier to audit and understand.
#272 Maintain identical runtime behavior of the temperature calibration logic (refactor and documentation only, with no functional changes).

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

Copy link
Copy Markdown

@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 - I've found 1 issue, and left some high level feedback:

  • There are now two ScienceLab class definitions in this module; please consolidate the changes into the existing class and remove the duplicate definition to avoid one silently shadowing the other.
  • The CTMU constant _CTMU_CURRENT_RANGE_55UA is named as a current range but is passed as the channel argument to _get_ctmu_voltage; consider renaming the constant or adjusting the call/signature so the naming clearly matches the parameter being set.
  • The module-level _CTMU_CURRENT_RANGE_55UA declaration has an extra leading space before the comment, which makes it appear visually (and potentially syntactically, depending on context) tied to the preceding class; move these constants clearly outside/above the class with consistent top-level indentation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- There are now two `ScienceLab` class definitions in this module; please consolidate the changes into the existing class and remove the duplicate definition to avoid one silently shadowing the other.
- The CTMU constant `_CTMU_CURRENT_RANGE_55UA` is named as a current range but is passed as the `channel` argument to `_get_ctmu_voltage`; consider renaming the constant or adjusting the call/signature so the naming clearly matches the parameter being set.
- The module-level `_CTMU_CURRENT_RANGE_55UA` declaration has an extra leading space before the comment, which makes it appear visually (and potentially syntactically, depending on context) tied to the preceding class; move these constants clearly outside/above the class with consistent top-level indentation.

## Individual Comments

### Comment 1
<location path="pslab/sciencelab.py" line_range="22-31" />
<code_context>
+class ScienceLab:
</code_context>
<issue_to_address>
**issue (bug_risk):** Two ScienceLab classes are now defined; this will shadow the later definition and is likely unintended.

The duplicate `ScienceLab` definition in this module will shadow the other one and can cause subtle bugs if callers rely on the behavior in the existing class (e.g., `temperature` property and `_get_ctmu_voltage`). Please fold the new initialization logic into the existing `ScienceLab` instead of defining a second class.
</issue_to_address>

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.

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.

Clarify and document CTMU temperature calibration logic

1 participant