--[[
ģƣ߹Ƿģʽ
ģ鹦ܣluaűӦõ߿
ʹ÷ʽοscript/demo/pm
ģ޸ʱ䣺2017.02.13
]]

--[[
һֵ˵
Ŀǰߴַʽ
һǵײcoreڲԶtcpͻ߽ʱԶѣͽս󣬻Զߣⲿֲluaű
һluaűʹpm.sleeppm.wakeпƣ磬uartΧ豸uartǰҪȥpm.wakeܱ֤ǰյݲҪͨʱpm.sleeplcdĿҲͬ
ʱ30mA
ǹƵĲߣһҪ֤pm.wake("A")ˣеطȥpm.sleep("A")
]]

--ģ,
local base = _G
local pmd = require"pmd"
local pairs = base.pairs
local assert = base.assert
module("pm")

--ѱǱ
local tags = {}
--luaӦǷߣtrueߣû
local flag = true

--[[
isleep
  ȡluaӦõ״̬
  
ֵtrueߣû
]]
function isleep()
	return flag
end

--[[
wake
  luaӦûϵͳ
  
		tagѱǣûԶ
ֵ
]]
function wake(tag)
	assert(tag and tag~=nil,"pm.wake tag invalid")
	--ѱд˻ѱλ1
	tags[tag] = 1
	--luaӦô״̬
	if flag == true then
		--Ϊ״̬
		flag = false
		--õײӿڣϵͳ
		pmd.sleep(0)
	end
end

--[[
sleep
  luaӦϵͳ
  
		tag߱ǣûԶ壬wakeеıǱһ
ֵ
]]
function sleep(tag)
	assert(tag and tag~=nil,"pm.sleep tag invalid")
	--ѱд߱λ0
	tags[tag] = 0

	if tags[tag] < 0 then
		base.print("pm.sleep:error",tag)
		tags[tag] = 0
	end

	--ֻҪκһǻ,˯
	for k,v in pairs(tags) do
		if v > 0 then
			return
		end
	end

	flag = true
	----õײӿڣϵͳ
	pmd.sleep(1)
end
