Support library for the ESP8266 Wireless Terminal. Can also be used for communicating with any VT100-compatible terminal.
espterm.cpp@1:09dfc9fd55f4, 2017-03-06 (annotated)
- Committer:
- MightyPork
- Date:
- Mon Mar 06 22:01:29 2017 +0000
- Revision:
- 1:09dfc9fd55f4
- Parent:
- 0:20fb68233f89
- Child:
- 3:1114012184bf
Initial code added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MightyPork | 0:20fb68233f89 | 1 | #include "mbed.h" |
MightyPork | 0:20fb68233f89 | 2 | #include "espterm.hpp" |
MightyPork | 0:20fb68233f89 | 3 | |
MightyPork | 1:09dfc9fd55f4 | 4 | #include <cstdarg> |
MightyPork | 1:09dfc9fd55f4 | 5 | |
MightyPork | 1:09dfc9fd55f4 | 6 | |
MightyPork | 1:09dfc9fd55f4 | 7 | ESPTerm::ESPTerm(Serial *s) |
MightyPork | 1:09dfc9fd55f4 | 8 | { |
MightyPork | 0:20fb68233f89 | 9 | this->ser = s; |
MightyPork | 0:20fb68233f89 | 10 | } |
MightyPork | 1:09dfc9fd55f4 | 11 | |
MightyPork | 1:09dfc9fd55f4 | 12 | |
MightyPork | 1:09dfc9fd55f4 | 13 | ESPTerm::ESPTerm(void) |
MightyPork | 1:09dfc9fd55f4 | 14 | { |
MightyPork | 1:09dfc9fd55f4 | 15 | this->ser = new Serial(PA_2, PA_3, 115200); |
MightyPork | 1:09dfc9fd55f4 | 16 | } |
MightyPork | 1:09dfc9fd55f4 | 17 | |
MightyPork | 1:09dfc9fd55f4 | 18 | |
MightyPork | 1:09dfc9fd55f4 | 19 | int ESPTerm::printf(const char *format, ...) |
MightyPork | 1:09dfc9fd55f4 | 20 | { |
MightyPork | 1:09dfc9fd55f4 | 21 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 22 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 23 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 24 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 25 | |
MightyPork | 1:09dfc9fd55f4 | 26 | return r; |
MightyPork | 1:09dfc9fd55f4 | 27 | } |
MightyPork | 1:09dfc9fd55f4 | 28 | |
MightyPork | 1:09dfc9fd55f4 | 29 | // alias of printf |
MightyPork | 1:09dfc9fd55f4 | 30 | int ESPTerm::print(const char *format, ...) |
MightyPork | 1:09dfc9fd55f4 | 31 | { |
MightyPork | 1:09dfc9fd55f4 | 32 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 33 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 34 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 35 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 36 | |
MightyPork | 1:09dfc9fd55f4 | 37 | return r; |
MightyPork | 1:09dfc9fd55f4 | 38 | } |
MightyPork | 1:09dfc9fd55f4 | 39 | |
MightyPork | 1:09dfc9fd55f4 | 40 | |
MightyPork | 1:09dfc9fd55f4 | 41 | int ESPTerm::println(const char *format, ...) { |
MightyPork | 1:09dfc9fd55f4 | 42 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 43 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 44 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 45 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 46 | |
MightyPork | 1:09dfc9fd55f4 | 47 | r += ser->puts("\r\n"); |
MightyPork | 1:09dfc9fd55f4 | 48 | return r; |
MightyPork | 1:09dfc9fd55f4 | 49 | } |
MightyPork | 1:09dfc9fd55f4 | 50 | |
MightyPork | 1:09dfc9fd55f4 | 51 | |
MightyPork | 1:09dfc9fd55f4 | 52 | void ESPTerm::fg(ANSI_Color c) |
MightyPork | 1:09dfc9fd55f4 | 53 | { |
MightyPork | 1:09dfc9fd55f4 | 54 | int ci = c; |
MightyPork | 1:09dfc9fd55f4 | 55 | if(ci > 7) { |
MightyPork | 1:09dfc9fd55f4 | 56 | ci += (90-8); |
MightyPork | 1:09dfc9fd55f4 | 57 | } else { |
MightyPork | 1:09dfc9fd55f4 | 58 | ci += 30; |
MightyPork | 1:09dfc9fd55f4 | 59 | } |
MightyPork | 1:09dfc9fd55f4 | 60 | ser->printf("\033[%dm", ci); |
MightyPork | 1:09dfc9fd55f4 | 61 | } |
MightyPork | 1:09dfc9fd55f4 | 62 | |
MightyPork | 1:09dfc9fd55f4 | 63 | |
MightyPork | 1:09dfc9fd55f4 | 64 | void ESPTerm::bg(ANSI_Color c) |
MightyPork | 1:09dfc9fd55f4 | 65 | { |
MightyPork | 1:09dfc9fd55f4 | 66 | int ci = c; |
MightyPork | 1:09dfc9fd55f4 | 67 | if(ci > 7) { |
MightyPork | 1:09dfc9fd55f4 | 68 | ci += (100-8); |
MightyPork | 1:09dfc9fd55f4 | 69 | } else { |
MightyPork | 1:09dfc9fd55f4 | 70 | ci += 40; |
MightyPork | 1:09dfc9fd55f4 | 71 | } |
MightyPork | 1:09dfc9fd55f4 | 72 | ser->printf("\033[%dm", ci); |
MightyPork | 1:09dfc9fd55f4 | 73 | } |
MightyPork | 1:09dfc9fd55f4 | 74 | |
MightyPork | 1:09dfc9fd55f4 | 75 | |
MightyPork | 1:09dfc9fd55f4 | 76 | void ESPTerm::colors(ANSI_Color fg, ANSI_Color bg) |
MightyPork | 1:09dfc9fd55f4 | 77 | { |
MightyPork | 1:09dfc9fd55f4 | 78 | this->fg(fg); |
MightyPork | 1:09dfc9fd55f4 | 79 | this->bg(bg); |
MightyPork | 1:09dfc9fd55f4 | 80 | } |
MightyPork | 1:09dfc9fd55f4 | 81 | |
MightyPork | 1:09dfc9fd55f4 | 82 | |
MightyPork | 1:09dfc9fd55f4 | 83 | void ESPTerm::reset_attribs(void) |
MightyPork | 1:09dfc9fd55f4 | 84 | { |
MightyPork | 1:09dfc9fd55f4 | 85 | ser->puts("\033[0m"); |
MightyPork | 1:09dfc9fd55f4 | 86 | } |
MightyPork | 1:09dfc9fd55f4 | 87 | |
MightyPork | 1:09dfc9fd55f4 | 88 | |
MightyPork | 1:09dfc9fd55f4 | 89 | // alias |
MightyPork | 1:09dfc9fd55f4 | 90 | void ESPTerm::rst(void) |
MightyPork | 1:09dfc9fd55f4 | 91 | { |
MightyPork | 1:09dfc9fd55f4 | 92 | this->reset_attribs(); |
MightyPork | 1:09dfc9fd55f4 | 93 | } |
MightyPork | 1:09dfc9fd55f4 | 94 | |
MightyPork | 1:09dfc9fd55f4 | 95 | |
MightyPork | 1:09dfc9fd55f4 | 96 | void ESPTerm::go_to(int y, int x) |
MightyPork | 1:09dfc9fd55f4 | 97 | { |
MightyPork | 1:09dfc9fd55f4 | 98 | ser->printf("\033[%d;%dH", y, x); |
MightyPork | 1:09dfc9fd55f4 | 99 | } |
MightyPork | 1:09dfc9fd55f4 | 100 | |
MightyPork | 1:09dfc9fd55f4 | 101 | |
MightyPork | 1:09dfc9fd55f4 | 102 | void ESPTerm::clear_screen(ClearMode mode) |
MightyPork | 1:09dfc9fd55f4 | 103 | { |
MightyPork | 1:09dfc9fd55f4 | 104 | ser->printf("\033[%dJ", mode); |
MightyPork | 1:09dfc9fd55f4 | 105 | } |
MightyPork | 1:09dfc9fd55f4 | 106 | |
MightyPork | 1:09dfc9fd55f4 | 107 | |
MightyPork | 1:09dfc9fd55f4 | 108 | void ESPTerm::clear_line(ClearMode mode) |
MightyPork | 1:09dfc9fd55f4 | 109 | { |
MightyPork | 1:09dfc9fd55f4 | 110 | ser->printf("\033[%dK", mode); |
MightyPork | 1:09dfc9fd55f4 | 111 | } |
MightyPork | 1:09dfc9fd55f4 | 112 | |
MightyPork | 1:09dfc9fd55f4 | 113 | |
MightyPork | 1:09dfc9fd55f4 | 114 | void ESPTerm::screen_reset(void) |
MightyPork | 1:09dfc9fd55f4 | 115 | { |
MightyPork | 1:09dfc9fd55f4 | 116 | ser->puts("\033c"); |
MightyPork | 1:09dfc9fd55f4 | 117 | } |
MightyPork | 1:09dfc9fd55f4 | 118 | |
MightyPork | 1:09dfc9fd55f4 | 119 | |
MightyPork | 1:09dfc9fd55f4 | 120 | void ESPTerm::show_cursor(bool yes) |
MightyPork | 1:09dfc9fd55f4 | 121 | { |
MightyPork | 1:09dfc9fd55f4 | 122 | if (yes) |
MightyPork | 1:09dfc9fd55f4 | 123 | ser->puts("\033[?25h"); |
MightyPork | 1:09dfc9fd55f4 | 124 | else |
MightyPork | 1:09dfc9fd55f4 | 125 | ser->puts("\033[?25l"); |
MightyPork | 1:09dfc9fd55f4 | 126 | } |
MightyPork | 1:09dfc9fd55f4 | 127 | |
MightyPork | 1:09dfc9fd55f4 | 128 | |
MightyPork | 1:09dfc9fd55f4 | 129 | void ESPTerm::factory_reset(void) |
MightyPork | 1:09dfc9fd55f4 | 130 | { |
MightyPork | 1:09dfc9fd55f4 | 131 | ser->puts("\033]FR\a"); |
MightyPork | 1:09dfc9fd55f4 | 132 | } |
MightyPork | 1:09dfc9fd55f4 | 133 | |
MightyPork | 1:09dfc9fd55f4 | 134 | |
MightyPork | 1:09dfc9fd55f4 | 135 | void ESPTerm::set_screen_size(int rows, int cols) |
MightyPork | 1:09dfc9fd55f4 | 136 | { |
MightyPork | 1:09dfc9fd55f4 | 137 | ser->printf("\033]W%d;%d\a", rows, cols); |
MightyPork | 1:09dfc9fd55f4 | 138 | } |