Avionics
Dropship Simulator
Analog.cpp
Go to the documentation of this file.
1 #include "Analog.h"
2 
3 namespace Devices
4 {
5  int CCONV Analog::AttachHandler(CPhidgetHandle IFK, void* usrptr)
6  {
7  Analog* analog = static_cast<Analog*>(usrptr);
8 
9  int serialNo;
10  const char* name;
11 
12  CPhidget_getDeviceName(IFK, &name);
13  CPhidget_getSerialNumber(IFK, &serialNo);
14 
15  if (analog->config->serialno == -1)
16  analog->config->serialno = serialNo;
17 
18  char msg[99];
19  sprintf_s(msg, 99, "Analog::AttachHandler %s (%i) attached!", name, serialNo);
20  analog->logger->Log(msg);
21  return 0;
22  }
23 
24  int CCONV Analog::DetachHandler(CPhidgetHandle IFK, void* usrptr)
25  {
26  Analog* analog = static_cast<Analog*>(usrptr);
27 
28  int serialNo;
29  const char* name;
30 
31  CPhidget_getDeviceName(IFK, &name);
32  CPhidget_getSerialNumber(IFK, &serialNo);
33 
34  char msg[99];
35  sprintf_s(msg, 99, "Analog::DetachHandler %s (%i) detached!", name, serialNo);
36  analog->logger->Log(msg, Logger::Error);
37 
38  return 0;
39  }
40 
41  int CCONV Analog::ErrorHandler(CPhidgetHandle IFK, void* usrptr, int ErrorCode, const char* unknown)
42  {
43  Analog* analog = static_cast<Analog*>(usrptr);
44  char msg[99];
45  sprintf_s(msg, 99, "Analog::ErrorHandler Error handled. %s", unknown);
46  analog->logger->Log(msg, Logger::Error, ErrorCode);
47  return 0;
48  }
49 
50  //Display the properties of the attached phidget to the screen. We will be displaying the name, serial number and version of the attached device.
51  //Will also display the number of inputs, outputs, and analog inputs on the interface kit as well as the state of the ratiometric flag
52  //and the current analog sensor sensitivity.
53  int Analog::display_properties(CPhidgetAnalogHandle phid) const
54  {
55  int serialNo, version, numAnalog;
56  double max, min;
57  const char* ptr;
58 
59  CPhidget_getDeviceType(reinterpret_cast<CPhidgetHandle>(phid), &ptr);
60  CPhidget_getSerialNumber(reinterpret_cast<CPhidgetHandle>(phid), &serialNo);
61  CPhidget_getDeviceVersion(reinterpret_cast<CPhidgetHandle>(phid), &version);
62  CPhidgetAnalog_getOutputCount(phid, &numAnalog);
63  CPhidgetAnalog_getVoltageMax(phid, 0, &max);
64  CPhidgetAnalog_getVoltageMin(phid, 0, &min);
65 
66  char msg[99];
67  sprintf_s(msg, 99, "Analog::display_properties Type: %s", ptr);
68  logger->Log(msg);
69  sprintf_s(msg, 99, "Analog::display_properties Serial Number: %i", serialNo);
70  logger->Log(msg);
71  sprintf_s(msg, 99, "Analog::display_properties Version: %i", version);
72  logger->Log(msg);
73  sprintf_s(msg, 99, "Analog::display_properties Analog Outputs: %d", numAnalog);
74  logger->Log(msg);
75  sprintf_s(msg, 99, "Analog::display_properties Maximum Output: +%0.1lfV", max);
76  logger->Log(msg);
77  sprintf_s(msg, 99, "Analog::display_properties Minimum Output: %0.1lfV", min);
78  logger->Log(msg);
79 
80  return 0;
81  }
82 
83  void Analog::Initialize(Logger* prmLogger, AnalogConfig* prmConfig, Bus* prmBus)
84  {
85  logger = prmLogger;
86  config = prmConfig;
87  bus = prmBus;
88 
89  int result;
90  const char* err;
91 
92  //Declare an InterfaceKit handle
93  ifKit = nullptr;
94 
95  //create the InterfaceKit object
96  CPhidgetAnalog_create(&ifKit);
97 
98  //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
99  CPhidget_set_OnAttach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), AttachHandler, this);
100  CPhidget_set_OnDetach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), DetachHandler, this);
101  CPhidget_set_OnError_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), ErrorHandler, this);
102 
103  //open the interfacekit for device connections
104  CPhidget_open(reinterpret_cast<CPhidgetHandle>(ifKit), config->serialno);
105 
106  //get the program to wait for an interface kit device to be attached
107  logger->Log("Analog::ctor Waiting for analog to be attached....");
108  result = CPhidget_waitForAttachment(reinterpret_cast<CPhidgetHandle>(ifKit), 5000);
109  if (result)
110  {
111  CPhidget_getErrorDescription(result, &err);
112  char msg[99];
113  sprintf_s(msg, 99, "Analog::ctor Problem waiting for attachment of %i: %s", config->serialno, err);
114  logger->Log(msg, Logger::Fatal);
115  config->enable = false;
116  return;
117  }
118 
119  for (UINT i = 0; i < config->channels.size(); i++)
120  {
121  if (config->channels[i].defined)
122  {
123  int enabled;
124  CPhidgetAnalog_getEnabled(ifKit, i, &enabled);
125  if (enabled == 1)
126  {
127  CPhidgetAnalog_getVoltage(ifKit, i, &config->channels[i].currentValue);
128  }
129  else
130  {
131  CPhidgetAnalog_setVoltage(ifKit, i, 0.0);
132  config->channels[i].currentValue = 0.0;
133  CPhidgetAnalog_setEnabled(ifKit, i, PTRUE);
134  }
135  }
136  else
137  {
138  CPhidgetAnalog_setEnabled(ifKit, i, PFALSE);
139  }
140  }
141 
142  // Log the properties of the attached interface kit device
144  }
145 
146  void Analog::Destroy() const
147  {
148  logger->Log("Analog::Destroy...");
149  CPhidget_close(reinterpret_cast<CPhidgetHandle>(ifKit));
150  CPhidget_delete(reinterpret_cast<CPhidgetHandle>(ifKit));
151  config->enable = false;
152  }
153 
154  void Analog::FrameMove() const
155  {
156  if (!config->enable) return;
157 
159  for (UINT i = 0; i < config->channels.size(); i++)
160  {
161  if (config->channels[i].defined)
162  {
163  double busValue;
164  if (config->channels[i].trigger == "Back Right Bellow")
165  busValue = bus->PlatformBackRightBellow;
166  else if (config->channels[i].trigger == "Back Left Bellow")
167  busValue = bus->PlatformBackLeftBellow;
168  else if (config->channels[i].trigger == "Front Right Bellow")
169  busValue = bus->PlatformFrontRightBellow;
170  else if (config->channels[i].trigger == "Front Left Bellow")
171  busValue = bus->PlatformFrontLeftBellow;
172  //else if (config->channels[i].trigger == "Avionics Blower Motor")
173  // busValue = bus->AvionicsBlowerMotor;
174  else
175  {
176  config->channels[i].defined = false;
177  char msg[99];
178  sprintf_s(msg, 99, "Analog::FrameMove Undefined analog channel (index %i) triggered: %s (%i)", i, config->channels[i].trigger.c_str(), config->serialno);
179  logger->Log(msg, Logger::Error);
180  this->Destroy();
181  return;
182  }
183 
184  //if (config->channels[i].currentValue != busValue)
185  {
186  CPhidgetAnalog_setVoltage(ifKit, i, busValue);
187  config->channels[i].currentValue = busValue;
188  }
189  }
190  }
191  }
192 }
double PlatformFrontRightBellow
Definition: Bus.h:32
AnalogConfig * config
Definition: Analog.h:27
void FrameMove() const
Definition: Analog.cpp:154
Bus * bus
Definition: Analog.h:28
static int CCONV DetachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Analog.cpp:24
double PlatformFrontLeftBellow
Definition: Bus.h:33
Definition: Logger.h:5
void Destroy() const
Definition: Analog.cpp:146
std::vector< Channel > channels
Definition: Analog.h:20
okay, the portable keyboard numbers don&#39;t work like the outside keypad because the outside keypad is ...
Definition: Analog.cpp:3
Logger * logger
Definition: Analog.h:26
void Initialize(Logger *logger, AnalogConfig *config, Bus *prmBus)
Definition: Analog.cpp:83
int display_properties(CPhidgetAnalogHandle phid) const
Definition: Analog.cpp:53
Definition: Bus.h:12
static int CCONV ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
Definition: Analog.cpp:41
static int CCONV AttachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Analog.cpp:5
double PlatformBackLeftBellow
Definition: Bus.h:31
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
double PlatformBackRightBellow
Definition: Bus.h:30
CPhidgetAnalogHandle ifKit
Definition: Analog.h:30