DUDS
Distributed Update of Data from Something
TextDisplay.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
11 #include <duds/general/Errors.hpp>
12 #include <thread>
13 
14 namespace duds { namespace hardware { namespace display {
15 
17 columnsize(-1), rowsize(-1), cpos(-1), rpos(-1) { }
18 
19 TextDisplay::TextDisplay(unsigned int c, unsigned int r) :
20 columnsize(c), rowsize(r), cpos(-1), rpos(-1) { }
21 
23 
25  // next line?
26  if (++cpos >= columnsize) {
27  cpos = 0;
28  if (++rpos >= rowsize) {
29  rpos = 0;
30  }
31  return true;
32  }
33  return false;
34 }
35 
36 void TextDisplay::move(unsigned int c, unsigned int r) {
37  // range check
38  if ((c >= columnsize) || (r >= rowsize)) {
42  );
43  }
44  // check for a change
45  if ((c != cpos) || (r != rpos)) {
46  moveImpl(c, r);
47  // record new position
48  cpos = c;
49  rpos = r;
50  }
51 }
52 
53 void TextDisplay::write(int c) {
54  writeImpl(c);
55  // advance position; may require repositioning
56  if (advance()) {
57  moveImpl(cpos, rpos);
58  }
59 }
60 
61 void TextDisplay::writeImpl(const std::string &text) {
62  // loop through characters
63  std::string::const_iterator iter = text.begin();
64  do {
65  write(*iter);
66  } while (++iter != text.end());
67 }
68 
69 void TextDisplay::write(const std::string &text) {
70  if (text.empty()) {
71  // already done
72  return;
73  }
74  writeImpl(text);
75 }
76 
78  const std::string &text,
79  unsigned int c,
80  unsigned int r
81 ) {
82  move(c, r);
83  // do the write
84  write(text);
85 }
86 
88  const std::string &text,
89  unsigned int c,
90  unsigned int r
91 ) {
92  // range check
93  if ((c >= columnsize) || (r >= rowsize)) {
97  );
98  }
99  if (text.empty()) {
100  // already done
101  return;
102  }
103  writeImpl(text, c, r);
104 }
105 
106 void TextDisplay::clearTo(unsigned int c, unsigned int r) {
107  // range check
108  if ((c >= columnsize) || (r >= rowsize)) {
112  );
113  }
114  while ((cpos != c) || (rpos != r)) {
115  write(' ');
116  }
117  write(' ');
118 }
119 
120 } } }
boost::error_info< struct Info_DisplayPosition, Info_DisplayColRow > TextDisplayPositionInfo
Column and row of a display position as part of an error.
The specified location is beyond the bounds of the display.
void move(unsigned int c, unsigned int r)
Moves the cursor to the given location.
Definition: TextDisplay.cpp:36
std::uint8_t cpos
Cursor column position.
Definition: TextDisplay.hpp:44
Stores column and row data for display errors.
virtual void writeImpl(int c)=0
Writes a single character onto the display at the current cursor location.
std::uint8_t columnsize
Number of columns on the display.
Definition: TextDisplay.hpp:36
std::uint8_t rowsize
Number of rows on the display.
Definition: TextDisplay.hpp:40
std::uint8_t rpos
Cursor row position.
Definition: TextDisplay.hpp:48
virtual ~TextDisplay()=0
Allows destruction of a pointer of this base class to properly destruct the derived object...
Definition: TextDisplay.cpp:22
TextDisplay()
Initializes the object with an invalid display size and cursor position.
Definition: TextDisplay.cpp:16
virtual void moveImpl(unsigned int c, unsigned int r)=0
Moves the display&#39;s cursor to the indicated position.
General errors.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
void clearTo(unsigned int c, unsigned int r)
Clear text from the current cursor position to the given position, inclusive.
void write(int c)
Writes a single character onto the display at the current cursor location and advances the cursor...
Definition: TextDisplay.cpp:53
virtual bool advance()
Advances the column position, and if it goes off the visible portion of the display, updates the row position.
Definition: TextDisplay.cpp:24
boost::error_info< struct Info_DisplaySize, Info_DisplayColRow > TextDisplaySizeInfo
Column and row size of a display as part of an error.