Support library for the ESP8266 Wireless Terminal. Can also be used for communicating with any VT100-compatible terminal.
espterm.cpp@3:1114012184bf, 2017-03-19 (annotated)
- Committer:
- MightyPork
- Date:
- Sun Mar 19 10:34:48 2017 +0000
- Revision:
- 3:1114012184bf
- Parent:
- 1:09dfc9fd55f4
- Child:
- 4:294e8f53ebcd
some code cleaning
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 | 3:1114012184bf | 18 | // ----- Printing ----- |
MightyPork | 3:1114012184bf | 19 | |
MightyPork | 1:09dfc9fd55f4 | 20 | |
MightyPork | 1:09dfc9fd55f4 | 21 | int ESPTerm::printf(const char *format, ...) |
MightyPork | 1:09dfc9fd55f4 | 22 | { |
MightyPork | 1:09dfc9fd55f4 | 23 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 24 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 25 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 26 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 27 | |
MightyPork | 1:09dfc9fd55f4 | 28 | return r; |
MightyPork | 1:09dfc9fd55f4 | 29 | } |
MightyPork | 1:09dfc9fd55f4 | 30 | |
MightyPork | 1:09dfc9fd55f4 | 31 | // alias of printf |
MightyPork | 1:09dfc9fd55f4 | 32 | int ESPTerm::print(const char *format, ...) |
MightyPork | 1:09dfc9fd55f4 | 33 | { |
MightyPork | 1:09dfc9fd55f4 | 34 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 35 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 36 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 37 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 38 | |
MightyPork | 1:09dfc9fd55f4 | 39 | return r; |
MightyPork | 1:09dfc9fd55f4 | 40 | } |
MightyPork | 1:09dfc9fd55f4 | 41 | |
MightyPork | 3:1114012184bf | 42 | // print without args |
MightyPork | 3:1114012184bf | 43 | int ESPTerm::puts(const char *str) |
MightyPork | 3:1114012184bf | 44 | { |
MightyPork | 3:1114012184bf | 45 | return ser->puts(str); |
MightyPork | 3:1114012184bf | 46 | } |
MightyPork | 3:1114012184bf | 47 | |
MightyPork | 1:09dfc9fd55f4 | 48 | |
MightyPork | 1:09dfc9fd55f4 | 49 | int ESPTerm::println(const char *format, ...) { |
MightyPork | 1:09dfc9fd55f4 | 50 | std::va_list arg; |
MightyPork | 1:09dfc9fd55f4 | 51 | va_start(arg, format); |
MightyPork | 1:09dfc9fd55f4 | 52 | int r = ser->vprintf(format, arg); |
MightyPork | 1:09dfc9fd55f4 | 53 | va_end(arg); |
MightyPork | 1:09dfc9fd55f4 | 54 | |
MightyPork | 1:09dfc9fd55f4 | 55 | r += ser->puts("\r\n"); |
MightyPork | 1:09dfc9fd55f4 | 56 | return r; |
MightyPork | 1:09dfc9fd55f4 | 57 | } |
MightyPork | 1:09dfc9fd55f4 | 58 | |
MightyPork | 1:09dfc9fd55f4 | 59 | |
MightyPork | 3:1114012184bf | 60 | // ----- Colors ----- |
MightyPork | 3:1114012184bf | 61 | |
MightyPork | 3:1114012184bf | 62 | |
MightyPork | 3:1114012184bf | 63 | void ESPTerm::fg(Color c) |
MightyPork | 1:09dfc9fd55f4 | 64 | { |
MightyPork | 1:09dfc9fd55f4 | 65 | int ci = c; |
MightyPork | 1:09dfc9fd55f4 | 66 | if(ci > 7) { |
MightyPork | 1:09dfc9fd55f4 | 67 | ci += (90-8); |
MightyPork | 1:09dfc9fd55f4 | 68 | } else { |
MightyPork | 1:09dfc9fd55f4 | 69 | ci += 30; |
MightyPork | 1:09dfc9fd55f4 | 70 | } |
MightyPork | 1:09dfc9fd55f4 | 71 | ser->printf("\033[%dm", ci); |
MightyPork | 1:09dfc9fd55f4 | 72 | } |
MightyPork | 1:09dfc9fd55f4 | 73 | |
MightyPork | 1:09dfc9fd55f4 | 74 | |
MightyPork | 3:1114012184bf | 75 | void ESPTerm::bg(Color c) |
MightyPork | 1:09dfc9fd55f4 | 76 | { |
MightyPork | 1:09dfc9fd55f4 | 77 | int ci = c; |
MightyPork | 1:09dfc9fd55f4 | 78 | if(ci > 7) { |
MightyPork | 1:09dfc9fd55f4 | 79 | ci += (100-8); |
MightyPork | 1:09dfc9fd55f4 | 80 | } else { |
MightyPork | 1:09dfc9fd55f4 | 81 | ci += 40; |
MightyPork | 1:09dfc9fd55f4 | 82 | } |
MightyPork | 1:09dfc9fd55f4 | 83 | ser->printf("\033[%dm", ci); |
MightyPork | 1:09dfc9fd55f4 | 84 | } |
MightyPork | 1:09dfc9fd55f4 | 85 | |
MightyPork | 1:09dfc9fd55f4 | 86 | |
MightyPork | 3:1114012184bf | 87 | void ESPTerm::colors(Color fg, Color bg) |
MightyPork | 1:09dfc9fd55f4 | 88 | { |
MightyPork | 1:09dfc9fd55f4 | 89 | this->fg(fg); |
MightyPork | 1:09dfc9fd55f4 | 90 | this->bg(bg); |
MightyPork | 1:09dfc9fd55f4 | 91 | } |
MightyPork | 1:09dfc9fd55f4 | 92 | |
MightyPork | 1:09dfc9fd55f4 | 93 | |
MightyPork | 1:09dfc9fd55f4 | 94 | void ESPTerm::reset_attribs(void) |
MightyPork | 1:09dfc9fd55f4 | 95 | { |
MightyPork | 1:09dfc9fd55f4 | 96 | ser->puts("\033[0m"); |
MightyPork | 1:09dfc9fd55f4 | 97 | } |
MightyPork | 1:09dfc9fd55f4 | 98 | |
MightyPork | 1:09dfc9fd55f4 | 99 | |
MightyPork | 1:09dfc9fd55f4 | 100 | // alias |
MightyPork | 1:09dfc9fd55f4 | 101 | void ESPTerm::rst(void) |
MightyPork | 1:09dfc9fd55f4 | 102 | { |
MightyPork | 1:09dfc9fd55f4 | 103 | this->reset_attribs(); |
MightyPork | 1:09dfc9fd55f4 | 104 | } |
MightyPork | 1:09dfc9fd55f4 | 105 | |
MightyPork | 1:09dfc9fd55f4 | 106 | |
MightyPork | 3:1114012184bf | 107 | // ----- Cursor control & erasing ----- |
MightyPork | 3:1114012184bf | 108 | |
MightyPork | 3:1114012184bf | 109 | |
MightyPork | 1:09dfc9fd55f4 | 110 | void ESPTerm::go_to(int y, int x) |
MightyPork | 1:09dfc9fd55f4 | 111 | { |
MightyPork | 1:09dfc9fd55f4 | 112 | ser->printf("\033[%d;%dH", y, x); |
MightyPork | 1:09dfc9fd55f4 | 113 | } |
MightyPork | 1:09dfc9fd55f4 | 114 | |
MightyPork | 1:09dfc9fd55f4 | 115 | |
MightyPork | 1:09dfc9fd55f4 | 116 | void ESPTerm::clear_screen(ClearMode mode) |
MightyPork | 1:09dfc9fd55f4 | 117 | { |
MightyPork | 1:09dfc9fd55f4 | 118 | ser->printf("\033[%dJ", mode); |
MightyPork | 1:09dfc9fd55f4 | 119 | } |
MightyPork | 1:09dfc9fd55f4 | 120 | |
MightyPork | 1:09dfc9fd55f4 | 121 | |
MightyPork | 1:09dfc9fd55f4 | 122 | void ESPTerm::clear_line(ClearMode mode) |
MightyPork | 1:09dfc9fd55f4 | 123 | { |
MightyPork | 1:09dfc9fd55f4 | 124 | ser->printf("\033[%dK", mode); |
MightyPork | 1:09dfc9fd55f4 | 125 | } |
MightyPork | 1:09dfc9fd55f4 | 126 | |
MightyPork | 1:09dfc9fd55f4 | 127 | |
MightyPork | 1:09dfc9fd55f4 | 128 | void ESPTerm::screen_reset(void) |
MightyPork | 1:09dfc9fd55f4 | 129 | { |
MightyPork | 1:09dfc9fd55f4 | 130 | ser->puts("\033c"); |
MightyPork | 1:09dfc9fd55f4 | 131 | } |
MightyPork | 1:09dfc9fd55f4 | 132 | |
MightyPork | 1:09dfc9fd55f4 | 133 | |
MightyPork | 1:09dfc9fd55f4 | 134 | void ESPTerm::show_cursor(bool yes) |
MightyPork | 1:09dfc9fd55f4 | 135 | { |
MightyPork | 1:09dfc9fd55f4 | 136 | if (yes) |
MightyPork | 1:09dfc9fd55f4 | 137 | ser->puts("\033[?25h"); |
MightyPork | 1:09dfc9fd55f4 | 138 | else |
MightyPork | 1:09dfc9fd55f4 | 139 | ser->puts("\033[?25l"); |
MightyPork | 1:09dfc9fd55f4 | 140 | } |
MightyPork | 1:09dfc9fd55f4 | 141 | |
MightyPork | 1:09dfc9fd55f4 | 142 | |
MightyPork | 3:1114012184bf | 143 | // ----- System commands ----- |
MightyPork | 3:1114012184bf | 144 | |
MightyPork | 3:1114012184bf | 145 | |
MightyPork | 1:09dfc9fd55f4 | 146 | void ESPTerm::factory_reset(void) |
MightyPork | 1:09dfc9fd55f4 | 147 | { |
MightyPork | 1:09dfc9fd55f4 | 148 | ser->puts("\033]FR\a"); |
MightyPork | 1:09dfc9fd55f4 | 149 | } |
MightyPork | 1:09dfc9fd55f4 | 150 | |
MightyPork | 1:09dfc9fd55f4 | 151 | |
MightyPork | 1:09dfc9fd55f4 | 152 | void ESPTerm::set_screen_size(int rows, int cols) |
MightyPork | 1:09dfc9fd55f4 | 153 | { |
MightyPork | 1:09dfc9fd55f4 | 154 | ser->printf("\033]W%d;%d\a", rows, cols); |
MightyPork | 1:09dfc9fd55f4 | 155 | } |