@@ -96,8 +96,8 @@ def __init__(
9696 data_rate : Optional [int ] = None ,
9797 mode : int = Mode .SINGLE ,
9898 comparator_queue_length : int = 0 ,
99- comparator_low_threshold : int = 0x8000 ,
100- comparator_high_threshold : int = 0x7FF0 ,
99+ comparator_low_threshold : int = - 2048 ,
100+ comparator_high_threshold : int = 2047 ,
101101 address : int = _ADS1X15_DEFAULT_ADDRESS ,
102102 ):
103103 # pylint: disable=too-many-arguments
@@ -187,17 +187,24 @@ def comparator_low_threshold(self) -> int:
187187
188188 @comparator_low_threshold .setter
189189 def comparator_low_threshold (self , comparator_low_threshold : int ) -> None :
190- """Sets 12-bit threshold in 16-bit register in unsigned format. """
191- if comparator_low_threshold < 0 or comparator_low_threshold > 65535 :
190+ """Sets Comparator low threshold value """
191+ if comparator_low_threshold < - 2048 or comparator_low_threshold > 2047 :
192192 raise ValueError (
193- "Comparator Low Threshold must be unsigned 16-bit integer between 0 and 65535 "
193+ "Comparator Low Threshold must be an integer between -2048 and 2047 "
194194 )
195195 self ._comparator_low_threshold = comparator_low_threshold
196196
197+ """Convert integer value to 12-bit twos complement and bit shift"""
198+ if comparator_low_threshold < 0 :
199+ tempval = 4096 + comparator_low_threshold
200+ else :
201+ tempval = comparator_low_threshold
202+ tempval <<= 4
203+
197204 """Write value to chip"""
198205 self .buf [0 ] = _ADS1X15_POINTER_LO_THRES
199- self .buf [1 ] = (self . _comparator_low_threshold >> 8 ) & 0xFF
200- self .buf [2 ] = self . _comparator_low_threshold & 0xFF
206+ self .buf [1 ] = (tempval >> 8 ) & 0xFF
207+ self .buf [2 ] = tempval & 0xFF
201208 with self .i2c_device as i2c :
202209 i2c .write (self .buf )
203210
@@ -208,17 +215,24 @@ def comparator_high_threshold(self) -> int:
208215
209216 @comparator_high_threshold .setter
210217 def comparator_high_threshold (self , comparator_high_threshold : int ) -> None :
211- """Sets 12-bit threshold in 16-bit register in unsigned format. """
212- if comparator_high_threshold < 0 or comparator_high_threshold > 65535 :
218+ """Sets Comparator high threshold value """
219+ if comparator_high_threshold < - 2048 or comparator_high_threshold > 2047 :
213220 raise ValueError (
214- "Comparator High Threshold must be unsigned 16-bit integer between 0 and 65535 "
221+ "Comparator High Threshold must be an integer between -2048 and 2047 "
215222 )
216223 self ._comparator_high_threshold = comparator_high_threshold
217224
225+ """Convert integer value to 12-bit twos complement and bit shift"""
226+ if comparator_high_threshold < 0 :
227+ tempval = 4096 + comparator_high_threshold
228+ else :
229+ tempval = comparator_high_threshold
230+ tempval <<= 4
231+
218232 """Write value to chip"""
219233 self .buf [0 ] = _ADS1X15_POINTER_HI_THRES
220- self .buf [1 ] = (self . _comparator_high_threshold >> 8 ) & 0xFF
221- self .buf [2 ] = self . _comparator_high_threshold & 0xFF
234+ self .buf [1 ] = (tempval >> 8 ) & 0xFF
235+ self .buf [2 ] = tempval & 0xFF
222236 with self .i2c_device as i2c :
223237 i2c .write (self .buf )
224238
0 commit comments