diff --git a/Code/Python_Code/07.1.1_ADC/ADCDevice.py b/Code/Python_Code/07.1.1_ADC/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/07.1.1_ADC/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/08.1.1_Softlight/ADCDevice.py b/Code/Python_Code/08.1.1_Softlight/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/08.1.1_Softlight/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/09.1.1_ColorfulSoftlight/ADCDevice.py b/Code/Python_Code/09.1.1_ColorfulSoftlight/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/09.1.1_ColorfulSoftlight/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/10.1.1_Nightlamp/ADCDevice.py b/Code/Python_Code/10.1.1_Nightlamp/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/10.1.1_Nightlamp/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/11.1.1_Thermometer/ADCDevice.py b/Code/Python_Code/11.1.1_Thermometer/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/11.1.1_Thermometer/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/12.1.1_Joystick/ADCDevice.py b/Code/Python_Code/12.1.1_Joystick/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/12.1.1_Joystick/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value diff --git a/Code/Python_Code/13.1.1_Motor/ADCDevice.py b/Code/Python_Code/13.1.1_Motor/ADCDevice.py new file mode 100644 index 0000000..6319305 --- /dev/null +++ b/Code/Python_Code/13.1.1_Motor/ADCDevice.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +######################################################################## +# Filename : ADCDevice.py +# Description : Freenove ADC Module library. +# Author : www.freenove.com +# modification: 2020/04/21 +######################################################################## + +import smbus + +class ADCDevice(object): + def __init__(self): + self.cmd = 0 + self.address = 0 + self.bus=smbus.SMBus(1) + # print("ADCDevice init") + + def detectI2C(self,addr): + try: + self.bus.write_byte(addr,0) + print("Found device in address 0x%x"%(addr)) + return True + except: + print("Not found device in address 0x%x"%(addr)) + return False + + def close(self): + self.bus.close() + +class PCF8591(ADCDevice): + def __init__(self): + super(PCF8591, self).__init__() + self.cmd = 0x40 # The default command for PCF8591 is 0x40. + self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module. + + def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3 + value = self.bus.read_byte_data(self.address, self.cmd+chn) + value = self.bus.read_byte_data(self.address, self.cmd+chn) + return value + + def analogWrite(self,value): # write DAC value + self.bus.write_byte_data(address,cmd,value) + +class ADS7830(ADCDevice): + def __init__(self): + super(ADS7830, self).__init__() + self.cmd = 0x84 + self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module. + + def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 + value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4)) + return value