From 7bbaa174ca338cacbd5132b2b588e141009f2cee Mon Sep 17 00:00:00 2001 From: vabhravi Date: Wed, 28 Jan 2026 21:11:54 +0530 Subject: [PATCH 1/4] Docs: Add docstring to _calibrate method in AnalogInput --- pslab/instrument/analog.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pslab/instrument/analog.py b/pslab/instrument/analog.py index bc3ccca0..ec706b25 100644 --- a/pslab/instrument/analog.py +++ b/pslab/instrument/analog.py @@ -144,6 +144,12 @@ def resolution(self, value: int): self._calibrate() def _calibrate(self): + """ + Calculates the scaling coefficients based on current gain and resolution. + + This prepares the linear functions (_scale and _unscale) used to convert + between raw integer ADC values and floating point voltages. + """ A = INPUT_RANGES[self._name][0] / self._gain B = INPUT_RANGES[self._name][1] / self._gain slope = B - A @@ -243,4 +249,4 @@ def lowres_waveform_table(self) -> np.ndarray: def _range_normalize(self, x: np.ndarray, norm: int = 1) -> np.ndarray: """Normalize waveform table to the digital output range.""" x = (x - self.RANGE[0]) / (self.RANGE[1] - self.RANGE[0]) * norm - return np.int16(np.round(x)).tolist() + return np.int16(np.round(x)).tolist() \ No newline at end of file From c7d6293986846aa564780f947b8aeeb434bd33b4 Mon Sep 17 00:00:00 2001 From: vabhravi Date: Wed, 28 Jan 2026 21:28:48 +0530 Subject: [PATCH 2/4] Docs: Remove placeholder text and fix whitespace --- pslab/instrument/analog.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pslab/instrument/analog.py b/pslab/instrument/analog.py index ec706b25..3ebc03b4 100644 --- a/pslab/instrument/analog.py +++ b/pslab/instrument/analog.py @@ -143,19 +143,20 @@ def resolution(self, value: int): self._resolution = 2**value - 1 self._calibrate() - def _calibrate(self): - """ - Calculates the scaling coefficients based on current gain and resolution. + def _calibrate(self): + """ + Calculates the scaling coefficients based on current gain and resolution. - This prepares the linear functions (_scale and _unscale) used to convert - between raw integer ADC values and floating point voltages. - """ - A = INPUT_RANGES[self._name][0] / self._gain - B = INPUT_RANGES[self._name][1] / self._gain - slope = B - A - intercept = A - self._scale = np.poly1d([slope / self._resolution, intercept]) - self._unscale = np.poly1d( + This updates the internal _scale and _unscale callables as a side effect, + preparing them to convert between raw integer ADC values and floating + point voltages. + """ + A = INPUT_RANGES[self._name][0] / self._gain + B = INPUT_RANGES[self._name][1] / self._gain + slope = B - A + intercept = A + self._scale = np.poly1d([slope / self._resolution, intercept]) + self._unscale = np.poly1d( [self._resolution / slope, -self._resolution * intercept / slope] ) @@ -245,8 +246,7 @@ def lowres_waveform_table(self) -> np.ndarray: """numpy.ndarray: 32-value waveform table loaded on this output.""" # Max PWM duty cycle out of 64 clock cycles. return self._range_normalize(self._waveform_table[::16], 63) - - def _range_normalize(self, x: np.ndarray, norm: int = 1) -> np.ndarray: - """Normalize waveform table to the digital output range.""" - x = (x - self.RANGE[0]) / (self.RANGE[1] - self.RANGE[0]) * norm - return np.int16(np.round(x)).tolist() \ No newline at end of file +def _range_normalize(self, x: np.ndarray, norm: int = 1) -> np.ndarray: + """Normalize waveform table to the digital output range.""" + x = (x - self.RANGE[0]) / (self.RANGE[1] - self.RANGE[0]) * norm + return np.int16(np.round(x)).tolist() \ No newline at end of file From ff20446bfd8bcc7293cf889a2d68cf4b87a6ae69 Mon Sep 17 00:00:00 2001 From: vabhravi Date: Sat, 31 Jan 2026 18:40:42 +0530 Subject: [PATCH 3/4] Fix: Correct indentation in analog.py --- pslab/instrument/analog.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pslab/instrument/analog.py b/pslab/instrument/analog.py index 3ebc03b4..14ee2ee7 100644 --- a/pslab/instrument/analog.py +++ b/pslab/instrument/analog.py @@ -143,20 +143,20 @@ def resolution(self, value: int): self._resolution = 2**value - 1 self._calibrate() - def _calibrate(self): - """ - Calculates the scaling coefficients based on current gain and resolution. + def _calibrate(self): + """ + Calculates the scaling coefficients based on current gain and resolution. - This updates the internal _scale and _unscale callables as a side effect, - preparing them to convert between raw integer ADC values and floating - point voltages. - """ - A = INPUT_RANGES[self._name][0] / self._gain - B = INPUT_RANGES[self._name][1] / self._gain - slope = B - A - intercept = A - self._scale = np.poly1d([slope / self._resolution, intercept]) - self._unscale = np.poly1d( + This updates the internal _scale and _unscale callables as a side effect, + preparing them to convert between raw integer ADC values and floating + point voltages. + """ + A = INPUT_RANGES[self._name][0] / self._gain + B = INPUT_RANGES[self._name][1] / self._gain + slope = B - A + intercept = A + self._scale = np.poly1d([slope / self._resolution, intercept]) + self._unscale = np.poly1d( [self._resolution / slope, -self._resolution * intercept / slope] ) @@ -246,7 +246,8 @@ def lowres_waveform_table(self) -> np.ndarray: """numpy.ndarray: 32-value waveform table loaded on this output.""" # Max PWM duty cycle out of 64 clock cycles. return self._range_normalize(self._waveform_table[::16], 63) -def _range_normalize(self, x: np.ndarray, norm: int = 1) -> np.ndarray: - """Normalize waveform table to the digital output range.""" - x = (x - self.RANGE[0]) / (self.RANGE[1] - self.RANGE[0]) * norm - return np.int16(np.round(x)).tolist() \ No newline at end of file + + def _range_normalize(self, x: np.ndarray, norm: int = 1) -> np.ndarray: + """Normalize waveform table to the digital output range.""" + x = (x - self.RANGE[0]) / (self.RANGE[1] - self.RANGE[0]) * norm + return np.int16(np.round(x)).tolist() \ No newline at end of file From e05454c6c85f1a85c7a65b3bed4b5bc8c504799f Mon Sep 17 00:00:00 2001 From: vabhravi Date: Sat, 31 Jan 2026 19:36:37 +0530 Subject: [PATCH 4/4] Fix TypeError in SPI tests frequency calculation --- tests/test_spi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_spi.py b/tests/test_spi.py index ecd73d92..9672018a 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -30,7 +30,7 @@ CS = "LA3" SPIMaster._primary_prescaler = PPRE = 0 SPIMaster._secondary_prescaler = SPRE = 0 -PWM_FERQUENCY = SPIMaster._frequency * 2 / 3 +PWM_FERQUENCY = 166666.67 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes.