Terminal interface for communicating with serial ANSI/VT100 terminals
Dependents: Terminal_HelloWorld geigercounter01 geigercounter04 DiscoTech ... more
Revision 0:2bf27af3c759, committed 2009-12-31
- Comitter:
- simon
- Date:
- Thu Dec 31 09:50:48 2009 +0000
- Child:
- 1:96ae39e58792
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Terminal.cpp Thu Dec 31 09:50:48 2009 +0000
@@ -0,0 +1,40 @@
+/* mbed ANSI/VT100 Terminal Library
+ * Copyright (c) 2007-2009 sford
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "Terminal.h"
+
+#include "mbed.h"
+
+Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
+
+void Terminal::cls() {
+ this->printf("\033[2J");
+}
+
+void Terminal::locate(int column, int row) {
+ // Cursor Home <ESC>[{ROW};{COLUMN}H
+ this->printf("\033[%d;%dH%c", row + 1, column + 1);
+}
+
+static int rgb888tobgr111(int colour) {
+ int r = (colour >> 23) & 1;
+ int g = (colour >> 15) & 1;
+ int b = (colour >> 7) & 1;
+ return (b << 2) | (g << 1) | (r << 0);
+}
+
+void Terminal::foreground(int colour) {
+ // Set Attribute Mode <ESC>[{n}m
+ // Foreground Colours : 30 + bgr
+ int c = 30 + rgb888tobgr111(colour);
+ this->printf("\033[%dm", c);
+}
+
+void Terminal::background(int colour) {
+ // Set Attribute Mode <ESC>[{n}m
+ // Background Colours : 40 + bgr
+ int c = 40 + rgb888tobgr111(colour);
+ this->printf("\033[%dm", c);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Terminal.h Thu Dec 31 09:50:48 2009 +0000
@@ -0,0 +1,27 @@
+/* mbed Terminal TextDisplay Library
+ * Copyright (c) 2007-2009 sford
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * Implementation of ANSI/VT100 Terminal escape codes
+ * for use with e.g. Teraterm, Hyperterminal
+ */
+
+#include "mbed.h"
+
+#ifndef MBED_TERMINAL_H
+#define MBED_TERMINAL_H
+
+class Terminal : public Serial {
+public:
+
+ Terminal(PinName tx, PinName rx);
+
+ // printf(), put(), baud() etc - inherited from Serial
+
+ void cls();
+ void locate(int column, int row);
+ void foreground(int colour);
+ void background(int colour);
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Dec 31 09:50:48 2009 +0000
@@ -0,0 +1,21 @@
+// simple test for a ANSI/VT100 Terminal, sford
+
+#include "mbed.h"
+
+#include "Terminal.h"
+
+Terminal term(USBTX, USBRX); // tx, rx
+
+int main() {
+ term.printf("Hello World!\nHow are you?");
+
+ wait(2);
+
+ term.locate(3,1);
+ term.foreground(0xFF0000);
+ term.printf("I'm Great!");
+
+ wait(3);
+
+ term.cls();
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 31 09:50:48 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/32af5db564d4
Simon Ford