1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
#ifndef DBVALUE_H
#define DBVALUE_H
#include <epicsTypes.h>
#include <epicsTime.h>
#include <string>
#include <vector>
#include <typeinfo>
#include <pv/sharedPtr.h>
struct baseValue {
bool remoteWritable;
typedef std::vector<size_t> shape_t;
shape_t shape;
std::string format;
unsigned short severity;
std::string message;
struct timespec timeStamp;
// static meta-data
std::string egu;
int precision;
baseValue();
};
template<typename PVT>
struct scalarNumericValue : public baseValue {
typedef PVT value_type;
typedef PVT element_type;
value_type value;
value_type displayHigh, displayLow;
value_type warnHigh, warnLow;
value_type alarmHigh, alarmLow;
value_type ctrlHigh, ctrlLow;
value_type step;
scalarNumericValue();
};
struct scalarEnumValue : public baseValue {
typedef epicsUInt32 value_type;
typedef epicsUInt32 element_type;
value_type value;
std::vector<std::string> choices;
scalarEnumValue() : value(0) {}
};
struct scalarStringValue : public baseValue {
typedef std::string value_type;
typedef std::string element_type;
value_type value;
std::vector<std::string> choices;
};
template<typename PVT>
struct vectorNumericValue : public baseValue {
typedef std::tr1::shared_ptr<PVT> value_type;
typedef PVT element_type;
value_type value;
element_type displayHigh, displayLow;
vectorNumericValue() : displayHigh(0), displayLow(0) {}
};
template<typename PVT>
scalarNumericValue<PVT>::scalarNumericValue()
:baseValue()
,value(0)
,displayHigh(0)
,displayLow(0)
,warnHigh(0)
,warnLow(0)
,alarmHigh(0)
,alarmLow(0)
,ctrlHigh(0)
,ctrlLow(0)
,step(1)
{}
#endif // DBVALUE_H
baseValue::baseValue()
:remoteWritable(true)
,severity(0)
,message()
,precision(0)
{
timeStamp.tv_sec = timeStamp.tv_nsec = 0;
}
#ifndef SIM_H
#define SIM_H
//#include <tr1/functional>
#include <epicsThread.h>
#include <epicsEvent.h>
struct SimADC : public std::tr1::enable_shared_from_this<SimADC>,
public epicsThreadRunable
{
typedef std::tr1::shared_ptr<SimADC> smart_pointer_type;
epicsMutex mutex;
scalarNumericValue<double> mult, shift, offset, freq, rate;
scalarNumericValue<epicsUInt32> nSamples;
epicsUInt32 prev_nSamples;
scalarEnumValue operation;
vectorNumericValue<double> data;
vectorNumericValue<epicsUInt32> X;
epicsThread runner;
bool runner_stop;
epicsEvent updated;
SimADC();
virtual ~SimADC();
virtual void run ();
void cycle();
};
SimADC::smart_pointer_type createSimADC(const std::string& name);
void shutdownSimADCs();
SimADC::smart_pointer_type getSimADC(const std::string& name);
#endif // SIM_H
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <map>
#include <stdexcept>
#include <epicsGuard.h>
#include <epicsMutex.h>
#include <epicsThread.h>
// 180 / PI
#define RAD2DEG 57.2957795131
// 100 / (2*PI)
#define RAD2PCT 15.9154943092
struct sim_global_type {
typedef epicsGuard<epicsMutex> guard_t;
typedef epicsGuardRelease<epicsMutex> unguard_t;
epicsMutex lock;
typedef std::map<std::string, SimADC::smart_pointer_type> sims_t;
sims_t sims;
};
static
sim_global_type *sim_global = 0;
static
void sim_global_init(void*)
{
try {
sim_global = new sim_global_type;
} catch(...) {
abort();
}
}
static
epicsThreadOnceId sim_mute_once = EPICS_THREAD_ONCE_INIT;
SimADC::SimADC()
:runner(*this, "Runner",
epicsThreadGetStackSize(epicsThreadStackBig),
epicsThreadPriorityMedium)
,runner_stop(false)
{
mult.value= rate.value= 1.0;
shift.value= offset.value= 0.0;
freq.value=90.0;
offset.egu = mult.egu = "V";
rate.egu = "Hz";
rate.displayLow = rate.ctrlLow = 0.0;
rate.displayHigh = rate.ctrlHigh = 1e6;
shift.displayLow = shift.ctrlLow = 0.0;
shift.displayHigh = shift.ctrlHigh = 99.999999;
shift.egu = "%";
freq.egu = "deg/pt";
nSamples.value = 10;
prev_nSamples = 0;
cycle();
operation.choices.resize(2);
operation.choices[0] = "Stop";
operation.choices[1] = "Run";
operation.value = 0;
data.remoteWritable = false;
X.remoteWritable = false;
runner.start();
}
SimADC::~SimADC()
{
{
sim_global_type::guard_t G(mutex);
runner_stop = true;
}
runner.exitWait();
}
namespace {
template<typename T>
struct Freeme {
void operator()(T* p) {
free(p);
}
};
}
void SimADC::cycle()
{
epicsTime now = epicsTime::getCurrent();
if(nSamples.value != prev_nSamples) {
X.value.reset();
data.value.reset();
prev_nSamples = nSamples.value;
}
if(!X.value) {
X.value.reset((unsigned int*)malloc(sizeof(epicsUInt32)*prev_nSamples), Freeme<unsigned int>());
unsigned int *val = X.value.get();
for(size_t i=0; i<prev_nSamples; i++)
val[i] = 2*i;
X.shape.resize(1);
X.shape[0] = prev_nSamples;
X.displayLow = val[0];
X.displayHigh = val[prev_nSamples-1];
}
if(!data.value || !data.value.unique())
data.value.reset((double*)malloc(sizeof(double)*prev_nSamples), Freeme<double>());
X.timeStamp = now;
if(!X.value) {
X.severity = 3;
X.message = "Alloc fails";
}
if(!data.value) {
data.severity = 3;
data.message = "Alloc fails";
return;
}
double *val = data.value.get();
for(size_t i=0; i<prev_nSamples; i++)
val[i] = mult.value * sin((freq.value/RAD2DEG)*i + (shift.value/RAD2PCT)) + offset.value;
data.shape.resize(1);
data.shape[0] = prev_nSamples;
updated.signal();
}
void SimADC::run()
{
const double min_sleep = epicsThreadSleepQuantum();
sim_global_type::guard_t G(mutex);
while(true) {
{
double zzz = rate.value>0.0 ? 1.0/rate.value : min_sleep;
sim_global_type::unguard_t U(G);
epicsThreadSleep(zzz);
}
if(runner_stop)
break;
cycle();
}
//printf("SimADC shutdown\n");
}
SimADC::smart_pointer_type createSimADC(const std::string& name)
{
epicsThreadOnce(&sim_mute_once, &sim_global_init, 0);
sim_global_type::guard_t G(sim_global->lock);
sim_global_type::sims_t &sims = sim_global->sims;
SimADC::smart_pointer_type P(new SimADC);
sims[name] = P;
return P;
}
void shutdownSimADCs()
{
assert(sim_global);
sim_global_type::sims_t sims;
{
/* swap out so the global lock is not held during delete */
sim_global_type::guard_t G(sim_global->lock);
sims.swap(sim_global->sims);
}
sims.clear();
}
SimADC::smart_pointer_type getSimADC(const std::string& name)
{
epicsThreadOnce(&sim_mute_once, &sim_global_init, 0);
sim_global_type::guard_t G(sim_global->lock);
sim_global_type::sims_t::const_iterator it = sim_global->sims.find(name);
if(it==sim_global->sims.end())
return SimADC::smart_pointer_type();
return it->second;
}
|