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-19
Revision:
3:1114012184bf
Parent:
2:f9f27e2b64a7
Child:
4:294e8f53ebcd

File content as of revision 3:1114012184bf:

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

/*
 * ESP8266 Wireless Terminal interfacing library
 * ---------------------------------------------
 *
 * Tested with ESP firmware v0.5.4
 *
 * This library provides simplified 
 */


/**
 * ESP8266 Wireless Terminal interface
 *
 * Can also be used for sending ANSI sequences to a regular serial terminal
 */
class ESPTerm
{
    Serial* ser;
    
public:
    /** 
     * ANSI colors supported by the ESP Terminal
     */
    enum Color {
        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,
    };
    
    
    /**
     * Argument for screen or line clear commands
     */
    enum ClearMode {
        CLEAR_TO_CURSOR = 0,
        CLEAR_FROM_CURSOR = 1,
        CLEAR_ALL = 2,
    };
    

    /**
     * @brief Create a terminal instance from already initialized serial port
     *
     * Example:
     * Serial ser(PA_2, PA_3, 115200); // eg. 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, ...);
    
    
    /** 
     * @brief Print with no args
     */
    int puts(const char *str);
    
    
    /** 
     * 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(Color c);
    
    
    /** 
     * @brief Set background text color
     * @param c - color
     */ 
    void bg(Color c);
    
    
    /** 
     * @brief Set both text colors
     * @param fg - foregorund color
     * @param bg - background color
     */ 
    void colors(Color fg, 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);
};


/** Convenience alias for using class enums etc */
typedef ESPTerm VT;

#endif /* ESPTERM_H */