qavrg 0.0.28
qavrgfillpattern.cpp
Go to the documentation of this file.
00001 #include "qavrgfillpattern.h"
00002 #include <epics/cadef.h>
00003 #include <stdio.h>
00004 
00005 static void valueChanged(struct event_handler_args args);
00006 static void connectionChanged(struct connection_handler_args args);
00007 
00008 static const long MAGIC_VALUE = 0xdeadbeef;
00009 
00010 QavrgFillPattern::QavrgFillPattern(QavrgEpicsInterface *ifc)
00011   : QObject(),
00012     m_EpicsInterface(ifc),
00013     m_FillPattern(),
00014 //    m_FillSignature(QCryptographicHash::Sha1),
00015     m_Magic(MAGIC_VALUE)
00016 {
00017   int stat;
00018   stat = ca_create_channel("Mt:S:FillPatternWF",::connectionChanged, this, CA_PRIORITY_DEFAULT, &m_Ch);
00019 
00020   if (stat != ECA_NORMAL) {
00021     printf("ca_create_channel failed:\n%s\n",ca_message(stat));
00022   }
00023 
00024   ca_set_puser(m_Ch, this);
00025 }
00026 
00027 QavrgFillPattern::~QavrgFillPattern()
00028 {
00029   ca_clear_channel(m_Ch);
00030 }
00031 
00032 static void connectionChanged(struct connection_handler_args args)
00033 {
00034   chid pCh = args.chid;
00035   int stat;
00036 
00037   switch (ca_state(pCh)) {
00038 
00039   case cs_conn:
00040     printf("Connected to %s\n", ca_name(pCh));
00041     stat = ca_array_get_callback(DBR_CHAR,ca_element_count(pCh),pCh,
00042                                  valueChanged,NULL);
00043     if (stat != ECA_NORMAL) {
00044       printf("ca_array_get_callback:\n%s\n", ca_message(stat));
00045       return;
00046     }
00047 
00048     break;
00049 
00050   case cs_never_conn:
00051     printf("Cannot connect to %s\n", ca_name(pCh));
00052     break;
00053 
00054   case cs_prev_conn:
00055     printf("Lost connection to %s\n", ca_name(pCh));
00056     break;
00057 
00058   case cs_closed:
00059     printf("Connection closed %s\n", ca_name(pCh));
00060     break;
00061   }
00062 }
00063 
00064 static void valueChanged(struct event_handler_args args)
00065 {
00066   if (args.status == ECA_NORMAL && args.dbr) {
00067     QavrgFillPattern *patt = reinterpret_cast<QavrgFillPattern*>(ca_puser(args.chid));
00068 
00069     if (patt) {
00070       patt->valueChanged(args);
00071     }
00072   }
00073 }
00074 
00075 void QavrgFillPattern::valueChanged(struct event_handler_args args)
00076 {
00077   if (m_Magic != MAGIC_VALUE) {
00078     printf("Bad magic value\n");
00079   }
00080 
00081   if (args.status == ECA_NORMAL && args.dbr) {
00082     char* p = (char*) args.dbr;
00083     chid pCh = args.chid;
00084     int nelem = ca_element_count(pCh);
00085 
00086     m_FillPattern.resize(0);
00087 
00088     int first = true;
00089 
00090     for (int i=0; i<nelem; i++) {
00091       m_FillPattern.append(p[i]);
00092 
00093       if (p[i]) {
00094         if (first) {
00095           printf("Fill: %d", i);
00096           first = false;
00097         } else {
00098           printf(", %d", i);
00099         }
00100       }
00101     }
00102 
00103     printf("\n");
00104 
00105 //     m_FillSignature.reset();
00106 //     m_FillSignature.addData(m_FillPattern);
00107 
00108 //     printf("Fill Pattern Signature: %s\n", (const char*) m_FillSignature.result().toHex());
00109 
00110     emit fillPatternChanged();
00111   } else {
00112     printf("Bad value\n");
00113   }
00114 }
00115 
00116 QVector<bool> QavrgFillPattern::fillPattern()
00117 {
00118   QVector<bool> res;
00119   double v;
00120 
00121   foreach(v, m_FillPattern) {
00122     res.append(v);
00123   }
00124 
00125   return res;
00126 }
00127 
00128 int QavrgFillPattern::filledBunch(int n)
00129 {
00130   int nfilled = 0;
00131   int i;
00132   int nf = m_FillPattern.count();
00133 
00134   for (i=0; i<nf; i++) {
00135     if (m_FillPattern.value(i)) {
00136       nfilled++;
00137 
00138       if (nfilled == n) {
00139         return i;
00140       }
00141     }
00142   }
00143 
00144   return -1;
00145 }