PDA

View Full Version : Counter



oldpapa49
16-01-2013, 16:41
Hey all,

I made a real short UI with console display.

It displays a Left, Center, Right buttons (inactive, just a box)

I used a control add label to place the count number under the button (like Left). But as it counted to like a 1000, it went nuts. I'm sure what I did is using memory up big time.

Just a simple display for counting.

I have the buttons disabled.. and now just showing the count in the console.



Case "LEFT"
Control Enable hDlg, %ID_Button_01
button1 = button1 + 1
'If button1 = 10 Then Control Add Label hDlg, %ID_Button_20, "Reached Count "+ button1 ,_
' 10, 90, 100, 8, %SS_CENTER
' Control Add Label hDlg, %ID_Button_20, "Count is "+ button1,_
' 10, 70, 40, 8, %SS_CENTER
Console_PrintLine button1
Sleep 10
'button1 = button1 + 1
Control Disable hDlg, %ID_Button_01


I rem the statement out above

Maybe add label is the not the way to go..

Michael Clease
16-01-2013, 18:41
Sleep in UI is not a good idea you could have a message queue that is quite large.

What type of variable is button1?

oldpapa49
16-01-2013, 19:38
button1 is the value of counts. dword

I used the sleep as a debounce for the input.

The input is serial and the signal is "LEFT". The output is from an Adruino 2560 board. It watches a few pins for a LOW and sends LEFT (Currently only left, there are center and right)

Every time "LEFT" is recv, I do the select and count button1 up 1

Then I try to display it to the UI screen. Console worked great.. Just I lack the knowledge of printing info on the UI screen.

But it almost worked :)

Michael Clease
16-01-2013, 21:21
Without seeing the whole UI routine its difficult to see any problems but the code you have posted looks perfect to me.

A couple of questions :

Are you using callbacks?
Is the main window Modal or Modeless?
are you using timers?

Mike C.

oldpapa49
16-01-2013, 23:07
Here ya go, you will see I copied most from other programs that I have worked on.. easier to make.. rather faster :)



' Empty GUI script created on 10-19-2012 07:19:11 by (ThinAIR)
' Footpedal Counter
' Program by W Little and Others
' 01/14/2013 rev . 03
' This program works with Arduino 2560
' It will read via the USB as serial and Left Footpedal presses
'

' Screen info available to user
' This Program would not be possible if it
' where not the HELP from thinBasic Forum
' www.community.thinbasic.com GURU's
' Petr Schreiber, Eros Olmi,
' LionHeart008, Michael Clease and many
' other hero's behind the screen!
' Kudo's to them ALL!
' W Little wlfredl@yahoo.com
'
'--------------------------------------------
'
'--------------------------------------------
Uses "UI"
Uses "CONSOLE"
Uses "COMM"
Console_WriteLine "Here now"
Dim hDLG As DWord
'---------------------------------
Dim reList(20) As DWord
'--------------------------------- G
' I Know, Time to Clean Soon!
Dim Win1Params As DWord Value = %WS_POPUP Or %WS_VISIBLE Or %WS_CLIPCHILDREN Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX Or _
%WS_THICKFRAME
Dim hComm As Long
Dim ID_List01 As Long
Dim MaxItems As Long
Dim nBytes As Long
Dim value2 As String
Dim value3 As DWord
Dim sBuffer As String
Dim MaxComm As DWord = 8'Change me to increase vList
Dim vList(MaxComm) As String
Dim CurItem As Long
' RS232 stuff
Dim commport As String ' Select from main menu
Dim COMMDATA As DWord ' 0 to 255 dec. Input from arrow or up down buttons
Dim commcode As DWord ' 0 to 15, 40 (64 hex), 41 (65 hex) to rs232
Dim tmpStr As String
Dim commstr As String
Dim button1 As DWord = 0
' code sent to rs232 for adj should be commcode, commdata

Begin ControlID

%ID_Button_00 'Brightness
%ID_Button_01 'Left
%ID_Button_02 'Right
%ID_Button_03 'Center
%ID_Button_04 'Reserve
%ID_Button_05 'Reserve
%ID_Button_06 'Reserve
%ID_Button_07 'Not Used
%ID_Button_08 'Not Used
%ID_Button_09 'Reserve
%ID_Button_10 'Reserve
%ID_Button_11 'Reserve
%ID_Button_12 'Reserve
%ID_Button_13 'Reserve
%ID_Button_14 'Reserve
%ID_Button_15 'Reserve
%ID_Button_16 'Reserve
%ID_Button_17 'Reserve
%ID_Button_18 'Reserve
%ID_Button_19 'Reserve
%ID_Button_20 'Count Label
%ID_Button_21 'Reserve
%ID_BUTTON_22 'Set



'----------------------------------------
%Text01
%List01
%Butt01
%ID_HELP_LABEL
%CtrlRes
%MsgText
%HelpText
%helpText2
%CtrlRes2
%ButtonClose

%mainTimer
End ControlID

' New Code




' If you want more Com ports, change MaxElements DIM statement
Long i

For i = 1 To UBound(vList)
vList(i) = "Com" + Format$(i)
Next

commport = "Com4"
commcode = "-1"
' commdata = 127 'midrange start
commstr = "NO Function Selected"


hComm = COMM_FreeFile
COMM_Open(commport, hComm)
COMM_Set(hComm, %COMM_BAUD, 57600)

Function TBMain( ) As Long

Dialog New 0, "Foot Pedal Test (Rv .03) Default Com4 Default Baud 57600", -1, -1, 320, 130, Win1Params, 0 To hDlg
Control Add Label, hDlg, %HelpText,"Selected Commport is " , 3, 1, 70,12
'Control Add Label, hDlg, %HelpText,"Selected Commport is ", commport, 3, 1, 150, 12

'Control Add Textbox , hDlg, %CtrlRes, commdata, 129, 250, 30, 15, Win1Params

Control Add Button hDlg, %ButtonClose, "Click to kill", 280, 0, 40, 25
Control Add Button, hDlg, %ID_Button_22, "Set" , 72,15, 20, 12, %WS_TABSTOP | %BS_DEFAULT, Call cbCheckButtonWin

Control Add Button hDlg, %ID_Button_01, "LEFT" , 20, 60, 40, 25, %BS_CENTER | %WS_TABSTOP | %BS_MULTILINE
'Control Disable hDlg, %ID_Button_01

Control Add Button hDlg, %ID_Button_02, "Right" , 240, 60, 40, 25, %BS_CENTER | %WS_TABSTOP | %BS_MULTILINE
Control Disable hDlg, %ID_Button_02

Control Add Button hDlg, %ID_Button_03, "Center" , 120, 60, 40, 25, %BS_CENTER | %WS_TABSTOP | %BS_MULTILINE
Control Disable hDlg, %ID_Button_03

'Control Add Button hDlg, %ID_Button_04, "Fluoflavor 2" , 440, 40, 40, 25, %BS_CENTER | %WS_TABSTOP | %BS_MULTILINE
'Control Disable hDlg, %ID_Button_04

'Control Add Button hDlg, %ID_Button_05, "Fluoflavor 3" , 500, 40, 40, 25, %BS_CENTER | %WS_TABSTOP | %BS_MULTILINE
'Control Disable hDlg, %ID_Button_05

'Control Add Button, hDlg, %ID_Button_06, "Image Grab" , 20, 80, 40, 25,%BS_CENTER| %WS_TABSTOP | %BS_MULTILINE
' Control Disable hDlg, %ID_Button_06

'Control Add Button, hDlg, %ID_Button_07, "Not Used" , 140, 80, 40, 25,%BS_CENTER| %WS_TABSTOP | %BS_DEFAULT
'Control Disable hDlg, %ID_Button_07

'Control Add Button, hDlg, %ID_Button_08, "Not Used" , 200, 80, 40, 25,%BS_CENTER| %WS_TABSTOP | %BS_MULTILINE
'Control Disable hDlg, %ID_Button_08

'Control Add Button, hDlg, %ID_Button_09, "Reset Buzzer" , 500, 80, 40, 25,%BS_CENTER| %WS_TABSTOP | %BS_MULTILINE
'Control Disable hDlg, %ID_Button_09






'---Set window minimum size
Dialog Set Minsize hDlg, 560,320

Dialog Show Modal hDlg, Call cbDialog

End Function

CallBack Function cbDialog () As Long

Select Case CBMSG
' -- When program starts, we start timer
Case %WM_INITDIALOG
Dialog Set Timer CBHNDL, %mainTimer, 1

Control Add Label CBHNDL, %CtrlRes, " ", 73,1,30,15, %SS_LEFT
Control Add Label CBHNDL, %CtrlRes2, "", 150, 1, 100, 28, %SS_CENTER

Control Add LISTBOX , CBHNDL, %List01 , vList( ), 4, 30, 40, 40, 0, 0
Control Add Textbox , CBHNDL, %Text01 , vList( 1 ), 4, 15, 30, 12
Control Add Button , CBHNDL, %Butt01 , "Select" , 40, 15, 30, 12



Case %WM_COMMAND
If CBCTL = %ButtonClose And CBCTLMSG = %BN_CLICKED Then
Dialog End CBHNDL

End If
Select Case CBCTL
'---Something has happened with %List01
Case %List01

Select Case CBCTLMSG
Case %LBN_SELCHANGE
LISTBOX Get Text CBHNDL, %List01 To tmpStr
Control Set Text CBHNDL, %Text01, tmpStr

End Select
Case %Butt01

'---Get the text from a textbox and place into a string
Control Get Text CBHNDL, %Text01 To tmpStr
commport = tmpStr
'---Get the window unique ID of the control
Control Handle CBHNDL, %List01 To ID_List01

'---Get the number of items present into listbox
MaxItems = SendMessage( ID_List01, %LB_GETCOUNT, 0, 0 )

'---Get current selected item (remember it start at zero position)
'CurItem = sendmessage(ID_List01, %LB_GETCURSEL, 0, 0) + 1
LISTBOX Get Selected CBHNDL, %List01 To CurItem

'---Now delete selected item ...
SendMessage( ID_List01, %LB_DELETESTRING, CurItem - 1, 0 )

'---...and insert new text in same position
SendMessage( ID_List01, %LB_INSERTSTRING, CurItem - 1, StrPtr( tmpStr ))

End Select

' -- Once timer ticks
Case %WM_TIMER
If CBCTL = %mainTimer Then

nBytes = COMM_Get(hComm, %COMM_RXQUE)

COMM_Recv(hComm, nBytes, sBuffer)
Sleep 150
value2 = LEFT$(sBuffer, nBytes)
'value2 = sBuffer
If Asc(value2) = 0 Then Exit Function
If Asc(value2) > 0 Then Call displayBox
value3 = DIGIT$(value2)
'Console_Print value2
End If

' -- Shutdown of the timer
Case %WM_DESTROY
Dialog Kill Timer CBHNDL, %mainTimer

End Select

End Function


' Procedure for handling value3
'------------------------------------------------------------------------------


Function displayBox( ) As Long


Select Case value3

'Case 0430015600
' Control Enable hDlg, %ID_Button_06
' Sleep 500
' Control Disable hDlg, %ID_Button_06

Case "LEFT"
Control Enable hDlg, %ID_Button_01
button1 = button1 + 1
'If button1 = 10 Then Control Add Label hDlg, %ID_Button_20, "Reached Count "+ button1 ,_
' 10, 90, 100, 8, %SS_CENTER
' Control Add Label hDlg, %ID_Button_20, "Count is "+ button1,_
' 10, 70, 40, 8, %SS_CENTER
Console_PrintLine button1
Sleep 10
'button1 = button1 + 1
Control Disable hDlg, %ID_Button_01

Case "OFF"

Control Disable hDlg, %ID_Button_01

' Case 1606032704
' Control Enable hDlg, %ID_Button_02
' Sleep 500
' Control Disable hDlg, %ID_Button_02

' Case 2345309936
' Control Enable hDlg, %ID_Button_03
' Sleep 500
' Control Disable hDlg, %ID_Button_03

' Case 2355309936
' Control Enable hDlg, %ID_Button_04
' Sleep 500
' Control Disable hDlg, %ID_Button_04

' Case 2365309936
' Control Enable hDlg, %ID_Button_05
' Sleep 500
' Control Disable hDlg, %ID_Button_05

' Case 0490015600
' Control Enable hDlg, %ID_Button_09
' Sleep 500
' Control Disable hDlg, %ID_Button_09

'Case 4110015600
' Control Enable hDlg, %ID_Button_07
' Sleep 500
' Control Disable hDlg, %ID_Button_07

'Case 4110015600
' Control Enable hDlg, %ID_Button_08
' Sleep 500
' Control Disable hDlg, %ID_Button_08


End Select

End Function

CallBack Function cbCheckButtonWin( ) As Long 'commdata display between up/dn/left/right

' -- Text assignment moved to end of this routine

'--------------------------------------------------------------------------
If CBMSG = %WM_COMMAND Then 'This selects function and acts upon it
If CBCTLMSG = %BN_CLICKED Then
Select Case CBCTL

Case %ID_Button_22 ' Test Com
Call cbGetCode
End Select
End If
End If
End Function

CallBack Function cbGetCode() As Long
COMM_Close(hcomm)
Console_WriteLine commport
Control Set Text hDlg, %CtrlRes, commport
COMM_Open(commport, hComm)
COMM_Set(hComm, %COMM_BAUD, 57600)

End Function



'You Have Reach the Bottom, there is no other place to go other than UP!

Michael Clease
17-01-2013, 00:40
I'm not sure whats happening but here some things I noticed.

You call displaybox before setting up the value3 variable.

If Asc(value2) > 0 Then Call displayBox
value3 = DIGIT$(value2)


Value3 is a DWORD but you are testing it for ASCII text ?? and you do not reset the button1 counter.


Select Case value3
Case "LEFT"


This should result in an extra 150ish calls while you are asleep


Dialog Set Timer CBHNDL, %mainTimer, 1
.
.
.
' -- Once timer ticks
Case %WM_TIMER
If CBCTL = %mainTimer Then

nBytes = COMM_Get(hComm, %COMM_RXQUE)

COMM_Recv(hComm, nBytes, sBuffer)
Sleep 150



Try this code and see what results you get it shows an important thing about timers and why you should kill the timer while processing it. Play with %SleepTime to see what happens.


Uses "UI"' -- ID numbers of controls
Begin ControlID
%lTimer1
%lTimer2
%lTimer3
%Timer1
%Timer2
%Timer3
End ControlID


Begin Const
%MAIN_WIDTH = 320
%MAIN_HEIGHT = 240
%Time = 1
%SleepTime = 500
End Const
Global Counter1 As DWord
Global Counter2 As DWord
Global Counter3 As DWord


' -- Create dialog here
Function TBMain()
Local hDlg As DWord


Dialog New Pixels, 0, "<Enter title here>",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Dialog Show Modal hDlg, Call cbDialog


End Function


' -- Callback for dialog
CallBack Function cbDialog()


' -- Test for messages
Select Case CBMSG


Case %WM_INITDIALOG
' -- Place controls here
Control Add Label, CBHNDL, %lTimer1, "", 10,10, 100, 25
Dialog Set Timer CBHNDL, %Timer1 , %Time
Control Add Label, CBHNDL, %lTimer2, "", 10,40, 100, 25
Dialog Set Timer CBHNDL, %Timer2 , %Time
Control Add Label, CBHNDL, %lTimer3, "", 10,80, 100, 25
Dialog Set Timer CBHNDL, %Timer3 , %Time


Case %WM_TIMER


Select Case CBCTL

Case %Timer1
Dialog Kill Timer CBHNDL, %Timer1
Incr Counter1
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer1,Counter1
Dialog Set Timer CBHNDL, %Timer1, %Time
Case %Timer2
Incr counter2
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer2,Counter2
Case %Timer3
Dialog Kill Timer CBHNDL, %Timer3
Incr Counter3
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer3,Counter3
Dialog Set Timer CBHNDL, %Timer3, %Time

End Select


Case %WM_CLOSE
Dialog Kill Timer CBHNDL, %Timer1
Dialog Kill Timer CBHNDL, %Timer2
Dialog Kill Timer CBHNDL, %Timer3
Dialog End CBHNDL


End Select


End Function

oldpapa49
17-01-2013, 15:53
Thanks Sir,

I will give this a go today and report back..

oldpapa49
17-01-2013, 20:30
Michael,

ok.. I got so lost on the program and at work doing other things, ...

Well the 1st part

If Asc(value2) > 0 Then Call displayBox
value3 = DIGIT$(value2)

All I have to say is oops..value2 is still a string, but I tested value3 because it worked.. must of been a second pass that got the value3 with something..

Maybe I should explain how this is connected.

If you recall my earlier project for the TSO, I connected the TSO to a SparkLan Can-Bus shield to an Adruino UNO board. The software on the UNO would detect can-bus data and send it serially to the PC via USB on what ever comport it was on (normally on my PC it was 4).

Then thinBasic was used to sniff comport for this code. I determined all codes of switches, analog signals, etc and used the case to turn on and off the buttons on my UI screen. This is just to show it was pressed on the TSO and the UI buttons had not action on the UI screen at all.

I also happen to find out that the button on the TSO would have an enable and disable code. I did do sleeps prior to this on all buttons to turn them off. But I then added disable to the case that related to the depress button TSO when it was released..

I then learned that of all the different TSO's, the functions of the buttons had the same code relation (enable and disable). Thus I decided instead of making a program / TSO, I made 1 big play field of buttons for all TSO's.

Being LAZY on my part, I grabbed the old OEC monitor program that you guys helped me with (And it is still in use today BTW), and used that as a template for the TSO program. I stripped and added and got it working and working really good, again thanks on that.

Now, comes to the counter.. So the TSO was used as a template for the counter. So I stripped and made it to test 3 switches at the beginning, but it went to test only one.

Since I had the UI, I just wanted to read the closure of the switch (code sent via serial from a Arduino Mega 2560 board now) as LEFT (RIGHT, CENTER never gets sent but is programmed and set).
I wanted to send the count of presses to the UI via Add Label. This program was to test the switch for 200K closure for documentation. But it went haywire at around 1000 presses.
I tried a few things and it got worst, so for the last part I just display the UI and console console_print showed the count only..This has worked and they finished that test. More to come since what we do is all medical related things have to be twisted, turned and flipped thousands of time.

Now your up to speed..

Select Case value3
Case "LEFT"

Dunno what happened, but it did work.. Like I said, maybe after the second pass..

Dialog Set Timer CBHNDL, %mainTimer, 1.
.
.
' -- Once timer ticks
Case %WM_TIMER
If CBCTL = %mainTimer Then

nBytes = COMM_Get(hComm, %COMM_RXQUE)

COMM_Recv(hComm, nBytes, sBuffer)
Sleep 150

I stuck it there for switch debounce.. Maybe wrong place...
But I had better counts when added..

Now you know the story..

:)