--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
--//仭 LED僨乕僞曄姺僗僋儕僾僩 - dLedSeqC梡data弌椡 仭
--//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭

--// * Fri Feb 27 21:11:43 JST 2015 Naoyuki Sawa
--// - 1st 儕儕乕僗丅

--//婸搙價僢僩悢傪掕媊偟傑偡丅
--// - 0=摟柧,1=徚摂,2乣((2^DIMMER_BIT)-1)=揰摂偱偡丅
--//   偨偲偊偽丄乽DIMMER_BIT=5乿偲掕媊偟偨応崌丄埲壓偺傛偆偵側傝傑偡:
--//
--//	DIMMER =   0 :      摟柧
--//	DIMMER =   1 :      徚摂
--//	DIMMER =   2 :  1/30揰摂
--//	DIMMER =   3 :  2/30揰摂
--//	     乣     
--//	DIMMER =  30 : 29/30揰摂
--//	DIMMER =  31 : 30/30揰摂
--//
--// - LedConfig.lua偱掕媊偟偰傕椙偐偭偨偺偱偡偑丄昿斏偵曄峏偡傞偙偲偼柍偄偩傠偆偲峫偊丄LedConv.lua偵娷傔傑偟偨丅
--//   LedConfig.lua偱嵞掕媊偟偰忋彂偒偟偨傝丄乽-e乿僆僾僔儑儞偱曄峏偡傞偙偲偼壜擻偱偡丅
--//
DIMMER_BIT = 5

--//*************************************************************************
--//	嵟弶偵堦搙偩偗屇傃弌偝傟傞娭悢
--//*************************************************************************

function BEGIN()
	--//僞僽嬫愗傝僼傽僀儖偐傜丄LED掕媊傪撉傒崬傓丅
	LED = { }
	for line in io.lines(LED_FNAME) do
		local a = { }						--//劉
		for i in string.gmatch(line .. "\t", "(.-)" .. "\t") do	--//劌僞僽嬫愗傝偱僼傿乕儖僪暘妱傪峴偆丅嬻僼傿乕儖僪傕擣傔傞丅
			table.insert(a, i)				--//劆
		end							--//劊
		local vLed = { }
--//巊傢側偄	vLed.id     = assert(tonumber(a[1])) assert((vLed.id   >= 0) and (vLed.id <= (2^16)-1))	--//LED斣崋(0乣(2^16)-1))
		vLed.c      = assert(tonumber(a[2])) assert((vLed.c    == 1) or  (vLed.c  ==        3))	--//梫慺(1=扨怓LED,3=僼儖僇儔乕LED)
--//巊傢側偄	vLed.name   = assert(tostring(a[3])) assert(#vLed.name ~= 0)				--//柤徧
		vLed.x      = assert(tonumber(a[4])) assert((vLed.x    >= 0) and (vLed.x  <= (2^16)-1))	--//庢傝崬傒嬮宍x
		vLed.y      = assert(tonumber(a[5])) assert((vLed.y    >= 0) and (vLed.y  <= (2^16)-1))	--//庢傝崬傒嬮宍y
		vLed.w      = assert(tonumber(a[6])) assert((vLed.w    >= 0) and (vLed.w  <= (2^16)-1))	--//庢傝崬傒嬮宍w
		vLed.h      = assert(tonumber(a[7])) assert((vLed.h    >= 0) and (vLed.h  <= (2^16)-1))	--//庢傝崬傒嬮宍h
--//巊傢側偄	vLed.symbol = assert(tostring(a[8]))	--//摉僗僋儕僾僩偱偼巊梡偟側偄丅偙傟埲崀偼柍帇偡傞偺偱丄壗傪彂偄偰傕峔傢側偄丅
		table.insert(LED, vLed)
	end
	--//婸搙偺嵟戝抣傪媮傔傑偡丅
	assert((DIMMER_BIT >= 2) and (DIMMER_BIT <= 6))	--//7傕壜擻偱偼偁傞偑丄LED偺摿惈忋僆乕僶乕僗儁僢僋偱偁傠偆偐傜彍奜偟偨丅
	DIMMER_MAX = (2 ^ DIMMER_BIT) - 1
	--//弌椡僼傽僀儖傪嶌惉偡傞丅弌椡僼傽僀儖偑巜掕偝傟偰偄側偗傟偽丄昗弨弌椡傊弌椡偡傞丅
	if OUT_FNAME then
		OutFile = assert(io.open(OUT_FNAME, "w"))
	else
		OutFile = io.stdout
	end
end

--//*************************************************************************
--//	奺僼儗乕儉偵懳偟偰丄堦搙偯偮屇傃弌偝傟傞娭悢
--//*************************************************************************

function FRAME()
	local comma
	OutFile:write("data ")
	--//奺LED偵偮偄偰乧
	for i, vLed in ipairs(LED) do
		--//婸搙傪庢摼偡傞丅
		local r, g, b = GetPixel(vLed.x + vLed.w / 2, vLed.y + vLed.h / 2)
		--//扨怓LED側傜偽c=1偵偮偄偰,僼儖僇儔乕LED側傜偽c=1,2,3偵偮偄偰乧
		for c = 1, vLed.c do
			local v = 0
			--///堦梫慺偱傕0偱側偗傟偽乧
			if (r ~= 0) or (g ~= 0) or (b ~= 0) then
				    if (c == 1) then
					v = r
				elseif (c == 2) then
					v = g
				elseif (c == 3) then
					v = b
				else
					v = math.max(r, g, b)
				end
				v = math.floor(v * DIMMER_MAX) + 1		--//[0,1]傪[1,DIMMER_MAX]傊嬒摍偵曄姺偡傞
				if v > DIMMER_MAX then v = DIMMER_MAX end	--//1偑(DIMMER_MAX+1)偵曄姺偝傟傞働乕僗偵偺傒曗惓偡傞
			end
			if (i ~= 1) or (c ~= 1) then
				OutFile:write(",")
			end
			    if DIMMER_MAX <=  9 then
				OutFile:write(string.format("%1d", v))
			elseif DIMMER_MAX <= 99 then
				OutFile:write(string.format("%2d", v))
			else
				OutFile:write(string.format("%3d", v))
			end
		end
	end
	OutFile:write("\n")
end

--//*************************************************************************
--//	嵟屻偵堦搙偩偗屇傃弌偝傟傞娭悢
--//*************************************************************************

function END()
	--//弌椡僼傽僀儖傪暵偠傞丅偨偩偟丄昗弨弌椡偩偭偨応崌偼暵偠側偄丅
	if OutFile ~= io.stdout then
		OutFile:close()
	end
end
