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

espterm.cpp

Committer:
MightyPork
Date:
2017-03-19
Revision:
3:1114012184bf
Parent:
1:09dfc9fd55f4
Child:
4:294e8f53ebcd

File content as of revision 3:1114012184bf:

#include "mbed.h"
#include "espterm.hpp"

#include <cstdarg>


ESPTerm::ESPTerm(Serial *s)
{
    this->ser = s;
}


ESPTerm::ESPTerm(void)
{
    this->ser = new Serial(PA_2, PA_3, 115200);
}

// ----- Printing -----


int ESPTerm::printf(const char *format, ...)
{    
    std::va_list arg;
    va_start(arg, format);
    int r = ser->vprintf(format, arg);
    va_end(arg);
    
    return r;
}

// alias of printf
int ESPTerm::print(const char *format, ...)
{    
    std::va_list arg;
    va_start(arg, format);
    int r = ser->vprintf(format, arg);
    va_end(arg);
    
    return r;
}

// print without args
int ESPTerm::puts(const char *str)
{    
    return ser->puts(str);
}


int ESPTerm::println(const char *format, ...) {
    std::va_list arg;
    va_start(arg, format);
    int r = ser->vprintf(format, arg);
    va_end(arg);
        
    r += ser->puts("\r\n");    
    return r;   
}


// ----- Colors -----


void ESPTerm::fg(Color c)
{
    int ci = c;
    if(ci > 7) {
        ci += (90-8);
    } else {
        ci += 30;
    }
    ser->printf("\033[%dm", ci);
}


void ESPTerm::bg(Color c)
{
    int ci = c;
    if(ci > 7) {
        ci += (100-8);
    } else {
        ci += 40;
    }
    ser->printf("\033[%dm", ci);
}


void ESPTerm::colors(Color fg, Color bg)
{
    this->fg(fg);
    this->bg(bg);
}


void ESPTerm::reset_attribs(void)
{
    ser->puts("\033[0m");
}


// alias
void ESPTerm::rst(void)
{
    this->reset_attribs();
}


// ----- Cursor control & erasing -----


void ESPTerm::go_to(int y, int x)
{
    ser->printf("\033[%d;%dH", y, x);
}


void ESPTerm::clear_screen(ClearMode mode)
{
    ser->printf("\033[%dJ", mode);
}


void ESPTerm::clear_line(ClearMode mode)
{
    ser->printf("\033[%dK", mode);
}


void ESPTerm::screen_reset(void)
{
    ser->puts("\033c");
}


void ESPTerm::show_cursor(bool yes)
{
    if (yes)
        ser->puts("\033[?25h");
    else
        ser->puts("\033[?25l");
}


// ----- System commands -----


void ESPTerm::factory_reset(void)
{
    ser->puts("\033]FR\a");
}


void ESPTerm::set_screen_size(int rows, int cols)
{
    ser->printf("\033]W%d;%d\a", rows, cols);
}