JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
GUIserial.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2024 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#ifdef JEVOIS_PRO
19
21
22// ##############################################################################################################
25
26// ##############################################################################################################
27bool jevois::GUIserial::readSome(std::string & str)
28{
29 bool ret = jevois::Serial::readSome(str);
30
31 if (ret)
32 {
33 // We received a complete string, keep a copy:
34 std::lock_guard<std::mutex> _(itsDataMtx);
35 itsData.push_back(std::make_pair(true, str));
36 }
37
38 return ret;
39}
40
41// ##############################################################################################################
42void jevois::GUIserial::writeString(std::string const & str)
43{
44 {
45 // Keep a copy:
46 std::lock_guard<std::mutex> _(itsDataMtx);
47 itsData.push_back(std::make_pair(false, str));
48 while (itsData.size() > 1000) itsData.pop_front();
49 }
50
51 // Send it off over serial:
53}
54
55// ##############################################################################################################
57{
58 // Keep this in sync with GUIconsole::draw() for aesthetic consistency
59
60 static ImVec4 const col_from { 1.0f, 0.8f, 0.6f, 1.0f };
61 static ImVec4 const col_to { 0.6f, 0.6f, 0.4f, 1.0f };
62 static ImVec4 const col_to_ok { 0.2f, 1.0f, 0.2f, 1.0f };
63 static ImVec4 const col_to_dbg { 0.2f, 0.2f, 1.0f, 1.0f };
64 static ImVec4 const col_to_inf { 0.4f, 0.7f, 0.4f, 1.0f };
65 static ImVec4 const col_to_err { 1.0f, 0.4f, 0.4f, 1.0f };
66 static ImVec4 const col_to_ftl { 1.0f, 0.0f, 0.0f, 1.0f };
67
68 ImGui::Text("Legend: "); ImGui::SameLine();
69 ImGui::TextColored(col_from, "From Serial Device; "); ImGui::SameLine();
70 ImGui::TextColored(col_to, "To Serial Device, with accents for "); ImGui::SameLine();
71 ImGui::TextColored(col_to_ok, "OK, "); ImGui::SameLine();
72 ImGui::TextColored(col_to_dbg, "DBG, "); ImGui::SameLine();
73 ImGui::TextColored(col_to_inf, "INF, "); ImGui::SameLine();
74 ImGui::TextColored(col_to_err, "ERR, "); ImGui::SameLine();
75 ImGui::TextColored(col_to_ftl, "FTL");
76 ImGui::Separator();
77
78 ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
79
80 // Right click on the log to get a popup menu that can clear it:
81 if (ImGui::BeginPopupContextWindow())
82 {
83 if (ImGui::Selectable("Clear")) clear();
84 ImGui::EndPopup();
85 }
86
87 // Tighten spacing:
88 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1));
89
90 // Colorize and draw each data line:
91 std::lock_guard<std::mutex> _(itsDataMtx);
92 for (auto const & p : itsData)
93 {
94 ImVec4 color;
95 auto const & s = p.second;
96
97 if (p.first)
98 // Data we have read from serial device:
99 color = col_from;
100 else
101 {
102 // Data we have written to serial device:
103 if (s == "OK") color = col_to_ok;
104 else if (jevois::stringStartsWith(s, "DBG ")) color = col_to_dbg;
105 else if (jevois::stringStartsWith(s, "INF ")) color = col_to_inf;
106 else if (jevois::stringStartsWith(s, "ERR ")) color = col_to_err;
107 else if (jevois::stringStartsWith(s, "FTL ")) color = col_to_ftl;
108 else color = col_to;
109 }
110 ImGui::PushStyleColor(ImGuiCol_Text, color);
111 ImGui::TextUnformatted(s.c_str());
112 ImGui::PopStyleColor();
113 }
114
115 static bool autoScroll = true;
116 static bool scrollToBottom = true;
117
118 if (scrollToBottom || (autoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) ImGui::SetScrollHereY(1.0f);
119 scrollToBottom = false;
120
121 ImGui::PopStyleVar();
122 ImGui::EndChild();
123}
124
125// ##############################################################################################################
127{
128 std::lock_guard<std::mutex> _(itsDataMtx);
129 itsData.clear();
130}
131
132#endif // JEVOIS_PRO
void clear()
Clear the contents of the window.
Definition GUIserial.C:126
void draw()
Render into ImGui.
Definition GUIserial.C:56
bool readSome(std::string &str) override
Read some bytes if available, and return true and a string when one is complete.
Definition GUIserial.C:27
virtual ~GUIserial()
Virtual destructor for safe inheritance.
Definition GUIserial.C:23
void writeString(std::string const &str) override
Write a string, using the line termination convention of serial::linestyle.
Definition GUIserial.C:42
void writeString(std::string const &str) override
Write a string, using the line termination convention of serial::linestyle.
Definition Serial.C:316
bool readSome(std::string &str) override
Read some bytes if available, and return true and a string when one is complete.
Definition Serial.C:262
bool stringStartsWith(std::string const &str, std::string const &prefix)
Return true if str starts with prefix (including if both strings are equal)
Definition Utils.C:294