Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
App.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "App.h"
30 #include "Error.h"
31 
32 #include "Dispatcher.h"
33 #include "Composer.h"
34 #include "PerfTracker.h"
35 
36 namespace ArgusSamples
37 {
38 
39 App::App(const char *appName)
40  : m_options(appName)
41 {
42 }
43 
45 {
46  shutdown();
47 }
48 
50 {
51  PROPAGATE_ERROR(Window::getInstance().registerObserver(this));
52 
53  const char *description =
54  "Press 'Ctrl-Up' to increase the focus position, press 'Ctrl-Down' to decrease the focus\n"
55  "position.\n"
56  "Press 'd' to dump runtime information.\n"
57  "Press 'Esc' to exit.\n";
58  PROPAGATE_ERROR(m_options.addDescription(description));
59 
60  return true;
61 }
62 
64 {
65  PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
66 
67  // shutdown the composer
68  PROPAGATE_ERROR(Composer::getInstance().shutdown());
69 
70  // shutdown the window
71  PROPAGATE_ERROR(Window::getInstance().shutdown());
72 
73  // shutdown the dispatcher
74  PROPAGATE_ERROR(Dispatcher::getInstance().shutdown());
75 
76  return true;
77 }
78 
79 bool App::run(int argc, char **argv)
80 {
81  PROPAGATE_ERROR(PerfTracker::getInstance().onEvent(GLOBAL_EVENT_APP_START));
82 
83  PROPAGATE_ERROR(initialize());
84 
85  PROPAGATE_ERROR(PerfTracker::getInstance().onEvent(GLOBAL_EVENT_APP_INITIALIZED));
86 
87  // parse and execute the options
88  PROPAGATE_ERROR(m_options.parse(argc, argv));
89 
90  // if exit had not been requested start the window event loop
91  if (!m_options.requestedExit())
92  {
93  Window &window = Window::getInstance();
94 
95  // start the active module
96  PROPAGATE_ERROR(start());
97 
98  // start the event loop
99  PROPAGATE_ERROR(window.eventLoop());
100  }
101 
102  return true;
103 }
104 
105 /**
106  * Moves the focus position by one percent in 'direction'
107  * @param [in] direction either '-1' to move focus position down, or '+1' to move it up
108  */
109 static bool changeFocusPosition(int32_t direction)
110 {
111  Dispatcher &dispatcher = Dispatcher::getInstance();
112  const Argus::Range<int32_t> focusPositionRange = dispatcher.getDeviceFocusPositionRange();
113 
114  if ((direction != -1) && (direction != 1))
115  ORIGINATE_ERROR("Invalid direction");
116 
117  const int32_t diff = ((focusPositionRange.max() - focusPositionRange.min()) + 99) / 100;
118 
119  int32_t newPosition = dispatcher.m_focusPosition.get() + diff * direction;
120 
121  newPosition =
122  std::min(focusPositionRange.max(), std::max(focusPositionRange.min(), newPosition));
123 
124  PROPAGATE_ERROR(dispatcher.m_focusPosition.set(newPosition));
125 
126  PROPAGATE_ERROR(dispatcher.message("Changed focuser position to %d in range [%d, %d]\n",
127  newPosition, focusPositionRange.min(), focusPositionRange.max()));
128 
129  return true;
130 }
131 
132 /**
133  * Moves the aperture motor steps by one percent in 'direction'
134  * @param [in] direction either '-1' to move aperture motor step down, or '+1' to move it up
135  */
136 static bool changeApertureMotorStep(int32_t direction)
137 {
138  Dispatcher &dispatcher = Dispatcher::getInstance();
139  const Argus::Range<int32_t> apertureMotorStepRange = dispatcher.getDeviceApertureMotorStepRange();
140  const int32_t diff = 10;
141  int32_t i, newStep;
142 
143  if ((direction != -1) && (direction != 1))
144  ORIGINATE_ERROR("Invalid direction");
145 
146  for (i = 0; i < 10; i++)
147  {
148  newStep = dispatcher.m_apertureMotorStep.get() + i * diff * direction;
149  newStep =
150  std::min(apertureMotorStepRange.max(), std::max(apertureMotorStepRange.min(), newStep));
151 
152  PROPAGATE_ERROR(dispatcher.m_apertureMotorStep.set(newStep));
153 
154  PROPAGATE_ERROR(dispatcher.message("Changed aperture motor step to %d in range [%d, %d]\n",
155  newStep, apertureMotorStepRange.min(), apertureMotorStepRange.max()));
156  }
157  return true;
158 }
159 
160 /**
161  * Set the aperture motor steps/second
162  *
163  */
164 static bool changeApertureMotorSpeed()
165 {
166  Dispatcher &dispatcher = Dispatcher::getInstance();
167  const Argus::Range<float> apertureMotorSpeedRange = dispatcher.getDeviceApertureMotorSpeedRange();
168 
169  float newSpeed = dispatcher.m_apertureMotorSpeed.get();
170 
171  newSpeed =
172  std::min(apertureMotorSpeedRange.max(), std::max(apertureMotorSpeedRange.min(), newSpeed));
173 
174  PROPAGATE_ERROR(dispatcher.m_apertureMotorSpeed.set(apertureMotorSpeedRange.min()));
175 
176  PROPAGATE_ERROR(dispatcher.message("Changed aperture motor speed to %d in range [%d, %d]\n",
177  newSpeed, apertureMotorSpeedRange.min(), apertureMotorSpeedRange.max()));
178 
179  return true;
180 }
181 
182 bool App::onKey(const Key &key)
183 {
184  if ((key == Key("Escape")) ||
185  (key == Key("c", KeyModifier(KeyModifier::MASK_CONTROL))))
186  {
187  PROPAGATE_ERROR(Window::getInstance().requestExit());
188  }
189  else if (key == Key("d"))
190  {
192  }
193  else if (key == Key("Up", KeyModifier(KeyModifier::MASK_CONTROL)))
194  {
195  PROPAGATE_ERROR(changeFocusPosition(+1));
196  }
197  else if (key == Key("Down", KeyModifier(KeyModifier::MASK_CONTROL)))
198  {
199  PROPAGATE_ERROR(changeFocusPosition(-1));
200  }
201  else if (key == Key("Left", KeyModifier(KeyModifier::MASK_CONTROL)))
202  {
203  PROPAGATE_ERROR(changeApertureMotorStep(+1));
204  }
205  else if (key == Key("Right", KeyModifier(KeyModifier::MASK_CONTROL)))
206  {
207  PROPAGATE_ERROR(changeApertureMotorStep(-1));
208  }
209  else
210  {
211  PROPAGATE_ERROR(changeApertureMotorSpeed());
212  }
213  // silently ignore unhandled keys
214  return true;
215 }
216 
217 }; // namespace ArgusSamples
218