C++ Instrument Catalog
USBRemoteBoxController.cpp
Go to the documentation of this file.
1 
7 
8 using namespace MTL;
9 using namespace MTL::Instrument;
10 
11 const std::string CUSBRemoteBoxController::RSRC_FILTER = "USB[0-9]+::0x1BFA::0x1F6D::[0-9]+::RAW";
12 const int CUSBRemoteBoxController::REFRESH_RATE_MS = 100;
13 
15 
17  : m_rRsrcMan(rRM),
18  m_RBCat(rCatInstance),
19  m_ASManagerNotifier(&CUSBRemoteBoxController::l_PostManagerNotification, this),
20  m_pRemoteBox(nullptr),
21  m_Timer(&CUSBRemoteBoxController::l_PostTimerEvent, this)
22 {
23  m_RBCat.oUSBRemoteBusyState.Subscribe(m_ASManagerNotifier);
24 }
25 
27 {
28  m_Timer.Stop();
29  if (nullptr != m_pRemoteBox)
30  {
31  m_LEDs.LEDs.ON = false;
32  m_LEDs.LEDs.BUSY = false;
33  m_pRemoteBox->Update(m_LEDs, m_Buttons);
34 
35  delete m_pRemoteBox;
36  m_pRemoteBox = nullptr;
37  }
38 }
39 
41 {
42  CThreadedMailBox::ThreadStart();
43  m_Timer.Start(std::chrono::milliseconds(REFRESH_RATE_MS));
44 }
45 
47 {
48  m_Timer.Stop();
49  CThreadedMailBox::ThreadStop();
50 }
51 
52 void CUSBRemoteBoxController::l_PostTimerEvent()
53 {
54  CThreadedMailBox::l_SendMsg(sUSBRemoteBoxControllerMailBoxMsg(sUSBRemoteBoxControllerMailBoxMsg::eMsgType::kTimerEvent, nullptr));
55 }
56 
57 void CUSBRemoteBoxController::l_PostManagerNotification(CAsyncNotification * pASNot)
58 {
59  CThreadedMailBox::l_SendMsg(sUSBRemoteBoxControllerMailBoxMsg(sUSBRemoteBoxControllerMailBoxMsg::eMsgType::kCatalogNotification, pASNot));
60 }
61 
62 void CUSBRemoteBoxController::l_Refresh()
63 {
64  try
65  {
66  // If no remote is currently connected
67  if (nullptr == m_pRemoteBox)
68  {
69  // Find new resources
70  CResourceList l_RSL;
71  m_rRsrcMan.FindResources(l_RSL, RSRC_FILTER);
72  if (!l_RSL.empty())
73  {
74  m_pRemoteBox = new CUSBRemoteBox(m_rRsrcMan, l_RSL.front());
75  if (!m_pRemoteBox->Connect())
76  throw CUSBRemoteBoxControllerException("Could not connect to the usb remote box", MTL__LOCATION__);
77 
78  m_LEDs.LEDs.ON = true;
79  m_LEDs.LEDs.BUSY = false;
80  if (!m_pRemoteBox->Update(m_LEDs, m_Buttons))
81  throw CUSBRemoteBoxControllerException("Could not update remote box state", MTL__LOCATION__);
82  }
83  }
84 
85  // Check RB state, if any
86  if (nullptr != m_pRemoteBox)
87  {
88  if (!m_pRemoteBox->Update(m_LEDs, m_Buttons))
89  throw CUSBRemoteBoxControllerException("Could not update remote box state", MTL__LOCATION__);
90 
91  // Send relevant signals
92  if (m_Buttons.BUTs.ResetEvent)
93  m_RBCat.cUSBRemoteBoxReset.RequestControl(true);
94  if (m_Buttons.BUTs.StartEvent)
95  m_RBCat.cUSBRemoteBoxStart.RequestControl(true);
96  }
97  }
99  {
100  MTL_Unused(rE);
101  if (nullptr != m_pRemoteBox)
102  {
103  delete m_pRemoteBox;
104  m_pRemoteBox = nullptr;
105  }
106  }
107 }
108 
109 void CUSBRemoteBoxController::l_SetBusyState(bool Busy)
110 {
111  try
112  {
113  if (nullptr != m_pRemoteBox)
114  {
115  m_LEDs.LEDs.BUSY = Busy;
116  if (!m_pRemoteBox->Update(m_LEDs, m_Buttons))
117  throw CUSBRemoteBoxControllerException("Could not update remote box state", MTL__LOCATION__);
118  }
119  }
121  {
122  MTL_Unused(rE);
123  if (nullptr != m_pRemoteBox)
124  {
125  delete m_pRemoteBox;
126  m_pRemoteBox = nullptr;
127  }
128  }
129 }
130 
131 void CUSBRemoteBoxController::l_ThreadTask(eEventType EvtType, sUSBRemoteBoxControllerMailBoxMsg * pMsg)
132 {
133  switch (EvtType)
134  {
135  case eEventType::kExit:
136  break;
137  case eEventType::kUserMsg:
138  if (nullptr != pMsg)
139  {
140  switch (pMsg->MsgType)
141  {
142  case sUSBRemoteBoxControllerMailBoxMsg::eMsgType::kTimerEvent:
143  l_Refresh();
144  break;
145  case sUSBRemoteBoxControllerMailBoxMsg::eMsgType::kCatalogNotification:
146  l_AsyncManagerNotificationHandler(pMsg->pCatNot);
147  break;
148  }
149  }
150  break;
151  }
152 }
153 
154 void CUSBRemoteBoxController::l_AsyncManagerNotificationHandler(CAsyncNotification * pASNot)
155 {
156  CObserverNotifier_untyped & rNotifier = pASNot->Notifier();
157  CObserver_untyped & rVar = pASNot->Observer();
158 
159  const CAsyncSnapshotNotification * l_pASNot = static_cast<CAsyncSnapshotNotification *>(pASNot);
160 
161  if (rVar.Is(m_RBCat.oUSBRemoteBusyState))
162  {
163  const bool * l_pVal = static_cast<const bool *>(l_pASNot->DataCopy());
164  l_SetBusyState(*l_pVal);
165  }
166 
167  delete pASNot;
168 }
MTL::Instrument::CUSBRemoteBox::uButtons::sButtons::ResetEvent
uint32_t ResetEvent
Definition: USBRemoteBox.h:34
MTL::Instrument::CResourceList
Definition: VISAInstrumentTypes.h:23
MTL::Instrument::CVISAResourceManager::FindResources
bool FindResources(CResourceList &rList, std::string Filter="?*")
Definition: VISAInstrument.cpp:121
MTL::Instrument::CUSBRemoteBox::uButtons::BUTs
struct MTL::Instrument::CUSBRemoteBox::uButtons::sButtons BUTs
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification
Definition: CatalogInterface.h:180
MTL::CUSBRemoteBoxInterface::cUSBRemoteBoxStart
CControl< bool > cUSBRemoteBoxStart
Definition: MFCManagerCatalog.h:142
MTL::Instrument::CUSBRemoteBox
Definition: USBRemoteBox.h:15
MTL::sUSBRemoteBoxControllerMailBoxMsg
Definition: USBRemoteBoxController.h:25
MTL::Instrument::eEventType
eEventType
Definition: VISAInstrumentTypes.h:95
MTL::CUSBRemoteBoxController::Terminate
void Terminate()
Definition: USBRemoteBoxController.cpp:46
MTL::Instrument::CUSBRemoteBox::uButtons::sButtons::StartEvent
uint32_t StartEvent
Definition: USBRemoteBox.h:32
MTL::CUSBRemoteBoxController::~CUSBRemoteBoxController
virtual ~CUSBRemoteBoxController()
Definition: USBRemoteBoxController.cpp:26
MTL::CUSBRemoteBoxController
Definition: USBRemoteBoxController.h:33
MTL::InstrumentCatalogInterface::CAsyncSnapshotNotification::DataCopy
const void * DataCopy() const
Definition: CatalogInterface.h:195
MTL
Definition: CPT2026PeripheralROM.h:19
MTL::Instrument
Definition: MFC3045.h:25
MTL::InstrumentCatalogInterface::CObserver::Subscribe
void Subscribe(CObserverNotifier_untyped &rNotifier)
Definition: CatalogInterface.h:95
MTL::InstrumentCatalogInterface::CControl::RequestControl
void RequestControl(const DataType NewValue)
Definition: CatalogInterface.h:28
CUSBRemoteBoxControllerException
CException< CUSBRemoteBoxController > CUSBRemoteBoxControllerException
Definition: USBRemoteBoxController.cpp:14
MTL::InstrumentCatalogInterface::CAsyncNotification::Notifier
CObserverNotifier_untyped & Notifier() const
Definition: CatalogInterface.h:160
MTL::InstrumentCatalogInterface::CObserver_untyped
Definition: CatalogInterface.h:62
MTL::CUSBRemoteBoxInterface::oUSBRemoteBusyState
CObserver< bool > oUSBRemoteBusyState
Definition: MFCManagerCatalog.h:139
MTL::ThreadedMailbox::CTimer::Stop
void Stop()
Definition: ThreadedMailbox.h:203
MTL::Instrument::CUSBRemoteBox::uLEDs::sLEDs::BUSY
uint32_t BUSY
Definition: USBRemoteBox.h:23
MTL::InstrumentCatalogInterface::CObserverNotifier_untyped
Definition: CatalogInterface.h:204
MTL::Instrument::CUSBRemoteBox::uLEDs::sLEDs::ON
uint32_t ON
Definition: USBRemoteBox.h:22
MTL::sUSBRemoteBoxControllerMailBoxMsg::pCatNot
CAsyncNotification * pCatNot
Definition: USBRemoteBoxController.h:27
MTL::CException
Exception to be thrown.
Definition: Exception.h:16
MTL::InstrumentCatalogInterface::CAsyncNotification
Definition: CatalogInterface.h:144
MTL::CUSBRemoteBoxInterface::cUSBRemoteBoxReset
CControl< bool > cUSBRemoteBoxReset
Definition: MFCManagerCatalog.h:143
MTL::CUSBRemoteBoxController::CUSBRemoteBoxController
CUSBRemoteBoxController(CVISAResourceManager &rRM, CMFCManagerCatalog &rCatInstance)
Definition: USBRemoteBoxController.cpp:16
MTL::sUSBRemoteBoxControllerMailBoxMsg::MsgType
enum MTL::sUSBRemoteBoxControllerMailBoxMsg::eMsgType MsgType
MTL::CMFCManagerCatalog
Definition: MFCManagerCatalog.h:19
MTL::ThreadedMailbox::CTimer::Start
bool Start(std::chrono::milliseconds Period_ms)
Definition: ThreadedMailbox.h:190
MTL::Instrument::CUSBRemoteBox::Connect
bool Connect()
Definition: USBRemoteBox.cpp:40
MTL::CUSBRemoteBoxController::Operate
void Operate()
Definition: USBRemoteBoxController.cpp:40
MTL_Unused
#define MTL_Unused(x)
Definition: Helpers.h:47
USBRemoteBoxController.h
MTL::Instrument::CVISAResourceManager
VISA Resource Manager class.
Definition: VISAInstrument.h:35
MTL::Instrument::CUSBRemoteBox::uLEDs::LEDs
struct MTL::Instrument::CUSBRemoteBox::uLEDs::sLEDs LEDs
MTL::InstrumentCatalogInterface::CObserver_untyped::Is
bool Is(const CObserver_untyped &rObs)
Definition: CatalogInterface.h:70
MTL::Instrument::CUSBRemoteBox::Update
bool Update(const uLEDs LEDs, uButtons &rButtons)
Definition: USBRemoteBox.cpp:76
MTL::InstrumentCatalogInterface::CAsyncNotification::Observer
CObserver_untyped & Observer() const
Definition: CatalogInterface.h:162
MTL__LOCATION__
#define MTL__LOCATION__
Definition: Helpers.h:22