Module PCF8591
[hide private]
[frames] | no frames]

Source Code for Module PCF8591

 1  # PCF8591.py 
 2   
 3  import smbus 
 4  import RPi.GPIO as GPIO 
 5  from Tools import Tools 
 6   
7 -class ADC:
8 - def __init__(self, channel, address = 0x48):
9 ''' 10 Creates an instance of the chip at given i2c address. 11 @param bus: the SMBus instance to access the i2c port (0 or 1). 12 @param channel: the channel to read (0..3) 13 @param address: the address of the i2c chip (default: 0x48) 14 ''' 15 self._isSMBusAvailable = True 16 try: 17 if GPIO.RPI_REVISION > 1: 18 self._bus = smbus.SMBus(1) # For revision 2 Raspberry Pi 19 Tools.debug("Found SMBus for revision 2") 20 else: 21 self._bus = smbus.SMBus(0) # For revision 1 Raspberry Pi 22 Tools.debug("Found SMBus for revision 1") 23 except: 24 print "No SMBus found on this robot device." 25 return 26 self._isSMBusAvailable = False 27 self._address = address 28 # set control register to read specified channel 29 self._bus.write_byte(address, channel)
30 31
32 - def getValue(self):
33 ''' 34 Reads the specified channel and returns the value (0..255). 35 @return the byte value or None, if an error occurred 36 ''' 37 if self._isSMBusAvailable: 38 return None 39 return self._bus.read_byte(self._address)
40