qmca  0.0.20
epicsenumeration.cpp
Go to the documentation of this file.
1 #include "epicsenumeration.h"
2 #include <QMutexLocker>
3 
4 #include <stdio.h>
5 
6 EpicsEnumeration::EpicsEnumeration(QString pvName, QObject *parent)
7  : EpicsObject(pvName, parent)
8 {
9 }
10 
12 {
13 }
14 
16 {
17  if (!m_HasEventHandler) {
18  SEVCHK(ca_create_subscription(DBR_GR_ENUM, 0, m_ChannelID, DBE_VALUE|DBE_ALARM,
19  get_event_handler(), this, &m_EventID), NULL);
20 
22 
23 // printf("Channel %s connected\n", qPrintable(pvName()));
24  }
25 }
26 
27 void EpicsEnumeration::event_handler(struct event_handler_args args)
28 {
29  union db_access_val *pb = (union db_access_val *) args.dbr;
30 
31  struct dbr_gr_enum res;
32  memset(&res, 0xff, sizeof(res));
33  switch (args.type) {
34  case DBR_GR_ENUM:
35  res = pb->genmval;
36  break;
37  default:
38  printf("data type %ld not supported\n", args.type);
39  }
40 
41  EpicsEnumeration* e = ((EpicsEnumeration*) args.usr);
42 
43  if (e && e->debug()) {
44  printf("%s Epics Enumeration Event Handler called %ld = %d\n", ca_name(args.chid), args.type, res.value);
45  }
46 
47  ((EpicsEnumeration*) args.usr) -> changeValue(res);
48 }
49 
51 {
53 }
54 
55 void EpicsEnumeration::changeValue(struct dbr_gr_enum newval)
56 {
57  if (m_Debug) {
58  printf("EpicsEnumeration::changeValue\n");
59  }
60 
61  QMutexLocker lock(&m_Mutex);
62 
63  int valchanged=0;
64  int vlschanged=0;
65 
66  if (m_Value.value != newval.value) {
67  valchanged=1;
68  }
69 
70  if (m_Value.no_str != newval.no_str) {
71  vlschanged=1;
72  } else {
73  for (int i=0; i<m_Value.no_str; i++) {
74  if (strncmp(m_Value.strs[i],newval.strs[i],MAX_ENUM_STRING_SIZE)!=0) {
75  vlschanged=1;
76  break;
77  }
78  }
79  }
80 
81  m_Value = newval;
82 
83  if (m_Debug) {
84  printf("valchanged:%d vlschanged:%d\n", valchanged, vlschanged);
85  }
86 
87  if (vlschanged) {
88  emit valueListChanged();
89  }
90 
91  if (valchanged) {
92  emit valueChanged(m_Value.value);
93  }
94 }
95 
97 {
98  ca_put(DBR_INT, channelID(), &val);
99  ca_pend_io(0.5);
100 }
101 
103 {
104  QMutexLocker lock(&m_Mutex);
105 
106  return m_Value.value;
107 }
108 
110 {
111  QStringList res;
112 
113  for (int i=0; i<m_Value.no_str; i++) {
114  res << m_Value.strs[i];
115  }
116 
117  return res;
118 }
119 
void valueListChanged()
virtual ~EpicsEnumeration()
bool m_HasEventHandler
Definition: epicsobject.h:50
static void event_handler(struct event_handler_args arg)
QStringList valueList()
virtual caEventCallBackFunc * get_event_handler()
void caput(int val)
evid m_EventID
Definition: epicsobject.h:49
EpicsEnumeration(QString pvName, QObject *parent=0)
void changeValue(struct dbr_gr_enum val)
void valueChanged()
static QMutex m_Mutex
Definition: epicsobject.h:42
chid channelID()
Definition: epicsobject.cpp:63
chid m_ChannelID
Definition: epicsobject.h:48
void ca_pend_io(double t)
struct dbr_gr_enum m_Value