Argus Camera Sample Argus Camera Sample

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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
44 App::~App()
45 {
46 shutdown();
47 }
48
49 bool App::initialize()
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
63 bool App::shutdown()
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 bool App::onKey(const Key &key)
133 {
134 if ((key == Key("Escape")) ||
135 (key == Key("c", KeyModifier(KeyModifier::MASK_CONTROL))))
136 {
137 PROPAGATE_ERROR(Window::getInstance().requestExit());
138 }
139 else if (key == Key("d"))
140 {
141 Dispatcher::getInstance().dumpSessionInfo();
142 }
143 else if (key == Key("Up", KeyModifier(KeyModifier::MASK_CONTROL)))
144 {
145 PROPAGATE_ERROR(changeFocusPosition(+1));
146 }
147 else if (key == Key("Down", KeyModifier(KeyModifier::MASK_CONTROL)))
148 {
149 PROPAGATE_ERROR(changeFocusPosition(-1));
150 }
151
152 // silently ignore unhandled keys
153 return true;
154 }
155
156 }; // namespace ArgusSamples
157