Minor improvement in timing and jitter of simulation

r3654 | dcl | 2012-07-19 14:55:45 +1000 (Thu, 19 Jul 2012) | 1 line
This commit is contained in:
Douglas Clowes
2012-07-19 14:55:45 +10:00
parent 87e623c9af
commit 45f27b98cc

View File

@@ -895,9 +895,12 @@ typedef struct device_private_t
{ {
/** extended 64-bit counter value */ /** extended 64-bit counter value */
unsigned long long count64; unsigned long long count64;
/* TODO time and count */ /** count rate to be simulated */
int source; /* count rate to be simulated */ int source;
struct timeval last_time; /** time that count64 was last updated */
struct timeval prev_time;
/** time of most recent read operation */
struct timeval curr_time;
} DEVICE_PRIVATE; } DEVICE_PRIVATE;
int hware_ctor(const char* device_name, pHWARE* ptr) int hware_ctor(const char* device_name, pHWARE* ptr)
@@ -908,56 +911,70 @@ int hware_ctor(const char* device_name, pHWARE* ptr)
*ptr = hware; *ptr = hware;
memset(hware, 0, sizeof(DEVICE_PRIVATE)); memset(hware, 0, sizeof(DEVICE_PRIVATE));
/* TODO time and count */ /* set initital conditions */
hware->source = 1000; /* initial count rate */ hware->source = 1000; /* initial count rate */
gettimeofday(&hware->last_time, NULL); hware->count64 = 0; /* initial counter value */
#if 0 /* DEBUG */ gettimeofday(&hware->curr_time, NULL); /* current time */
printf("hware_ctor: %s at %p\n", device_name, *ptr); hware->prev_time = hware->curr_time; /* update time */
#endif
return 0; return 0;
} }
int hware_read(pHWARE hware, unsigned long long* value) int hware_read(pHWARE hware, unsigned long long* value)
{ {
struct timeval this_time;
double delta_time; double delta_time;
long long delta_count = 0; unsigned long long delta_count = 0;
/* TODO time and count */ unsigned long long total_count = 0;
gettimeofday(&this_time, NULL); /* calculate time since count64 update in microseconds */
delta_time = (this_time.tv_sec + 1e-6 * this_time.tv_usec) - gettimeofday(&hware->curr_time, NULL);
(hware->last_time.tv_sec + 1e-6 * hware->last_time.tv_usec); delta_time = (hware->curr_time.tv_sec + 1e-6 * hware->curr_time.tv_usec) -
delta_count = (long long)(0.5 + 1000 * delta_time * hware->source); (hware->prev_time.tv_sec + 1e-6 * hware->prev_time.tv_usec);
hware->count64 += delta_count; /* calculate counts since then */
hware->last_time = this_time; delta_count = (long long)(0.5 + delta_time * hware->source);
*value = hware->count64 / 1000; /* calculate the new total counts */
total_count = hware->count64 + delta_count;
/* periodically update count64 */
if (delta_time > 60) {
hware->count64 = total_count;
hware->prev_time = hware->curr_time;
}
/* return the total count */
*value = total_count;
return 0; return 0;
} }
/*
* Change the count rate
*/
int hware_source(pHWARE hware, int value) int hware_source(pHWARE hware, int value)
{ {
#if 0 /* DEBUG */ /* calculate the new count rate */
int old_source = hware->source; int new_source;
#endif
switch (value) switch (value)
{ {
case 0: case 0:
hware->source = 10000; new_source = 10000;
break; break;
case 1: case 1:
hware->source = 20000000; new_source = 20000000;
break; break;
case 2: case 2:
hware->source = 100000; new_source = 100000;
break; break;
case 3: case 3:
hware->source = 80000000; new_source = 80000000;
break; break;
default: default:
hware->source = value; new_source = abs(value);
break; break;
} }
#if 0 /* DEBUG */ /* only do this if it has changed */
printf("hware_source: %d moves %d to %d at %p\n", value, old_source, hware->source, hware); if (new_source != hware->source) {
#endif /* save the current count where the slope changes */
hware_read(hware, &hware->count64);
/* save the current time where the slope changes */
hware->prev_time = hware->curr_time;
/* save the new slope */
hware->source = new_source;
}
return 0; return 0;
} }
int hware_filter(pHWARE hware, int value) int hware_filter(pHWARE hware, int value)