KeyboardAdapter

Defines

BS
LF
CR
ESC
DEL
C2_CSI_TERMINAL_MIN
C2_CSI_TERMINAL_MAX
THRESHOLD
CSI_BUFFER_SIZE
class KeyboardAdapter : public InputInterface
#include <KeyboardAdapter.h>

Input interface to keyboard. Uses default buttons layout:

  • Arrows for LEFT/RIGHT/UP/DOWN;

  • Enter for ENTER;

  • Escape for BACK;

  • Delete for CLEAR;

  • Backspace for BACKSPACE;

Keyboard can send multiple-bytes commands. Implementation should convert it to one byte command.

Implementation details.

Mapping:

  • First 128 of ASCII -> as is

  • \r -> ENTER

  • \n -> ENTER

  • \r\n -> ENTER

  • ESC -> BACK

  • ESC [ A (up arrow) -> UP

  • ESC [ B (down arrow) -> DOWN

  • ESC [ C (right arrow) -> RIGHT

  • ESC [ D (left arrow) -> LEFT

  • ESC [ 3 ~ (Delete button) -> CLEAR

Public Functions

inline KeyboardAdapter(LcdMenu *menu, Stream *stream)
inline virtual void observe() override

Private Types

enum class CodeSet

Values:

enumerator C0

Main ASCII code set. Initially defined as part of ASCII, the default C0 control code set is now defined in ISO 6429 (ECMA-48).

enumerator C1

Fe Escape sequences. Starts with ESC. Terminates with [0x40, 0x5F].

enumerator C2_CSI

Control Sequence Introducer. Starts with ESC [. Terminates with [0x40, 0x7E].

enumerator C2_DCS

Device Control String. Starts with ESC P. Terminates with ESC .

enumerator C2_OSC

Operating System Command. Starts with ESC ]. Terminates with ESC . Currently not implemented.

enumerator C3

Expect string terminator. Currently not implemented.

Private Functions

inline void reset()

Reset to initial state.

inline bool hasLastChar()
inline void saveLastChar(unsigned char command)
inline void handleIdle()

Handle idle state when there are no input for some time.

See also

THRESHOLD - timeout in ms.

inline void handleReceived(unsigned char command)

Handle received command.

Parameters:

command – the received command

Private Members

Stream *stream = NULL

Input stream.

CodeSet codeSet = CodeSet::C0

Internal state of current code set. As stream receives bytes asynchronously, multiple bytes command can arrive in several calls. Need to store current state between calls.

unsigned char lastChar

Last received character. Used to detect 2 chars sequences, e.g. \r\n.

unsigned long lastCharTimestamp

Milliseconds timestamp of last received character. Used for detecting ESC with no chars next or single \r without \n.

char csiBuffer[CSI_BUFFER_SIZE]

Buffer to store already read values of “intermediate bytes”. Multiple bytes command for CSI is in form of ESC [ <intermediate bytes> <terminal byte>.

uint8_t csiBufferCursor = 0

Points to next available byte in csiBuffer.