Skip to content

Commit dd4910d

Browse files
committed
Allow ADC max voltage to be passed into analog read function and conversions.
1 parent 36249db commit dd4910d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

xbee_helper/device.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ def hex_to_int(value):
4848
return int(value.encode("hex"), 16)
4949

5050

51-
def adc_to_percentage(value, clamp=True):
51+
def adc_to_percentage(value, max_volts, clamp=True):
5252
"""
5353
Convert the ADC raw value to a percentage.
5454
"""
5555
percentage = (100.0 / const.ADC_MAX_VAL) * value
5656
return max(min(100, percentage), 0) if clamp else percentage
5757

5858

59-
def adc_to_volts(value):
59+
def adc_to_volts(value, max_volts):
6060
"""
6161
Convert the ADC raw value to Volts.
6262
"""
63-
return (float(MAX_VOLTAGE) / const.ADC_MAX_VAL) * value
63+
return (float(max_volts) / const.ADC_MAX_VAL) * value
6464

6565

66-
def adc_to_millivolts(value):
66+
def adc_to_millivolts(value, max_volts):
6767
"""
6868
Convert the ADC raw value to Millivolts
6969
"""
70-
return int(adc_to_volts(value) * 1000)
70+
return int(adc_to_volts(value, max_volts) * 1000)
7171

7272

73-
def convert_adc(value, output_type):
73+
def convert_adc(value, output_type, max_volts):
7474
"""
7575
Converts the output from the ADC into the desired type.
7676
"""
@@ -79,7 +79,7 @@ def convert_adc(value, output_type):
7979
const.ADC_PERCENTAGE: adc_to_percentage,
8080
const.ADC_VOLTS: adc_to_volts,
8181
const.ADC_MILLIVOLTS: adc_to_millivolts
82-
}[output_type](value)
82+
}[output_type](value, max_volts)
8383

8484

8585
class ZigBee(object):
@@ -187,7 +187,8 @@ def read_digital_pin(self, pin_number, dest_addr_long=None):
187187
% (pin_number, const.IO_PIN_COMMANDS[pin_number]))
188188

189189
def read_analog_pin(
190-
self, pin_number, dest_addr_long=None, output_type=const.ADC_RAW):
190+
self, pin_number, adc_max_volts,
191+
dest_addr_long=None, output_type=const.ADC_RAW):
191192
"""
192193
Fetches a sample and returns the integer value of the requested analog
193194
pin. output_type should be one of the following constants from
@@ -200,7 +201,10 @@ def read_analog_pin(
200201
sample = self.get_sample(dest_addr_long=dest_addr_long)
201202
try:
202203
return convert_adc(
203-
sample[const.ANALOG_PINS[pin_number]], output_type)
204+
sample[const.ANALOG_PINS[pin_number]],
205+
output_type,
206+
adc_max_volts
207+
)
204208
except KeyError:
205209
raise exceptions.ZigBeePinNotConfigured(
206210
"Pin %s (%s) is not configured as an analog input." % (

0 commit comments

Comments
 (0)