interface.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 //---------------------------------------------------------------------------
15 #include "measure.h"
16 #include "interface.h"
17 #include "xnodeconnector.h"
18 #include <string>
19 #include "driver.h"
20 
21 //---------------------------------------------------------------------------
22 
23 XInterface::XInterfaceError::XInterfaceError(const XString &msg, const char *file, int line)
24  : XKameError(msg, file, line) {}
25 XInterface::XConvError::XConvError(const char *file, int line)
26  : XInterfaceError(i18n("Conversion Error"), file, line) {}
27 XInterface::XCommError::XCommError(const XString &msg, const char *file, int line)
28  : XInterfaceError(i18n("Communication Error") + ", " + msg, file, line) {}
29 XInterface::XOpenInterfaceError::XOpenInterfaceError(const char *file, int line)
30  : XInterfaceError(i18n("Open Interface Error"), file, line) {}
31 
32 
33 XInterface::XInterface(const char *name, bool runtime, const shared_ptr<XDriver> &driver) :
34  XNode(name, runtime),
35  m_driver(driver),
36  m_device(create<XComboNode>("Device", false, true)),
37  m_port(create<XStringNode>("Port", false)),
38  m_address(create<XUIntNode>("Address", false)),
39  m_control(create<XBoolNode>("Control", true)) {
40 
41  iterate_commit([=](Transaction &tr){
42  lsnOnControlChanged = tr[ *control()].onValueChanged().connectWeakly(
43  shared_from_this(), &XInterface::onControlChanged);
44  });
45 }
46 
47 XString
49  if(m_label.empty())
50  return driver()->getLabel();
51  else
52  return driver()->getLabel() + ":" + m_label;
53 }
54 
55 void
56 XInterface::onControlChanged(const Snapshot &shot, XValueNodeBase *) {
57  if(shot[ *control()]) {
58  g_statusPrinter->printMessage(driver()->getLabel() + i18n(": Starting..."));
59  start();
60  }
61  else {
62  control()->setUIEnabled(false);
63  Snapshot shot( *this);
64  shot.talk(shot[ *this].onClose(), this);
65  }
66 }
67 
68 void
69 XInterface::start() {
70  XScopedLock<XInterface> lock( *this);
71  try {
72  if(isOpened()) {
73  gErrPrint(getLabel() + i18n("Port has already opened"));
74  return;
75  }
76  open();
77  }
78  catch (XInterfaceError &e) {
79  e.print(getLabel() + i18n(": Opening interface failed, because "));
80  iterate_commit([=](Transaction &tr){
81  tr[ *control()] = false;
82  tr.unmark(lsnOnControlChanged);
83  });
84  return;
85  }
86 
87  Snapshot shot = iterate_commit([=](Transaction &tr){
88  tr[ *device()].setUIEnabled(false);
89  tr[ *port()].setUIEnabled(false);
90  tr[ *address()].setUIEnabled(false);
91 
92  tr[ *control()] = true;
93  tr.unmark(lsnOnControlChanged);
94  });
95  shot.talk(shot[ *this].onOpen(), this);
96 }
97 void
98 XInterface::stop() {
99  XScopedLock<XInterface> lock( *this);
100 
101  try {
102  close();
103  }
104  catch (XInterfaceError &e) {
105  e.print(getLabel() + i18n(": Closing interface failed, because"));
106  }
107 
108  iterate_commit([=](Transaction &tr){
109  tr[ *device()].setUIEnabled(true);
110  tr[ *port()].setUIEnabled(true);
111  tr[ *address()].setUIEnabled(true);
112  tr[ *control()].setUIEnabled(true);
113 
114  tr[ *control()] = false;
115  tr.unmark(lsnOnControlChanged);
116  });
117  //g_statusPrinter->clear();
118 }

Generated for KAME4 by  doxygen 1.8.3