--分窗标志定义
WIN_0 = 0x0001
WIN_1 = 0x0002
WIN_2 = 0x0004
WIN_3 = 0x0008
WIN_4 = 0x0010
WIN_5 = 0x0020
WIN_6 = 0x0040
WIN_7 = 0x0080
WIN_8 =  0x0100
WIN_9 =  0x0200
WIN_10 = 0x0400
WIN_11 = 0x0800
WIN_12 = 0x1000
WIN_13 = 0x2000
WIN_14 = 0x4000
WIN_15 = 0x8000


--GV索引字符串
local GVstr = {
	"State_Index_U",
	"State_Index_U_Elapse",
	"State_Index_N",
	"State_Index_N_Elapse",
	"NC_ARRAY_u8_0",
	"NC_ARRAY_u8_01",
	"NC_ARRAY_u8_1",
	"NC_ARRAY_u8_11",
	"NC_ARRAY_u8_2",
	"MK32_Key.RxIndex",
	"MK32_Key.CH0_RY_H",
	"MK32_Key.CH1_RY_V", 
	"MK32_Key.CH2_LY_V",
	"MK32_Key.CH3_LY_H",
	"MK32_Key.CH4_SA",
	"MK32_Key.CH5_SB",
	"MK32_Key.CH6_SC",
	"MK32_Key.CH7_SD",
	"MK32_Key.CH8_SE",
	"MK32_Key.CH9_SF",
	"MK32_Key.CH10_LD1",
	"MK32_Key.CH11_RD1",
	"MK32_Key.CH12_S1",
	"MK32_Key.CH13_S2",
	"MK32_Key.CH14_LT",
	"MK32_Key.CH15_RT",
	"MK32_Key.IsOnline",
	"Motor_P[0]MotorID",
	"Motor_P[0]RxIndex",
	"Motor_P[0]Run_Mode", 
	"Motor_P[0]Current",
	"Motor_P[0]Target_Current",
	"Motor_P[0]Velcity",
	"Motor_P[0]Target_Velcity",
	"Motor_P[0]Position",
	"Motor_P[0]Target_Position",
	"Motor_P[0]ERROR_Flag",
	"Motor_P[0]Temperature_Motor",
	"Motor_P[0]Temperature_PCB",
	"Motor_P[1]MotorID",
	"Motor_P[1]RxIndex",
	"Motor_P[1]Run_Mode", 
	"Motor_P[1]Current",
	"Motor_P[1]Target_Current",
	"Motor_P[1]Velcity",
	"Motor_P[1]Target_Velcity",
	"Motor_P[1]Position",
	"Motor_P[1]Target_Position",
	"Motor_P[1]ERROR_Flag",
	"Motor_P[1]Temperature_Motor",
	"Motor_P[1]Temperature_PCB",
	"Motor_P[2]MotorID",
	"Motor_P[2]RxIndex",
	"Motor_P[2]Run_Mode", 
	"Motor_P[2]Current",
	"Motor_P[2]Target_Current",
	"Motor_P[2]Velcity",
	"Motor_P[2]Target_Velcity",
	"Motor_P[2]Position",
	"Motor_P[2]Target_Position",
	"Motor_P[2]ERROR_Flag",
	"Motor_P[2]Temperature_Motor",
	"Motor_P[2]Temperature_PCB"
}

--CAN 解码函数，返回的字符串将展示到数据窗口
--id 帧ID
--std_ext 0表示标准帧, 1表示扩展帧
--data_remote 0表示数据帧, 1表示远程帧
--datalen 数据长度，字节
--data_bin 二进制数据
--函数返回: ret1解码字符串 , ret2分窗控制字, 波形数据字符串
function can_decoder(id, std_ext, data_remote, datalen, data_bin)
	local ret1 = ""		--返回值1 解码显示结果
	local ret2 = 0  	--返回值2 分窗标志
	local bin = {}
	local i
	local str_wave = ""	
	
    local _index = 0
    local _value = 0
	--local a_string = string.pack(">i2", your_integer) 
    --local an_integer = string.unpack(">i2", a_string, start) 将二进制字符串转换为数字类型，从第start个字符开始，<小端 >大端，类型i2，长度2

    --#将字符串转换到整数数组存放
	for i = 1,#data_bin,1 do
		bin[i] = tonumber(string.byte(data_bin, i,i))
	end
	
	--计算解码结果,将显示在数据包后面
	if (id == 0x120) then		
        _index = string.unpack("<i2", data_bin, 1)
        _value = string.unpack("<i4", data_bin, 3)
        ret1 = string.format("设置CV：Index %d = %d", _index, _value)
	elseif (id == 0x121) then	
        _index = string.unpack("<i2", data_bin, 1)
        ret1 = string.format("请求CV：Index %d", _index, _value)
	elseif (id == 0x122) then	
        _index = string.unpack("<i2", data_bin, 1)
        _value = string.unpack("<i4", data_bin, 3)
        ret1 = string.format("读取CV：Index %d = %d", _index, _value)	
	elseif (id == 0x1FF) then	
        if(datalen == 1) then
			ret1 = string.format("## Boot程序启动")	
		elseif (datalen == 3) then
			ret1 = string.format("## Boot启动APP")	
		elseif (datalen == 2) then
			ret1 = string.format("## 进入刷写模式")	
		elseif (datalen == 4) then
			ret1 = string.format("## 刷写错误")	
		elseif (datalen == 5) then
			ret1 = string.format("## APP程序启动")	
		end
	elseif (id == 0x110) then	
		if(data_bin[1]==0x55) then
        	ret1 = string.format("## 请求重启系统")	
		end
	elseif (id == 0x130) then	
        _index = string.unpack("<i2", data_bin, 1)
        _value = string.unpack("<i2", data_bin, 3)
        ret1 = string.format("请求GV.%d,%s, Length = %d", _index, GVstr[_index+1],_value)	
	elseif (id == 0x132) then
        _index = string.unpack("<i2", data_bin, 1)
        _value = string.unpack("<i4", data_bin, 3)
        ret1 = string.format("______读取GV.%d,%s = %d", _index, GVstr[_index+1],_value)	
	end
	
	--计算分窗标志, 决定数据包同步显示到哪些子窗口
	if (id == 0x120 or  id == 0x121 or id == 0x122) then			
		ret2 = ret2 | WIN_0
	elseif (id == 0x1FF or id == 0x110) then	
		ret2 = ret2 | WIN_1
	elseif (id == 0x130 or id == 0x132) then	
		ret2 = ret2 | WIN_2
	end

	return ret1, ret2, str_wave
end
