@@ -48,29 +48,29 @@ def hex_to_int(value):
48
48
return int (value .encode ("hex" ), 16 )
49
49
50
50
51
- def adc_to_percentage (value , clamp = True ):
51
+ def adc_to_percentage (value , max_volts , clamp = True ):
52
52
"""
53
53
Convert the ADC raw value to a percentage.
54
54
"""
55
55
percentage = (100.0 / const .ADC_MAX_VAL ) * value
56
56
return max (min (100 , percentage ), 0 ) if clamp else percentage
57
57
58
58
59
- def adc_to_volts (value ):
59
+ def adc_to_volts (value , max_volts ):
60
60
"""
61
61
Convert the ADC raw value to Volts.
62
62
"""
63
- return (float (MAX_VOLTAGE ) / const .ADC_MAX_VAL ) * value
63
+ return (float (max_volts ) / const .ADC_MAX_VAL ) * value
64
64
65
65
66
- def adc_to_millivolts (value ):
66
+ def adc_to_millivolts (value , max_volts ):
67
67
"""
68
68
Convert the ADC raw value to Millivolts
69
69
"""
70
- return int (adc_to_volts (value ) * 1000 )
70
+ return int (adc_to_volts (value , max_volts ) * 1000 )
71
71
72
72
73
- def convert_adc (value , output_type ):
73
+ def convert_adc (value , output_type , max_volts ):
74
74
"""
75
75
Converts the output from the ADC into the desired type.
76
76
"""
@@ -79,7 +79,7 @@ def convert_adc(value, output_type):
79
79
const .ADC_PERCENTAGE : adc_to_percentage ,
80
80
const .ADC_VOLTS : adc_to_volts ,
81
81
const .ADC_MILLIVOLTS : adc_to_millivolts
82
- }[output_type ](value )
82
+ }[output_type ](value , max_volts )
83
83
84
84
85
85
class ZigBee (object ):
@@ -187,7 +187,8 @@ def read_digital_pin(self, pin_number, dest_addr_long=None):
187
187
% (pin_number , const .IO_PIN_COMMANDS [pin_number ]))
188
188
189
189
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 ):
191
192
"""
192
193
Fetches a sample and returns the integer value of the requested analog
193
194
pin. output_type should be one of the following constants from
@@ -200,7 +201,10 @@ def read_analog_pin(
200
201
sample = self .get_sample (dest_addr_long = dest_addr_long )
201
202
try :
202
203
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
+ )
204
208
except KeyError :
205
209
raise exceptions .ZigBeePinNotConfigured (
206
210
"Pin %s (%s) is not configured as an analog input." % (
0 commit comments