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, 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 "Renderer.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 'Esc' to exit.\n";
57  PROPAGATE_ERROR(m_options.addDescription(description));
58 
59  return true;
60 }
61 
63 {
64  PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
65 
66  // shutdown the renderer
67  PROPAGATE_ERROR(Renderer::getInstance().shutdown());
68 
69  // shutdown the window
70  PROPAGATE_ERROR(Window::getInstance().shutdown());
71 
72  // shutdown the dispatcher
73  PROPAGATE_ERROR(Dispatcher::getInstance().shutdown());
74 
75  return true;
76 }
77 
78 bool App::run(int argc, char **argv)
79 {
81 
82  PROPAGATE_ERROR(initialize());
83 
85 
86  // parse and execute the options
87  PROPAGATE_ERROR(m_options.parse(argc, argv));
88 
89  // if exit had not been requested start the window event loop
90  if (!m_options.requestedExit())
91  {
92  Window &window = Window::getInstance();
93 
94  // start the active module
95  PROPAGATE_ERROR(start());
96 
97  // start the event loop
98  PROPAGATE_ERROR(window.eventLoop());
99  }
100 
101  return true;
102 }
103 
104 /**
105  * Moves the focus position by one percent in 'direction'
106  * @param [in] direction either '-1' to move focus position down, or '+1' to move it up
107  */
108 static bool changeFocusPosition(int32_t direction)
109 {
110  Dispatcher &dispatcher = Dispatcher::getInstance();
111  const Argus::Range<int32_t> focusPositionRange = dispatcher.getDeviceFocusPositionRange();
112 
113  if ((direction != -1) && (direction != 1))
114  ORIGINATE_ERROR("Invalid direction");
115 
116  const int32_t diff = ((focusPositionRange.max - focusPositionRange.min) + 99) / 100;
117 
118  int32_t newPosition = dispatcher.m_focusPosition.get() + diff * direction;
119 
120  newPosition =
121  std::min(focusPositionRange.max, std::max(focusPositionRange.min, newPosition));
122 
123  PROPAGATE_ERROR(dispatcher.m_focusPosition.set(newPosition));
124 
125  PROPAGATE_ERROR(dispatcher.message("Changed focuser position to %d in range [%d, %d]\n",
126  newPosition, focusPositionRange.min, focusPositionRange.max));
127 
128  return true;
129 }
130 
131 bool App::onKey(const Key &key)
132 {
133  if ((key == Key("Escape")) ||
134  (key == Key("c", KeyModifier(KeyModifier::MASK_CONTROL))))
135  {
136  PROPAGATE_ERROR(Window::getInstance().requestExit());
137  }
138  else if (key == Key("Up", KeyModifier(KeyModifier::MASK_CONTROL)))
139  {
140  PROPAGATE_ERROR(changeFocusPosition(+1));
141  }
142  else if (key == Key("Down", KeyModifier(KeyModifier::MASK_CONTROL)))
143  {
144  PROPAGATE_ERROR(changeFocusPosition(-1));
145  }
146 
147  // silently ignore unhandled keys
148  return true;
149 }
150 
151 }; // namespace ArgusSamples
152