C: C++ timers: Difference between revisions

From FSDeveloper Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 8: Line 8:




Most of you may be familiar with the TICK18 token variable which is incremented at 18Hz. Another option to make use of the 18Hz increment system (like making 0.5 second timers and such) is to call a function from the PANEL_SERVICE_PRE_UPDATE case of your callback function which increments your timing variable which you simply reset to 0 when it is equal to 9 (for a 0.5 second timer). However there is another option that allows for timers that range from several minutes down to 1 millisecond.  
Most of you may be familiar with the TICK18 token variable which is incremented at 18Hz. Another option to make use of the 18Hz increment system (like making 0.5 second timers and such) is to call a function from the PANEL_SERVICE_PRE_UPDATE case of your callback function which increments your timing variable which you simply reset to 0 when it is equal to 9 (for a 0.5 second timer).  


#include windows.h
The following however is an undocumented function in the simulator which returns total time elapsed in seconds with sub-18Hz accuracy.


void CALLBACK My_Timer();
In your choice of your project's header files, do the following function prototype:
UINT_PTR IDT_TIMER1;


The above code creates the function prototype for the callback function which will be executed at the desired interval. It also creates the timer ID variable.
typedef double (__cdecl *fGetSimTime)();


To create the timer in your gauge callback function:
Then in your module_init function:
 
  case PANEL_SERVICE_PRE_INITIALIZE:
  HMODULE hsimsched = NULL;
  SetTimer(NULL, IDT_TIMER1, 1000, (TIMERPROC)My_Timer);
  fGetSimTime GetSimTime6 = NULL;
  //1st parameter is optional and not required in this
void FSAPI module_init(void)
//case, second parameter is timer ID, third parameter is timer interval in milliseconds, 4th parameter is callback
  {
//function
  GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
break;
  if(hsimsched)
 
  GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));
Once you are finished with the timer, remove it as shown below:
}


case PANEL_SERVICE_DISCONNECT:
Now wherever you wish to call this function, include the header where the prototype is contained and add:
KillTimer(NULL, IDT_TIMER1);
break;


To increment your timer variable:
extern fGetSimTime GetSimTime6;


int timer_1sec = 0;
To call it and find out how much time has passed:
void CALLBACK My_Timer()
{
timer ++;
}


double time = GetSimTime6();
[[Category:Aircraft Design]]
[[Category:Aircraft Design]]
[[Category:Panel and Gauge Design]]
[[Category:Panel and Gauge Design]]

Revision as of 07:39, 15 March 2015


Most of you may be familiar with the TICK18 token variable which is incremented at 18Hz. Another option to make use of the 18Hz increment system (like making 0.5 second timers and such) is to call a function from the PANEL_SERVICE_PRE_UPDATE case of your callback function which increments your timing variable which you simply reset to 0 when it is equal to 9 (for a 0.5 second timer).

The following however is an undocumented function in the simulator which returns total time elapsed in seconds with sub-18Hz accuracy.

In your choice of your project's header files, do the following function prototype:

typedef double (__cdecl *fGetSimTime)();

Then in your module_init function:

HMODULE hsimsched = NULL;
fGetSimTime GetSimTime6 = NULL;
void FSAPI module_init(void)
{
 GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
 if(hsimsched)
  GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));
}

Now wherever you wish to call this function, include the header where the prototype is contained and add:

extern fGetSimTime GetSimTime6;

To call it and find out how much time has passed:

double time = GetSimTime6();