# GR-SAKURA mruby version                 
# 2013 copyright(c) NPO QUEST             
# AUTHOR 2013.2 N.Yamashita               

$DEBUG = 0
if $DEBUG == 1 then
	class Led 
		def initialize i,j
		end
		def write i
		print "write ",i ,"\n"
		end
	end
	class Board
		def delay i
			print "delay ",i,"\n"
		end
	end
	class Gyaro
		def turn?
			return true 
		end
	end
end
	
def debug
	led = Led.new(3,1)
	board = Board.new
	p "Exception" if $DEBUG == 1
	loop do
		led.write 1
		board.delay 1000
		led.write 0
		board.delay 1000
	end
	
end

def led_write led,board, data, count, len
	i = data[count]

# これだと途中で止まる
#	4.times do |x|
#		led[x].write((i >> x) & 0x1) 
#	end
	
	led[3].write((i >> 3) & 0x1)
	led[2].write((i >> 2) & 0x1)
	led[1].write((i >> 1) & 0x1)
	led[0].write(i & 0x1) 

	board.delay 1

	if (count == (len - 1)) then 
		led[3].write 0
		led[2].write 0
		led[1].write 0
		led[0].write 0
		board.delay 100
	end
end

def led_clean led
		led[3].write 0
		led[2].write 0
		led[1].write 0
		led[0].write 0
end

data = [0x0f,0x08,0x0f,0x08,0x0f,0x00,0x0f,0x04,\
				0x08,0x00,0x0f,0x01,0x0f,0x00,0x0f,0x05,\
				0x07,0x00,0x0c,0x04,0x0f]
len = data.length- 1

led = []
(0..3).each do |i|
	led << Led.new(i,1)
end

board = Board.new
count = 0 
len = data.length
g = Gyaro.new

loop do
	count = count % len 

	if (g.turn?) then
		led_write led,board, data, count,len
	else
		led_clean led
	end
	
	count = count + 1 
end


