PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
synchronousprinter.h
Go to the documentation of this file.
1 
26 #pragma once
27 
29 
30 namespace PropWare {
31 
40  public:
47  SynchronousPrinter (const Printer &printer)
48  : m_printer(&printer),
49  m_lock(locknew()),
50  m_borrowed(false) {
51  lockclr(this->m_lock);
52  }
53 
58  lockclr(this->m_lock);
59  lockret(this->m_lock);
60  }
61 
66  bool has_lock () const {
67  return -1 != this->m_lock;
68  }
69 
78  bool refreshLock () {
79  if (this->has_lock()) {
80  // Wait for any other cogs using the lock to return
81  while (lockset(this->m_lock));
82  lockclr(this->m_lock);
83  lockret(this->m_lock);
84  }
85 
86  this->m_lock = locknew();
87  return this->has_lock();
88  }
89 
97  const Printer *borrow_printer () {
98  while (lockset(this->m_lock));
99  this->m_borrowed = true;
100  return this->m_printer;
101  }
102 
106  bool return_printer (const Printer *printer) {
107  if (printer == this->m_printer) {
108  lockclr(this->m_lock);
109  this->m_borrowed = false;
110  return true;
111  } else
112  return false;
113  }
114 
118  template<typename T>
119  void print (const T var) const {
120  while (lockset(this->m_lock));
121  this->m_printer->print(var);
122  lockclr(this->m_lock);
123  }
124 
130  void println (const char string[]) const {
131  while (lockset(this->m_lock));
132  this->m_printer->println(string);
133  lockclr(this->m_lock);
134  }
135 
139  void printf (const char fmt[]) const {
140  while (lockset(this->m_lock));
141  this->m_printer->puts(fmt);
142  lockclr(this->m_lock);
143  }
144 
148  template<typename T, typename... Targs>
149  void printf (const char fmt[], const T first, const Targs... remaining) const {
150  while (lockset(this->m_lock));
151  this->m_printer->printf(fmt, first, remaining...);
152  lockclr(this->m_lock);
153  }
154 
155  protected:
156  const Printer *m_printer;
157  int m_lock;
158  bool m_borrowed;
159 };
160 
161 }
162 
pwSyncOut
const PropWare::SynchronousPrinter pwSyncOut
Global and shared instance for easy printing to the terminal (thread safe)
printer.h
PropWare::SynchronousPrinter::has_lock
bool has_lock() const
Determine if an instance of a SynchronousPrinter successfully retrieved a lock.
Definition: synchronousprinter.h:66
PropWare::Printer::printf
void printf(const char fmt[], const T first, const Targs... remaining) const
Similar in functionality to the C-standard function printf.
Definition: printer.h:497
PropWare::SynchronousPrinter::refreshLock
bool refreshLock()
Retrieve a new lock.
Definition: synchronousprinter.h:78
PropWare::SynchronousPrinter::~SynchronousPrinter
~SynchronousPrinter()
Ensure that, when a SynchronousPrinter is no longer being used, the lock is returned.
Definition: synchronousprinter.h:57
PropWare::SynchronousPrinter::print
void print(const T var) const
Definition: synchronousprinter.h:119
PropWare::SynchronousPrinter::printf
void printf(const char fmt[]) const
Definition: synchronousprinter.h:139
PropWare::SynchronousPrinter::SynchronousPrinter
SynchronousPrinter(const Printer &printer)
Creates a synchronous instance of a Printer that can be used from multiple cogs simultaneously.
Definition: synchronousprinter.h:47
PropWare::SynchronousPrinter::borrow_printer
const Printer * borrow_printer()
Retrieve the printer and acquire the lock. Useful when a class that only supports Printer and not Syn...
Definition: synchronousprinter.h:97
lockset
#define lockset(lockid)
Set a lock.
Definition: propeller.h:163
lockret
#define lockret(lockid)
Return lock to pool.
Definition: propeller.h:155
PropWare::Printer::print
void print(const char c, const Format &format=DEFAULT_FORMAT) const
Print a single character.
Definition: printer.h:580
lockclr
#define lockclr(lockid)
Clear lock.
Definition: propeller.h:170
PropWare::SynchronousPrinter::return_printer
bool return_printer(const Printer *printer)
After calling SynchronousPrinter::borrow_printer, this method.
Definition: synchronousprinter.h:106
locknew
#define locknew()
Get a new lock from the pool of Propeller hardware locks.
Definition: propeller.h:147
PropWare::SynchronousPrinter::println
void println(const char string[]) const
Print a string along with a newline at the end.
Definition: synchronousprinter.h:130
PropWare::SynchronousPrinter::printf
void printf(const char fmt[], const T first, const Targs... remaining) const
Definition: synchronousprinter.h:149
PropWare::SynchronousPrinter
Print formatted text to a serial terminal, an LCD, or any other device from any cog at any time with ...
Definition: synchronousprinter.h:39
PropWare::Printer::puts
void puts(const char string[]) const
Send a null-terminated character array.
Definition: printer.h:188
PropWare::Printer
Container class that has formatting methods for human-readable output. This class can be constructed ...
Definition: printer.h:76
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Printer::println
void println(const char string[]) const
Print a null-terminated string followed by a newline (' ')
Definition: printer.h:599