eDisp library and sample code

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
todotani
Date:
Sun Mar 07 00:45:31 2010 +0000
Child:
1:134b6e987450
Commit message:

Changed in this revision

eDisp.cpp Show annotated file Show diff for this revision Revisions of this file
eDisp.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eDisp.cpp	Sun Mar 07 00:45:31 2010 +0000
@@ -0,0 +1,75 @@
+/* mbed eDISP Library
+ * Copyright (c) 2010 todotani
+ * Version 0.1 (March 6, 2010)
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "eDisp.h"
+#include "error.h"
+
+using namespace mbed;
+
+eDisp::eDisp(PinName tx, PinName rx, int baud, int columns, int rows)
+     :_serial(tx, rx), _columns(columns), _rows(rows) {
+    _serial.baud(baud);
+    cls();
+}
+
+int eDisp::_putc(int value) {
+    if(value == '\n') {
+        newline();
+    } else {
+        _serial.putc(value);
+    }
+    return value;
+}
+
+int eDisp::_getc() {
+    return 0;
+}
+
+void eDisp::newline() {
+    _column = 0;
+    _row++;
+    if (_row >= _rows) {
+        _row = 0;
+    }
+    locate(_column, _row);
+}
+
+void eDisp::locate(int column, int row) {
+    if (column < 0 || column >= _columns || row < 0 || row >= _rows) {
+        error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
+        return;
+    }
+
+    _row = row;
+    _column = column;
+    _serial.printf("\x1B[%d;%dH", _row, _column);
+}
+
+void eDisp::cls() {
+    locate(0, 0);
+    _serial.printf("\x1B[J");
+}
+
+void eDisp::reset() {
+    _serial.printf("\x1B[m");
+    cls();
+}
+
+void eDisp::textColor (int color) {
+    _serial.printf("\x1B[%dm", color);
+}
+
+void eDisp::backgroundColor (int color) {
+    _serial.printf("\x1B[%dm", color + 10);
+}
+
+void eDisp::fillRect (int buffer, int width, int hight, int x, int y, int color ) {
+    _serial.printf("\x1B@0;%d;%d;%d;%d;%d;%dz", buffer, width, hight, x, y, color);
+}
+
+void eDisp::drawLine (int buffer, int x0, int y0, int x1, int y1, int color) {
+    _serial.printf("\x1B@2;%d;%d;%d;%d;%d;%dz", buffer, x0, y0, x1, y1, color);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eDisp.h	Sun Mar 07 00:45:31 2010 +0000
@@ -0,0 +1,157 @@
+/* mbed eDISP Library
+ * Copyright (c) 2010 todotani
+ * Version 0.1 (March 6, 2010)
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#ifndef MBED_EDISP_H
+#define MBED_EDISP_H
+
+#include "mbed.h"
+#include "Stream.h"
+
+// Text Color code
+#define BLACK   30
+#define RED     31
+#define GREEN   32
+#define YELLOW  33
+#define BLUE    34
+#define PURPLE  35
+#define AQUA    36
+#define WHITE   37
+#define TRANSPARENT 49      // default background color
+
+// RGB555 color code (converted from 32bit color code)
+#define RGB_Black   (0      << 10) + (0      << 5) + 0
+#define RGB_Navy    (0      << 10) + (0      << 5) + 0x80/8
+#define RGB_Silver  (0xc0/8 << 10) + (0xc0/8 << 5) + 0xc0/8
+#define RGB_Blue    (0      << 10) + (0      << 5) + 0xff/8
+#define RGB_Maroon  (0x80/8 << 10) + (0      << 5) + 0
+#define RGB_Purple  (0x80/8 << 10) + (0      << 5) + 0x80/8
+#define RGB_Red     (0xff/8 << 10) + (0      << 5) + 0
+#define RGB_Fuchsia (0xff/8 << 10) + (0      << 5) + 0xff/8
+#define RGB_Green   (0      << 10) + (0x80/8 << 5) + 0
+#define RGB_Teal    (0      << 10) + (0x80/8 << 5) + 0x80/8
+#define RGB_Lime    (0      << 10) + (0xff/8 << 5) + 0
+#define RGB_Aqua    (0      << 10) + (0xff/8 << 5) + 0xff/8
+#define RGB_Olive   (0x80/8 << 10) + (0x80/8 << 5) + 0
+#define RGB_Gray    (0x80/8 << 10) + (0x80/8 << 5) + 0x80/8
+#define RGB_Yellow  (0xff/8 << 10) + (0xff/8 << 5) + 0
+#define RGB_White   (0xff/8 << 10) + (0xff/8 << 5) + 0xff/8
+
+
+namespace mbed {
+
+/* Class: eDisp
+ * Driver for eDISP (http://www.ddlab.jp/shop/edisp/syouhin.cgi)
+ *
+ * Allows you to print to a Text LCD screen, set color and locate/cls. 
+ * Also support graphics functions fillRect, drawLine
+ *
+ */
+class eDisp : public Stream {
+public:
+    /* Constructor: eDisp
+     * Create a eDisp object, connected to the specified pins
+     *
+     * All signals must be connected to pair of Serial Tx/Rx pin
+     * (p9/p10, p13/p14 or p28/p27)
+     *
+     * Variables:
+     *  tx - Used to specify tx of Serial port
+     *  rx - Used to specify rx of Serial port (acturally not used)
+     *  baud - baudrate of serial port
+     */
+    eDisp(PinName tx, PinName rx, int baud = 19200,
+        int columns = 40, int rows = 15);
+
+#if 0 // Inhereted from Stream, for documentation only
+    /* Function: putc
+     *  Write a character
+     *
+     * Variables:
+     *  c - The character to write to the serial port
+     */
+    int putc(int c);
+
+    /* Function: printf
+     *  Write a formated string
+     *
+     * Variables:
+     *  format - A printf-style format string, followed by the
+     *      variables to use in formating the string.
+     */
+    int printf(const char* format, ...);
+#endif
+
+    /* Function: locate
+     * set cursor position
+     * Variables:
+     *   colum - x position (0 - 39)
+     *   row   - y position (0 - 14)
+     */
+    void locate(int column, int row);
+
+    /* Function: textColor
+     * Set text color
+     * Variables:
+     *   color - Text color code
+     */
+    void textColor(int color);
+
+    /* Function: backgroundColor
+     * Set backgound color
+     * Variables:
+     *   color - Text color code
+     */
+    void backgroundColor(int color);
+
+    /* Function: cls
+     * Clear the screen, and locate to 0,0
+     */
+    void cls();
+
+    /* Function: reset
+     * Set default text/backgroudn color, clear the screen, and locate to 0,0
+     */
+    void reset();
+
+    /* Function: fillRect
+     * Fill secreen with specified color
+     * Variables:
+     *   buffer - number of screen buffer (0 - 3)
+     *   width  - width of rectangle (1 - 320)
+     *   hight  - hight of rectangle (1 - 240)
+     *   x      - x coordinate (0 - 319) of upper left point
+     *   y      - y coordinate (0 - 239) of upper left point
+     *   color  - RGB555 color code  0rrrrrgggggbbbbb
+     */
+    void fillRect(int buffer, int width, int hight, int x, int y, int color );
+
+    /* Function: drawLine
+     * draw line from (x0, y0) to (x1, y1)
+     * Variables:
+     *   buffer - number of screen buffer (0 - 3)
+     *   x0     - coordinate x (0 - 319) of start point
+     *   y0     - coordinate y (0 - 319) of start point
+     *   x1     - coordinate x (0 - 319) of end point
+     *   y1     - coordinate y (0 - 319) of end point
+     *   color  - RGB555 color code  0rrrrrgggggbbbbb
+     */
+    void drawLine(int buffer, int x0, int y0, int x1, int y1, int color);
+
+protected:
+    virtual int _putc(int c);
+    virtual int _getc();
+    void newline();
+
+    Serial _serial;
+    int _baud;
+    int _row;
+    int _column;
+    int _columns;   // Number of max columns
+    int _rows;      // Number of max rows
+};
+
+#endif
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 07 00:45:31 2010 +0000
@@ -0,0 +1,90 @@
+/* Sample code for mbed eDISP Library
+ * Copyright (c) 2010 todotani
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "mbed.h"
+#include "eDisp.h"
+
+// RGB color code table
+int RGB_color[16] = {
+    RGB_Navy,
+    RGB_Silver,
+    RGB_Blue,
+    RGB_Maroon,
+    RGB_Purple,
+    RGB_Red,
+    RGB_Fuchsia,
+    RGB_Green,
+    RGB_Teal,
+    RGB_Lime,
+    RGB_Aqua,
+    RGB_Olive,
+    RGB_Gray,
+    RGB_Yellow,
+    RGB_White,
+    RGB_Black };
+
+// RGB color name table
+char* colorName[16] = {
+    "Navy",
+    "Silver",
+    "Blue",
+    "Maroon",
+    "Purple",
+    "Red",
+    "Fuchsia",
+    "Green",
+    "Teal",
+    "Lime",
+    "Aqua",
+    "Olive",
+    "Gray",
+    "Yellow",
+    "White",
+    "Black" };
+
+
+eDisp display(p9, p10, 19200);     // tx, rx, baud
+
+int main() {
+    int i;
+
+    while (1) {
+        // Test-1
+        // clear graphics screen
+        display.fillRect(0, 320, 240, 0, 0, RGB_Black); 
+        display.reset();
+        for (i = 0; i < 15; i++) {
+            display.fillRect(0, 320, 16, 0, i*16, RGB_color[i]);
+            display.locate(0, i);
+            display.printf("%s", colorName[i] );
+        }
+        wait(2);
+
+        // Test-2
+        display.fillRect(0, 320, 240, 0, 0, RGB_Black);
+        display.cls();
+        for (i = 0; i < 15; i++) {
+            display.drawLine(0, 1, i*16+1, 319, i*16+1, RGB_color[i]);
+            display.drawLine(0, i*21+1, 0, i*21+1, 239, RGB_color[i]);
+        }
+        wait(2);
+
+        // Test-3
+        for (i = 0; i < 15; i++) {
+            display.fillRect(0, 320, 240, 0, 0, RGB_color[i]);
+            wait(0.2);      // wait until completion of draw
+        }
+        wait(2);
+        
+        // Test-4
+        display.fillRect(0, 320, 240, 0, 0, RGB_Black);
+        display.cls();
+        for (i = 0; i < 15; i++) {
+            display.textColor(RED + i%7);
+            display.printf("漢字表示OK AABBCC\n");
+        }
+        wait(2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Mar 07 00:45:31 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0