An mbed library for the Embedded Artists QVGA OLED 2.8 inch panel

Committer:
gbloice
Date:
Wed Mar 09 19:45:23 2011 +0000
Revision:
5:b3f5d19945ff
Parent:
4:cd650f08e108
Fixed example with conditional compilation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gbloice 0:ae3d20db48fc 1 /* mbed library for driving the EA QVGA 2.8" OLED
gbloice 0:ae3d20db48fc 2 * Copyright (c) Graham Bloice 2011
gbloice 0:ae3d20db48fc 3 *
gbloice 0:ae3d20db48fc 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
gbloice 0:ae3d20db48fc 5 * of this software and associated documentation files (the "Software"), to deal
gbloice 0:ae3d20db48fc 6 * in the Software without restriction, including without limitation the rights
gbloice 0:ae3d20db48fc 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gbloice 0:ae3d20db48fc 8 * copies of the Software, and to permit persons to whom the Software is
gbloice 0:ae3d20db48fc 9 * furnished to do so, subject to the following conditions:
gbloice 0:ae3d20db48fc 10 *
gbloice 0:ae3d20db48fc 11 * The above copyright notice and this permission notice shall be included in
gbloice 0:ae3d20db48fc 12 * all copies or substantial portions of the Software.
gbloice 0:ae3d20db48fc 13 *
gbloice 0:ae3d20db48fc 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gbloice 0:ae3d20db48fc 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gbloice 0:ae3d20db48fc 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gbloice 0:ae3d20db48fc 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gbloice 0:ae3d20db48fc 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gbloice 0:ae3d20db48fc 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gbloice 0:ae3d20db48fc 20 * THE SOFTWARE.
gbloice 0:ae3d20db48fc 21 */
gbloice 0:ae3d20db48fc 22
gbloice 0:ae3d20db48fc 23 #ifndef MBED_EAQVGAOLED_H
gbloice 0:ae3d20db48fc 24 #define MBED_EAQVGAOLED_H
gbloice 0:ae3d20db48fc 25
gbloice 0:ae3d20db48fc 26 #include "mbed.h"
gbloice 0:ae3d20db48fc 27 #include "GraphicsDisplay.h"
gbloice 0:ae3d20db48fc 28
gbloice 1:b23bfa1be0d9 29 #define BLACK 0x0000 /* 0, 0, 0 */
gbloice 1:b23bfa1be0d9 30 #define NAVY 0x000F /* 0, 0, 128 */
gbloice 1:b23bfa1be0d9 31 #define DARK_GREEN 0x03E0 /* 0, 128, 0 */
gbloice 1:b23bfa1be0d9 32 #define DARK_CYAN 0x03EF /* 0, 128, 128 */
gbloice 1:b23bfa1be0d9 33 #define MAROON 0x7800 /* 128, 0, 0 */
gbloice 1:b23bfa1be0d9 34 #define PURPLE 0x780F /* 128, 0, 128 */
gbloice 1:b23bfa1be0d9 35 #define OLIVE 0x7BE0 /* 128, 128, 0 */
gbloice 1:b23bfa1be0d9 36 #define LIGHT_GRAY 0xC618 /* 192, 192, 192 */
gbloice 1:b23bfa1be0d9 37 #define DARK_GRAY 0x7BEF /* 128, 128, 128 */
gbloice 1:b23bfa1be0d9 38 #define BLUE 0x001F /* 0, 0, 255 */
gbloice 1:b23bfa1be0d9 39 #define GREEN 0x07E0 /* 0, 255, 0 */
gbloice 0:ae3d20db48fc 40 #define CYAN 0x07FF /* 0, 255, 255 */
gbloice 0:ae3d20db48fc 41 #define RED 0xF800 /* 255, 0, 0 */
gbloice 1:b23bfa1be0d9 42 #define MAGENTA 0xF81F /* 255, 0, 255 */
gbloice 1:b23bfa1be0d9 43 #define YELLOW 0xFFE0 /* 255, 255, 0 */
gbloice 1:b23bfa1be0d9 44 #define WHITE 0xFFFF /* 255, 255, 255 */
gbloice 0:ae3d20db48fc 45
gbloice 0:ae3d20db48fc 46 #define CURSOR_CLS 0
gbloice 0:ae3d20db48fc 47 #define CURSOR_UP 1
gbloice 0:ae3d20db48fc 48 #define CURSOR_DOWN 2
gbloice 0:ae3d20db48fc 49 #define CURSOR_LEFT 3
gbloice 0:ae3d20db48fc 50 #define CURSOR_RIGHT 4
gbloice 0:ae3d20db48fc 51
gbloice 4:cd650f08e108 52 /**
gbloice 1:b23bfa1be0d9 53 * @brief Uses SPI to drive the Embedded Artists 2.8" QVGA OLED panel
gbloice 1:b23bfa1be0d9 54 * @author Graham Bloice
gbloice 1:b23bfa1be0d9 55 * @see http://mbed.org/cookbook/EAQVGAOLED
gbloice 3:ebd9cdd757a2 56 * @see example.cpp
gbloice 1:b23bfa1be0d9 57 * @see API
gbloice 0:ae3d20db48fc 58 *
gbloice 1:b23bfa1be0d9 59 * <b>EAQVGAOLED</b> defines a library to drive the Embedded Artists QVGA 2.8" OLED panel.
gbloice 1:b23bfa1be0d9 60 * Currently the library uses SPI to drive the panel.
gbloice 1:b23bfa1be0d9 61 *
gbloice 1:b23bfa1be0d9 62 * Standard Example:
gbloice 1:b23bfa1be0d9 63 * @code
gbloice 1:b23bfa1be0d9 64 * #include "mbed.h"
gbloice 1:b23bfa1be0d9 65 * #include "EAQVGAOLED.h"
gbloice 1:b23bfa1be0d9 66 * int main() {
gbloice 1:b23bfa1be0d9 67 *
gbloice 1:b23bfa1be0d9 68 * // Create an instance of the display driver
gbloice 1:b23bfa1be0d9 69 * EAQVGAOLED display = EAQVGAOLED(p5, p6, p7, p8, p9, p10);
gbloice 1:b23bfa1be0d9 70 *
gbloice 1:b23bfa1be0d9 71 * display.background(BLACK);
gbloice 1:b23bfa1be0d9 72 * display.foreground(DARK_GRAY);
gbloice 1:b23bfa1be0d9 73 * display.cls();
gbloice 1:b23bfa1be0d9 74 * display.printf("Hello from mbed");
gbloice 1:b23bfa1be0d9 75 * }
gbloice 1:b23bfa1be0d9 76 * @endcode
gbloice 0:ae3d20db48fc 77 */
gbloice 0:ae3d20db48fc 78 class EAQVGAOLED : public GraphicsDisplay {
gbloice 0:ae3d20db48fc 79
gbloice 0:ae3d20db48fc 80 public:
gbloice 0:ae3d20db48fc 81
gbloice 4:cd650f08e108 82 /**
gbloice 4:cd650f08e108 83 * @brief The driver constructor
gbloice 4:cd650f08e108 84 *
gbloice 4:cd650f08e108 85 * Create an EA QVGA OLED object connected using
gbloice 0:ae3d20db48fc 86 * the specified SPI port, and two output pins;
gbloice 0:ae3d20db48fc 87 * the Chip Select pin and the Backlight pin.
gbloice 0:ae3d20db48fc 88 *
gbloice 1:b23bfa1be0d9 89 * @param mosi SPI data output pin
gbloice 1:b23bfa1be0d9 90 * @param miso SPI data input pin
gbloice 1:b23bfa1be0d9 91 * @param sclk SPI Clock pin
gbloice 0:ae3d20db48fc 92 * @param cs Pin to drive the display CS
gbloice 0:ae3d20db48fc 93 * @param reset Pin to drive the display reset
gbloice 0:ae3d20db48fc 94 * @param bl Pin to drive the display backlight
gbloice 0:ae3d20db48fc 95 */
gbloice 0:ae3d20db48fc 96 EAQVGAOLED(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName bl);
gbloice 0:ae3d20db48fc 97
gbloice 4:cd650f08e108 98 // These are the functions required to support the base class
gbloice 4:cd650f08e108 99
gbloice 4:cd650f08e108 100 /**
gbloice 4:cd650f08e108 101 * Set a pixel to the specified colour
gbloice 0:ae3d20db48fc 102 *
gbloice 2:4babceb1bfc2 103 * @ingroup API
gbloice 0:ae3d20db48fc 104 * @param x Pixel x position
gbloice 0:ae3d20db48fc 105 * @param y Pixel y position
gbloice 0:ae3d20db48fc 106 * @param colour Pixel colour
gbloice 0:ae3d20db48fc 107 */
gbloice 0:ae3d20db48fc 108 virtual void pixel(int x, int y, int colour);
gbloice 0:ae3d20db48fc 109
gbloice 4:cd650f08e108 110 /**
gbloice 4:cd650f08e108 111 * Accessor to obtain the display width
gbloice 0:ae3d20db48fc 112 *
gbloice 0:ae3d20db48fc 113 * @return the width of the display in pixels
gbloice 0:ae3d20db48fc 114 */
gbloice 0:ae3d20db48fc 115 virtual int width(void) { return 240; }
gbloice 0:ae3d20db48fc 116
gbloice 4:cd650f08e108 117 /**
gbloice 4:cd650f08e108 118 * Accessor to obtain the display height
gbloice 0:ae3d20db48fc 119 *
gbloice 0:ae3d20db48fc 120 * @return the height of the display in pixels
gbloice 0:ae3d20db48fc 121 */
gbloice 0:ae3d20db48fc 122 virtual int height(void) { return 320; }
gbloice 0:ae3d20db48fc 123
gbloice 4:cd650f08e108 124 /**
gbloice 4:cd650f08e108 125 * Clear the screen
gbloice 2:4babceb1bfc2 126 *
gbloice 2:4babceb1bfc2 127 * @ingroup API
gbloice 0:ae3d20db48fc 128 */
gbloice 0:ae3d20db48fc 129 virtual void cls() { fillRectangle(0, 0, width(), height(), _background); }
gbloice 0:ae3d20db48fc 130
gbloice 0:ae3d20db48fc 131 /* No overrides for these functions
gbloice 0:ae3d20db48fc 132
gbloice 0:ae3d20db48fc 133 virtual void window(int x, int y, int w, int h);
gbloice 0:ae3d20db48fc 134 virtual void putp(int colour);
gbloice 0:ae3d20db48fc 135
gbloice 0:ae3d20db48fc 136 virtual void fill(int x, int y, int w, int h, int colour);
gbloice 0:ae3d20db48fc 137 virtual void blit(int x, int y, int w, int h, const int *colour);
gbloice 0:ae3d20db48fc 138 virtual void blitbit(int x, int y, int w, int h, const char* colour);
gbloice 0:ae3d20db48fc 139
gbloice 0:ae3d20db48fc 140 virtual void character(int column, int row, int c);
gbloice 0:ae3d20db48fc 141 virtual int columns();
gbloice 0:ae3d20db48fc 142 virtual int rows();
gbloice 0:ae3d20db48fc 143
gbloice 0:ae3d20db48fc 144 */
gbloice 0:ae3d20db48fc 145
gbloice 0:ae3d20db48fc 146 // These are new functions for this class
gbloice 0:ae3d20db48fc 147
gbloice 4:cd650f08e108 148 /**
gbloice 4:cd650f08e108 149 * Draw a horizontal line
gbloice 0:ae3d20db48fc 150 *
gbloice 4:cd650f08e108 151 * @ingroup API
gbloice 0:ae3d20db48fc 152 * @param x0 Starting x position
gbloice 0:ae3d20db48fc 153 * @param y0 Starting y position
gbloice 0:ae3d20db48fc 154 * @param x1 End x position
gbloice 0:ae3d20db48fc 155 * @param colour Colour of line
gbloice 0:ae3d20db48fc 156 */
gbloice 0:ae3d20db48fc 157 void hLine(const uint16_t x0, const uint16_t y0, const uint16_t x1, const uint16_t colour);
gbloice 0:ae3d20db48fc 158
gbloice 4:cd650f08e108 159 /**
gbloice 4:cd650f08e108 160 * Draw a vertical line
gbloice 0:ae3d20db48fc 161 *
gbloice 4:cd650f08e108 162 * @ingroup API
gbloice 0:ae3d20db48fc 163 * @param x0 Starting x position
gbloice 0:ae3d20db48fc 164 * @param y0 Starting y position
gbloice 0:ae3d20db48fc 165 * @param y1 End y position
gbloice 0:ae3d20db48fc 166 * @param colour Colour of line
gbloice 0:ae3d20db48fc 167 */
gbloice 0:ae3d20db48fc 168 void vLine(const uint16_t x0, const uint16_t y0, const uint16_t y1, const uint16_t colour);
gbloice 0:ae3d20db48fc 169
gbloice 4:cd650f08e108 170 /**
gbloice 4:cd650f08e108 171 * Draw a rectangle in the specified colour
gbloice 0:ae3d20db48fc 172 *
gbloice 4:cd650f08e108 173 * @ingroup API
gbloice 0:ae3d20db48fc 174 * @param x0 Starting x position
gbloice 0:ae3d20db48fc 175 * @param y0 Starting y position
gbloice 0:ae3d20db48fc 176 * @param x1 Ending x position
gbloice 0:ae3d20db48fc 177 * @param y1 Ending y position
gbloice 0:ae3d20db48fc 178 * @param colour Outline colour of rectangle
gbloice 0:ae3d20db48fc 179 */
gbloice 0:ae3d20db48fc 180 void rectangle(const uint16_t x0, const uint16_t y0, const uint16_t x1, const uint16_t y1, uint16_t colour);
gbloice 0:ae3d20db48fc 181
gbloice 4:cd650f08e108 182 /**
gbloice 4:cd650f08e108 183 * Draw a filled rectangle in the specified colour
gbloice 0:ae3d20db48fc 184 *
gbloice 4:cd650f08e108 185 * @ingroup API
gbloice 0:ae3d20db48fc 186 * @param x0 Starting x position
gbloice 0:ae3d20db48fc 187 * @param y0 Starting y position
gbloice 0:ae3d20db48fc 188 * @param x1 Ending x position
gbloice 0:ae3d20db48fc 189 * @param y1 Ending y position
gbloice 5:b3f5d19945ff 190 * @param colour Filled colour of rectangle
gbloice 0:ae3d20db48fc 191 */
gbloice 0:ae3d20db48fc 192 void fillRectangle(const uint16_t x0, const uint16_t y0, const uint16_t x1, const uint16_t y1, uint16_t colour);
gbloice 0:ae3d20db48fc 193
gbloice 4:cd650f08e108 194 /**
gbloice 4:cd650f08e108 195 * Control the backlight
gbloice 0:ae3d20db48fc 196 *
gbloice 4:cd650f08e108 197 * ingroup API
gbloice 0:ae3d20db48fc 198 * @param on Enable the backlight
gbloice 0:ae3d20db48fc 199 */
gbloice 0:ae3d20db48fc 200 void backlightControl(bool on) {
gbloice 0:ae3d20db48fc 201 _bl = on ? 1 : 0;
gbloice 0:ae3d20db48fc 202 }
gbloice 0:ae3d20db48fc 203
gbloice 0:ae3d20db48fc 204 protected:
gbloice 0:ae3d20db48fc 205
gbloice 4:cd650f08e108 206 /**
gbloice 4:cd650f08e108 207 * Print a character, overridden to handle control characters
gbloice 0:ae3d20db48fc 208 *
gbloice 4:cd650f08e108 209 * @ingroup API
gbloice 4:cd650f08e108 210 * @param value The character value to print
gbloice 4:cd650f08e108 211 * @return The character value printed
gbloice 4:cd650f08e108 212 *
gbloice 0:ae3d20db48fc 213 */
gbloice 0:ae3d20db48fc 214 virtual int _putc(int value);
gbloice 0:ae3d20db48fc 215
gbloice 0:ae3d20db48fc 216 private:
gbloice 0:ae3d20db48fc 217 bool resetDisplay(void); // Reset the display
gbloice 0:ae3d20db48fc 218 void initHardware(void); // Initialise the hardware
gbloice 0:ae3d20db48fc 219 inline void setRegisterIndex(const uint8_t reg); // Write register
gbloice 0:ae3d20db48fc 220 inline void writeData(const uint16_t data); // Write data
gbloice 0:ae3d20db48fc 221 uint16_t readDataRegister(const uint8_t reg); // Read from a register
gbloice 0:ae3d20db48fc 222 void writeDataRegister(const uint8_t reg, const uint16_t data); // Write data to a register
gbloice 0:ae3d20db48fc 223 void movePen(const uint16_t x, const uint16_t y); // Set the pen position
gbloice 0:ae3d20db48fc 224
gbloice 0:ae3d20db48fc 225 SPI _spi; // The SPI Port to use for data I/O
gbloice 0:ae3d20db48fc 226 DigitalOut _cs; // The I/O pin for the Chip Select
gbloice 0:ae3d20db48fc 227 DigitalOut _reset; // The I/O pin for the reset
gbloice 0:ae3d20db48fc 228 DigitalOut _bl; // The I/O pin for the blacklight
gbloice 0:ae3d20db48fc 229 };
gbloice 0:ae3d20db48fc 230
gbloice 0:ae3d20db48fc 231 #endif