--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
--//仭 LED僨乕僞妋擣僗僋儕僾僩 - 2byteRLE斉 仭
--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭

--// * Sat Jan 31 17:30:32 JST 2015 Naoyuki Sawa
--// - 乽Lua 5.2.3乿佀乽Lua 5.3.0乿傊堏峴偟偨偙偲偵敽偆廋惓傪峴偄傑偟偨丅
--//   嬶懱揑偵偼丄埲壓偺擇揰偱偡丅
--// - 僗僋儕僾僩懁偺廋惓
--//   乽Lua 5.3.0乿偱偼丄昗弨僐儞僷僀儖偱bit32偑巊偊側偔側偭偨偺偱丄戙傝偵價僢僩墘嶼巕傪巊偆傛偆偵偟傑偟偨丅
--// - C娭悢懁偺廋惓
--//   乽Lua 5.3.0乿偱偼丄彫悢傪娷傓抣傪luaL_checkinteger()偱庢摼偟傛偆偲偡傞偲丄僄儔乕側傞傛偆偵側傝傑偟偨丅
--//   尩枾偵偼丄僗僋儕僾僩懁偱惍悢偵愗傝幪偰偰偐傜C娭悢傪屇傃弌偡傋偒側偺偱偡偑丄曄峏売強偑懡夁偓偰僄儞僶僌偑怱攝側偺偱丄
--//   戙傝偵C娭悢懁偱抣傪庢摼偡傞強傪luaL_checkinteger()佀luaL_checknumber()偵曄峏偟丄C娭悢懁偱愗傝幪偰傞傛偆偵偟傑偟偨丅

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

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

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

	--//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)	--//埑弅僨乕僞
			--//RLE揥奐傪峴偆
			local u = { }
			local i = 1
			while i <= #t do
				local RleV = (t[i] &  DIMMER_MAX)
				local RleL = (t[i] >> DIMMER_BIT)
				i = i + 1
				if RleL == (LENGTH_MAX + 1) then	--//楢挿(挿)偺宍幃偱僨僐乕僪偡傞
					RleL = RleL + t[i]
					i = i + 1
				end
				for j = 0, RleL do table.insert(u, RleV) end
			end
			--//揥奐屻偺婸搙僨乕僞悢偑丄僀儞僩儘晹暘枖偼儖乕僾晹暘偺僼儗乕儉悢偵摍偟偄帠傪妋擣偡傞丅
			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 = (v | (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 = (v | (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 = (v | (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
