_ENCRYPT_=0--gxhtc3

cw = {}

cw.I2C_write_timeid = 12
cw.I2C_write_timeout = 1000
cw.I2C_read_timeid = 13
cw.I2C_read_timeout = 500
---------------------------------------------------------------
--温度数组，记录最近三次测量的温度，用于计算平均值
local table_Temperature = {}
-- table_Temperature[0] = 0
-- table_Temperature[1] = 0
-- table_Temperature[2] = 0
-- table_Temperature[3] = 0

--温度数组，记录最近三次测量的温度，用于计算平均值
local table_Humidity = {}
-- table_Humidity[0] = 0
-- table_Humidity[1] = 0
-- table_Humidity[2] = 0
-- table_Humidity[3] = 0
-- table_Humidity[4] = 0
-- table_Humidity[5] = 0
-- table_Humidity[6] = 0
-- table_Humidity[7] = 0
-- table_Humidity[8] = 0
-- table_Humidity[9] = 0

local meas_num = 0
cw.I2C_write = 1
cw.I2C_write_timeout = 1000
cw.I2C_read = 2
cw.I2C_read_timeout = 20
cw.IIC_error_num = 0
cw.IIC_error1_num = 0
local average_Temperature = 0
local average_Humidity = 0
function I2C_temp_hum(code)
    local measurement_command={}
    local Temperature= nil
    local Humidity= nil
    local calibrate_Humidity = 0
    if code == 1
    then
        measurement_command[0] = 0x58
        measurement_command[1] = 0xE0
        i2c_write( 0xE0, 0, 0, measurement_command )
    end
    if code == 2
    then
        local read_table = i2c_read( 0xE0, 0, 0, 6 )
        
        local hum = ((read_table[0]<<8) | read_table[1])
        local tem = ((read_table[3]<<8) | read_table[4])
        print('tem'..tem)
        print('hum'..hum)
        
        if hum~=0 and hum~=65535 and tem~=0 and tem~=65535
        then
            Temperature= (175.0*tem/65535.0-45.0)  -- T = -45 + 175 * tem / (2^16-1)
            Humidity= (100.0*hum/65535.0)  -- RH = hum*100 / (2^16-1)
            print('Temperature'..Temperature)
            print('Humidity'..Humidity)
            
            if(
                (   Temperature<(average_Temperature-5)
                    or Temperature>(average_Temperature+5)
                    or Humidity<(average_Humidity-10)
                    or Humidity>(average_Humidity+10)
                )
                    and cw.IIC_error_num < 5
                    and meas_num >= 9
            )
            then
                cw.IIC_error_num = cw.IIC_error_num + 1
                print('cw.IIC_error_num'..cw.IIC_error_num)
                
            else
                if #table_Temperature>=10
                then
                    table.remove(table_Temperature,1)
                end
                table.insert(table_Temperature,Temperature)
                --table.sort(table_Temperature)

                if #table_Humidity>=10
                then
                    table.remove(table_Humidity,1)
                end
                table.insert(table_Humidity,Humidity)
                --table.sort(table_Humidity)
                
                cw.IIC_error_num = 0
            end
            
            if  #table_Temperature >= 10
            then
                --计算温度平均值
                local sum_Temperature = 0
                for i=1,10
                do
                    sum_Temperature = sum_Temperature + table_Temperature[i]
                end
                average_Temperature = sum_Temperature/10
                --取一位小数
                average_Temperature = math.ceil(average_Temperature*10)/10
                
                --计算湿度平均值
                --local average_Humidity = ( table_Humidity[0]+table_Humidity[1]+table_Humidity[2]+table_Humidity[3] )/4
                local sum_Humidity = 0
                for i=1,10
                do
                    sum_Humidity = sum_Humidity + table_Humidity[i]
                end
                average_Humidity = sum_Humidity/10
                --取一位小数
                average_Humidity = math.ceil(average_Humidity*10)/10
            end
            --测量次数小于3次时，使用当前值。测量次数达到3次时，使用平均值。
            if meas_num < 9 and Temperature~=nil and Humidity~=nil
            then
                meas_num = meas_num + 1
                -- Temperature = math.ceil(Temperature*10)/10
                -- average_Temperature = Temperature
                
                -- Humidity = math.ceil(Humidity*10)/10
                -- average_Humidity = Humidity
                            --计算温度平均值

                --计算温度平均值
                local sum_Temperature = 0
                for i=1,#table_Temperature
                do
                    sum_Temperature = sum_Temperature + table_Temperature[i]
                end
                average_Temperature = sum_Temperature/(#table_Temperature)
                --取一位小数
                average_Temperature = math.ceil(average_Temperature*10)/10

                --计算温度平均值
                local sum_Humidity = 0
                for i=1,#table_Humidity
                do
                    sum_Humidity = sum_Humidity + table_Humidity[i]
                end
                average_Humidity = sum_Humidity/(#table_Humidity)
                --取一位小数
                average_Humidity = math.ceil(average_Humidity*10)/10
            end
            print('average_Temperature'..average_Temperature)
            print('average_Humidity'..average_Humidity)
            --calibrate_Humidity = average_Humidity + 5.5 --校准值
            calibrate_Humidity = average_Humidity
        end
        return average_Temperature,calibrate_Humidity
    end
end
--------------------------------------------------------------------


cw.GXHTC3_Temperature=0
cw.GXHTC3_Humidity=0

function cw.on_init()
    stop_timer(cw.I2C_write_timeid)
    start_timer(cw.I2C_write_timeid, cw.I2C_write_timeout,0,0)
end

function cw.on_timer(timer_id)
    if timer_id == cw.I2C_write_timeid
    then
        I2C_temp_hum(cw.I2C_write)
        stop_timer(cw.I2C_read_timeid)
        start_timer(cw.I2C_read_timeid, cw.I2C_read_timeout,0,1)
    end

    if timer_id == cw.I2C_read_timeid
    then
        --I2C_temp_hum(cw.I2C_read)
        cw.GXHTC3_Temperature,cw.GXHTC3_Humidity = I2C_temp_hum(cw.I2C_read)
        set_float( VT_LW, useraddr.GXHTC3_Temperature, cw.GXHTC3_Temperature )
        set_float( VT_LW, useraddr.GXHTC3_Humidity, cw.GXHTC3_Humidity )
    end
end

function cw.on_systick()
    
end

-- function cw.on_update(slave,vtype,addr)
    -- if vtype == VT_LW
    -- then
        -- if useraddr.user_operation == addr
        -- then
            -- local uov = get_uint16(vtype, addr)
            -- if uov == 1
            -- then
                ------set_float( VT_LW, useraddr.degree_celsius2, cw.CurveToC_SHMF(31) )
            -- end
        -- end
    -- end
-- end
