--[[
  CDU.lua 
  $Id: CDU.lua,v 1.15 2006/07/02 07:36:48 mirage Exp $
]]
--------------------------------------------------------------------------------
-- common function and variavles for CDU.
-- 
--------------------------------------------------------------------------------
--Groval vars

cpos_lat = 0.0;
cpos_long = 0.0;
next_dir = 0.0;
next_dist = 0.0;

--Groval vars

cnt = 0;
cntMult = 1;
trantionAlt = 18000;

--------------------------------------------------------------------------------

contents_buffer = {

	LS_str = {};
	RS_str = {};
	LS_smallFont = {};
	RS_smallFont = {};
	LS_color = {};
	RS_color = {};
	LS_invert = {};
	RS_invert = {};
	lineSize = 0;
	
	LS_SYS_MESSAGE = nil;
	RS_SYS_MESSAGE = nil;
	LS_ERR_MESSAGE = nil;

};

	function contents_buffer:clear( size )
		contents_buffer.lineSize = size;
		for i = 0, size, 1
		do
			contents_buffer.LS_str[i] = ' ';
			contents_buffer.RS_str[i] = ' ';
			contents_buffer.LS_smallFont[i] = false;
			contents_buffer.RS_smallFont[i] = false;
			contents_buffer.LS_invert[i] = false;
			contents_buffer.RS_invert[i] = false;
			contents_buffer.LS_color[i] = 7;
			contents_buffer.RS_color[i] = 7;
		end
	end


	----------------------------
	function contents_buffer:draw( p )

		for i = 0, #contents_buffer.LS_str, 1
		do
			FMScore_setLine( p,
						 0,
						 i,
						 contents_buffer.LS_str[i],
						 string.len( contents_buffer.LS_str[i] ),
						 contents_buffer.LS_color[i],
						 contents_buffer.LS_smallFont[i],
						 contents_buffer.LS_invert[i] );
		end

		for i = 0, #contents_buffer.RS_str, 1
		do
			local len = string.len( contents_buffer.RS_str[i] );
			FMScore_setLine( p,
						 size_x - len,
						 i,
						 contents_buffer.RS_str[i],
						 len,
						 contents_buffer.RS_color[i],
						 contents_buffer.RS_smallFont[i],
						 contents_buffer.RS_invert[i] );
		end

		if contents_buffer.LS_ERR_MESSAGE ~= nil then
			FMScore_setLine( p,
						 0,
						 14,
						 contents_buffer.LS_ERR_MESSAGE,
						 0,
						 7 );
		elseif contents_buffer.LS_SYS_MESSAGE ~= nil then
			FMScore_setLine( p,
						 0,
						 14,
						 contents_buffer.LS_SYS_MESSAGE,
						 0,
						 7 );
		end

		if contents_buffer.RS_SYS_MESSAGE ~= nil then
			local len = string.len(contents_buffer.RS_SYS_MESSAGE );
			FMScore_setLine( p,
						 size_x - len,
						 14,
						 contents_buffer.RS_SYS_MESSAGE,
						 0,
						 7 );
		end

		scratchPad:draw( p );

	end

--------------------------------------------------------------------------------

scratchPad = {
	buf = { " " };
	len = 0;
}
	function scratchPad:bufToStr()

		local str = "\0"

		if scratchPad.len > 0 then
			str = string.char( scratchPad.buf[0] );

			for j = 1, scratchPad.len - 1, 1
			do
				str = str .. string.char( scratchPad.buf[j] );
			end
			str = str .. '\0';
		end

		return str
	end
	----------------------------
	function scratchPad:bufToNum()

		local str = scratchPad:bufToStr();
		return tonumber( str );
	end
	----------------------------

	function scratchPad:draw( p )

		local j;

		FMScore_setLine( p, 1, 13, scratchPad:bufToStr() );

		FMScore_setText( p, 0, 13, '[' );
		FMScore_setText( p, 24, 13, ']' );
	end

	----------------------------
	function scratchPad:popAll()

		local retStr = scratchPad:bufToStr();
		scratchPad.len = 0;
		return retStr;

	end

	----------------------------
	function scratchPad:push_back( c )

		scratchPad.buf[ scratchPad.len ] = c;
		scratchPad.len = scratchPad.len + 1;

	end

	----------------------------
	function scratchPad:erase_back()
		if scratchPad.len > 0 then
			scratchPad.len = scratchPad.len - 1;
		end
	end

	function scratchPad:setDelete()
		scratchPad:push_back( string.byte('D') );
		scratchPad:push_back( string.byte('E') );
		scratchPad:push_back( string.byte('L') );
		scratchPad:push_back( string.byte('E') );
		scratchPad:push_back( string.byte('T') );
		scratchPad:push_back( string.byte('E') );
		scratchPad.len = 6;
	end

--------------------------------------------------------------------------------
function latToStr( var )

	local lat_prefix = 'N';

	if var > 0 then
		lat_prefix = 'N';
	else
		lat_prefix = 'S';
	end

	local deg = math.floor( math.abs( var ) );
	local min_tmp = ( math.abs( var ) - deg ) * 60;
	local min = math.floor( min_tmp );
	local sec = math.floor( ( min_tmp - min ) * 100 );

	return string.format( " %s%d`%d.%d\0", lat_prefix, deg, min, sec );

end
--------------------------------------------------------------------------------
function longToStr( var )

	local long_prefix = 'E';

	if var > 0 then
		long_prefix = 'E';
	else
		long_prefix = 'W';
	end

	local deg = math.floor( math.abs( var ) );
	local min_tmp = ( math.abs( var ) - deg ) * 60;
	local min = math.floor( min_tmp );
	local sec = math.floor( ( min_tmp - min ) * 100 );

	return string.format( " %s%d`%d.%d\0", long_prefix, deg, min, sec );

end

--------------------------------------------------------------------------------
function hdgToStr( crs )

	if crs < 10 then
		crs = string.format( "00%d", crs );
	elseif crs < 100 then
		crs = string.format( "0%d", crs );
	else
		crs = string.format( "%d", crs );
	end
	return crs;

end