User:Andrew Nikitin/StopWatch

From J Wiki
Jump to navigation Jump to search

Stop watch

Stopwatch program can be used to time events during tests or benchmarking. [{{#file: "stopwatch.ijs"}} Download script: stopwatch.ijs ]

NB. stopwatch
NB. 2006-10-18 by nsg
«disclaimer»
«timer»
«form»
«events»
«special»
swf_run ''

Timer methods

Stopwatch uses system clock to count time. Time elapsed (swt_current) is calculated as a difference between current system time and system time when timer was last reset. Global T holds system time of last timer reset.

2 things can be done with the timer -- reset and ask for current timer value.

These methods are independent from user interface and can be used independently even without GUI -- as commands. [{{#file: "timer"}} Download script: timer ]

T=:6!:0 $0
swt_reset=:3 : 'T=:6!:0 $0'
swt_current=:3 : '(6!:0 $0) -&(24 60 60 #. _3 {. ]) T'

WARNING: swt_current will report incorrect value if timer rolls over day boundary. It's not an issue so far, but may be for somebody. [{{#file: "disclaimer"}} Download script: disclaimer ]

NB. Timer may return incorrect values when it rolls over day boundary.

Form methods

Form methods (as opposed to event handlers) have swf_ prefix.

Stopwatch-form.png [{{#file: "form"}} Download script: form ]

SW=: 0 : 0
pc sw nomax;
xywh 0 0 60 10;cc td static rightmove ss_center;cn "--:--:--.--";
xywh 0 10 30 10;cc tr button rightscale;cn "&Reset";
xywh 30 10 30 10;cc ts button leftscale rightmove;cn "&Split";
xywh 0 20 60 51;cc tl editm ws_vscroll es_autovscroll rightmove bottommove;
pas 0 0;
rem form end;
)

(tl has height 51 so it can fit exactly 6 lines at my screen resolution)

Form is initialized as being not "always on top", located in the center and timer display is updated every 100 ms. Timer update setting affects only the refresh rate of a running timer value and does not affect timer accuracy or resolution. [{{#file: "form"}} Download script: form ]

swf_run=: 3 : 0
wd SW,'pcenter;ptop 0;pshow;timer 100;'
tl=:''
swt_reset ''
)

Update display

swf_display updates timer display, but does not add anything to timer log. It may be called in sys_timer event if visual clue is desired. [{{#file: "form"}} Download script: form ]

swf_display=: 3 : 0
  wd 'set td *',0j1":swt_current''
)

Here is altenative version of it. In addition to updating display it also audible warning when counted time is close to some predetermined value (PREWARNING). It keeps playing warning after that in increasing intervals (detemined by EXTRA).

We used it when we needed to measure long time intervals (≈10 min) precisely, but did not want to sit and wait in front of a monitor. This way it would give a warning when it is about time to start watching closely and ready to push "Split" button.

swf_display=: 3 : 0
  if. (PREWARNING+EXTRA)<t=.swt_current'' do.
     wd 'winexec "sndrec32.exe /play /close C:\nsg\misc\wav\samples\bark.wav"'
     EXTRA=:EXTRA*2
  end.
  wd 'set td *',0j1":t
)

This approach also needs custom reset function that initializes new variables.

swt_reset=:3 : 0
  PREWARNING=:500
  EXTRA=:5
  T=:6!:0 $0
)

swf_log adds arbitrary text y to a new line of timer log (tl, edit box). setselect keeps latest entry in view. [{{#file: "form"}} Download script: form ]

swf_log=:3 : 0
  wd 'set tl *',t=.tl,y,~CRLF#~0<#tl
  wd 'setselect tl ',":2j1##t
)

Timer controls

Timer controls are connected to elements of user interface: buttons and keystrokes.

Reset timer and writes 0 + optional y to the log. [{{#file: "form"}} Download script: form ]

swf_reset=: 3 : 'swf_log ''0'',y [ swt_reset $0'

Write current value of running timer + optional y to the log. Timer keeps running. [{{#file: "form"}} Download script: form ]

swf_split=: 3 : 'swf_log (": swt_current 0),y'

Events

Close event stops system timer to stop timer refreshes. [{{#file: "events"}} Download script: events ]

sw_close=: wd bind 'timer 0;pclose;'

It may be useful to give visual clue that the timer is running, but if it turns out that because of the frequent display update system slowed down enough to cause interference with the phenomenon being measured, then sys_timer can be set to ], or, as alternative timer 0 command may be issued at form initialization. [{{#file: "events"}} Download script: events ]

sys_timer=:swf_display

2 GUI buttons control reset and split. [{{#file: "events"}} Download script: events ]

sw_tr_button=:swf_reset
sw_ts_button=:swf_split

Additional key handlers may be redefined for ease of access. For example pair of F1,F2 keys can be used if left hand is free during test and F11,F12 can be used if right hand is free during the test.

Different key handlers may be associated with different text labels to differentiate between flavours of events. For example F2 may be used for click and F3 may be used for clack, in this case text labels will provide information for further analysis. [{{#file: "events"}} Download script: events ]

sw_f1_fkey=: swf_reset
sw_f11_fkey=: swf_reset
sw_f2_fkey=: swf_split bind ' F2'
sw_f12_fkey=: swf_split bind ' F12'

Special calculations

The following functions are not of general interest. They are custom tailored to our specific measurement needs. They also may be useful as an illustration of how to implement custom commands.

Press F7 every time you hear a pulse. It calculates time interval from previous F7 and displays result in RPM units.

F8 is for faster rates. It is supposed to be pressed every 10-th pulse. Also it does not reset timer, so it is supposed to be used with one of the reset (F1 or F11) keys. Press F1 when you hear first pulse, count pulses and after 10 intevals (on 11-th) pulse press F8. It will display frequency in RPM. [{{#file: "special"}} Download script: special ]

sw_f7_fkey=: 3 : 0
  t=.swt_current 0
  swf_log '60/',(0j2": t),'=',0j2":60%t
  swt_reset''
)
sw_f8_fkey=: 3 : 0
  t=.swt_current 0
  swf_log '600/',(0j2": t),'=',0j2":600%t
)

Contributed by Andrew Nikitin


CategoryLiterate CategoryWorkInProgress