MyTerminal graphic prog

For issues with communication ports, protocols, etc.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

MyTerminal graphic prog

Post by dasyar »

Since it has been awhile since I did a program in freeBASIC, I thought I would try to convert "MyTerminal prog" into a graphic version.

This is basically a prototype program, just to see if I could get the thing to work. The way it works is, you click on the "Port" and " BAUD" menu items to set the rates. Then you would click on the the "Connect" menu item to start things up. The "Connect" will change to "Disconnect", you will click that to stop the comm session. To quit the program, you can press the 'Esc' key or click on the "X" in the window.

Comments are welcome, about the coding or the actual window style. This is a Linux version, I have a Gigabyte Brix box with a Raspberry Pi desktop installed.

Code: Select all

' fbtermg.bas
'
' October 23, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif



Dim e As EVENT

Dim Shared As String Key,title,titlecon,titledis,event1,blank1,buffer
Dim Shared As String titleport,port1,port2,port3,port4,port5,p
Dim Shared As String titlebaud,baud1,baud2,baud3,baud4,baud5,b
Dim Shared As Long res, x, y, buttons, column, row
Dim Shared As Integer title1
Dim Shared As Double t
Dim Shared As Double flag

Screen 20  ' 1024x768
WindowTitle "FBtermG Program"

Declare Sub Connect()
Declare Sub Disconnect()
Declare Sub CommBox()
Declare Sub CommBoxB()
Declare Sub PortBox()
Declare Sub PortBox1()
Declare Sub PortBox2()
Declare Sub PortBox3()
Declare Sub PortBox4()
Declare Sub PortBox5()
Declare Sub BaudBox()
Declare Sub BaudBox1()
Declare Sub BaudBox2()
Declare Sub BaudBox3()
Declare Sub BaudBox4()
Declare Sub BaudBox5()
Declare Sub BlankOut()
Declare Sub TextCursor(ByRef flag As Double)

Connect()
PortBox()
BaudBox()
color 0,0
locate 1,1
Do
	res = GetMouse(x, y, ,buttons)  ' Start mouse
	Key = InKey   ' Start keyboard
	TextCursor(flag)  ' Start cursor
	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_KEY_PRESS
			If(e.scancode = SC_ESCAPE) Then  ' Esc key to end program.
				Close
				End
			End If
		Case EVENT_WINDOW_CLOSE  ' Close window
			Close
			End
		End Select
	End If
' Mouse over connect/disconnect	
	If x > 5 And x <= 88 And y > 1 And y <= 18 And buttons And 1 Then		
		Sleep 200,1
		if title1 = 0 then
			Disconnect()
			CommBox()
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
			View Print 2 to 47
		elseif title1 = 1 then
			Connect()
			Close
			CommBoxB()
		End If
	End If
' Mouse over Port	
	If x > 93 And x <= 261 And y > 1 And y <= 18 And buttons And 1 Then
		PortBox1()
		PortBox2()
		PortBox3()
		PortBox4()
		PortBox5()
	End If
' Mouse over portbox1
	If x > 157 And x <= 261 And y > 18 And y <= 35 And buttons And 1 Then
		PortBox()
		port1 = "/dev/ttyAMA0:"
		Draw String (159,2),port1,4
		p = port1
		BlankOut()	
	End If
' Mouse over portbox2
	If x > 157 And x <= 261 And y > 36 And y <= 73 And buttons And 1 Then
		PortBox()
		port2 = "/dev/ttyUSB0:"
		Draw String (159,2),port2,4
		p = port2
		BlankOut()	
	End If
' Mouse over portbox3
	If x > 157 And x <= 261 And y > 52 And y <= 68 And buttons And 1 Then
		PortBox()
		port3 = "/dev/ttyUSB1:"
		Draw String (159,2),port3,4
		p = port3
		BlankOut()	
	End If
' Mouse over portbox4
	If x > 157 And x <= 261 And y > 68 And y <= 84 And buttons And 1 Then
		PortBox()
		port4 = "/dev/ttyUSB2:"
		Draw String (159,2),port4,4
		p = port4
		BlankOut()	
	End If
' Mouse over portbox5
	If x > 157 And x <= 261 And y > 80 And y <= 96 And buttons And 1 Then
		PortBox()
		port5 = "/dev/ttyUSB3:"
		Draw String (159,2),port5,4
		p = port5
		BlankOut()	
	End If	
' Mouse over BAUD	
	If x > 262 And x <= 382 And y > 1 And y <= 18 And buttons And 1 Then
		BaudBox1()
		BaudBox2()
		BaudBox3()
		BaudBox4()
		BaudBox5()
	End If
' Mouse over baudbox1
	If x > 300 And x <= 382 And y > 18 And y <= 35 And buttons And 1 Then
		BaudBox()
		baud1 = "9600"
		Draw String (316,2),baud1,4
		b = baud1
		BlankOut()		
	End If
' Mouse over baudbox2
	If x > 300 And x <= 382 And y > 36 And y <= 48 And buttons And 1 Then
		BaudBox()
		baud2 = "19200"
		Draw String (316,2),baud2,4
		b = baud2
		BlankOut()		
	End If
' Mouse over baudbox3
	If x > 300 And x <= 382 And y > 52 And y <= 68 And buttons And 1 Then
		BaudBox()
		baud3 = "38400"
		Draw String (316,2),baud3,4
		b = baud3
		BlankOut()		
	End If
' Mouse over baudbox4
	If x > 300 And x <= 382 And y > 68 And y <= 84 And buttons And 1 Then
		BaudBox()
		baud4 = "57600"
		Draw String (316,2),baud4,4
		b =baud4
		BlankOut()		
	End If
' Mouse over baudbox5
	If x > 300 And x <= 382 And y > 80 And y <= 96 And buttons And 1 Then
		BaudBox()
		baud5 = "115200"
		Draw String (316,2),baud5,4
		b = baud5
		BlankOut()		
	End If
			
	If Key <> "" Then
		If Key = Chr(13) Then
			Print Chr(13)
		Else
			print Key;
		End If
	End If	
	
	While LOC(1) > 0
		buffer = Input(LOC(1),#1)
		Print buffer;
	Wend

Sleep 1
Loop

Close
End
''''''''''''''''''''''''''''''
' Menu item Connect
Sub Connect()
titlecon = "Connect"
Draw "BM 8,1"               ' Start point x,y
Draw "C2"                    ' Set color
Draw "R85 D15 L85 U15"       ' Size of box
Draw "BM +1,1"               ' Move to inside of box
Draw "P 7,2"                 ' Flood fill up to border
Draw String (9,2),titlecon,2
title1 = 0
End Sub

' Menu item Disconnect
Sub Disconnect()
titledis = "Disconnect"
Draw "BM 8,1"               ' Start point x,y
Draw "C2"                    ' Set color
Draw "R85 D15 L85 U15"       ' Size of box
Draw "BM +1,1"               ' Move to inside of box
Draw "P 7,2"                 ' Flood fill up to border
Draw String (9,2),titledis,4
title1 = 1
End Sub

' Window for serial comms, open
Sub CommBox()
'Draw "BM 0,34"
'Draw "C2"
'Draw "R900 D700 L900 U700"
'Draw "BM +1,1"
'Draw "P 7,2"

line (1,18) - (900,740), 7, BF  'Create filled box
locate 2,1
color 0,7
End Sub

' Window for serial comms, close
Sub CommBoxB()
'Draw "BM 0,34"
'Draw "C0"
'Draw "R900 D700 L900 U700"
'Draw "BM +1,1"
'Draw "P 0,0"
Locate 2,1
color 0,0
line (1,18) - (900,740), 0, BF  'Blank filled box
End Sub

Sub PortBox()
titleport = "Port -"
Draw "BM 93,1"
Draw "C2"
'Draw "R85 D15 L85 U15"
Draw "R168 D15 L168 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (96,2),titleport,4
End Sub

Sub PortBox1()
port1 = "/dev/ttyAMA0:"
Draw "BM 157,16"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,18),port1,4
End Sub

Sub PortBox2()
port2 = "/dev/ttyUSB0:"
Draw "BM 157,32"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,34),port2,4
End Sub

Sub PortBox3()
port3 = "/dev/ttyUSB1"
Draw "BM 157,48"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,50),port3,4
End Sub

Sub PortBox4()
port4 = "/dev/ttyUSB2"
Draw "BM 157,64"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,66),port4,4
End Sub

Sub PortBox5()
port5 = "/dev/ttyUSB3"
Draw "BM 157,80"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,82),port5,4
End Sub

Sub BaudBox()
titlebaud = "BAUD -"
Draw "BM 262,1"
Draw "C2"
Draw "R120 D15 L120 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (264,2),titlebaud,4
End Sub

Sub BaudBox1()
baud1 = "9600"
Draw "BM 300,16"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,18),baud1,4
End Sub

Sub BaudBox2()
baud2 = "19200"
Draw "BM 300,32"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,34),baud2,4
End Sub

Sub BaudBox3()
baud3 = "38400"
Draw "BM 300,48"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,50),baud3,4
End Sub

Sub BaudBox4()
baud4 = "57600"
Draw "BM 300,64"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,66),baud4,4
End Sub

Sub BaudBox5()
baud5 = "115200"
Draw "BM 300,80"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,82),baud5,4
End Sub

Sub BlankOut()
Locate 2,1
color 0,0
line (1,18) - (900,740), 0, BF  'Blank filled box
End Sub

' Cursor display
Sub TextCursor(ByRef flag As Double)
column = Pos
row = CsrLin
If Timer - t > .5 Then
	flag = Not(flag)
	If flag Then Print "_" Else Print " ";
	Locate row,column
	t = Timer
End If
End Sub

MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: MyTerminal graphic prog

Post by MrSwiss »

@dasyar, are you again, trying to trick others, to do your "coding" for you?

Hopefully, nobody (in future) will fall into that "trap" of yours, again, ever ...
(I did, until realizing the mistake, I've made!)

THIS IS A WARNING TO ALL!
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

@MrSwiss
This is the second thread of mine that you have distorted, if you do not like my threads, just move on to something else. But, I guess you consider that to be a comment, maybe I should have specified , comments from everybody, except MrSwiss. I also reread my OP, and I did not ask for anybody to code my program.

Maybe the reason people do not come to this forum is because of the type of remarks that MrSwiss leaves in some of the threads. I would also prefer if you would delete your inflammatory post, or maybe one of the moderators could do that.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: MyTerminal graphic prog

Post by MrSwiss »

dasyar wrote:I would also prefer if you would delete ...
I bet, you would. (NO way!)
dasyar wrote:inflammatory post, or maybe one of the moderators could do that.
Posts can only be: "inflamatory", if they are untrue, which isn't the case here!

You've always lived of: "other peoples code"! At least, in this forum.
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

Re: MyTerminal graphic prog

Post by sean_vn »

I was jumped on and assaulted by two bullies on the nvidia developers forum recently, or that's what it felt like. Isn't the internet a wonderful thing? The secret id of every human can come creeping out to wreck damage and destruction and seek revenge, even for another person having a slightly different opinion to your own.
That's just me paraphrasing some lines in Forbidden Planet.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I have been around the forums for quite a while, I have developed a fairly thick skin. So, I will just ignore those kinds of remarks and posts.

In the listing that I have below, I have included the keyboard I/O, in terminal mode, which was missing in the previous version. The only problem with this is, when you are connected, you will see a keyboard echo. I guess I will have to figure out how to display some buttons to resolve this issue. Although creating a check box might be simpler.

This program was just an experiment to see what it would take to create a graphical program using freeBASIC. It is very time consuming and code intensive, in order to develop all the box, button check box, text area, ..., etc, items. But, at least I know it can be done using freeBASIC, without resorting to third party graphic tools. One less language to deal with.

Code: Select all

' fbtermg.bas
'
' October 23, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif



Dim e As EVENT

Dim Shared As String Key,title,titlecon,titledis,event1,blank1,buffer
Dim Shared As String titleport,port1,port2,port3,port4,port5,p
Dim Shared As String titlebaud,baud1,baud2,baud3,baud4,baud5,b
Dim Shared As Long res, x, y, buttons, column, row
Dim Shared As Integer title1
Dim Shared As Double t
Dim Shared As Double flag

Screen 20  ' 1024x768
WindowTitle "FBtermG Program"

Declare Sub Connect()
Declare Sub Disconnect()
Declare Sub CommBox()
Declare Sub CommBoxB()
Declare Sub PortBox()
Declare Sub PortBox1()
Declare Sub PortBox2()
Declare Sub PortBox3()
Declare Sub PortBox4()
Declare Sub PortBox5()
Declare Sub BaudBox()
Declare Sub BaudBox1()
Declare Sub BaudBox2()
Declare Sub BaudBox3()
Declare Sub BaudBox4()
Declare Sub BaudBox5()
Declare Sub BlankOut()
Declare Sub TextCursor(ByRef flag As Double)

Connect()
PortBox()
BaudBox()
color 0,0
locate 1,1

' Event driven code.
Do
	res = GetMouse(x, y, ,buttons)  ' Start mouse
	Key = InKey   ' Start keyboard
	TextCursor(flag)  ' Start cursor
	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_KEY_PRESS
			If(e.scancode = SC_ESCAPE) Then  ' Esc key to end program.
				Close
				End
			End If
		Case EVENT_WINDOW_CLOSE  ' Close window
			Close
			End
		End Select
	End If
' Mouse over connect/disconnect	
	If x > 5 And x <= 88 And y > 1 And y <= 18 And buttons And 1 Then		
		Sleep 200,1
		if title1 = 0 then
			Disconnect()
			CommBox()
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
			View Print 2 to 47
		elseif title1 = 1 then
			Connect()
			Close
			CommBoxB()
		End If
	End If
' Mouse over Port selection	
	If x > 93 And x <= 261 And y > 1 And y <= 18 And buttons And 1 Then
		PortBox1()
		PortBox2()
		PortBox3()
		PortBox4()
		PortBox5()
	End If
' Mouse over portbox1 selection
	If x > 157 And x <= 261 And y > 18 And y <= 35 And buttons And 1 Then
		PortBox()
		port1 = "/dev/ttyAMA0:"
		Draw String (159,2),port1,4
		p = port1
		BlankOut()	
	End If
' Mouse over portbox2 selection
	If x > 157 And x <= 261 And y > 36 And y <= 73 And buttons And 1 Then
		PortBox()
		port2 = "/dev/ttyUSB0:"
		Draw String (159,2),port2,4
		p = port2
		BlankOut()	
	End If
' Mouse over portbox3 selection
	If x > 157 And x <= 261 And y > 52 And y <= 68 And buttons And 1 Then
		PortBox()
		port3 = "/dev/ttyUSB1:"
		Draw String (159,2),port3,4
		p = port3
		BlankOut()	
	End If
' Mouse over portbox4 selection
	If x > 157 And x <= 261 And y > 68 And y <= 84 And buttons And 1 Then
		PortBox()
		port4 = "/dev/ttyUSB2:"
		Draw String (159,2),port4,4
		p = port4
		BlankOut()	
	End If
' Mouse over portbox5 selection
	If x > 157 And x <= 261 And y > 80 And y <= 96 And buttons And 1 Then
		PortBox()
		port5 = "/dev/ttyUSB3:"
		Draw String (159,2),port5,4
		p = port5
		BlankOut()	
	End If	
' Mouse over BAUD selection
	If x > 262 And x <= 382 And y > 1 And y <= 18 And buttons And 1 Then
		BaudBox1()
		BaudBox2()
		BaudBox3()
		BaudBox4()
		BaudBox5()
	End If
' Mouse over baudbox1 selection
	If x > 300 And x <= 382 And y > 18 And y <= 35 And buttons And 1 Then
		BaudBox()
		baud1 = "9600"
		Draw String (316,2),baud1,4
		b = baud1
		BlankOut()		
	End If
' Mouse over baudbox2 selection
	If x > 300 And x <= 382 And y > 36 And y <= 48 And buttons And 1 Then
		BaudBox()
		baud2 = "19200"
		Draw String (316,2),baud2,4
		b = baud2
		BlankOut()		
	End If
' Mouse over baudbox3 selection
	If x > 300 And x <= 382 And y > 52 And y <= 68 And buttons And 1 Then
		BaudBox()
		baud3 = "38400"
		Draw String (316,2),baud3,4
		b = baud3
		BlankOut()		
	End If
' Mouse over baudbox4 selection
	If x > 300 And x <= 382 And y > 68 And y <= 84 And buttons And 1 Then
		BaudBox()
		baud4 = "57600"
		Draw String (316,2),baud4,4
		b =baud4
		BlankOut()		
	End If
' Mouse over baudbox5 selection
	If x > 300 And x <= 382 And y > 80 And y <= 96 And buttons And 1 Then
		BaudBox()
		baud5 = "115200"
		Draw String (316,2),baud5,4
		b = baud5
		BlankOut()		
	End If
' Keyboard I/O	in terminal mode		
	If Key <> "" Then
		If Key = Chr(13) Then
			Print #1, Chr(13)
		Else
			Print #1, Key;
			print Key + " ";  ' This will echoe when connected.
		End If
	End If	
' Display incoming text.	
	While LOC(1) > 0
		buffer = Input(LOC(1),#1)
		Print buffer;
	Wend

Sleep 1
Loop

Close
End
''''''''''''''''''''''''''''''
' Menu item Connect
Sub Connect()
titlecon = "Connect"
Draw "BM 8,1"               ' Start point x,y
Draw "C2"                    ' Set color
Draw "R85 D15 L85 U15"       ' Size of box
Draw "BM +1,1"               ' Move to inside of box
Draw "P 7,2"                 ' Flood fill up to border
Draw String (9,2),titlecon,2
title1 = 0
End Sub

' Menu item Disconnect
Sub Disconnect()
titledis = "Disconnect"
Draw "BM 8,1"               ' Start point x,y
Draw "C2"                    ' Set color
Draw "R85 D15 L85 U15"       ' Size of box
Draw "BM +1,1"               ' Move to inside of box
Draw "P 7,2"                 ' Flood fill up to border
Draw String (9,2),titledis,4
title1 = 1
End Sub

' Window for serial comms, open
Sub CommBox()
'Draw "BM 0,34"
'Draw "C2"
'Draw "R900 D700 L900 U700"
'Draw "BM +1,1"
'Draw "P 7,2"

line (1,18) - (900,740), 7, BF  'Create filled box
locate 2,1
color 0,7
End Sub

' Window for serial comms, close
Sub CommBoxB()
'Draw "BM 0,34"
'Draw "C0"
'Draw "R900 D700 L900 U700"
'Draw "BM +1,1"
'Draw "P 0,0"
Locate 2,1
color 0,0
line (1,18) - (900,740), 0, BF  'Blank filled box
End Sub

Sub PortBox()
titleport = "Port -"
Draw "BM 93,1"
Draw "C2"
Draw "R168 D15 L168 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (96,2),titleport,4
End Sub

Sub PortBox1()
port1 = "/dev/ttyAMA0:"
Draw "BM 157,16"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,18),port1,4
End Sub

Sub PortBox2()
port2 = "/dev/ttyUSB0:"
Draw "BM 157,32"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,34),port2,4
End Sub

Sub PortBox3()
port3 = "/dev/ttyUSB1"
Draw "BM 157,48"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,50),port3,4
End Sub

Sub PortBox4()
port4 = "/dev/ttyUSB2"
Draw "BM 157,64"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,66),port4,4
End Sub

Sub PortBox5()
port5 = "/dev/ttyUSB3"
Draw "BM 157,80"
Draw "C2"
Draw "R104 D15 L104 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (159,82),port5,4
End Sub

Sub BaudBox()
titlebaud = "BAUD -"
Draw "BM 262,1"
Draw "C2"
Draw "R120 D15 L120 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (264,2),titlebaud,4
End Sub

Sub BaudBox1()
baud1 = "9600"
Draw "BM 300,16"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,18),baud1,4
End Sub

Sub BaudBox2()
baud2 = "19200"
Draw "BM 300,32"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,34),baud2,4
End Sub

Sub BaudBox3()
baud3 = "38400"
Draw "BM 300,48"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,50),baud3,4
End Sub

Sub BaudBox4()
baud4 = "57600"
Draw "BM 300,64"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,66),baud4,4
End Sub

Sub BaudBox5()
baud5 = "115200"
Draw "BM 300,80"
Draw "C2"
Draw "R82 D15 L82 U15"
Draw "BM +1,1"
Draw "P 7,2"
Draw String (316,82),baud5,4
End Sub

Sub BlankOut()
Locate 2,1
color 0,0
line (1,18) - (900,740), 0, BF  'Blank filled box
End Sub

' Cursor display
Sub TextCursor(ByRef flag As Double)
column = Pos
row = CsrLin
If Timer - t > .5 Then
	flag = Not(flag)
	If flag Then Print "_" Else Print " ";
	Locate row,column
	t = Timer
End If
End Sub

BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

dasyar wrote:This program was just an experiment to see what it would take to create a graphical program using freeBASIC. It is very time consuming and code intensive, in order to develop all the box, button check box, text area, ..., etc, items. But, at least I know it can be done using freeBASIC, without resorting to third party graphic tools. One less language to deal with.
And once done save them as a GUI library :)

Your buttons are active even if they are not visible.
I wouldn't use DRAW instead I would use LINE.
Also instead of all those subs just use a user defined TYPE.
This will be easier to modify later.
.
Here is a rough example of what I mean,

Code: Select all

screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255):cls
dim shared as integer mx,my,mb

type BUTTON
    as integer x
    as integer y
    as integer w
    as integer h
    as string  t
    as integer f   'Connect or disconnect flag
    as ulong   c1  'foreground color
    as ulong   c2  'background color
end type

sub drawButton(btn as BUTTON)
    line (btn.x, btn.y)-(btn.x + btn.w, btn.y + btn.h),btn.c2,bf    'clear
    line (btn.x, btn.y)-(btn.x + btn.w, btn.y + btn.h),rgb(0,0,0),b 'border
    draw string (btn.x+4, btn.y+4),btn.t,btn.c1
end sub

type COMBO_BOX
    as string  title         'main header
    as string  list(0 to 4)  'number of items to select
    as integer x
    as integer y
    as integer w
    as integer h
    as string  selItem
    as integer a   'active or not
end type

sub drawComboBox(cmb as COMBO_BOX)
    line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16),rgb(0,0,0),b
    draw string (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
    if cmb.a = 1 then
        line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b
        for i as integer = 0 to 4
            draw string (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
        next i
    end if
end sub

dim shared as BUTTON btnConnect
btnConnect.x = 0
btnConnect.y = 0
btnConnect.w = 8 * 12
btnConnect.h = 16
btnConnect.t = " Connect "
btnConnect.c1 = rgb(0,100,0)     'green
btnConnect.c2 = rgb(200,200,200) 'light gray

dim shared as COMBO_BOX cmbPort, cmbBaud

cmbPort.x = 100
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

cmbBaud.x = 300
cmbBaud.y = 0
cmbBaud.w = 8* 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:" 
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/ttyUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

cmbBaud.list(0) = "  9600"
cmbBaud.list(1) = " 19200"
cmbBaud.list(2) = " 38400"
cmbBaud.list(3) = " 57600"
cmbBaud.list(4) = "115200"

sub update()
    screenlock
    cls
    drawComboBox(cmbPort)
    drawComboBox(cmbBaud)
    drawButton(btnConnect)
    screenunlock
end sub

dim as integer hit

do
    getmouse mx,my,,mb
    if mb = 1 then
        hit = 0 'record if item hit
        if mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y and my<cmbBaud.y+16 then
            cmbBaud.a = 1
            cmbPort.a = 0
            hit = 1
        end if
        if mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y and my<cmbPort.y+16 then
            cmbPort.a = 1
            cmbBaud.a = 0
            hit = 1
        end if
        if cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
            hit = 1
        end if
        if cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
            cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
            hit = 1
        end if  
        if hit = 0 then  'deactivate objects
            cmbPort.a = 0
            cmbBaud.a =0
        end if
        
        if mx>btnConnect.x and mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+btnConnect.h then
            if btnConnect.f = 0 then
                btnConnect.f = 1
                btnConnect.t = " Disconnect"
            else
                btnConnect.f = 0
                btnConnect.t = "  Connect  "
            end if
        end if
        
        update()
        while mb=1
            getmouse mx,my,,mb
        wend
        
    end if
    update()
    sleep 2,1
loop until multikey(&H01)
Last edited by BasicCoder2 on Oct 26, 2017 9:45, edited 2 times in total.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

Thanks BasicCoder2, your code example has given me something to think about. The ComboBox, the way you present it makes it much easier to use, but the sub is very complex.

In my code I am having a very difficult time using 'Draw String', especially when you need to overwrite an existing String. I tried just about everything to blank out the String with no success.

I have never used 'Type ... End Type', but this does look a lot like a C structure.

Thanks
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

dasyar wrote:The ComboBox, the way you present it makes it much easier to use, but the sub is very complex.
It isn't complex. It may look that way to you because with all due respect you seem to have insufficient practice in some of the commands available to QBASIC and FreeBASIC.
I have never used 'Type ... End Type', but this does look a lot like a C structure.
There is no such thing as "a C structure". What we have are data structures for any languages.
Blow the dust off the old QBASIC manual and you will find TYPE. The C equivalent is called a STRUCT
An array is a data structure.
There are other types of structures such as trees and queues which you have to write yourself.

I have just edited the code in the previous post by adding a Connect button and fixing a bug.
In my code I am having a very difficult time using 'Draw String', especially when you need to overwrite an existing String. I tried just about everything to blank out the String with no success.
Well if you clear the whole screen as I have done there is no "overwrite" involved. If you only want to clear the screen section being updated then use the LINE command.

Code: Select all

screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255):cls

dim shared as ulong number
sub update()
    screenlock
    line (100,100)-(200,116),rgb(200,200,255),bf  'clear draw string area with light blue block
    draw string (104,104),str(number)
    screenunlock
end sub

update()

do
    number = number + 1
    update()
    sleep 20
loop until multikey(&H01)
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

From this point on, I want to keep this as sort of a tutorial style, provide some code, maybe some suggestions or remarks will appear, so somebody that is totally new might get something out of this. This first rendition will be a basic window with some functional button(s).

Below, the program is in the style that BasicCoder2 had provided. Functionality of the program is, the 'Quit' button works, but the other two buttons are there just for seeing how the button placement would work, and look like.

I tried to use the #include for a text file that I named mygui.a, did not work as expected. I had some 'Type' definition code in there, for some reason at compile time it was showing errors. I think it would be nice to figure how to create a mygui library at this time.

After creating the BUTTON Type, it looks like the buttons are very bland, how would I add some color to the Button without obscuring the title? Not sure how to add a foreground/background component to the Draw String definition, or would it be placed in the box outline definition.

Probably the next step is to create a Msg Type, so when you hit 'Quit' maybe a message box would come with a 'Yes' or 'No' choice.

Code: Select all

' fbtermg.bas
'
' October 26, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif

'Screen 20  '1024x768
Screenres 640,480,32
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Integer mx,my,mb

Type BUTTON_BOX
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As string selItem
	As Integer a
End Type




Dim Shared As BUTTON_BOX btnConnect, btnDisconnect, btnQuit

Declare Sub drawButton(btn as BUTTON_BOX)

Declare Sub update()

Dim As Integer hit

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"

' Disconnect button
btnDisconnect.x = 150
btnDisconnect.y = 0
btnDisconnect.w = 8 * 11
btnDisconnect.h = 16 * 6
btnDisconnect.title = "Disconnect"

Do
	getmouse mx,my,,mb  ' Start mouse
' Close the window using the "X"	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_WINDOW_CLOSE   ' X in window
			Close
			End
		End Select
	End If
	
	If mb = 1 Then
		hit = 0
' Mouse over btnQuit
		If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then
			btnQuit.a = 1
			btnConnect.a = 0
			btnDisconnect.a = 0
			hit =1
			Close
			End
		End If
		
	End If
	
	update()
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
	Sleep 2.1
Loop until multikey(&H01)  ' Esc key to close

''''''''''''''''''''''''''''''
End

' Button
Sub drawButton(btn as BUTTON_BOX)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(0,0,0), b
	Draw String (btn.x+4,btn.y+4),btn.title & btn.selItem
End Sub


' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnConnect)    ' Not active for now
	drawButton(btnDisconnect) ' Not active for now
	Screenunlock
End Sub


MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: MyTerminal graphic prog

Post by MrSwiss »

@admin,

please move this thread, to the appropriate section: BEGINNERS ...
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I just tested my mygui.bi file, and seems to work when I use the #include mygui.bi. I guess the compiler did not like mygui.a designation. Now I will be adding my Type definitions in there, not sure what else would be appropriate to place in there.

I also added a BF to the Line command, that seems to fill in the box.
Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf - this fills in the box with a antique white 2 color, visually it looks like a nice color for a button.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

dasyar wrote:I just tested my mygui.bi file, and seems to work when I use the #include mygui.bi. I guess the compiler did not like mygui.a designation.
Yes all your gui stuff can be in an include file so you don't have to declare them at the start although when developing a gui control I usually just place it at the start. Indeed I order them so a declare is not required.

I see you are using the extended library for events. If you are going to do that I would study it and make full use of it. I write my own event driven code.

I assume you got example code from here?
https://www.freebasic.net/wiki/wikka.ph ... creenevent
https://www.freebasic.net/wiki/wikka.ph ... KeyPgEvent

Now I will be adding my Type definitions in there, not sure what else would be appropriate to place in there.
The routines like drawButton(btn) that use the TYPE definitions.

Why a connect and disconnect button? You can't have both on at the same time. I would suggest it makes more sense to have a toggle button as I had in the example above. I would also just call it a BUTTON not a BUTTON_BOX.

You can add variables to the type definition and modify the drawButton to make use of them.
Below I have simply added foreground and background colors. You could add variables for showing the mouse is over a button and/or mouse button down on button. There are examples through out the forum posts. GUI code lends itself well to object orientated programming.

Code: Select all


' ========================  CAN PLACE IN A .BI FILE =========================
Type BUTTON
   As String  title
   As Integer x
   As Integer y
   As Integer w
   As Integer h
   as ulong   c1  'foreground color
   as ulong   c2  'background color
   As integer v   'visible or not
End Type

Sub drawButton(btn as BUTTON)
   Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),btn.c2, bf
   Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(0,0,0), b
   Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub

'======================= END OF .BI FILE ================================

'INITIALIZE

 Screenres 640,480,32
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim Shared as Integer mx,my,mb

Dim Shared As BUTTON btnQuit

' Quit button
btnQuit.x = 0           ' Window column
btnQuit.y = 0           ' Window row
btnQuit.w = 8  * 6      ' Width of button outline
btnQuit.h = 16 * 6      ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2  = rgb(238,223,204)    'background color
btnQuit.c1  = rgb(0,0,0)          'foreground color

Sub update()
   Screenlock
   Cls
   drawButton(btnQuit)
   Screenunlock
End Sub


dim as integer exitProgram = 0   'flag to exit main loop

Do
   getmouse mx,my,,mb  ' Start mouse
   
   If mb = 1 Then
       
      'Is Mouse down over btnQuit
      If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then
         exitProgram = 1
      End If

      While mb = 1
          getmouse mx,my,,mb
      Wend
      
   End If
   
   update()

   Sleep 2,1
   
Loop until exitProgram=1
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

dasyar wrote:From this point on, I want to keep this as sort of a tutorial style, provide some code, maybe some suggestions or remarks will appear, so somebody that is totally new might get something out of this.
Keep in mind this is a general question section not a section for a personal blog about your progress in learning how to write code.
Indeed there isn't any place in the forum for tutorials as such.
.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I am trying to stay away from making this look like my personal blog, and maybe "tutorial" was the wrong word to use.

The program below, I think has a bug, but for the life of me I can't find it. When you click on the ComboBox and make a selection, it is not displaying the chosen selection. Not sure what is going on.

I guess the next thing to do is make the 'Connection' button active. I will be using all the space below the menu line as text I/O. Will probably keep the 'Quit' button active to end the program. Maybe add a text message box with some options - quit, restart, cancel items. The restart would allow you to change the Port and Baud options.

Code: Select all

' fbtermg.bas
'
' October 26, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif
#include "mygui.bi"

'Screen 20  '1024x768
Screenres 640,480,32
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Integer mx,my,mb



Dim Shared As BUTTON btnConnect, btnQuit
Dim Shared As COMBO_BOX cmbPort, cmbBaud


Declare Sub update()

Dim As Integer hit

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
'btnQuit.c2 = rgb(238,223,204)  ' Button background
'btnQuit.c1 = rgb(0,250,154)        ' Button foreground

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
'btnQuit.c2 = rgb(238,223,204)
'btnQuit.c1 = rgb(0,250,154)

'Port combobox
cmbPort.x = 150
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/TTYUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

'Baud combobox
cmbBaud.x = 345
cmbBaud.y = 0
cmbBaud.w = 8 * 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"


Do
	getmouse mx,my,,mb  ' Start mouse
' Close the window using the "X"	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_WINDOW_CLOSE   ' X in window
			Close
			End
		End Select
	End If
	
	If mb = 1 Then
		hit = 0
' Mouse over btnQuit
		If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then
			btnQuit.a = 1
			btnConnect.a = 0
			cmbBaud.a = 0
			cmbPort.a = 0
			hit = 1
			Close
			End
		End If
		
		If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
			btnQuit.a = 0
			btnConnect.a = 1
			cmbBaud.a = 0
			cmbPort.a = 0
			hit = 1
		End If
		
		If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
		End If
		
		If cmbPort.a = 1 And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.selItem = cmbPort.list((my-cmbPort.y)\16)
			hit =1			
		End If
		
		If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
			cmbBaud.a = 1
			cmbPort.a = 0
			hit = 1
		End If
		
		If cmbBaud.a = 1 And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
			cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y)\16)
			hit =1
		End If
		
		If hit = 0 Then
			cmbPort.a = 0
			cmbBaud.a = 0
		End If
	End If
	
	update()
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
	Sleep 2.1
Loop until multikey(&H01)  ' Esc key to close

''''''''''''''''''''''''''''''
End




' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnConnect)    ' Not active for now
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	Screenunlock
End Sub


mygui.bi

Code: Select all

Type COMBO_BOX
	As String title
	As String List(0 to 4)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b		
		For i as Integer = 0 to 4			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub

Post Reply