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

© Copyright 2022 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
-- Communications Test - read up RAM values from an Engine and check their values
--
title = "NAK 0x0T Test"
--
-- This test repeatedly sends a n43 command once a second, attempting
-- to generate a 0x0T NAK.
-- 
-- We should be able to do this forever without any detected errors.
--
-- When you press STOP to end the test, the script prints out a "Loops per second"
-- value which can be used to determine the relative speed of the communications hardware
-- when run with different configurations (ie: WiFi vs RS232, panStamp vs Seeeduino, etc).
--
require("defines");
require("functions");

-- Counter Labels
Counter01Label = "Data Errors";
Counter02Label = "Comm Errors";
Counter03Label = "Loops";

--[[
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 DebugThreshold = 3;
local PacketCount = 0;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- the setup() function is called once at the beginning of the run
	print("setup(): Engine Number " .. Engine .. " TIU Number " .. TIU .. " Debug " .. Debug());


	print("Engine should be shutdown so no sound are actually emitted");
	print("Runs quietly unless Debug is at least " .. DebugThreshold);
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- The loop() function is repeatedly called
	-- Always call Sleep() at least once in this function if it returns true
	
	if (Debug() >= DebugThreshold - 1) then print(string.format("loop %.2f", RunTime())); end;

	Yield();
	--print("Bell Off");
	if (Bell(OFF, 0, Engine, TIU) == nil) then	-- bFFFB
		Beep();
		BumpCounter(COUNTER02);
		return Stop("Bell Off command failed");
		end;
	PacketCount = PacketCount + 1;
	if (PlaySound(43, 0, Engine, TIU) == nil) then -- SFS
		Beep();
		BumpCounter(COUNTER02);
		return Stop("n43 command failed");
		end;
	PacketCount = PacketCount + 1;	
	Sleep(0.5);
	Yield();
	--print("Whistle Off");
	if (Whistle(OFF, 0, Engine, TIU) == nil) then	-- bFFFD
		Beep();
		BumpCounter(COUNTER02);
		return Stop("Whistle Off command failed");
		end;
	PacketCount = PacketCount + 1;	
	if (PlaySound(43, 0, Engine, TIU) == nil) then -- SFS
		Beep();
		BumpCounter(COUNTER02);
		return Stop("n43 command failed");
		end;
	PacketCount = PacketCount + 1;		
	Sleep(0.5);
	
	BumpCounter(COUNTER03);
	return true;	-- false=do not loop, true=continue to loop
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- the cleanup() function is called once when the [STOP] button is pressed
	print(string.format("cleanup %.2f", RunTime()));
	-- print loops per second
	print("Loops per second = ", GetCounter(COUNTER03)/GetCounter(ELAPSEDTIME));
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
