Dr. Arne JachensDr. Arne Jachens

Heating and Ventilation Control

HvcReadSPI

Keine Erläuterungen gefunden.

"""
Taken and modified from https://pypi.org/project/mcp3208/
to enable chip select for  2 MCP3208
"""
try:
    import  Adafruit_GPIO.SPI as  SPI
except ImportError:
    class SPI(object):
        MSBFIRST = 1
        def  SpiDev(a, b, max_speed_hz): pass
        def  transfer(a): pass
        def  set_mode(a): pass
        def  set_bit_order(a): pass

class MCP3208(object):
    def  __init__(self,chip=0):
        self.spi = SPI.SpiDev(0, chip, max_speed_hz=1000000)
        self.spi.set_mode(0)
        self.spi.set_bit_order(SPI.MSBFIRST)

    def  __del__(self):
        self.spi.close()

    def  read(self, ch):
        if 7 <= ch <= 0:
            raise Exception('MCP3208 channel must be 0-7: ' + str(ch))

        cmd = 128  # 1000 0000
        cmd += 64  # 1100 0000
        cmd += ((ch & 0x07) << 3)
        ret = self.spi.transfer([cmd, 0x0, 0x0])

        # get the 12b out of the return
        val = (ret[0] & 0x01) << 11  # only B11 is here
        val |= ret[1] << 3           # B10:B3
        val |= ret[2] >> 5           # MSB has B2:B0 ... need to move down to LSB

        return (val & 0x0FFF)  # ensure we are only sending 12b

  
class HvcReadSPI:  
    """
    My specific class to connect my analougous sensors.
    """
    def  __init__(self):
        self.ADCPins={}
        self.ADCPins['haus'] =[0,1]
        #self.ADCPins['fire'] =[0,2]
        self.ADCPins['coll'] =[0,3]
        self.ADCPins['flow'] =[0,4]
        self.ADCPins['ret']  =[0,5]
        self.ADCPins['THot'] =[0,6]
        self.ADCPins['TLow'] =[0,7]
        self.ADCPins['ablu'] =[1,2] #1,0
        self.ADCPins['posBp']=[1,1]
        self.ADCPins['zulu'] =[1,0] #1,2
        self.ADCPins['erde'] =[1,3]
        self.ADCPins['folu'] =[1,4]
#        self.ADCPins['posBp']=[1,5]


    def  readADCverbose(self):
        """
        For debugging, this function returns a message with insights into the functionality. 
        """
        ADC={}
        
        adc0 = MCP3208(0)
        adc1 = MCP3208(1)

        message = "HvcReadSPI.readADCverbose \n"
        for  name in self.ADCPins:
            if self.ADCPins[name][0]==0:
                ADC[name] = adc0.read( self.ADCPins[name][1] )
            elif self.ADCPins[name][0]==1:
                ADC[name] = adc1.read( self.ADCPins[name][1] )
            else:
                message = message + "chip not defined! \n"

        message = message + str(ADC) + "\n"
            
        return ADC, message


    def  readADC(self):
        """
        For standard operation, the verbouse message is dropped.
        """
        ADC, message = HvcReadSPI.readADCverbose(self)
        return ADC

"""
for  testing
"""
if __name__ == "__main__":
    mySPI = HvcReadSPI()
    ADC,msg = mySPI.readADCverbose()
    print(msg)
    print("second call")
    ADC = mySPI.readADC()
    print(ADC)

    

Index of Library

1EnergyManager.py
2HvcControl.py
3HvcHCSR04ultrasonic.py
4HvcLightControl.py
5HvcMain.py
6HvcMotorDriver.py
7HvcMqtt.py
8HvcOneWire.py
9HvcOperationMode.py
10HvcRaw2phys.py
11HvcReadSPI.py
12HvcSendI2C.py
13HvcSetGPIO.py
14HvcTables.py
15HvcWeather.py
16HvcWifiRelay.py
17makeDoc.py

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.