module(...,package.seeall)

--[[
print
  ӡӿڣļедӡtestǰ׺
  
ֵ
]]
local function print(...)
	_G.print("test",...)
end

--i2cuseŵǰǷΪi2cʹãtrueʾǣıʾ
local i2cid,i2cuse = 0,true
--[[
i2copn
  i2c
  
ֵ
]]
local function i2copn()
	--ַ0x15ֻһӣʵʹʱΧ豸
	if i2c.setup(i2cid,i2c.SLOW,0x15) ~= i2c.SLOW then
		print("i2copn fail")
	end
end

--[[
i2close
  رi2c
  
ֵ
]]
local function i2close()
	i2c.close(i2cid)
end

--[[
switchtoi2c
  лi2cʹ
  
ֵ
]]
local function switchtoi2c()
	print("switchtoi2c",i2cuse)
	if not i2cuse then
		--رgpio
		pio.pin.close(pio.P0_6)
		pio.pin.close(pio.P0_7)
		--i2c
		i2copn()
		i2cuse = true
	end
end

--[[
switchtogpio
  лgpioʹ
  
ֵ
]]
local function switchtogpio()
	print("switchtogpio",i2cuse)
	if i2cuse then
		--رi2c
		i2close()
		--gpio
		pio.pin.setdir(pio.OUTPUT,pio.P0_6)
		pio.pin.setdir(pio.OUTPUT,pio.P0_7)
		--gpioƽ
		pio.pin.setval(1,pio.P0_6)
		pio.pin.setval(0,pio.P0_7)
		i2cuse = false
	end	
end

--[[
switch
  лi2cgpio
  
ֵ
]]
local function switch()
	if i2cuse then
		switchtogpio()
	else
		switchtoi2c()
	end
end

i2copn()
--ѭʱ5лһι
sys.timer_loop_start(switch,5000)
