import os import sys try: from IOPi import IOPi except ImportError: sys.path.append('mock') try: from IOPi import IOPi except ImportError: raise ImportError("IOPi not found") class Pin(object): """Abstraction of pin object. Every pin object contains a gpio number and a i2c bus address.""" bus_address = None pin_number = None bus = None def __init__(self, bus_address, pin_number): """Constructor. Args: bus_address (int): i2c bus address [0x20..0x23]. pin_number (int): Gpio pin number. """ self.bus_address = bus_address self.pin_number = pin_number class Attenuator(object): """Abstraction of ANALOG DEVICES HMC425A digital attenuator""" pin = None def __init__(self,pin_0_5db, pin_1_0db, pin_2_0db, pin_4_0db, pin_8_0db, pin_16_0db): """Constructor. Args: pin_0_5db (Pin): Pin object to control the 0.5 db gpio pin_1_0db (Pin): Pin object to control the 1.0 db gpio pin_2_0db (Pin): Pin object to control the 2.0 db gpio pin_4_0db (Pin): Pin object to control the 4.0 db gpio pin_8_0db (Pin): Pin object to control the 8.0 db gpio pin_16_0db (Pin): Pin object to control the 16.0 db gpio """ if type(pin_0_5db) is not Pin: raise TypeError("Wrong type for pin_0_5db") if type(pin_1_0db) is not Pin: raise TypeError("Wrong type for pin_1_0db") if type(pin_2_0db) is not Pin: raise TypeError("Wrong type for pin_2_0db") if type(pin_4_0db) is not Pin: raise TypeError("Wrong type for pin_4_0db") if type(pin_8_0db) is not Pin: raise TypeError("Wrong type for pin_8_0db") if type(pin_16_0db) is not Pin: raise TypeError("Wrong type for pin_16_0db") if pin_0_5db.bus_address not in range(0x20, 0x24): raise IndexError('pin_0_0db i2c bus address index out of range') if pin_1_0db.bus_address not in range(0x20, 0x24): raise IndexError('pin_1_0db i2c bus address index out of range') if pin_2_0db.bus_address not in range(0x20, 0x24): raise IndexError('pin_2_0db i2c bus address index out of range') if pin_4_0db.bus_address not in range(0x20, 0x24): raise IndexError('pin_4_0db i2c bus address index out of range') if pin_8_0db.bus_address not in range(0x20, 0x24): raise IndexError('pin_8_0db i2c bus address index out of range') if pin_16_0db.bus_address not in range(0x20, 0x24): raise IndexError('pin_16_0db i2c bus address index out of range') if pin_0_5db.pin_number not in range(1, 17): raise IndexError('pin_0_5db pin number index out of range') if pin_1_0db.pin_number not in range(1, 17): raise IndexError('pin_1_0db pin number index out of range') if pin_2_0db.pin_number not in range(1, 17): raise IndexError('pin_2_0db pin number index out of range') if pin_4_0db.pin_number not in range(1, 17): raise IndexError('pin_4_0db pin number index out of range') if pin_8_0db.pin_number not in range(1, 17): raise IndexError('pin_8_0db pin number index out of range') if pin_16_0db.pin_number not in range(1, 17): raise IndexError('pin_16_0db pin number index out of range') self.pin = [pin_0_5db, pin_1_0db, pin_2_0db, pin_4_0db, pin_8_0db, pin_16_0db] for pin in self.pin: pin.bus = IOPi(pin.bus_address) pin.bus.set_pin_direction(pin.pin_number, 0) pin.bus.write_pin(pin.pin_number, 0) def set_attenuation(self, attenuation): """Set attenuation. Args: attenuation (float): Value of attenuation to be set [0..31.5] """ if attenuation < 0: attenuation = 0 elif attenuation > 31.5: attenuation = 31.5 max_single_value = 16 * 2 attenuation *= 2 switch_pin = [1 for i in range(6)] index = 5 while index >= 0: if attenuation - max_single_value >= 0: attenuation -= max_single_value switch_pin[index] = 0 max_single_value /= 2 index -= 1 for i in range(0, len(switch_pin)): bus = IOPi(self.pin[i].bus_address) bus.write_pin(self.pin[i].pin_number, switch_pin[i])