JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
StdioInterface.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 
19 #include <jevois/Debug/Log.H>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <sys/select.h>
23 
24 // ####################################################################################################
25 jevois::StdioInterface::StdioInterface(std::string const & instance) :
26  jevois::UserInterface(instance), itsRunning(true)
27 {
28  itsThread = std::thread([&]{
29  struct timeval tv; fd_set fds; tv.tv_sec = 0; tv.tv_usec = 30000;
30  while (itsRunning.load())
31  {
32  FD_ZERO(&fds);
33  FD_SET(STDIN_FILENO, &fds);
34  int ret = select(STDIN_FILENO+1, &fds, nullptr, nullptr, &tv);
35  if (ret == -1) LERROR("Ignoring error on stdin: " << strerror(errno));
36  else if (ret > 0) // some input is available, read an entire line
37  {
38  std::string str; std::getline(std::cin, str);
39  std::lock_guard<std::mutex> _(itsMtx);
40  itsString = std::move(str);
41  }
42  }
43  });
44 }
45 
46 // ####################################################################################################
48 {
49  itsRunning.store(false);
50  itsThread.join();
51 }
52 
53 // ####################################################################################################
54 bool jevois::StdioInterface::readSome(std::string & str)
55 {
56  std::lock_guard<std::mutex> _(itsMtx);
57  if (itsString.empty() == false) { str = std::move(itsString); itsString = std::string(); return true; }
58  return false;
59 }
60 
61 // ####################################################################################################
62 void jevois::StdioInterface::writeString(std::string const & str)
63 {
64  std::lock_guard<std::mutex> _(itsMtx);
65  std::cout << str << std::endl;
66 }
67 
68 // ####################################################################################################
71 
jevois::StdioInterface::readSome
bool readSome(std::string &str) override
Read some bytes if available, and return true and a string when one is complete.
Definition: StdioInterface.C:54
jevois::UserInterface
Abstract base class for a string-based user interface.
Definition: UserInterface.H:32
StdioInterface.H
jevois::UserInterface::Type::Stdio
@ Stdio
jevois
Definition: Concepts.dox:1
jevois::StdioInterface::type
UserInterface::Type type() const override
Return our port type, here always Stdio.
Definition: StdioInterface.C:69
Log.H
jevois::StdioInterface::writeString
void writeString(std::string const &str) override
Write a string, using the line termination convention of serial::linestyle.
Definition: StdioInterface.C:62
jevois::StdioInterface::~StdioInterface
virtual ~StdioInterface()
Destructor.
Definition: StdioInterface.C:47
jevois::UserInterface::Type
Type
Enum for the interface type.
Definition: UserInterface.H:59
jevois::StdioInterface::StdioInterface
StdioInterface(std::string const &instance)
Constructor.
Definition: StdioInterface.C:25