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 positions by one percent in 'direction'
134  * @param [in] direction either '-1' to move aperture position down, or '+1' to move it up
135  */
136 static bool changeAperturePosition(int32_t direction)
137 {
138  Dispatcher &dispatcher = Dispatcher::getInstance();
139  const Argus::Range<int32_t> aperturePositionRange = dispatcher.getDeviceAperturePositionRange();
140 
141  if ((direction != -1) && (direction != 1))
142  ORIGINATE_ERROR("Invalid direction");
143 
144  int32_t newStep = dispatcher.m_aperturePosition.get() + direction;
145 
146  newStep =
147  std::min(aperturePositionRange.max(), std::max(aperturePositionRange.min(), newStep));
148 
149  PROPAGATE_ERROR(dispatcher.m_aperturePosition.set(newStep));
150 
151  PROPAGATE_ERROR(dispatcher.message("Changed aperture position to %d in range [%d, %d]\n",
152  newStep, aperturePositionRange.min(), aperturePositionRange.max()));
153 
154  return true;
155 }
156 
157 bool App::onKey(const Key &key)
158 {
159  if ((key == Key("Escape")) ||
160  (key == Key("c", KeyModifier(KeyModifier::MASK_CONTROL))))
161  {
162  PROPAGATE_ERROR(Window::getInstance().requestExit());
163  }
164  else if (key == Key("d"))
165  {
167  }
168  else if (key == Key("Up", KeyModifier(KeyModifier::MASK_CONTROL)))
169  {
170  PROPAGATE_ERROR(changeFocusPosition(+1));
171  }
172  else if (key == Key("Down", KeyModifier(KeyModifier::MASK_CONTROL)))
173  {
174  PROPAGATE_ERROR(changeFocusPosition(-1));
175  }
176  else if (key == Key("Left", KeyModifier(KeyModifier::MASK_CONTROL)))
177  {
178  PROPAGATE_ERROR(changeAperturePosition(+1));
179  }
180  else if (key == Key("Right", KeyModifier(KeyModifier::MASK_CONTROL)))
181  {
182  PROPAGATE_ERROR(changeAperturePosition(-1));
183  }
184 
185  // silently ignore unhandled keys
186  return true;
187 }
188 
189 }; // namespace ArgusSamples
190