Avionics
Dropship Simulator
InterfaceKit.cpp
Go to the documentation of this file.
1 #include "InterfaceKit.h"
2 
3 namespace Devices
4 {
5  int CCONV InterfaceKit::AttachHandler(CPhidgetHandle IFK, void* usrptr)
6  {
7  InterfaceKit* interfaceKit = static_cast<InterfaceKit*>(usrptr);
8 
9  int serialNo;
10  const char* name;
11 
12  CPhidget_getDeviceName(IFK, &name);
13  CPhidget_getSerialNumber(IFK, &serialNo);
14 
15  if (interfaceKit->config->serialno == -1)
16  interfaceKit->config->serialno = serialNo;
17 
18  char msg[99];
19  if (interfaceKit->wasDetached)
20  {
21  interfaceKit->wasDetached = false;
22  sprintf_s(msg, 99, "InterfaceKit::AttachHandler %s (%s) reattached!", name, interfaceKit->config->friendlyName.c_str());
23  interfaceKit->logger->Log(msg, Logger::Error);
24  }
25  else
26  {
27  sprintf_s(msg, 99, "InterfaceKit::AttachHandler %s (%s) attached!", name, interfaceKit->config->friendlyName.c_str());
28  interfaceKit->logger->Log(msg);
29  }
30 
31  interfaceKit->attached = true;
32 
33  return 0;
34  }
35 
36  int CCONV InterfaceKit::DetachHandler(CPhidgetHandle IFK, void* usrptr)
37  {
38  InterfaceKit* interfaceKit = static_cast<InterfaceKit*>(usrptr);
39 
40  int serialNo;
41  const char* name;
42 
43  CPhidget_getDeviceName(IFK, &name);
44  CPhidget_getSerialNumber(IFK, &serialNo);
45 
46  char msg[99];
47  sprintf_s(msg, 99, "InterfaceKit::DetachHandler %s (%s) detached!", name, interfaceKit->config->friendlyName.c_str());
48  interfaceKit->logger->Log(msg, Logger::Error);
49 
51  for (UINT i = 0; i < interfaceKit->config->sensors.size(); i++)
52  {
53  SensorChangeHandler(reinterpret_cast<CPhidgetInterfaceKitHandle>(IFK), usrptr, i, -1);
54  }
55  for (UINT i = 0; i < interfaceKit->config->inputs.size(); i++)
56  {
57  InputChangeHandler(reinterpret_cast<CPhidgetInterfaceKitHandle>(IFK), usrptr, i, -1);
58  }
59 
60  interfaceKit->attached = false;
61  interfaceKit->wasDetached = true;
62 
63  return 0;
64  }
65 
66  int CCONV InterfaceKit::ErrorHandler(CPhidgetHandle IFK, void* usrptr, int ErrorCode, const char* unknown)
67  {
68  InterfaceKit* interfaceKit = static_cast<InterfaceKit*>(usrptr);
69  char msg[99];
70  sprintf_s(msg, 99, "InterfaceKit::ErrorHandler Error handled. %s", unknown);
71  interfaceKit->logger->Log(msg, Logger::Error, ErrorCode);
72  return 0;
73  }
74 
75  //callback that will run if an input changes.
76  //Index - Index of the input that generated the event, State - boolean (0 or 1) representing the input state (on or off), -1 means no signal!
77  int CCONV InterfaceKit::InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void* usrptr, int Index, int State)
78  {
79  InterfaceKit* interfaceKit = static_cast<InterfaceKit*>(usrptr);
80  InterfaceKitConfig::Input input = interfaceKit->config->inputs.at(Index);
81 
82  if (!input.defined)
83  {
84  // char msg[99];
85  // sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler Received digital data for undefined index: %d (%d)", Index, State);
86  // interfaceKit->logger->Log(msg, Logger::Warn);
87  }
88  else
89  {
90  if (input.name == "Component")
91  {
92  char msg[99];
93  sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler Index %i: %i", input.name.c_str(), Index, State);
94  interfaceKit->logger->Log(msg);
95 
96  if (!input.ptrFloat)
97  input.ptrFloat = interfaceKit->bus->GetComponentSetStatePtr(input.guid);
98  if (!input.ptrFault)
99  input.ptrFault = interfaceKit->bus->GetComponentFaultStatePtr(input.guid);
100 
102  if (State != 0 && State != 1)
103  {
104  *input.ptrFloat = -999.0f;
106  }
107  else
108  {
109  *input.ptrFloat = static_cast<float>(State);
111  }
112  }
113  else
114  {
115  char msg[99];
116  sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler %s (%i): %i", input.name.c_str(), Index, State);
117  interfaceKit->logger->Log(msg);
118 
121  if (State == 1)
122  {
123  Command command;
124  command.name = input.name;
125  interfaceKit->bus->commandStream.push_back(command);
126 
127  sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler: Added command: %s", command.name.c_str());
128  interfaceKit->logger->Log(msg);
129  }
130  }
131  }
132 
133  return 0;
134  }
135 
136  //callback that will run if an output changes.
137  //Index - Index of the output that generated the event, State - boolean (0 or 1) representing the output state (on or off)
138  /*int CCONV InterfaceKit::OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
139  {
140  InterfaceKit* interfaceKit = (InterfaceKit*)usrptr;
141  char msg[99];
142  sprintf_s(msg, 99, "InterfaceKit::OutputChangeHandler Index: %d State: %d", Index, State);
143  interfaceKit->logger->Log(msg);
144  return 0;
145  }*/
146 
147  //callback that will run if the sensor value changes by more than the OnSensorChange trigger.
148  //Index - Index of the sensor that generated the event, Value - the sensor read value
149  int CCONV InterfaceKit::SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void* usrptr, int Index, int Value)
150  {
151  InterfaceKit* interfaceKit = static_cast<InterfaceKit*>(usrptr);
152  InterfaceKitConfig::Sensor sensor = interfaceKit->config->sensors.at(Index);
153 
154  if (!sensor.defined)
155  {
156  // char msg[99];
157  // sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler Received sensor data for undefined index: %d (%d)", Index, Value);
158  // interfaceKit->logger->Log(msg, Logger::Warn);
159  }
160  else
161  {
162  if (!sensor.ptrFloat)
163  sensor.ptrFloat = interfaceKit->bus->GetComponentSetStatePtr(sensor.guid);
164  if (!sensor.ptrFault)
165  sensor.ptrFault = interfaceKit->bus->GetComponentFaultStatePtr(sensor.guid);
166 
167 #pragma region // phidgets precision temperature sensor
168 
169  if (sensor.type == "1124_0")
170  {
171  if (Value == -1)
172  {
174  *sensor.ptrFloat = -1.0f;
175  char msg[99];
176  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
177  interfaceKit->logger->Log(msg);
178  }
179  else
180  {
181  float celciusTemp = (static_cast<float>(Value) * 0.2222f) - 61.111f;
182  if (sensor.inverse) celciusTemp = -celciusTemp;
183  celciusTemp += sensor.bias;
184  *sensor.ptrFault = Systems::Fault::None;
185  *sensor.ptrFloat = celciusTemp;
186  float farenheitTemp = celciusTemp * 9.0f / 5.0f + 32.0f;
187  char msg[99];
188  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f°C %.1f°F", sensor.guid.c_str(), Index, celciusTemp, farenheitTemp);
189  interfaceKit->logger->Log(msg);
190  }
191  }
192 #pragma endregion
193 #pragma region // 20 Amp Current Sensor DC Current (DC Amps)
194  else if (sensor.type == "1119_0" && sensor.subtype == "DC")
195  {
196  if (Value == -1)
197  {
199  *sensor.ptrFloat = -1.0f;
200  char msg[99];
201  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
202  interfaceKit->logger->Log(msg);
203  }
204  else
205  {
206  float dcAmps = static_cast<float>(Value) / 20.0f - 25.0f;
207  if (sensor.inverse) dcAmps = -dcAmps;
208  dcAmps += sensor.bias;
209  *sensor.ptrFault = Systems::Fault::None;
210  *sensor.ptrFloat = dcAmps;
211  char msg[99];
212  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f A (%.1f A)", sensor.guid.c_str(), Index, dcAmps, static_cast<float>(Value) / 20.0f - 25.0f);
214  //interfaceKit->logger->Log(msg);
215  }
216  }
217 #pragma endregion
218 #pragma region // 200 PSI pressure sensor Custom0
219  else if (sensor.type == "Custom0")
220  {
221  if (Value == -1)
222  {
224  *sensor.ptrFloat = -1.0f;
225  char msg[99];
226  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
227  interfaceKit->logger->Log(msg);
228  }
229  else
230  {
231  // sensor value is always 0-1000 for 0V to 5V
232  // for the sensor I bought on ebay at 0PSI at 0.5V, 200PSI at 4.5V
233  // so 0PSI is 0.5V is 100.0f = 0.0 PSI
234  // so 100 PSI is 2.5V is 500.0f = 100 PSI
235  float psi = (static_cast<float>(Value) - 100.0f) / 800.0f * 200.0f;
236  if (sensor.inverse) psi = -psi;
237  psi += sensor.bias;
238  *sensor.ptrFault = Systems::Fault::None;
239  *sensor.ptrFloat = psi;
240  //char msg[99];
241  //sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f PSI (%.1f)", sensor.guid.c_str(), Index, dcAmps, (float)Value);
242  //interfaceKit->logger->Log(msg);
243  }
244  }
245 #pragma endregion
246 #pragma region // Vibration sensor
247  else if (sensor.type == "1104_0")
248  {
249  if (Value == -1)
250  {
252  *sensor.ptrFloat = -1.0f;
253  char msg[99];
254  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
255  interfaceKit->logger->Log(msg);
256  }
257  else
258  {
259  float mils = (static_cast<float>(Value)-500.0f) / 10.0f; // -50 to 50?
260  if (sensor.inverse) mils = -mils;
261  mils += sensor.bias;
262  *sensor.ptrFault = Systems::Fault::None;
263  *sensor.ptrFloat = mils;
264  char msg[99];
265  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f", sensor.guid.c_str(), Index, mils);
266  }
267  }
268 #pragma endregion
269  else
270  {
272  *sensor.ptrFloat = -1.0f;
273  char msg[99];
274  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler Unknown sensor type \"%s\" at index: %d (%d)", sensor.type, Index, Value);
275  interfaceKit->logger->Log(msg, Logger::Warn);
276  }
277  }
278 
279  return 0;
280  }
281 
282  //Display the properties of the attached phidget to the screen. We will be displaying the name, serial number and version of the attached device.
283  //Will also display the number of inputs, outputs, and analog inputs on the interface kit as well as the state of the ratiometric flag
284  //and the current analog sensor sensitivity.
285  int InterfaceKit::display_properties(CPhidgetInterfaceKitHandle phid) const
286  {
287  int serialNo, version, numInputs, numOutputs, numSensors, triggerVal, ratiometric = 0, i;
288  const char* ptr;
289 
290  CPhidget_getDeviceType(reinterpret_cast<CPhidgetHandle>(phid), &ptr);
291  CPhidget_getSerialNumber(reinterpret_cast<CPhidgetHandle>(phid), &serialNo);
292  CPhidget_getDeviceVersion(reinterpret_cast<CPhidgetHandle>(phid), &version);
293 
294  CPhidgetInterfaceKit_getInputCount(phid, &numInputs);
295  CPhidgetInterfaceKit_getOutputCount(phid, &numOutputs);
296  CPhidgetInterfaceKit_getSensorCount(phid, &numSensors);
297  if (numSensors)
298  CPhidgetInterfaceKit_getRatiometric(phid, &ratiometric);
299 
300  char msg[99];
301  sprintf_s(msg, 99, "InterfaceKit::display_properties Type: %s", ptr);
302  logger->Log(msg);
303  sprintf_s(msg, 99, "InterfaceKit::display_properties Serial Number: %i", serialNo);
304  logger->Log(msg);
305  sprintf_s(msg, 99, "InterfaceKit::display_properties Version: %i", version);
306  logger->Log(msg);
307  sprintf_s(msg, 99, "InterfaceKit::display_properties Digital Inputs: %d", numInputs);
308  logger->Log(msg);
309  sprintf_s(msg, 99, "InterfaceKit::display_properties Digital Outputs: %d", numOutputs);
310  logger->Log(msg);
311  sprintf_s(msg, 99, "InterfaceKit::display_properties Sensors: %d", numSensors);
312  logger->Log(msg);
313 
314  for (i = 0; i < numSensors; i++)
315  {
316  CPhidgetInterfaceKit_getSensorChangeTrigger(phid, i, &triggerVal);
317 
318  sprintf_s(msg, 99, "InterfaceKit::display_properties Sensor#: %d > Sensitivity Trigger: %d", i, triggerVal);
319  logger->Log(msg);
320  }
321 
322  if (numSensors)
323  {
324  sprintf_s(msg, 99, "InterfaceKit::display_properties Ratiometric: %d", ratiometric);
325  logger->Log(msg);
326  }
327 
328  return 0;
329  }
330 
331  void InterfaceKit::Initialize(Logger* prmLogger, InterfaceKitConfig* prmConfig, Bus* prmBus)
332  {
333  logger = prmLogger;
334  config = prmConfig;
335  bus = prmBus;
336 
337  int result;
338  const char* err;
339 
340  //Declare an InterfaceKit handle
341  ifKit = nullptr;
342 
343  //create the InterfaceKit object
344  CPhidgetInterfaceKit_create(&ifKit);
345 
346  //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.
347  CPhidget_set_OnAttach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), AttachHandler, this);
348  CPhidget_set_OnDetach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), DetachHandler, this);
349  CPhidget_set_OnError_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), ErrorHandler, this);
350 
351  //Registers a callback that will run if an input changes.
352  //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
353  CPhidgetInterfaceKit_set_OnInputChange_Handler(ifKit, InputChangeHandler, this);
354 
355  //Registers a callback that will run if the sensor value changes by more than the OnSensorChange trigger.
356  //Requires the handle for the IntefaceKit, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
357  CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifKit, SensorChangeHandler, this);
358 
359  //Registers a callback that will run if an output changes.
360  //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
361  //CPhidgetInterfaceKit_set_OnOutputChange_Handler(ifKit, OutputChangeHandler, this);
362 
363  //open the interfacekit for device connections
364  CPhidget_open(reinterpret_cast<CPhidgetHandle>(ifKit), config->serialno);
365 
366  //get the program to wait for an interface kit device to be attached
367  char msg[99];
368  sprintf_s(msg, 99, "InterfaceKit::ctor Waiting for interface kit %i to be attached...", config->serialno);
369  logger->Log(msg);
370  result = CPhidget_waitForAttachment(reinterpret_cast<CPhidgetHandle>(ifKit), 5000);
371  if (result)
372  {
373  CPhidget_getErrorDescription(result, &err);
374  sprintf_s(msg, 99, "InterfaceKit::ctor Problem waiting for attachment of %i: %s", config->serialno, err);
375  logger->Log(msg, Logger::Fatal);
376  config->enable = false;
377  return;
378  }
379 
380  CPhidgetInterfaceKit_setRatiometric(ifKit, config->ratiometric ? 1 : 0);
381  for (UINT i = 0; i < config->sensors.size(); i++)
382  {
383  if (config->sensors.at(i).defined)
384  {
385  CPhidgetInterfaceKit_setSensorChangeTrigger(ifKit, i, config->sensors.at(i).sensitivity); // integral values update
386  }
387  }
388 
389  for (UINT i = 0; i < config->outputs.size(); i++)
390  {
391  if (config->outputs.at(i).defined && config->outputs.at(i).trigger == "Component")
392  {
393  config->outputs.at(i).busCurrentState = bus->GetComponentCurrentStatePtr(config->outputs.at(i).guid);
394  }
395  }
396 
397  // Log the properties of the attached interface kit device
399  }
400 
402  {
403  logger->Log("InterfaceKit::Destroy...");
404  CPhidget_close(reinterpret_cast<CPhidgetHandle>(ifKit));
405  CPhidget_delete(reinterpret_cast<CPhidgetHandle>(ifKit));
406  }
407 
409  {
410  if (!attached)
411  return;
412 
413 #pragma region Command Stream
414  for (UINT i = 0; i < bus->commandStream.size(); i++)
415  {
416  Command command = bus->commandStream.at(i);
417  if (command.delay != 0.0f) continue;
418 
419  for (UINT j = 0; j < config->outputs.size(); j++)
420  {
421  if (!config->outputs.at(j).defined) continue;
422  if (config->outputs.at(j).trigger == "Component") continue;
423 
424  if (command.name == config->outputs.at(j).trigger)
425  {
426  char msg[199];
427  sprintf_s(msg, 199, "InterfaceKit::FrameMove Sending digital output signal %i to index %i!", command.ivalue, j);
428  logger->Log(msg);
429  try
430  {
431  int result = CPhidgetInterfaceKit_setOutputState(ifKit, j, command.ivalue);
432  if (result)
433  {
434  const char* err;
435  CPhidget_getErrorDescription(result, &err);
436  sprintf_s(msg, 199, "InterfaceKit::FrameMove Problem sending digital output signal: %s (%i)", err, config->serialno);
437  logger->Log(msg, Logger::Error, result);
438  }
439  }
440  catch (...)
441  {
442  logger->Log("InterfaceKit::FrameMove Exception raised and caught!", Logger::Error);
443  }
444 
445  bus->commandStream.erase(bus->commandStream.begin() + i);
446  break;
447  }
448  }
449  }
450 #pragma endregion
451 
452 #pragma region Components
453  for (UINT j = 0; j < config->outputs.size(); j++)
454  {
455  if (!config->outputs.at(j).defined) continue;
456  if (config->outputs.at(j).trigger == "Component")
457  {
458  bool currentState = *config->outputs.at(j).busCurrentState > 0.5f;
459  if (!config->outputs.at(j).internalState.is_set() || config->outputs.at(j).internalState.get() != currentState)
460  {
461  config->outputs.at(j).internalState = currentState;
462  int ivalue = currentState ? 1 : 0;
463  char msg[199];
464  sprintf_s(msg, 199, "InterfaceKit::FrameMove Sending digital output signal %i to index %i!", ivalue, j);
465  logger->Log(msg);
466  try
467  {
468  int result = CPhidgetInterfaceKit_setOutputState(ifKit, j, ivalue);
469  if (result)
470  {
471  const char* err;
472  CPhidget_getErrorDescription(result, &err);
473  sprintf_s(msg, 199, "InterfaceKit::FrameMove Problem sending digital output signal: %s (%i)", err, config->serialno);
474  logger->Log(msg, Logger::Error, result);
475  }
476  }
477  catch (...)
478  {
479  logger->Log("InterfaceKit::FrameMove Exception raised and caught!", Logger::Error);
480  }
481  }
482  }
483  }
484 #pragma endregion
485  }
486 }
CPhidgetInterfaceKitHandle ifKit
Definition: InterfaceKit.h:61
static int CCONV ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
std::vector< Command > commandStream
Definition: Bus.h:20
Definition: Logger.h:5
Systems::Fault * GetComponentFaultStatePtr(std::string guidStr)
Definition: Bus.cpp:141
static int CCONV InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
InterfaceKitConfig * config
Definition: InterfaceKit.h:58
static int CCONV SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int Value)
std::vector< Output > outputs
Definition: InterfaceKit.h:51
okay, the portable keyboard numbers don&#39;t work like the outside keypad because the outside keypad is ...
Definition: Analog.cpp:3
Definition: Bus.h:12
std::string name
command name
Definition: Command.h:11
std::vector< Input > inputs
Definition: InterfaceKit.h:40
static int CCONV DetachHandler(CPhidgetHandle IFK, void *userptr)
static int CCONV AttachHandler(CPhidgetHandle IFK, void *userptr)
Definition: InterfaceKit.cpp:5
Definition: Command.h:5
int display_properties(CPhidgetInterfaceKitHandle phid) const
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
void Initialize(Logger *logger, InterfaceKitConfig *config, Bus *prmBus)
int ivalue
Definition: Command.h:21
float * GetComponentSetStatePtr(std::string guidStr)
Definition: Bus.cpp:109
float delay
wait number of seconds before executing command
Definition: Command.h:8
std::vector< Sensor > sensors
Definition: InterfaceKit.h:29