Console input

Files CCore/inc/ReadCon.h CCore/src/ReadCon.cpp

ReadCon is a class that can read a console input. Any class with the following interface can be used as a console reader. There is a default console reader, defined in the header CCore/inc/ReadCon.h. Console reader performs the "non-buffered read without echo".


class ReadCon : NoCopy
 {
   ....

  public:

   ReadCon();
   
   ~ReadCon();
   
   // get
   
   char get();
   
   bool get(MSec timeout,char &ret);
   
   bool get(TimeScope time_scope,char &ret);
   
   // put
   
   void put(char ch);
   
   void put(const char *str,ulen len);
   
   void put(StrLen str);
 };

get() returns the next character from the console. This function can block indefinitely.

get(MSec,char &) returns the next character from the console, but waits up to time-out. The return value is true, if the character was received.

get(TimeScope,char &) the same as previous, time-out is given as the TimeScope.

put(char) puts the single character to the console.

put(const char *,ulen) puts the string of characters to the console.

put(StrLen) puts the string of characters to the console.

All methods put() flushes output.

HCore console reader

HCore ReadCon is built upon the target-provided class Sys::ConRead, declared in the header sys/SysCon.h.


struct Sys::ConRead
 {
  // private

  ....

  // public
  
  struct IOResult
   {
    ulen len;
    ErrorType error;
   };
  
  ErrorType init() noexcept;
  
  ErrorType exit() noexcept;
  
  IOResult read(char *buf,ulen len) noexcept;
  
  IOResult read(char *buf,ulen len,MSec timeout) noexcept;
 };

XCore console reader

XCore ReadCon constructor has the extra argument timeout. It is used in methods put() for nano-packet allocation. Normally, you should use the default value.


class ReadCon : NoCopy
 {
   ....

  public:

   ReadCon(MSec timeout=DefaultTimeout);
   

This class is implemented using the class Sys::OwnConInput, declared in the header sys/SysCon.h.


/* consts */ 

enum ConInputResult
 {
  Con_Drop = 0,
  Con_Echo,
  Con_NoEcho,
  Con_EOL
 };
 
/* types */  

using ConInputFunction = Function<ConInputResult (char)> ;

/* class OwnConInput */

class OwnConInput : NoCopy
 {
   ....
   
  public:
   
   OwnConInput(TextLabel name,ConInputFunction input_any);
   
   ~OwnConInput();
 };

Constructor of this class "owns" the console input and directs it to the provided function. The function can be called in any execution context, including interrupt context. It is called when a character has arrived, the character is passed as the argument. The function must return a value, which indicates the required reaction:

Con_Drop — character must be counted as dropped.

Con_Echo — character must be echoed.

Con_NoEcho — character must not be echoed.

Con_EOL — character must be echoed as the End-Of-Line character.

Destructor releases the console input.

(XCore) StdioReadCon

XCore provides an implementation of some C standard library functions, including stdio functions. By default, the stdin stream is not connected to any input. To connect this stream to the console input, the class StdioReadCon is used.


class StdioReadCon : NoCopy
 {
   ....

  public:
   
   StdioReadCon();
   
   ~StdioReadCon();
   
   class Register;
   
   class Access;
 };

Constructor makes connection, destructor disconnects.

(XCore) RedirectPTPCon

Usually XCore console is backed by a serial port. But it is possible to redirect console input/output to another device. The class RedirectPTPCon redirects the console to a PTP Con client device.


class RedirectPTPCon : NoCopy
 {
   ....
    
  public:  
  
   RedirectPTPCon(StrLen con_device_name,StrLen name,const Net::PTPCon::Cfg &cfg=Net::PTPCon::Cfg());
   
   ~RedirectPTPCon();
 };

Constructor opens PTP console with the given name and configuration parameters using the given PTP Con device. Then it redirects console to the PTP console.

Destructor stops redirection and closes the PTP console.

ReadPTPCon

The class ReadPTPCon is a PTP Con device reader. It is declared in the header print/PrintPTPCon.h.


class ReadPTPCon : NoCopy
 {
   ....

  public:
   
   // constructors
   
   explicit ReadPTPCon(PTPConOpenClose &con_openclose,MSec timeout=DefaultTimeout,ulen max_packets=DefaultMaxPackets);
   
   ~ReadPTPCon();
   
   // get
  
   char get();
   
   bool get(MSec timeout,char &ret);
   
   bool get(TimeScope time_scope,char &ret);
   
   // put, ignore errors
   
   void put(char ch);
   
   void put(const char *str,ulen len);
   
   void put(StrLen str);
 };

Constructor binds the object to the PTPConOpenClose object to perform operations on the opened PTP console.