#! /usr/bin/env python
#import modules
import spidev
import time
import hal

#initialise SPIDev
spi=spidev.SpiDev()

try:
    h = hal.component('ADS1158_SPIhal')
    h.newpin('in', hal.HAL_FLOAT, hal.HAL_IN)
    h.newpin('out', hal.HAL_FLOAT, hal.HAL_OUT)
    time.sleep(0.05)
    h.ready()
except:
    print('\nERROR: ADS1158 component could not be initialized\n')
    raise SystemExit

# out is when we send the converted float distance data out
# in is when we request a new update

#create our list to send on the SPI bus
CONFIG0 = [0b01100000,0b00001010]
CONFIG1 = [0b01100001,0b00000011]
MUXDIF = [0b01100011,0]
MUXSG0 = [0b01100100,1]
MUXSG1 = [0b01100101,0]
SYSRED = [0b01100110,0]
GPIOC = [0b01100111,0xFF]
distance = 0.0 
#set all configuration regsiter

def write_reg(data):
    spi.open(0,0) #open(bus,CE), allow MUX controller, but not use in this case
    spi.max_speed_hz = 4000000 # at 4million Hx
    spi.mode = 0 #mode 0, where mode = [COP , CPA ]
    spi.lsbfirst = False #only take either True or False, not 1 or 0
    #time.sleep(0.0005) #sleep llow time to connect. 
    spi.xfer2(data)
    spi.close()
    
def read_reg(data):
    spi.open(0,0)
    spi.max_speed_hz = 4000000
    spi.mode = 0
    spi.lsbfirst = False
    #time.sleep(0.0005)
    spi.xfer2(data)
    spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(CONFIG0)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(CONFIG1)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(MUXDIF)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(MUXSG0)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(MUXSG1)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(SYSRED)
spi.close()

spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
spi.xfer2(GPIOC)
spi.close()
#read reg to verify
read_buf = []
reg_adr = [0b01000000,0b11110000]
spi.open(0,0)
spi.max_speed_hz = 4000000
spi.mode = 0
spi.lsbfirst = False
#time.sleep(0.0005)
read_buf = spi.xfer2(reg_adr)
result16 = read_buf[1]
spi.close()
#print(result16)

try:
    while True:
        read_buf = []
        reg_cmd = [0b10000000,0b00111000,0x08,0x08,0x08]
        #explanation to the reg_cmd
        #first byte, tell the ADC to get a new update for next reading
        #2nd, request a reading from channel
        #3rd, return status bytes
        #th and 5th, spacer byte to get return of MSB and LSB froom ADC
        
        spi.open(0,0)
        spi.max_speed_hz = 4000000
        spi.mode = 0
        spi.lsbfirst = False
        #time.sleep(0.0005)
        read_buf = spi.xfer2(reg_cmd)
        result16 = read_buf[3]<<8 |read_buf[4]
        spi.close()
        #print(read_buf[2])
        #print(result16)
        #convert signed 16 bits int to float (5V) and distance (10m)
        # 5V around 30900, 10m
        # 3.3V around 20390
        # 0V around 10, should be around 1.5mV,
        # 200mm (min reading) = 4mA = 1V, should be around 6553.6
        distance = result16*0.39644-2250
        #print(distance)
        
        #send distance result to Hal compoent out
        h.out = distance
except KeyboardInterrupt:
    spi.close()
    raise SystemExit
"""  

"""

