--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2025 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "Watchdog"
require([[defines]]);
require([[functions]]);

--[[
When using this module, all variables (including functions!) must be declared through 
a regular assignment (even assigning nil will do) in a strict scope before being used 
anywhere or assigned to inside a nested scope.
https://github.com/lua-stdlib/strict
]]
local _ENV = require 'std.strict' (_G)	-- strict.lua will detect undeclared global variables
local _;

local MyEngineNo, MyTIUNo;
title = "Watchdog Test"
local iChan, iAIU;
iChan = 8;
iAIU = 1;
--	
-- Note that there is no loop() function in this sketch
-- All actions are initiated by the buttons
--
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	MyEngineNo = Engine;
	MyTIUNo = TIU; 
	--
	print(string.format("setup(): Debug level %d", Debug())); 
		
	print("setup : TIU " .. MyTIUNo .. " Engine " .. MyEngineNo .. " Debug " .. Debug());
	assert((MyTIUNo >= 1 and MyTIUNo <= 5),"TIU Number out of range");	
	print("setup : AIU " .. iAIU .. " Channel " .. iChan);
	
	return true;		-- false=setup failed, true=setup succeeded
	end

--[[---------------------------------------------------------------------------------------]]
function cleanup()
	-- this function is called once when the user presses the [STOP] button or the Stop() function is called
	print("cleanup : TIU " .. MyTIUNo .. " Engine " .. MyEngineNo .. " Debug " .. Debug());		
	local iResult = OffButton();
	return iResult;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function OnButton()
	print("Track Power On : " .. GetAccName(iChan, iAIU, MyTIUNo));
	-- energize track
	if (Siding(ON, iChan, iAIU, MyTIUNo) == nil) then
		print("Siding Power On failed");
		return false;
		end;
	return true;
end
Function01Name, Function01Label = OnButton, "Track Power On";
--[[---------------------------------------------------------------------------------------]]

function OffButton()
	print("Track Power Off : " .. GetAccName(iChan, iAIU, MyTIUNo));
	-- deenergize track
	if (Siding(OFF, iChan, iAIU, MyTIUNo) == nil) then
		print ("Siding Power Off failed");
		return false;
		end;
	return true;
end
Function02Name, Function02Label = OffButton, "Track Power Off";
--[[---------------------------------------------------------------------------------------]]
function WatchdogButton()
	print("Watchdog : TIU #" .. MyTIUNo);
	-- Send Watchdog signal
	if (Watchdog(MyTIUNo) == nil) then
		print("Watchdog failed");
		return false;
		end;
	return true;
end
Function03Name, Function03Label = WatchdogButton, "Watchdog";
--[[---------------------------------------------------------------------------------------]]