From c59a3979be6a32f3f80ffe806c5d3388b0104ecf Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Wed, 11 Jul 2012 15:08:50 +1000 Subject: [PATCH] Implement simulated counter hardware and more string literal changes r3637 | dcl | 2012-07-11 15:08:50 +1000 (Wed, 11 Jul 2012) | 1 line --- site_ansto/hardsup/Monitor/Makefile | 7 ++ site_ansto/hardsup/Monitor/hware.c | 109 ++++++++++++++++++++++++++++ site_ansto/hardsup/Monitor/params.c | 8 +- site_ansto/hardsup/Monitor/params.h | 6 +- 4 files changed, 124 insertions(+), 6 deletions(-) diff --git a/site_ansto/hardsup/Monitor/Makefile b/site_ansto/hardsup/Monitor/Makefile index 638a7500..f3dc908b 100644 --- a/site_ansto/hardsup/Monitor/Makefile +++ b/site_ansto/hardsup/Monitor/Makefile @@ -8,6 +8,8 @@ LDFLAGS += -g TARGETS = Monitor OBJS = Monitor.o params.o utility.o sock.o display.o device.o hware.o +ifeq ($(origin simulate_hardware),undefined) +$(info "Simulate Hardware is not defined") ifeq ($(wildcard /usr/local/lib/libnidaqmx.so),) OBJECTS = LIBS = @@ -26,6 +28,11 @@ else LIBS = nidaqmx LIBFLAGS = $(foreach lib,$(LIBS),-l$(lib)) endif +else +$(info "Simulate Hardware is from $(origin simulate_hardware)") +$(warning "Counter Hardware will be simulated in this build") +CXXFLAGS += -DSIMULATE_HARDWARE=1 +endif all: $(TARGETS) diff --git a/site_ansto/hardsup/Monitor/hware.c b/site_ansto/hardsup/Monitor/hware.c index a19b2fa4..58802f7d 100644 --- a/site_ansto/hardsup/Monitor/hware.c +++ b/site_ansto/hardsup/Monitor/hware.c @@ -7,6 +7,7 @@ #include #include +#ifndef SIMULATE_HARDWARE #define DEFAULT_DEVICE "dev1/ctr0" #ifdef REGISTER_LEVEL_PROGRAMMING @@ -888,3 +889,111 @@ void initChan(pHWARE hware) } #else #endif +#else /* SIMULATE_HARDWARE */ +#include +typedef struct device_private_t +{ + /** extended 64-bit counter value */ + unsigned long long count64; + /* TODO time and count */ + int source; /* count rate to be simulated */ + struct timeval last_time; +} DEVICE_PRIVATE; + +int hware_ctor(const char* device_name, pHWARE* ptr) +{ + pHWARE hware = NULL; + + hware = (DEVICE_PRIVATE*) malloc(sizeof(DEVICE_PRIVATE)); + *ptr = hware; + memset(hware, 0, sizeof(DEVICE_PRIVATE)); + + /* TODO time and count */ + hware->source = 1000; /* initial count rate */ + gettimeofday(&hware->last_time, NULL); +#if 0 /* DEBUG */ + printf("hware_ctor: %s at %p\n", device_name, *ptr); +#endif + return 0; +} + +int hware_read(pHWARE hware, unsigned long long* value) +{ + struct timeval this_time; + double delta_time; + long long delta_count = 0; + /* TODO time and count */ + gettimeofday(&this_time, NULL); + delta_time = (this_time.tv_sec + 1e-6 * this_time.tv_usec) - + (hware->last_time.tv_sec + 1e-6 * hware->last_time.tv_usec); + delta_count = (long long)(0.5 + delta_time * hware->source); + hware->count64 += delta_count; + hware->last_time = this_time; + *value = hware->count64; + return 0; +} +int hware_source(pHWARE hware, int value) +{ +#if 0 /* DEBUG */ + int old_source = hware->source; +#endif + switch (value) + { + case 0: + hware->source = 10000; + break; + case 1: + hware->source = 20000000; + break; + case 2: + hware->source = 100000; + break; + case 3: + hware->source = 80000000; + break; + default: + if (value < 1000) + hware->source = 1000; + else + hware->source = value; + break; + } +#if 0 /* DEBUG */ + printf("hware_source: %d moves %d to %d at %p\n", value, old_source, hware->source, hware); +#endif + return 0; +} +int hware_filter(pHWARE hware, int value) +{ + /* nothing to do */ + return 0; +} +int hware_outp(pHWARE hware, int value) +{ + /* nothing to do */ + return 0; +} +void hware_sync(pHWARE hware, bool external) +{ + /* nothing to do */ +} +int hware_dtor(pHWARE* ptr) +{ + if (ptr && *ptr) + { + /* release the storage */ + free(*ptr); + *ptr = NULL; + } + return 0; +} +bool hware_failed(int error) +{ + return false; +} +void hware_errmsg(char* buff, int len) +{ + if (buff) + strncpy(buff, "Simulated error message", len); +} +#endif diff --git a/site_ansto/hardsup/Monitor/params.c b/site_ansto/hardsup/Monitor/params.c index 98cb2603..3d8aef2c 100644 --- a/site_ansto/hardsup/Monitor/params.c +++ b/site_ansto/hardsup/Monitor/params.c @@ -61,7 +61,7 @@ static struct param_command_t { }; #define NUM_CMDS ((int) ((sizeof(param_command)/sizeof(param_command[0])))) -static int name_to_command(char* name) +static int name_to_command(const char* name) { int i; for (i = 0; i < NUM_CMDS; ++i) { @@ -73,7 +73,7 @@ static int name_to_command(char* name) return -1; } -bool param_set(pPARAMETERS pp, char* name, char* value) +bool param_set(pPARAMETERS pp, const char* name, const char* value) { bool result = false; switch (name_to_command(name)) @@ -190,8 +190,10 @@ bool param_set(pPARAMETERS pp, char* name, char* value) pp->source = strtol(value, NULL, 10); if (pp->source < 1) pp->source = 0; +#ifndef SIMULATE_HARDWARE else if (pp->source > 3) pp->source = 0; +#endif dbg_printf(0, "=>%d\n", pp->source); break; case CMD_FILTER: @@ -243,7 +245,7 @@ bool param_set(pPARAMETERS pp, char* name, char* value) return result; } -bool param_set_cmd(pPARAMETERS pp, char* cmd) +bool param_set_cmd(pPARAMETERS pp, const char* cmd) { char name[100]; char value[100]; diff --git a/site_ansto/hardsup/Monitor/params.h b/site_ansto/hardsup/Monitor/params.h index fbd2bb01..81b64f70 100644 --- a/site_ansto/hardsup/Monitor/params.h +++ b/site_ansto/hardsup/Monitor/params.h @@ -48,7 +48,7 @@ typedef struct parameter_t } PARAMETERS, *pPARAMETERS; -bool param_set(pPARAMETERS pp, char* name, char* value); -bool param_set_cmd(pPARAMETERS pp, char* cmd); -bool param_get_cmd(pPARAMETERS pp, char* cmd, int n); +bool param_set(pPARAMETERS pp, const char* name, const char* value); +bool param_set_cmd(pPARAMETERS pp, const char* cmd); +bool param_get_cmd(pPARAMETERS pp, const char* cmd, int n); #endif