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

Revision:
4:294e8f53ebcd
Parent:
3:1114012184bf
Child:
5:7379bd37f3e2
diff -r 1114012184bf -r 294e8f53ebcd espterm.hpp
--- a/espterm.hpp	Sun Mar 19 10:34:48 2017 +0000
+++ b/espterm.hpp	Sun Mar 19 17:19:16 2017 +0000
@@ -10,7 +10,10 @@
  *
  * This library provides simplified 
  */
-
+ 
+// Max nr of CSI parameters
+#define CSI_N_MAX 3
+#define OSC_BUF_LEN 16
 
 /**
  * ESP8266 Wireless Terminal interface
@@ -19,9 +22,46 @@
  */
 class ESPTerm
 {
+    /** 
+     * Status flag set by the rx handler when device responds with "device OK".
+     * Cleared before sending Device Status Request.
+     */
+    bool device_ok;
+    
+    
+    /** Serial comm used to talk to the ESP */
     Serial* ser;
     
-public:
+    
+    /** Internal init func, called from constructor */
+    void init(Serial* ser);
+    
+    
+    /** Internal rxchar handler with parser */
+    void ser_rx_char(void);
+    
+    // ---- Ragel parser variables ----
+    int cs;
+
+    // The CSI code is built here
+    char csi_leading;      //!< Leading char, 0 if none
+    int  csi_ni;           //!< Number of the active digit
+    int  csi_n[CSI_N_MAX]; //!< Param digits
+    char csi_char;         //!< CSI action char (end)
+
+    char osc_buff[OSC_BUF_LEN];
+    int osc_i;
+    
+    // Ragel parser funcs
+    void ansi_parser(const char *newdata, size_t len);
+    void apars_handle_plainchar(char c);
+    void apars_handle_csi(char lead, const int* nums, char keychar);
+    void apars_handle_osc(const char *c);
+    void apars_handle_badseq(void);
+    
+    
+public:    
+    
     /** 
      * ANSI colors supported by the ESP Terminal
      */
@@ -39,10 +79,39 @@
      */
     enum ClearMode {
         CLEAR_TO_CURSOR = 0,
-        CLEAR_FROM_CURSOR = 1,
+        CLEAR_FROM_CURSOR = 1,        
         CLEAR_ALL = 2,
     };
     
+    
+    enum CtlKey {
+        KEY_LEFT=0, KEY_RIGHT, KEY_UP, KEY_DOWN
+    };
+    
+    
+    /** Fired when user taps the screen at the given coords */
+    void (*on_mouse_click)(int row, int col);
+    
+    
+    /** Fired when the user presses one of the blue buttons (index is 1 thru 5) */
+    void (*on_button)(int index);
+    
+    
+    /** Fired on rx of the "ESP reset notification" byte */
+    void (*on_esp_reset)(void);
+    
+    
+    /** Fired on each received plain character */
+    void (*on_char_rx)(char c);
+    
+    
+    /** Fired on each key press (keyboard arrows etc) */
+    void (*on_key_press)(CtlKey key);
+    
+    
+    /** Handle generic OSC command (may be used in the future) */
+    void (*on_osc_rx)(const char *str);
+    
 
     /**
      * @brief Create a terminal instance from already initialized serial port
@@ -53,6 +122,18 @@
      */
     ESPTerm(Serial *s);
     
+
+    /**
+     * @brief Create a terminal instance with given params
+     */
+    ESPTerm(PinName txPin, PinName rxPin, int baud=115200);
+    
+
+    /**
+     * @brief Create a terminal instance with given params & 115200 baud
+     */
+    //ESPTerm(PinName txPin, PinName rxPin);
+    
     
     /**
      * @brief Create with Serial at (PA_2, PA_3, 115200)
@@ -60,6 +141,9 @@
     ESPTerm(void);
     
     
+    // ----- Printing -----
+    
+    
     /** 
      * @brief printf() over the Serial
      *
@@ -77,7 +161,19 @@
     /** 
      * @brief Print with no args
      */
-    int puts(const char *str);
+    int puts(const char *str) 
+    {    
+        return ser->puts(str);
+    }
+    
+    
+    /** 
+     * @brief Print a single char
+     */
+    void putc(const char c) 
+    {
+        ser->putc(c);    
+    }
     
     
     /** 
@@ -89,6 +185,9 @@
     int println(const char *fmt, ...);    
     
     
+    // ----- Colors -----
+    
+    
     /** 
      * @brief Set foreground text color
      * @param c - color
@@ -108,7 +207,11 @@
      * @param fg - foregorund color
      * @param bg - background color
      */ 
-    void colors(Color fg, Color bg);
+    void colors(Color fg, Color bg)
+    {
+        this->fg(fg);
+        this->bg(bg);
+    }
     
     
     /** 
@@ -120,7 +223,13 @@
     /** 
      * @brief Reset all attributes (alias)
      */ 
-    void rst(void);
+    void rst(void)
+    {
+        this->reset_attribs();
+    }
+    
+    
+    // ----- Cursor & clearing -----
     
     
     /**
@@ -174,6 +283,9 @@
     void show_cursor(bool yes);
     
     
+    // ----- System cmds -----
+    
+    
     /**
      * @brief Perform a factory reset of the ESP Terminal
      *
@@ -196,6 +308,27 @@
      * @param cols - number of columns (screen width)
      */
     void set_screen_size(int rows, int cols);
+    
+    
+    /** 
+     * Clear the Device OK flag 
+     */
+    void clear_status(void)
+    {
+        device_ok = false;    
+    }
+      
+        
+    /**
+     * Query device status, clears "device_ok" and it'll be set by
+     * the response message.
+     *
+     * To use the query function, call clear_status() first, and then poll 
+     * query_status() with some delays in between.
+     *
+     * @return the status flag, from a previous query
+     */
+    bool query_status(void);
 };