#import_plugin LokiPlugin as loki_port

/*-- Keyboard Defines -- */
#constant KB_esc 		27
#constant KB_tab 		9
#constant KB_space 		32

/*-- Packet Defines   -- */

#constant DEST_ADDR		0
#constant DATA_LEN		1
#constant HOST_ADDR		2
#constant CMD_HDR		3
#constant CHECKSUM		4

#constant PACKET_LEN		5
/*-- Some Vars for Testing --*/

Global disp_txt as String

Global ports as Integer

Global bytes_sent as Integer
Global bytes_rcvd as Integer

Global byte_int as Integer
Global byte_csum as Integer

Global receive_timer as Float
Global receive_delay as Float


Dim    packet_array[ 256 ] as Integer

/*-- Some Vars for Testing --*/
	
SetErrorMode( 2 )

// Set Window Properties
SetWindowTitle( "Loki Raw Comms Plugin" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// Set Display Properties

SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

SetPrintSize( 16 )

/*-- Reset Vars --*/

bytes_sent = 0
bytes_rcvd = 0
byte_int = 0
byte_csum = 0
receive_delay = 0.1 // Check Every 10th of a second..

/*-- Some Vars for Testing --*/
	
update_text ( "Starting..." )

ports = loki_port.get_nof_ports( 10 ) // Look for 10 Com Ports.. 

update_text ( "Comm Ports Found : " + Str( ports ) )

// Using 9600 8N1 - 8N1 is set by DLL, as tends to be widely used
                        
if( loki_port.open_com_port( "COM8", 9600, 1 ) > 0 )

	update_text ( "Opened COM8" )

else	

	update_text ( "COM8 Failed " )
										
endif

receive_timer = Timer()

/*-- Main Loop --*/

do
	if( GetRawKeyPressed( KB_esc ) )

		loki_port.close_com_port( 0 )	
		
		exit
	
	endif
	
	if( GetRawKeyPressed( KB_tab ) )
		
		update_text( '\n' + "Send Simple Poll" )
		
		start_packet_byte()

		packet_array[ DEST_ADDR ] = 2
		packet_array[ DATA_LEN ]  = 0
		packet_array[ HOST_ADDR ] = 1
		packet_array[ CMD_HDR ]   = 254
		packet_array[ CHECKSUM ]  = 0		

		for idx = DEST_ADDR To ( CHECKSUM - 1 )

			loki_port.set_tx_byte( idx, packet_array[ idx ] )
			add_packet_byte( packet_array[ idx ] )
		
		next idx
				
		simple_checksum( PACKET_LEN )

		loki_port.set_tx_byte( CHECKSUM, packet_array[ CHECKSUM ] )		

		add_packet_byte( packet_array[ CHECKSUM ] )
		end_packet_byte()
		
		bytes_sent = loki_port.write_nof_bytes_out( PACKET_LEN )

		update_text( Str(bytes_sent) + " Bytes Sent" )
	
	endif

	if( GetRawKeyPressed( KB_space ) )

		update_text( '\n' + "Send Address Poll" )

		start_packet_byte()
		
		packet_array[ DEST_ADDR ] = 0
		packet_array[ DATA_LEN ]  = 0
		packet_array[ HOST_ADDR ] = 1
		packet_array[ CMD_HDR ]   = 253
		packet_array[ CHECKSUM ]  = 0		

		for idx = DEST_ADDR To ( CHECKSUM - 1 )

			loki_port.set_tx_byte( idx, packet_array[ idx ] )
			add_packet_byte( packet_array[ idx ] )
		
		next idx
				
		simple_checksum( PACKET_LEN )

		loki_port.set_tx_byte( CHECKSUM, packet_array[ CHECKSUM ] )		

		add_packet_byte( packet_array[ CHECKSUM ] )
		end_packet_byte()
		
		bytes_sent = loki_port.write_nof_bytes_out( PACKET_LEN )

		update_text( Str(bytes_sent) + " Bytes Sent : CheckSum " + Str( packet_array[ 4 ] ) )
					
	endif
	
	if( Timer() > receive_timer )
	
		receive_timer = ( Timer() + receive_delay )
			
		bytes = loki_port.read_nof_bytes_in( 256 )
		
		if( bytes > 0 )
		
			update_text( Str( bytes ) + " Bytes Received " )
						
			start_packet_byte()
			
			for idx = 0 To ( bytes - 1 ) 
			
				add_packet_byte( loki_port.get_rx_byte( idx ) )
			
			next idx
			
			end_packet_byte()
			
		endif
					
	endif
		
	
	ClearScreen()

	Print( disp_txt )

	Sync()
		
	Sleep( 50 )
			
loop

loki_port.close_com_port( 0 )	

end

function simple_checksum( pkt_length as Integer )
	
	byte_csum = 0
	
	for index = 0 To (  pkt_length - 1 )
		
		byte_csum = byte_csum + packet_array[ index ]
		
	next index
	
	while( byte_csum > 255 )
	
		dec byte_csum, 256
		
	endwhile
	
	if (byte_csum = 0) then byte_csum = 256
		
	packet_array[ pkt_length - 1 ] =  ( 256 - byte_csum )

endfunction
	
function update_text( txt as String )
	
	disp_txt = disp_txt + txt + '\n'
	
	ClearScreen()
	Print( disp_txt )
	Sync()

endfunction

function start_packet_byte( /* void */ )

	disp_txt = disp_txt + "Packet : ["

endfunction

function add_packet_byte( byte as Integer )
	
	disp_txt = disp_txt + " " + Str( byte ) + "," 
	
endfunction

function end_packet_byte( /* void */ )

	disp_txt = disp_txt + " ] " + '\n'
	
	ClearScreen()
	Print( disp_txt )
	Sync()

endfunction
