Support library for the ESP8266 Wireless Terminal. Can also be used for communicating with any VT100-compatible terminal.

espterm.hpp

Committer:
MightyPork
Date:
2017-03-06
Revision:
1:09dfc9fd55f4
Parent:
0:20fb68233f89
Child:
2:f9f27e2b64a7

File content as of revision 1:09dfc9fd55f4:

#ifndef ESPTERM_H
#define ESPTERM_H
#include "mbed.h"


/** 
 * ANSI colors supported by the ESP Terminal
 */
typedef enum {
    BLACK = 0, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, GRAY_LT,
    GRAY, RED_LT, GREEN_LT, YELLOW_LT, BLUE_LT, PURPLE_LT, CYAN_LT, WHITE,
    
    // aliases    
    RED_DK=1, GREEN_DK=2, YELLOW_DK=3, BLUE_DK=4, PURPLE_DK=5, CYAN_DK=6, SILVER=7,
} ANSI_Color;


/**
 * Argument for screen or line clear commands
 */
typedef enum {
    CLEAR_TO_CURSOR = 0,
    CLEAR_FROM_CURSOR = 1,
    CLEAR_ALL = 2,
} ClearMode;


/**
 * ESP8266 Wireless Terminal interface
 *
 * Can also be used for sending ANSI sequences to a regular serial terminal
 */
class ESPTerm
{
    Serial* ser;
    
public:
    /**
     * @brief Create a terminal instance from already initialized serial port
     *
     * Example:
     * Serial ser(PA_2, PA_3, 115200); // for STM32F042
     * ESPTerm term(&ser);
     */
    ESPTerm(Serial *s);
    
    
    /**
     * @brief Create with Serial at (PA_2, PA_3, 115200)
     */
    ESPTerm(void);
    
    
    /** 
     * @brief printf() over the Serial
     *
     * Use for raw strings without formatting, or with custom escape sequences.
     */
    int printf(const char *fmt, ...);
    
    
    /** 
     * @brief Alias for printf()
     */
    int print(const char *fmt, ...);
    
    
    /** 
     * printf() over the Serial, followed by a newline (\r\n)
     *
     * Use for raw strings without formatting, or with custom escape sequences.
     * Especially useful for log messages, automatically terminating the line.
     */
    int println(const char *fmt, ...);    
    
    
    /** 
     * @brief Set foreground text color
     * @param c - color
     */    
    void fg(ANSI_Color c);
    
    
    /** 
     * @brief Set background text color
     * @param c - color
     */ 
    void bg(ANSI_Color c);
    
    
    /** 
     * @brief Set both text colors
     * @param fg - foregorund color
     * @param bg - background color
     */ 
    void colors(ANSI_Color fg, ANSI_Color bg);
    
    
    /** 
     * @brief Send \e[0m - reset all attributes
     */ 
    void reset_attribs(void);
    
    
    /** 
     * @brief Reset all attributes (alias)
     */ 
    void rst(void);
    
    
    /**
     * @brief Move cursor to a given position
     *
     * Screen coordinates are 1-based, starting left-top,
     * growing down and right.
     *
     * @param y - row
     * @param x - column
     */     
    void go_to(int y, int x);
    
    
    /**
     * @brief Clear screen
     *
     * Replace part of the screen with spaces (ASCII 32) of the currently 
     * selected foregorund and background colors.
     *
     * @param mode - what to clear: part or all
     */
    void clear_screen(ClearMode mode);
    
    
    /**
     * @brief Clear in line
     *
     * Replace part of the current line with spaces (ASCII 32) of the 
     * currently selected foregorund and background colors.
     *
     * @param mode - what to clear: part or all
     */
    void clear_line(ClearMode mode);
    
    
    /**
     * @brief Clear the whole screen and move cursor to 1,1
     */
    void screen_reset(void);
    
    
    /**
     * @brief Set cursor visibility
     *
     * If hidden, there will be no blinking square on the screen,
     * but the cursor still works (ie. go_to works).
     *
     * Hiding the cursor is useful for ASCII-art GUIs (dialog boxes etc)
     */
    void show_cursor(bool yes);
    
    
    /**
     * @brief Perform a factory reset of the ESP Terminal
     *
     * This clears any saved WiFi config, restores the default AP name
     * and switches to AP mode. The module will reset itself.
     *
     * Expect some ASCII output from the module (and a burst of garbage
     * during the reset).
     */
    void factory_reset(void);


    /**
     * @brief Set screen size
     *
     * The total number of characters (rows*cols) is limited by the Terminal
     * firmware, at v0.5.2 that is 2000 characters (25x80)
     * 
     * @param rows - number of rows (screen height)
     * @param cols - number of columns (screen width)
     */
    void set_screen_size(int rows, int cols);
};

#endif /* ESPTERM_H */