@@ -109,8 +109,14 @@ def __get_smbus(bus):
109109 break
110110 try :
111111 return SMBus (i2c__bus )
112- except IOError :
113- raise 'Could not open the i2c bus'
112+ except FileNotFoundError :
113+ raise FileNotFoundError ("Bus not found. Check that you have selected the correct I2C bus." )
114+ except OSError as err :
115+ raise ("OS error: {0}" .format (err ))
116+ except IOError as err :
117+ raise ("IO error: {0}" .format (err ))
118+ except Exception as err :
119+ raise err
114120
115121 def __updatebyte (self , byte , mask , value ):
116122 """
@@ -135,8 +141,9 @@ def __setchannel(self, channel):
135141
136142 :param channel: selected channel
137143 :type channel: int
144+ :raises ValueError: __setchannel: channel out of range 1 to 8
138145 """
139- if channel < 5 :
146+ if channel >= 1 and channel <= 4 :
140147 if channel != self .__adc1_channel :
141148 self .__adc1_channel = channel
142149 if channel == 1 : # bit 5 = 1, bit 6 = 0
@@ -151,7 +158,7 @@ def __setchannel(self, channel):
151158 elif channel == 4 : # bit 5 = 1, bit 6 = 1
152159 self .__adc1_conf = self .__updatebyte (self .__adc1_conf ,
153160 0x9F , 0x60 )
154- else :
161+ elif channel >= 5 and channel <= 8 :
155162 if channel != self .__adc2_channel :
156163 self .__adc2_channel = channel
157164 if channel == 5 : # bit 5 = 1, bit 6 = 0
@@ -166,6 +173,8 @@ def __setchannel(self, channel):
166173 elif channel == 8 : # bit 5 = 1, bit 6 = 1
167174 self .__adc2_conf = self .__updatebyte (self .__adc2_conf ,
168175 0x9F , 0x60 )
176+ else :
177+ raise ValueError ('__setchannel: channel out of range 1 to 8' )
169178 return
170179
171180 # init object with i2caddress, default is 0x68, 0x69 for ADCoPi board
@@ -176,25 +185,77 @@ def __init__(self, address=0x68, address2=0x69, rate=18, bus=None):
176185
177186 :param address: I2C address for channels 1 to 4, defaults to 0x68
178187 :type address: int, optional
188+ :raises ValueError: address out of range 0x68 to 0x6F
179189 :param address2: I2C address for channels 5 to 8, defaults to 0x69
180190 :type address2: int, optional
191+ :raises ValueError: address2 out of range 0x68 to 0x6F
181192 :param rate: bit rate, defaults to 18
182193 :type rate: int, optional
183194 :param bus: I2C bus number. If no value is set the class will try to
184195 find the i2c bus automatically using the device name
185196 :type bus: int, optional
186197 """
187198 self .__bus = self .__get_smbus (bus )
188- self .__adc1_address = address
189- self .__adc2_address = address2
199+ if address >= 0x68 and address <= 0x6F :
200+ self .__adc1_address = address
201+ else :
202+ raise ValueError ('address out of range 0x68 to 0x6F' )
203+
204+ if address2 >= 0x68 and address2 <= 0x6F :
205+ self .__adc2_address = address2
206+ else :
207+ raise ValueError ('address2 out of range 0x68 to 0x6F' )
190208 self .set_bit_rate (rate )
191209
210+ def set_i2c_address1 (self , address ):
211+ """
212+ Set the I2C address for the ADC on channels 1 to 4
213+
214+ :param address: I2C address for channels 1 to 4, defaults to 0x68
215+ :type address: int, optional
216+ :raises ValueError: address out of range 0x68 to 0x6F
217+ """
218+ if address >= 0x68 and address <= 0x6F :
219+ self .__adc1_address = address
220+ else :
221+ raise ValueError ('address out of range 0x68 to 0x6F' )
222+
223+ def set_i2caddress2 (self , address ):
224+ """
225+ Set the I2C address for the ADC on channels 5 to 8
226+
227+ :param address: I2C address for channels 5 to 8, defaults to 0x68
228+ :type address: int, optional
229+ :raises ValueError: address out of range 0x68 to 0x6F
230+ """
231+ if address >= 0x68 and address <= 0x6F :
232+ self .__adc2_address = address
233+ else :
234+ raise ValueError ('address out of range 0x68 to 0x6F' )
235+
236+ def get_i2c_address1 (self ):
237+ """
238+ Get the I2C address for the ADC on channels 1 to 4
239+ :return: I2C address
240+ :rtype: number
241+ """
242+ return self .__adc1_address
243+
244+ def get_i2c_address2 (self ):
245+ """
246+ Get the I2C address for the ADC on channels 5 to 8
247+ :return: I2C address
248+ :rtype: number
249+ """
250+ return self .__adc2_address
251+
192252 def read_voltage (self , channel ):
193253 """
194254 Returns the voltage from the selected ADC channel
195255
196256 :param channel: 1 to 8
197257 :type channel: int
258+ :raises ValueError: read_voltage: channel out of range (1 to 8 allowed)
198259 :return: voltage
199260 :rtype: float
200261 """
0 commit comments