JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
Module.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <jevois/Core/Module.H>
19 #include <jevois/Core/VideoInput.H>
21 #include <jevois/Core/Engine.H>
23 
24 // ####################################################################################################
25 jevois::InputFrame::InputFrame(std::shared_ptr<jevois::VideoInput> const & cam, bool turbo) :
26  itsCamera(cam), itsDidGet(false), itsDidDone(false), itsTurbo(turbo)
27 { }
28 
29 // ####################################################################################################
31 {
32  // If itsCamera is invalidated, we have been moved to another object, so do not do anything here:
33  if (itsCamera.get() == nullptr) return;
34 
35  // If we did not yet get(), just end now, camera will drop this frame:
36  if (itsDidGet == false) return;
37 
38  // If we did get() but not done(), signal done now:
39  if (itsDidDone == false) try { itsCamera->done(itsImage); } catch (...) { }
40 }
41 
42 // ####################################################################################################
43 jevois::RawImage const & jevois::InputFrame::get(bool casync) const
44 {
45  itsCamera->get(itsImage);
46  itsDidGet = true;
47  if (casync && itsTurbo) itsImage.buf->sync();
48  return itsImage;
49 }
50 
51 // ####################################################################################################
53 {
54  itsCamera->done(itsImage);
55  itsDidDone = true;
56 }
57 
58 // ####################################################################################################
59 // ####################################################################################################
60 jevois::OutputFrame::OutputFrame(std::shared_ptr<jevois::VideoOutput> const & gad) :
61  itsGadget(gad), itsDidGet(false), itsDidSend(false)
62 { }
63 
64 // ####################################################################################################
66 {
67  // If itsGadget is invalidated, we have been moved to another object, so do not do anything here:
68  if (itsGadget.get() == nullptr) return;
69 
70  // If we did not get(), just end now:
71  if (itsDidGet == false) return;
72 
73  // If we did get() but not send(), send now (the image will likely contain garbage):
74  if (itsDidSend == false) try { itsGadget->send(itsImage); } catch (...) { }
75 }
76 
77 // ####################################################################################################
79 {
80  itsGadget->get(itsImage);
81  itsDidGet = true;
82  return itsImage;
83 }
84 
85 // ####################################################################################################
87 {
88  itsGadget->send(itsImage);
89  itsDidSend = true;
90 }
91 
92 // ####################################################################################################
93 // ####################################################################################################
94 jevois::Module::Module(std::string const & instance) :
95  jevois::Component(instance)
96 { }
97 
98 // ####################################################################################################
100 { }
101 
102 // ####################################################################################################
103 void jevois::Module::process(InputFrame && JEVOIS_UNUSED_PARAM(inframe), OutputFrame && JEVOIS_UNUSED_PARAM(outframe))
104 { LFATAL("Not implemented in this module"); }
105 
106 // ####################################################################################################
107 void jevois::Module::process(InputFrame && JEVOIS_UNUSED_PARAM(inframe))
108 { LFATAL("Not implemented in this module"); }
109 
110 // ####################################################################################################
111 void jevois::Module::sendSerial(std::string const & str)
112 {
113  jevois::Engine * e = dynamic_cast<jevois::Engine *>(itsParent);
114  if (e == nullptr) LFATAL("My parent is not Engine -- CANNOT SEND SERIAL");
115 
116  e->sendSerial(str);
117 }
118 
119 // ####################################################################################################
120 void jevois::Module::parseSerial(std::string const & JEVOIS_UNUSED_PARAM(str),
121  std::shared_ptr<jevois::UserInterface> JEVOIS_UNUSED_PARAM(s))
122 { throw std::runtime_error("Unsupported command"); }
123 
124 // ####################################################################################################
125 void jevois::Module::supportedCommands(std::ostream & os)
126 { os << "None" << std::endl; }
127 
void done() const
Indicate that user processing is done with the image previously obtained via get() ...
Definition: Module.C:52
RawImage const & get(bool casync=false) const
Get the next captured camera image.
Definition: Module.C:43
Exception-safe wrapper around a raw camera input frame.
Definition: Module.H:54
RawImage const & get() const
Get a pre-allocated image so that we can fill the pixel data and later send out over USB using send()...
Definition: Module.C:78
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:142
Module(std::string const &instance)
Constructor.
Definition: Module.C:94
InputFrame(InputFrame &&other)=default
Move constructor.
virtual void supportedCommands(std::ostream &os)
Human-readable description of this Module's supported custom commands.
Definition: Module.C:125
void sendSerial(std::string const &str, bool islog=false)
Send a string to all serial ports.
Definition: Engine.C:640
A component of a model hierarchy.
Definition: Component.H:176
virtual void process(InputFrame &&inframe, OutputFrame &&outframe)
Processing function, version that receives a frame from camera and sends a frame out over USB...
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
void send() const
Send an image out over USB to the host computer.
Definition: Module.C:86
virtual void sendSerial(std::string const &str)
Send a string over the 'serout' serial port.
Definition: Module.C:111
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:205
JeVois processing engine - gets images from camera sensor, processes them, and sends results over USB...
Definition: Engine.H:223
virtual void parseSerial(std::string const &str, std::shared_ptr< UserInterface > s)
Receive a string from a serial port which contains a user command.
Definition: Module.C:120
Exception-safe wrapper around a raw image to be sent over USB.
Definition: Module.H:110
~InputFrame()
Destructor, returns the buffers to the driver as needed.
Definition: Module.C:30
~OutputFrame()
Destructor, returns the buffers to the driver as needed.
Definition: Module.C:65
virtual ~Module()
Virtual destructor for safe inheritance.
Definition: Module.C:99