Code archives/Miscellaneous/Callback Timer

This code has been declared by its author to be Public Domain code.

Download source code

Callback Timer by Kryzon2014
This is a simple modification of the cross-platform timers present in the BRL.Timer module. Save the source code as "TCallbackTimer.bmx" or similar and then 'Import' this file into your programs.

For this to work, you need to modify your BRL.timer.mod module (located in BlitzMax/mod/brl.mod/timer.mod/timer.bmx). Change the "timer" parameters of the Extern functions from "TTimer" to "Object" type, like so:

Extern 'OLD.
Function bbTimerStart( hertz#,timer:TTimer )
Function bbTimerStop( handle,timer:TTimer )
End Extern
Extern 'NEW
Function bbTimerStart( hertz#,timer:Object )
Function bbTimerStop( handle,timer:Object ) 'Changed to Object type.
End Extern
Then rebuild your modules.

Instead of emitting an event, a "callback timer" calls a BlitzMax function that you specify.
This is useful if you have a function that you would like to be called with a certain frequency, but you don't want to keep polling time and calling it manually.
All of it is done by the OS and BlitzMax.

The callback function that you create cannot return a value, but it can accept a parameter 'data:Object.' You can use this to transfer arbitrary data to the function (a string or a custom type object).

You can stop (and consequently free) the timer whenever you want, and you can change the callback function or the data argument while the timer is active.

There's a practical example in the first comment at the bottom.
Import BRL.Timer

Type TCallbackTimer

	Method Ticks:Int()
		Return _ticks
	End Method
	
	Method Stop()
		If Not _handle Return
		bbTimerStop _handle,Self
		_handle=0
		_func=Null
		_data=Null
	End Method
	
	Method Fire()		
		If Not _handle Return
		_ticks:+1
		
		'Invoke the function with the data argument.
		
		_func( _data )		
	End Method

	Method Wait:Int()
		If Not _handle Return -1
		Local n:Int
		Repeat
			WaitSystem
			n=_ticks-_wticks
		Until n
		_wticks:+n
		Return n
	End Method
	
	Method SetCallback( newFunc( data:Object ) )
		_func = newFunc
	End Method

	Method SetData( newData:Object )
		_data = newData
	End Method

	Method GetData:Object()
		Return _data		
	End Method
	
	Function Create:TCallbackTimer( hertz#, func( data:Object ), newData:Object = Null )
		Local t:TCallbackTimer=New TCallbackTimer
		Local handle:Int=bbTimerStart( hertz,t )
		If Not handle Return Null
		t._func=func
		t._handle=handle
		t._data=newData
		Return t
	End Function

	Field _ticks:Int
	Field _wticks:Int
	Field _func( data:Object )
	Field _handle:Int
	Field _data:Object

End Type

Function CreateCallbackTimer:TCallbackTimer( hertz#, func( data:Object ), newData:Object = Null )
	Return TCallbackTimer.Create( hertz, func, newData )
End Function

Function SetTimerCallback( cTimer:TCallbackTimer, func( data:Object ) )
	cTimer.SetCallback( func )
End Function

Function SetTimerData( cTimer:TCallbackTimer, newData:Object = Null )
	cTimer.SetData( newData )
End Function

Function GetTimerData:Object( cTimer:TCallbackTimer )
	Return cTimer.GetData()
End Function

Function WaitCallbackTimer:Int( cTimer:TCallbackTimer )
	Return cTimer.Wait()
End Function

Function StopCallbackTimer( cTimer:TCallbackTimer )
	cTimer.Stop()
End Function

Comments

Kryzon2014
EXAMPLE:




Code Archives Forum