
import MAX11270
import math
import MCP4822
import time

def adc2T(dU):
    #constants
    #VREF = (REF+) – (REF–)
    #Vref = 2.5
    #wheatstonebridge
    #   U0_____
    #          |
    #          R0
    #   Uw_____|__
    #      |      |
    #      R2     R4
    #      |__dU__|
    #      |      |
    #      R1     R3
    #      |______|
    #  Gnd_____|_
    #
    # R1 = resistance of thermistor
    R0 = 0
    R2 = 5100 #(Ohm)
    R3 = 97600 #(Ohm)
    R4 = 5100 #(Ohm)
    U0 = 3 #(V)
    #steinhart-Hart Coefficients 10uA
    # NTC_A = 9.7142e-4;
    # NTC_B = 2.3268e-4;
    # NTC_C = 8.0591e-8;
    #steinhart-Hart Coefficients 100uA
    NTC_A = 9.6542e-4
    NTC_B = 2.3356e-4
    NTC_C = 7.7781e-8

    R1 = -(R0*R2*dU - R2*R3*U0 + R0*R3*dU + R0*R4*dU + R2*R3*dU + R2*R4*dU)/(R4*U0 + R0*dU + R3*dU + R4*dU)
    #calculate temperature
    try:
        T = 1/(NTC_A + NTC_B*math.log(float(R1)) + NTC_C*math.pow(math.log(float(R1)),3)) - 273.15
    except ValueError:
        T = 6666
    return T

myADC = MAX11270.MAX11270(21,20,19,17,27,22)
myDAC = MCP4822.MCP4822(11, 10, 8, 5)

#myDAC.write(0,1)
#myDAC.write(0,0)

print('-Status-')
print(myADC.readStatusRegister(1))
#myADC.performCalibration('offsetCalibration')
#print(myADC.readStatusRegister(1))

print('--------Readout ADC value ---------')
for i in range(0,100):
    print(adc2T(myADC.read_differential()))
    time.sleep(10)
