--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
--//仭 LED僨乕僞妋擣僗僋儕僾僩 - 1byteRLE仌帿彂埑弅斉 仭
--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭

--//{{LedConv1.lua巊梡帪偺掕媊偵堦抳偝偣傞帠
DIMMER_BIT = 5
--//}}LedConv1.lua巊梡帪偺掕媊偵堦抳偝偣傞帠

--//*************************************************************************
--//	
--//*************************************************************************

function BEGIN()
	--//婸搙偺嵟戝抣傪媮傔傑偡丅
	DIMMER_MAX = (2 ^ DIMMER_BIT) - 1
	--//楢挿價僢僩悢傪媮傔傑偡丅
	LENBGH_BIT = (8 - DIMMER_BIT)
	--//楢挿偺嵟戝抣傪媮傔傑偡丅
	LENGTH_MAX = (2 ^ LENBGH_BIT) - 1

	--//Bin僼傽僀儖傪撉傒崬傓
	local buf = { }
	local file = assert(io.open(BIN_FNAME, "rb"))
	for i in file:lines(1) do
		table.insert(buf, string.byte(i))
	end
	file:close()

	--//=== 僨乕僞揥奐張棟 ===
	local nFrameLen = { }
	nFrameLen[1] = get_2byte(buf)	--//僀儞僩儘僼儗乕儉悢(2byte)
	nFrameLen[2] = get_2byte(buf)	--//儖乕僾僼儗乕儉悢(2byte)
	--//奺LED偵偮偄偰乧
	LED = { }
	while true do
		local id = get_2byte(buf)		--//LED斣崋(2byte)
		if id == (2^16)-1 then break end	--//(2^16)-1=廔抂
		local vLed = { }
		vLed.id = id
		vLed.data = { }
		--//僀儞僩儘晹暘偲儖乕僾晹暘偵偮偄偰乧
		for iIntroLoop = 1, 2 do
			local n = get_3byte(buf)	--//埑弅僨乕僞僒僀僘(3byte)
			local t = get_array(buf, n)	--//埑弅僨乕僞
			--//揥奐偡傞
		  --//{{2014/11/24曄峏:埑弅曽幃偺捛壛偵敽偄丄娭悢柤傪Compress佀CompressTlz,Uncompress佀UncompressTlz偵曄峏偟傑偟偨丅
		  --//	t = Uncompress(t)
		  --//伀2014/11/24曄峏:埑弅曽幃偺捛壛偵敽偄丄娭悢柤傪Compress佀CompressTlz,Uncompress佀UncompressTlz偵曄峏偟傑偟偨丅
			t = UncompressTlz(t)
		  --//}}2014/11/24曄峏:埑弅曽幃偺捛壛偵敽偄丄娭悢柤傪Compress佀CompressTlz,Uncompress佀UncompressTlz偵曄峏偟傑偟偨丅
		--//{{RLE
			local u = { }
			for i, v in ipairs(t) do
				local RleV = bit32.band(             v             , DIMMER_MAX)	--//( v             &DIMMER_MAX)
				local RleL = bit32.band(bit32.rshift(v, DIMMER_BIT), LENGTH_MAX)	--//((v>>DIMMER_BIT)&LENGTH_MAX)
				for j = 0, RleL do table.insert(u, RleV) end
			end
		--//}}RLE
			--//揥奐屻偺婸搙僨乕僞悢偑丄僀儞僩儘晹暘枖偼儖乕僾晹暘偺僼儗乕儉悢偵摍偟偄帠傪妋擣偡傞丅
			assert(#u == nFrameLen[iIntroLoop])
			--//揥奐屻偺婸搙僨乕僞傪奿擺偡傞丅
			vLed.data[iIntroLoop] = u
		end
		table.insert(LED, vLed)
	end

	--//=== 僨乕僞幚峴張棟 ===
	io.write("\t")			--//僼儗乕儉斣崋偺楍
	for iLed, vLed in ipairs(LED) do
		io.write("\t")		--//僀儞僩儘or儖乕僾偺楍
		io.write(vLed.id)	--//LED斣崋
	end
	io.write("\n")
	iFrame = 0
	--//僀儞僩儘晹暘偲儖乕僾晹暘偵偮偄偰乧
	for iIntroLoop = 1, 2 do
		for i = 1, nFrameLen[iIntroLoop] do
			io.write(iFrame)					--//僼儗乕儉斣崋
			io.write("\t")
			io.write(string.sub("IL", iIntroLoop, iIntroLoop))	--//僀儞僩儘or儖乕僾
			for iLed, vLed in ipairs(LED) do
				io.write("\t")
				io.write(vLed.data[iIntroLoop][i])		--//婸搙
			end
			iFrame = iFrame + 1
			io.write("\n")
		end
	end
end

function FRAME() end	--//僟儈乕
function END() end	--//僟儈乕

--//*************************************************************************
--//	
--//*************************************************************************

function get_1byte(t)
	t.i = (t.i or 0) + 1
	local v = assert(t[t.i])				--//嘆
	assert((v >= 0) and (v <= (2^ 8)-1))
	return v
end
function get_2byte(t)
	local v =                     get_1byte(t)		--//嘆
	v = bit32.bor(v, bit32.lshift(get_1byte(t),  8))	--//仩嘇
	assert((v >= 0) and (v <= (2^16)-1))
	return v
end
function get_3byte(t)
	local v =                     get_2byte(t)		--//嘆嘇
	v = bit32.bor(v, bit32.lshift(get_1byte(t), 16))	--//仩仩嘊
	assert((v >= 0) and (v <= (2^24)-1))
	return v
end
function get_4byte(t)
	local v =                     get_3byte(t)		--//嘆嘇嘊
	v = bit32.bor(v, bit32.lshift(get_1byte(t), 24))	--//仩仩仩嘋
	assert((v >= 0) and (v <= (2^32)-1))
	return v
end
function get_array(t, n)
	local a = { }
	for i = 1, n do table.insert(a, get_1byte(t)) end
	return a
end

