#!/usr/bin/env python
#-*- coding: utf-8 -*-
#----------------------------------------------------------------------
# bibliothèques "système"
#----------------------------------------------------------------------
import time
import sys
import smbus2

#----------------------------------------------------------------------
# bibliothèques "locales"
#----------------------------------------------------------------------
import globals
import CRC8

#----------------------------------------------------------------------
# variables et constantes
#----------------------------------------------------------------------
I2C_SHT35 = 0x45

#--------------------------------------------------------------------------------------------------
def SHT35_open() :

	global SHT35_open_flag

	try :
		SHT35_open_flag = False

		if globals.DEBUG_SHT35 :
			print("SHT35_open -> starting...")

		if ( globals.BUS_8_OK_flag ) :
			if SHT35_heater_off() :
				SHT35_open_flag = True
			else :
				if globals.DEBUG_SHT35 :
					print("SHT35_open -> SHT35_heater_off -> erreur !!!")

		else :
			if globals.DEBUG_SHT35:
				print("SHT35_open -> le bus i2c-8 n'est pas OK !!!")

	except :
		if globals.DEBUG_SHT35 :
			print("SHT35_open -> exception !!!")
		
	finally :
		return SHT35_open_flag

#-----------------------------------------------------------------------------
def SHT35_init():
	
	try:
	
		SHT35_init_STATUS = False
		
		if SHT35_open_flag :

			if globals.DEBUG_SHT35:
				print("SHT35_init -> starting...")

			if SHT35_reset():
			
				serial_number = SHT35_serial_number()
				
				if serial_number[0] :
					if globals.DEBUG_SHT35 :
						print('SHT35_init -> serial number = '  + serial_number[1] )

				else :
					if globals.DEBUG_SHT35 :
						print('SHT35_init -> serial number -> erreur !!! ' )

				SHT35_init_STATUS = True
			
			else :
				if globals.DEBUG_SHT35 :
					print('SHT35_init -> reset -> erreur !!! ' )
			
		else :
			if globals.DEBUG_SHT35 :
				print("SHT35_init -> SHT35_open_flag = False !!!")
		
	except :
	
		SHT35_init_STATUS = False
		if globals.DEBUG_SHT35 :
			print("SHT35_init -> exception !!!")

	finally :
		return SHT35_init_STATUS
		
#----------------------------------------------------------------------
# reset : [0x30, 0xA2]
#----------------------------------------------------------------------
def SHT35_reset():
	try:
		SHT35_reset_STATUS = False
		
		globals.bus_8.write_i2c_block_data(I2C_SHT35, 0x30, [0xA2])

		time.sleep(0.002)

		if globals.DEBUG_SHT35:
			if globals.DEBUG_SHT35 :
				print("  SHT35_reset -> OK")
		
		SHT35_reset_STATUS = True
		
	except:
		SHT35_reset_STATUS = False
		if globals.DEBUG_SHT35 :
			print("  SHT35_reset -> exception !")

	finally :
		return SHT35_reset_STATUS
		
#----------------------------------------------------------------------
# heater off : [0x30, 0x66]
#----------------------------------------------------------------------
def SHT35_heater_off():

	SHT35_heater_off_STATUS = False

	try:
		
		globals.bus_8.write_i2c_block_data(I2C_SHT35, 0x30, [0x66])

		time.sleep(0.002)

		if globals.DEBUG_SHT35:
			print("  SHT35_heater_off -> OK")
		
		SHT35_heater_off_STATUS = True
		
	except:
	
		SHT35_heater_off_STATUS = False
		if globals.DEBUG_SHT35 :
			print("  SHT35_heater_off -> exception !")

	finally :
	
		return SHT35_heater_off_STATUS

#----------------------------------------------------------------------
# serial_number : [0x36 0x82]
#----------------------------------------------------------------------
def SHT35_serial_number():

	SHT35_SN_STATUS 		= False
	serial_number_string 	= "0x00000000"

	try :
	
		globals.bus_8.write_i2c_block_data(I2C_SHT35, 0x36, [0x82])
		time.sleep(0.001)
		data = globals.bus_8.read_i2c_block_data(I2C_SHT35, 0x00, 6)
		
		if ( len(data) == 6 ):
		
			data_crc8 = [0,0]
			Flag_crc8 = True
			
			for i in range(2):
				data_crc8[i] = CRC8.crc8_scd30(data[3*i],data[3*i+1])
				
				Flag_crc8 = Flag_crc8 and ( data[3*i+2] == data_crc8[i] )
				
			if Flag_crc8:
				sn = data[0] << 24 | data[1] << 16 | data[3] << 8 | data[4]
				serial_number_string = "0x%08x" % sn
				SHT35_SN_STATUS = True
				 
			else:
				if globals.DEBUG_SHT35:
					print("SHT35_serial_number -> erreur CRC !")
		else:
			if globals.DEBUG_SHT35:
				print("SHT35_serial_number -> erreur nb bytes !")
		
	except :
		SHT35_SN_STATUS = False
		if globals.DEBUG_SHT35 :
			print("  SHT35_serial_number -> exception !")
	
	finally :
		return [SHT35_SN_STATUS, serial_number_string]

#----------------------------------------------------------------------
# stop : [0x30 0x93]
#----------------------------------------------------------------------
def SHT35_stop():

	SHT35_STOP_STATUS = False

	try :
		globals.bus_8.write_i2c_block_data(SHT35_ADDR, 0x30, [0x93])
		time.sleep(0.001)
		SHT35_STOP_STATUS = True
	
	except :
		SHT35_STOP_STATUS = False
		if globals.DEBUG_SHT35 :
			print("  SHT35_stop -> exception !")
	
	finally :
		return SHT35_STOP_STATUS

#----------------------------------------------------------------------
# start one shot : [0x24 0x00] (high repeatability)
#----------------------------------------------------------------------
def SHT35_read():

	chaine_SHT35 = [False, "0.00", "0.00"]
	
	try:

		if True :

			globals.bus_8.write_i2c_block_data(I2C_SHT35, 0x24, [0x00])
			time.sleep(0.020)
			data   = globals.bus_8.read_i2c_block_data(I2C_SHT35, 0x00, 6)

			if ( len(data) == 6 ):
			
				data_crc8 = [0,0]
				Flag_crc8 = True
				
				for i in range(2):
					data_crc8[i] = CRC8.crc8_scd30(data[3*i],data[3*i+1])
					
					Flag_crc8 = Flag_crc8 and ( data[3*i+2] == data_crc8[i] )
					
				if Flag_crc8:
					SHT35_T    = ( -45.0 ) + ( 175.0 / 65535.0 ) * ( float(256*data[0] + data[1]) )
					SHT35_HR   = ( 100.0 / 65535.0) * ( float(256*data[3] + data[4]) )
					
					chaine_SHT35 = [ True ,	"{0:.2f}".format( SHT35_T ), "{0:.2f}".format( SHT35_HR ) ]
					 
				else:
					globals.Flag_mesures_OK = ( globals.Flag_mesures_OK & 0x0FFFFFFFFFFF ) | 0x100000000000
					if globals.DEBUG_SHT35:
						print("SHT35_read -> erreur CRC !")
			else:
				globals.Flag_mesures_OK = ( globals.Flag_mesures_OK & 0x0FFFFFFFFFFF ) | 0x200000000000
				if globals.DEBUG_SHT35:
					print("SHT35_read -> erreur nb bytes !")
		else:
			globals.Flag_mesures_OK = ( globals.Flag_mesures_OK & 0x0FFFFFFFFFFF ) | 0x300000000000
			if globals.DEBUG_SHT35:
				print("SHT35_read -> data not ready !")
			
	except Exception as e:

		globals.Flag_mesures_OK = ( globals.Flag_mesures_OK & 0x0FFFFFFFFFFF ) | 0x400000000000

		exception_type, exception_object, exception_traceback = sys.exc_info()
		line_number = exception_traceback.tb_lineno
		exception_string = "SHT35_read -> exception -> type : " + str(exception_type) + " ; ligne = " + str(line_number)

		if globals.DEBUG_SHT35 :
			print(exception_string)

	finally:
			
		return chaine_SHT35
		
#-----------------------------------------------------------------------------
