User Tools

Site Tools


mmbasic:simple_screen_control_for_ansi_based_consoles_vt100_etc

VT Console Pack. Screen Positioning, Sane Key Scanning And a String Editing Routine

Not all projects require an LCD panel but a simple console connection can often benefit from a bit more than a dumb scrolling display.

The following Subs and Functions provide functionality similar to that found on personal computers prevalent in the 80s. Console displays can be made quite pleasant with this output and hark back to the character based screens of early PCs.

At() - Screen positioning uses the “At” function for cursor positioning. It uses less program space and is arguably more intuitive than a stand-alone LOCATE command. Note that because it produces a string, strictly a ; or + should be used when printing following output, but because of a quirk in how lines are parsed when printing, it can be omitted resulting in the pleasing.

Print At(10,10) "fred"

VTKey$() - Key Scanning for non-ASCII keys (Function keys, Home, PageUp etc.) is a real pain in the neck. The Function VTKey$() can be used to replace Inkey$ and will return either the ASCII value of the key pressed or a predictable three-letter token for any of the special keys.

Your console emulator must support ANSI screen/keyboard codes - usually defined as VT52, VT100 etc. emulation. Personally I use PuTTY.

Dependancies

  • Option Base 0</li>
  • Requires the [VB-Work-a-like-LTrim-and-RTRIM-Functions|LTrim and RTRIM Functions (VB work-a-like)] or the equivalent [LTrim-RTrim-cFunctions|LTrim RTrim CFunctions] (note this latter does not treat TABs as white-space and so are not trimmed).</li>

Example Usage

	Option Base 0
	InitVTKeys
	
Main:' just something to demonstrate usage

	Clr ' clear the screen

	For n=1 To 25 ' print out a diagonal line of numbers
		Print At(n,n);Str$(n)
	Next

	Window 6,10 ' define  window
	For n=1 To 25 ' demonstrate the contained area of the screen
		Print n
	Next
	
	Window 1,25
	
	Clr
	
	a$=StrEdit$("Fred",10,10,60)
	
	? At(1,1);a$

The Code

	Function At(v As Integer,h As Integer) As String
		At=Chr$(27)+"["+Str$(v)+";"+Str$(h)+"H"
	End Function

	Sub Clr
		Window 1,25' redefine the whole screen
		Print Chr$(27)+"[2J";
	End Sub

	Sub Window(Pt As Integer, Pb As Integer)
	'defines a window from line t to line b and positions the cursor in the start of it
	'Clr and At still work on the whole screen. Only when scrolling (or CRLF) enteres the
	'window does it capture the cursor. Note 0<Pt<Pb is enforced by the console emulator
		Print Chr$(27)+"["+Str$(Pt)+";"+Str$(Pb)+"r"+At(Pt,1);
	End Sub
	
	Function VTKey$() ' replaces Inkey$ and will return the character pressed or the special token _xx
		Local a$
		VTKey$=Inkey$
		Pause 25 ' give the remaining sequence (if any) time to arrive. Any less than 25 can give mis-reads of expanded tokens
		Do
			a$=Inkey$:If a$="" Then Exit Do ' jump out when we are scanning ""
			VTKey$=VTKey$+a$ ' build up the character codes
		Loop		
		If Len(VTKey$)>1 Then VTKey$=ExpandKey(VTKey$)
	End Function
	
	Function ExpandKey(c$) As String' called from VTKey$
		Local n
		For n=0 To Kct
			If KCode$(n)=c$ Then ExpandKey=KTkn$(n):Exit Function
		Next
		ExpandKey=""
	End Function

	Sub InitVTKeys
		Dim Integer Kct
		Local x$
		Local Integer q
KeyData:
		' <esc> [
		Data 27,91,68,0,"_LT"
		Data 27,91,67,0,"_RT"
		Data 27,91,65,0,"_UP"
		Data 27,91,66,0,"_DN"
		Data 27,91,49,126,0,"_HM"
		Data 27,91,50,126,0,"_IN"
		Data 27,91,51,126,0,"_DL"
		Data 27,91,52,126,0,"_ED"
		Data 27,91,53,126,0,"_PU"
		Data 27,91,54,126,0,"_PD"
		Data 27,91,55,126,0,"_HM"
		Data 27,91,56,126,0,"_ED"
		Data 27,91,49,49,126,0,"_F1"
		Data 27,91,49,50,126,0,"_F2"
		Data 27,91,49,51,126,0,"_F3"
		Data 27,91,49,52,126,0,"_F4"
		Data 27,91,49,53,126,0,"_F5"
		Data 27,91,49,55,126,0,"_F6"
		Data 27,91,49,56,126,0,"_F7"
		Data 27,91,49,57,126,0,"_F8"
		Data 27,91,50,48,126,0,"_F9"
		Data 27,91,50,49,126,0,"_FA"
		Data 27,91,50,51,126,0,"_FB"
		Data 27,91,50,52,126,0,"_FC"
		Data 255		

		Restore KeyData 'determine size of the key table
		Do
			Read q
			If q=255 Then Exit Do
			If q=0 Then Kct=Kct+1:Read x$
		Loop

		Dim KCode$(Kct-1) Length 5
		Dim KTkn$(Kct-1) Length 3

		Restore KeyData
		Kct=0
		Do
			Read q
			If q=255 Then Kct=Kct-1:Exit Do ' adjust Kct to point at top of key array
			If q=0 Then
				Read KTkn$(Kct)
				Kct=Kct+1
			Else
				KCode$(Kct)=KCode$(Kct)+Chr$(q)
			EndIf
		Loop

		'Here KCode$(n) is the key sequences we get from the keypress
		'     KTkn$(n)  is the shorthand code name _xx
	End Sub

See Also: String editing on_screen for VT compatible Terminals

mmbasic/simple_screen_control_for_ansi_based_consoles_vt100_etc.txt · Last modified: 2024/01/19 09:30 by 127.0.0.1