Revision 0:775da631eb9a, committed 2010-01-17
- Comitter:
- paulg
- Date:
- Sun Jan 17 11:06:20 2010 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 775da631eb9a TextStar.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TextStar.cpp Sun Jan 17 11:06:20 2010 +0000
@@ -0,0 +1,147 @@
+/* mbed TextStar serial LCD Library
+ * Copyright (c) 2009-2010 Paul Griffith
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * Last edit: 10 Jan 2010
+ *
+ * TextStar CW-LCD-02 16x2 serial LCD module
+ * Cat's Whisker Technologies, http://www.cats-whisker.com
+ */
+
+#include "TextStar.h"
+#include "mbed.h"
+
+
+TextStar::TextStar(PinName tx, PinName rx) : Serial(tx, rx) {
+ reset();
+}
+
+void TextStar::cls() {
+ //clear screen = 254,'S'
+ //does not affect auto-blank and auto-scroll
+ this->putc('\014');
+}
+
+void TextStar::locate(int column, int row) {
+ //position cursor = 254,'P',line (0-15), col (0-15)
+ this->printf("\376P%c%c", row + 1, column + 1);
+}
+
+void TextStar::foreground(int colour) {
+ //colours not supported
+}
+
+void TextStar::background(int colour) {
+ //colours not supported
+}
+
+void TextStar::reset(int autoblank, int autoscroll) {
+ //reset screen = 254,'R'
+ this->printf("\376R");
+ //set up auto-blank and auto-scroll as per arguments
+ _autoblank = (autoblank == 0) ? 0 : 1;
+ _autoscroll = (autoscroll == 0) ? 0 : 1;
+ this->printf("\376G%c", 1 + (_autoblank * 64) + (_autoscroll * 64) );
+// this->printf("AB = %d AS = %d", _autoblank, _autoscroll);
+// wait(2.0);
+// cls();
+}
+
+void TextStar::left(void) {
+ //move cursor left = 8
+ this->putc('\010');
+}
+
+void TextStar::right(void) {
+ //move cursor right = 9
+ this->putc('\011');
+}
+
+void TextStar::down(void) {
+ //move cursor down one line = 10
+ this->putc('\012');
+}
+
+void TextStar::up(void) {
+ //move cursor up one line = 11
+ this->putc('\013');
+}
+
+void TextStar::home(void) {
+ //cursor home = 254,'H'
+ this->printf("\376H");
+}
+
+void TextStar::crlf(void) {
+ //carriage return = 13
+ this->putc('\015');
+}
+
+void TextStar::del(void) {
+ //delete character = 127
+ this->putc('\177');
+}
+
+void TextStar::set_cursor(int style) {
+ //styles: 0 = none, 1 = solid block, 2 = flash block, 3 = solid uline, 4 = flash uline
+ if (style < 0 || style > 4)
+ return;
+ this->printf("\376C%c", style);
+}
+
+void TextStar::window(int line) {
+ //move visible window to line (0-14)
+ if (line < 0 || line > 14)
+ return;
+ this->printf("\376G%c", line + 1 + (_autoblank * 64) + (_autoscroll * 128) );
+}
+
+void TextStar::scroll_up(void) {
+ //move visble window up one line
+ this->printf("\376O%c", 1);
+}
+
+void TextStar::scroll_down(void) {
+ //move visible window down one line
+ this->printf("\376O%c", 0);
+}
+
+void TextStar::bar_graph(int capped, int length, int percentage) {
+ if (length < 1 || length > 16) {
+ this->printf("Bad len %d", length);
+ wait(2.0);
+ return;
+ }
+ if (percentage < 0 || percentage > 100) {
+ this->printf("Bad val %d", percentage);
+ wait(2.0);
+ return;
+ }
+ this->printf("\376%c%c%c", (capped == 0)? 'B' : 'b', length, percentage);
+}
+
+void TextStar::custom_char(int num, char *bitmap) {
+ //define custom character = 254,'D',bm1,bm2,bm3,bm4,bm5,bm6,bm7,bm8
+ if (num < 128 || num > 135) {
+ this->printf("Bad CC num %d", num);
+ wait(2.0);
+ return;
+ }
+ this->printf("\376D%c%c%c%c%c%c%c%c%c", num, bitmap[0], bitmap[1], bitmap[2], bitmap[3],
+ bitmap[4], bitmap[5], bitmap[6], bitmap[7]);
+}
+
+void TextStar::version(void) {
+ //display firmware version and custom characters = 254,'V'
+ this->printf("\376V");
+}
+
+void TextStar::send_version(void) {
+ //send firmware version = 254,'v'
+ this->printf("\376v");
+}
+
+void TextStar::send_keys(void) {
+ //send key states = 254,'K'
+ this->printf("\376K");
+}
\ No newline at end of file
diff -r 000000000000 -r 775da631eb9a TextStar.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TextStar.h Sun Jan 17 11:06:20 2010 +0000
@@ -0,0 +1,56 @@
+/* mbed TextStar serial LCD Library
+ * Copyright (c) 2009-2010 Paul Griffith
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * Last edit: 10 Jan 2010
+ *
+ * TextStar CW-LCD-02 16x2 serial LCD module
+ * Cat's Whisker Technologies, http://www.cats-whisker.com
+ */
+
+#include "mbed.h"
+
+#ifndef MBED_TEXTSTAR_H
+#define MBED_TEXTSTAR_H
+
+class TextStar : public Serial {
+public:
+
+ TextStar(PinName tx, PinName rx);
+
+ //printf(), putc(), baud() etc are inherited from Serial
+
+ //basic commands common to all mbed Text Display libraries
+
+ void cls();
+ void locate(int column, int row);
+ void foreground(int colour);
+ void background(int colour);
+
+ //TextStar specific commands
+
+ void reset(int autoblank =1, int autoscroll =1);
+ void left();
+ void right();
+ void down();
+ void up();
+ void home();
+ void crlf();
+ void del();
+ void set_cursor(int style);
+ void window(int line);
+ void scroll_up();
+ void scroll_down();
+ void bar_graph(int capped, int length, int percentage);
+ void custom_char(int num, char *bitmap);
+ void version();
+ void send_version();
+ void send_keys();
+
+protected:
+
+ int _autoblank, _autoscroll;
+
+};
+
+#endif
diff -r 000000000000 -r 775da631eb9a main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Jan 17 11:06:20 2010 +0000
@@ -0,0 +1,290 @@
+/* Test harness for TextStar serial LCD library
+ * Paul Griffith
+ * Last edit: 10 Jan 2010
+ */
+
+#include "mbed.h"
+#include "TextStar.h"
+
+void bargraph_demo(int);
+void printhelps(void);
+void bad_args(void);
+
+Serial pc(USBTX, USBRX);
+TextStar lcd(p9, p10); // tx, rx
+
+int main() {
+ char buf[120], *cp;
+ char arg1[50], arg2[50], arg3[50];
+ int argc, cnt = 0, i, j, k, l;
+
+
+ printf("TextStar Serial LCD test harness\n");
+ printf("Type ? for help\n");
+ lcd.printf("TextStar Serial\rLCD test harness");
+
+ while (1) {
+ printf("> ");
+ cp = buf;
+ while ( (*cp++ = putchar(getchar())) != '\r') ; //get a line of input
+ *cp = '\0'; //terminate buffer
+ printf("\n");
+ argc = sscanf(buf, "%s%s%s", arg1, arg2, arg3); //extract command and arguments
+ if (argc < 1)
+ continue;
+ switch (arg1[0]) {
+
+ case 'b': //bar graph demo
+ bargraph_demo(0);
+ wait(1.0);
+ bargraph_demo(1);
+ break;
+
+ case 'c': //clear display
+ lcd.cls();
+ break;
+
+ case 'C': //set cursor style
+ if (argc != 2) {
+ bad_args();
+ break;
+ }
+ sscanf(arg2, "%d", &i); //extract style number
+ lcd.set_cursor(i);
+ break;
+
+ case 'd': //move cursor down
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.down();
+ break;
+
+ case 'D': //define custom character
+ sscanf(buf, "%s%d%x%x", arg1, &i, &j, &k); //extract arguments
+ arg3[0] = (j >> 24) & 0xFF;
+ arg3[1] = (j >> 16) & 0xFF;
+ arg3[2] = (j >> 8) & 0xFF;
+ arg3[3] = j & 0xFF;
+ arg3[4] = (k >> 24) & 0xFF;
+ arg3[5] = (k >> 16) & 0xFF;
+ arg3[6] = (k >> 8) & 0xFF;
+ arg3[7] = k & 0xFF;
+ lcd.custom_char(i, arg3);
+ lcd.cls();
+ lcd.printf("Custom chars");
+ lcd.locate(0, 1);
+ lcd.printf("128-135 %c%c%c%c%c%c%c%c", 0x80, 0x81,
+ 0x82, 0x83, 0x84, 0x85, 0x86, 0x87);
+ break;
+
+ case 'e': //enter text at current position
+ if (argc < 2) {
+ bad_args();
+ break;
+ }
+ *--cp = '\0'; //get rid of \r at end of buffer
+ sscanf(buf, "%s%n", arg1, &i); //extract arguments
+// cp = buf + i;
+// while (*cp++ == ' ') ; //find start of text
+// --cp;
+ cp = buf + i + 1; //allow leading white space
+ lcd.printf("%s", cp);
+ break;
+
+ case 'h': //cursor home
+ lcd.home();
+ break;
+
+ case 'k': //get key states
+ while (lcd.readable() )
+ lcd.getc(); //empty buffer
+ printf("Key states = ");
+ lcd.send_keys();
+ i = lcd.getc();
+ j = lcd.getc();
+ k = lcd.getc();
+ l = lcd.getc();
+ printf("%c%c%c%c\n", i, j, k, l);
+ break;
+
+ case 'K': //get key output until any char typed on pc
+ printf("Key output: ");
+ while (pc.readable() == 0) {
+ if (lcd.readable() )
+ pc.putc(lcd.getc() );
+ }
+ while (pc.readable() )
+ pc.getc();
+ printf("\nAll done\n");
+ break;
+
+ case 'o': //output demo
+ lcd.cls();
+ j = 20;
+ if (argc == 2) {
+ sscanf(arg2, "%d", &j);
+ }
+ for (i = 0; i < j; i++) {
+ lcd.printf("Count = %d\r", cnt++);
+ wait( (i == 0) ? 1.0 : 0.2);
+ }
+ break;
+
+ case 'l': //move cursor left
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.left();
+ break;
+
+ case 'p': //set cursor position
+ if (argc < 3) {
+ bad_args();
+ break;
+ }
+ sscanf(buf, "%s%d%d", arg1, &i, &j); //extract arguments
+ lcd.locate(j, i);
+ break;
+
+ case 'q': //quit
+ exit(1);
+ break;
+
+ case 'r': //move cursor right
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.right();
+ break;
+
+ case 'R': //reset display (and set autoblank (and autoscroll) )
+ if (argc == 1) {
+ lcd.reset();
+ }
+ else if (argc == 2) {
+ sscanf(arg2, "%d", &i);
+ lcd.reset(i);
+ }
+ else if (argc >= 3) {
+ sscanf(arg2, "%d", &i);
+ sscanf(arg3, "%d", &j);
+ lcd.reset(i, j);
+ }
+ break;
+
+ case 's': //scroll down
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.scroll_down();
+ break;
+
+ case 'S': //scroll up
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.scroll_up();
+ break;
+
+ case 'u': //move cursor up
+ j = 1;
+ if (argc == 2)
+ sscanf(arg2, "%d", &j);
+ for (i = 0; i < j; i++)
+ lcd.up();
+ break;
+
+ case 'v': //display firmware version
+ lcd.version();
+ break;
+
+
+ case 'w': //move visible window
+ if (argc != 2) {
+ bad_args();
+ break;
+ }
+ sscanf(arg2, "%d", &i); //extract line number
+ lcd.window(i);
+ break;
+
+ case 'x': //send hex data to display
+ argc = sscanf(buf, "%s%x%x%x%x", arg1, &i, &j, &k, &l); //extract arguments
+ if (argc > 1)
+ lcd.putc(i & 0xFF);
+ if (argc > 2)
+ lcd.putc(j & 0xFF);
+ if (argc > 3)
+ lcd.putc(k & 0xFF);
+ if (argc > 4)
+ lcd.putc(l & 0xFF);
+ break;
+
+ case '?': //print help
+ printhelps();
+ break;
+
+ default:
+ printf("?? Unknown command\n");
+ break;
+ }
+ }
+}
+
+//Command functions
+
+void printhelps(void) {
+ printf("Command summary:\n");
+ printf("b bar graph demo\n");
+ printf("c clear display\n");
+ printf("C style set cursor style (0-4)\n");
+ printf("d [n] cursor down n times (default 1)\n");
+ printf("D n bm1 bm2 define custom char n (128-135) with bitmap in hex\n");
+ printf(" bm1 = bitmap rows 1-4, bm2 = rows 5-8\n");
+ printf("e text enter text at current position\n");
+ printf("h cursor home\n");
+ printf("k get key states\n");
+ printf("K show key output\n");
+ printf("l [n] cursor left n times (default 1)\n");
+ printf("o output demo\n");
+ printf("p line col set cursor position to line (0-15) and column (0-15)\n");
+ printf("q quit\n");
+ printf("r [n] cursor right n times (default 1)\n");
+ printf("R [ab] [as] reset display, set auto-blank (0/1) and auto-scroll (0/1)\n");
+ printf("s [n] scroll down n times (default 1)\n");
+ printf("S [n] scroll up n times (default 1)\n");
+ printf("u [n] cursor up n times (default 1)\n");
+ printf("v display firmware version\n");
+ printf("w line move display window to specified line (0-14)\n");
+ printf("x b b b b send hex data to display, up to 4 bytes\n");
+}
+
+void bargraph_demo(int capped) {
+
+ int i;
+
+ lcd.cls();
+ lcd.printf("BAR GRAPH DEMO %d", capped + 1);
+ for (i = 0; i <= 100; i++) {
+ lcd.locate(0, 1);
+ lcd.bar_graph(capped, 16, i);
+ wait( (i == 0) ? 1.0 : 0.02);
+ }
+ wait(1.0);
+ for (i = 100; i >= 0; i--) {
+ lcd.locate(0, 1);
+ lcd.bar_graph(capped, 16, i);
+ wait( (i == 0) ? 1.0 : 0.02);
+ }
+}
+
+void bad_args(void) {
+ printf("?? Bad arguments\n");
+}
+
diff -r 000000000000 -r 775da631eb9a mbed.bld
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld Sun Jan 17 11:06:20 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0