Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
166 lines (117 sloc) 3.96 KB
/* -----------------------------------------------------------------------------
BrainBay - Version 1.7, GPL 2003-2010
MODULE: TIMER.CPP
Author: Chris Veigl
This Module provides the basic timing-functions for sample-processing
The TimerProc() - function performs reading from COM-ports or archives and
triggers the worker-functions of all objects in case of an archive read.
The Windows PerformanceCounter-functions are used to retrieve excat system time
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
--------------------------------------------------------------------------------*/
#include "brainBay.h"
#include "neurobit_api\\api.h"
extern TProtocolEngine NdProtocolEngine;
void init_system_time(void)
{
QueryPerformanceFrequency((_LARGE_INTEGER *)&(TIMING.pcfreq));
TTY.packettime=(LONGLONG)(TIMING.pcfreq/PACKETSPERSECOND); // milliseconds for one Packet-Read
TIMING.ppscounter=0;
TIMING.packetcounter=0;
TIMING.dialog_update=0;
TIMING.draw_update=0;
}
void process_packets(void)
{
static double calc;
int t;
TIMING.ppscounter++;
TIMING.packetcounter++;
if (TIMING.dialog_update++ >= GLOBAL.dialog_interval) TIMING.dialog_update=0;
if (TIMING.draw_update++ >= GLOBAL.draw_interval) TIMING.draw_update=0;
if (GLOBAL.session_length && (TIMING.packetcounter>=GLOBAL.session_end))
{
int sav_fly=GLOBAL.fly;
SendMessage(ghWndStatusbox,WM_COMMAND,IDC_STOPSESSION,0);
TIMING.packetcounter= GLOBAL.session_start;
for (t=0;t<GLOBAL.objects;t++) objects[t]->session_pos(TIMING.packetcounter);
if ((!sav_fly)&&(GLOBAL.session_loop) )
{
reset_oscilloscopes();
SendMessage(ghWndStatusbox,WM_COMMAND,IDC_RUNSESSION,0);
}
}
else
{
for (t=0;t<GLOBAL.objects;t++)
if (objects[t]) objects[t]->work();
}
if (!TIMING.dialog_update) update_statusinfo();
}
void CALLBACK TimerProc(UINT uID,UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2)
{
LONGLONG pc;
MSG msg;
QueryPerformanceCounter((_LARGE_INTEGER *)&pc);
//TIMING.acttime=pc;
check_keys();
if (GLOBAL.neurobit_available) NdProtocolEngine();
if ((!TIMING.pause_timer) && (!GLOBAL.loading))
{
// one second passed ? -> update PPS-info
if (pc-TIMING.timestamp >= TIMING.pcfreq)
{ TIMING.timestamp+=TIMING.pcfreq;
TIMING.actpps=(int) TIMING.ppscounter;
TIMING.ppscounter=0;
}
// Reading from Archive & next packet demanded? -> read from File and Process Packets
while ((pc-TIMING.readtimestamp >= TTY.packettime) || (GLOBAL.fly))
{
TIMING.readtimestamp+=TTY.packettime;
TIMING.acttime=TIMING.readtimestamp;
if(CAPTFILE.do_read&&(CAPTFILE.offset<=TIMING.packetcounter)&&(CAPTFILE.offset+CAPTFILE.length>TIMING.packetcounter))
{
long tmp;
tmp=TIMING.packetcounter;
read_captfile(TTY.amount_to_read);
ParseLocalInput(TTY.amount_to_read);
if ((TIMING.packetcounter-tmp-1)>0)
TIMING.readtimestamp+=TTY.packettime*(TIMING.packetcounter-tmp-1);
//return;
}
// process packets in case of no File-Read and no Com-Read
else if ((TTY.read_pause) && (!GLOBAL.neurobit_available)) process_packets();
if (GLOBAL.fly)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
}
void start_timer(void)
{
if (TIMING.timerid) stop_timer();
QueryPerformanceCounter((_LARGE_INTEGER *)&TIMING.timestamp);
TIMING.readtimestamp=TIMING.timestamp;
TIMING.ppscounter=0;
if(!(TIMING.timerid= timeSetEvent(1,0,TimerProc,1,TIME_PERIODIC | TIME_CALLBACK_FUNCTION)))
report_error("Could not set Timer!");
else GLOBAL.running=TRUE;
}
void stop_timer(void)
{
if(TIMING.timerid)
{
if(timeKillEvent(TIMING.timerid)!=TIMERR_NOERROR)
report_error("Could not stop Timer!");
else TIMING.timerid=0;
}
GLOBAL.running=FALSE;
mute_all_midi();
}