Sharp Memory LCD library

Dependencies:   Fonts Images BurstSPI

Committer:
star297
Date:
Tue Aug 25 21:15:06 2015 +0000
Revision:
1:1bd2b42b305d
Parent:
0:0a76610c48a1
set 128 default display size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:0a76610c48a1 1
star297 0:0a76610c48a1 2 #ifndef MBED_SharpLCD_H
star297 0:0a76610c48a1 3 #define MBED_SharpLCD_H
star297 0:0a76610c48a1 4
star297 0:0a76610c48a1 5 #include "mbed.h"
star297 0:0a76610c48a1 6 #include <stdarg.h>
star297 0:0a76610c48a1 7 #include "BurstSPI.h"
star297 0:0a76610c48a1 8
star297 0:0a76610c48a1 9 #define incx() x++, dxt += d2xt, t += dxt
star297 0:0a76610c48a1 10 #define incy() y--, dyt += d2yt, t += dyt
star297 0:0a76610c48a1 11
star297 0:0a76610c48a1 12 /// Set the display geometry depending on the resolution of the display used
star297 0:0a76610c48a1 13 /// common types are 96x96, 128x128, 400x240
star297 0:0a76610c48a1 14 /** MemoryLCD width in pixels */
star297 1:1bd2b42b305d 15 #define DISPLAY_WIDTH (128)
star297 0:0a76610c48a1 16 /** MemoryLCD height in pixels */
star297 1:1bd2b42b305d 17 #define DISPLAY_HEIGHT (128)
star297 0:0a76610c48a1 18
star297 0:0a76610c48a1 19 /** Maximum length of a printf to the display */
star297 0:0a76610c48a1 20 #define MAX_PRINTF_CHARS 40
star297 0:0a76610c48a1 21 /** Data type for storing buffer the pixel buffer */
star297 0:0a76610c48a1 22 #define DISPLAY_BUFFER_TYPE uint8_t
star297 0:0a76610c48a1 23 #define DISPLAY_BUFFER_TYPE_SIZE (sizeof(DISPLAY_BUFFER_TYPE) * 8)
star297 0:0a76610c48a1 24 #define DISPLAY_BUFFER_ELEMENTS ((DISPLAY_WIDTH*DISPLAY_HEIGHT)/DISPLAY_BUFFER_TYPE_SIZE)
star297 0:0a76610c48a1 25
star297 0:0a76610c48a1 26 /** Color definitions */
star297 0:0a76610c48a1 27 #define White 0xFFFF
star297 0:0a76610c48a1 28 #define Black 0x0000
star297 0:0a76610c48a1 29 #define foreground White
star297 0:0a76610c48a1 30 #define background Black
star297 0:0a76610c48a1 31
star297 0:0a76610c48a1 32 /** Sharp Memory LCD.
star297 0:0a76610c48a1 33 * Supports 96x96, 128x128, 400x240 displays.
star297 0:0a76610c48a1 34 * Uses a Frame Buffer in RAM depending on resolution of display as follows:
star297 0:0a76610c48a1 35 * 96 Display - 1.7k, 128 Display - 2.6k, 400 Display - 12.4k
star297 0:0a76610c48a1 36 *
star297 0:0a76610c48a1 37 * Example:
star297 0:0a76610c48a1 38 * @code
star297 0:0a76610c48a1 39 #include "mbed.h"
star297 0:0a76610c48a1 40 #include "SharpLCD.h"
star297 0:0a76610c48a1 41
star297 0:0a76610c48a1 42 //KL25
star297 0:0a76610c48a1 43 SharpLCD display(PTC6, NC, PTC5, PTC3, PTC4, NC); //mosi, miso(not used), sck, cs, enable, extcom
star297 0:0a76610c48a1 44
star297 0:0a76610c48a1 45 int main()
star297 0:0a76610c48a1 46 {
star297 0:0a76610c48a1 47 display.enableDisplay();
star297 0:0a76610c48a1 48
star297 0:0a76610c48a1 49 while(1)
star297 0:0a76610c48a1 50 {
star297 0:0a76610c48a1 51 display.clearImmediate();
star297 0:0a76610c48a1 52 // draw ellipse (To draw circle, set the last two axis the same)
star297 0:0a76610c48a1 53 for (int j=0; j<60; j++) {
star297 0:0a76610c48a1 54 display.ellipse(64,64,j,60,Black);
star297 0:0a76610c48a1 55 display.update();
star297 0:0a76610c48a1 56 //wait(.01);
star297 0:0a76610c48a1 57 display.ellipse(64,64,j,50,White); // offset last axis here gives interesting effect!
star297 0:0a76610c48a1 58 display.locate(5,6);
star297 0:0a76610c48a1 59 display.printf("Sharp");
star297 0:0a76610c48a1 60 display.locate(2,8);
star297 0:0a76610c48a1 61 display.printf("Draw Ellipse");
star297 0:0a76610c48a1 62 display.locate(4,10);
star297 0:0a76610c48a1 63 display.printf("MemoryLCD");
star297 0:0a76610c48a1 64 display.update();
star297 0:0a76610c48a1 65 }
star297 0:0a76610c48a1 66 // draw rectangle
star297 0:0a76610c48a1 67 for (int j=0; j<128; j++) {
star297 0:0a76610c48a1 68 display.rect(0,0,j,j,Black);
star297 0:0a76610c48a1 69 display.update();
star297 0:0a76610c48a1 70 //wait(.01);
star297 0:0a76610c48a1 71 display.rect(0,0,j,j,White);
star297 0:0a76610c48a1 72 display.locate(5,6);
star297 0:0a76610c48a1 73 display.printf("Sharp");
star297 0:0a76610c48a1 74 display.locate(1,8);
star297 0:0a76610c48a1 75 display.printf("Draw Rectangle");
star297 0:0a76610c48a1 76 display.locate(4,10);
star297 0:0a76610c48a1 77 display.printf("MemoryLCD");
star297 0:0a76610c48a1 78 display.update();
star297 0:0a76610c48a1 79 }
star297 0:0a76610c48a1 80 for (int j=60; j>0; j--) {
star297 0:0a76610c48a1 81 display.circle(64,64,j,Black);
star297 0:0a76610c48a1 82 display.update();
star297 0:0a76610c48a1 83 wait(.01);
star297 0:0a76610c48a1 84 display.circle(64,64,j,White);
star297 0:0a76610c48a1 85 display.locate(5,6);
star297 0:0a76610c48a1 86 display.printf("Sharp");
star297 0:0a76610c48a1 87 display.locate(2,8);
star297 0:0a76610c48a1 88 display.printf("Draw Circle");
star297 0:0a76610c48a1 89 display.locate(4,10);
star297 0:0a76610c48a1 90 display.printf("MemoryLCD");
star297 0:0a76610c48a1 91 display.update();
star297 0:0a76610c48a1 92 }
star297 0:0a76610c48a1 93 }
star297 0:0a76610c48a1 94 }
star297 0:0a76610c48a1 95 * @endcode
star297 0:0a76610c48a1 96 */
star297 0:0a76610c48a1 97
star297 0:0a76610c48a1 98
star297 0:0a76610c48a1 99 class SharpLCD {
star297 0:0a76610c48a1 100 public:
star297 0:0a76610c48a1 101
star297 0:0a76610c48a1 102 SharpLCD(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName enable, PinName extcom);
star297 0:0a76610c48a1 103
star297 0:0a76610c48a1 104 /**
star297 0:0a76610c48a1 105 * Sets external font usage, eg. dispaly.set_font(Arial12x12);
star297 0:0a76610c48a1 106 * This uses pixel positioning.
star297 0:0a76610c48a1 107 * display.set_font(NULL); returns to internal default font.
star297 0:0a76610c48a1 108 * void set_font(const unsigned char * f);
star297 0:0a76610c48a1 109 */
star297 0:0a76610c48a1 110 void set_font(const unsigned char * f);
star297 0:0a76610c48a1 111 /**
star297 0:0a76610c48a1 112 * Sets position of the next character or string print.
star297 0:0a76610c48a1 113 * External font, set pixel x(column),y(row) position.
star297 0:0a76610c48a1 114 * internal(default) font, set character column and row position
star297 0:0a76610c48a1 115 */
star297 0:0a76610c48a1 116 void locate(int column, int row);
star297 0:0a76610c48a1 117 /**
star297 0:0a76610c48a1 118 * printf (from Stream).
star297 0:0a76610c48a1 119 * Full printf funtionality only available with internal(default) font.
star297 0:0a76610c48a1 120 * CR and LF have no effect with external fonts.
star297 0:0a76610c48a1 121 */
star297 0:0a76610c48a1 122 void printf(const char* format, ...);
star297 0:0a76610c48a1 123 /**
star297 0:0a76610c48a1 124 * Enables (power) display.
star297 0:0a76610c48a1 125 */
star297 0:0a76610c48a1 126 void enableDisplay(void);
star297 0:0a76610c48a1 127 /**
star297 0:0a76610c48a1 128 * Dissables (power) display.
star297 0:0a76610c48a1 129 */
star297 0:0a76610c48a1 130 void disableDisplay(void);
star297 0:0a76610c48a1 131 /**
star297 0:0a76610c48a1 132 * Set a pixel in the pixel buffer.
star297 0:0a76610c48a1 133 *
star297 0:0a76610c48a1 134 * @param x X-Position.
star297 0:0a76610c48a1 135 * @param y Y-Position.
star297 0:0a76610c48a1 136 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 137 */
star297 0:0a76610c48a1 138 void pixel(int x, int y, int colour);
star297 0:0a76610c48a1 139 /**
star297 0:0a76610c48a1 140 * Draw rectangle one pixel wide (wire frame) in the pixel buffer.
star297 0:0a76610c48a1 141 *
star297 0:0a76610c48a1 142 * @param x0 X-Start position.
star297 0:0a76610c48a1 143 * @param y0 Y-Start position.
star297 0:0a76610c48a1 144 * @param x1 X-End position.
star297 0:0a76610c48a1 145 * @param y1 Y-End position.
star297 0:0a76610c48a1 146 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 147 */
star297 0:0a76610c48a1 148 void rect(int x0, int y0, int x1, int y1, int colour);
star297 0:0a76610c48a1 149 /**
star297 0:0a76610c48a1 150 * Draw filled rectangle in the pixel buffer.
star297 0:0a76610c48a1 151 *
star297 0:0a76610c48a1 152 * @param x0 X-Start position.
star297 0:0a76610c48a1 153 * @param y0 Y-Start position.
star297 0:0a76610c48a1 154 * @param w Width.
star297 0:0a76610c48a1 155 * @param h Height.
star297 0:0a76610c48a1 156 * @param colour Colour to fill rectangle to. White or Black.
star297 0:0a76610c48a1 157 */
star297 0:0a76610c48a1 158 void fillrect(int x0, int y0, int w, int h, int colour);
star297 0:0a76610c48a1 159 /**
star297 0:0a76610c48a1 160 * Draw Circle one pixel wide in the pixel buffer.
star297 0:0a76610c48a1 161 *
star297 0:0a76610c48a1 162 * @param x0 X-Centre position.
star297 0:0a76610c48a1 163 * @param y0 Y-Centre position.
star297 0:0a76610c48a1 164 * @param r Radius.
star297 0:0a76610c48a1 165 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 166 */
star297 0:0a76610c48a1 167 void circle(int x, int y, int r, int colour);
star297 0:0a76610c48a1 168 /**
star297 0:0a76610c48a1 169 * Draw Ellipse or Circle one pixel wide in the pixel buffer.
star297 0:0a76610c48a1 170 * Set v & h to same values to produce a circle.
star297 0:0a76610c48a1 171
star297 0:0a76610c48a1 172 * @param xc X-Centre position.
star297 0:0a76610c48a1 173 * @param yc Y-Centre position.
star297 0:0a76610c48a1 174 * @param v Vertical radius.
star297 0:0a76610c48a1 175 * @param h Horizontal radius.
star297 0:0a76610c48a1 176 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 177 */
star297 0:0a76610c48a1 178 void ellipse(int xc, int yc, int v, int h, unsigned int colour);
star297 0:0a76610c48a1 179 /**
star297 0:0a76610c48a1 180 * Draw Filled Ellipse or Circle in the pixel buffer.
star297 0:0a76610c48a1 181 * Set v & h to same values to produce a circle.
star297 0:0a76610c48a1 182
star297 0:0a76610c48a1 183 * @param xc X-Centre position.
star297 0:0a76610c48a1 184 * @param yc Y-Centre position.
star297 0:0a76610c48a1 185 * @param v Vertical radius.
star297 0:0a76610c48a1 186 * @param h Horizontal radius.
star297 0:0a76610c48a1 187 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 188 */
star297 0:0a76610c48a1 189 void fillellipse(int xc, int yc, int v, int h, unsigned int colour);
star297 0:0a76610c48a1 190 /**
star297 0:0a76610c48a1 191 * Draw a horizontal line one pixel wide in the pixel buffer.
star297 0:0a76610c48a1 192 *
star297 0:0a76610c48a1 193 * @param x0 X-Start position.
star297 0:0a76610c48a1 194 * @param x1 X-End position.
star297 0:0a76610c48a1 195 * @param y Y-position.
star297 0:0a76610c48a1 196 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 197 */
star297 0:0a76610c48a1 198 void hline(int x0, int x1, int y, int colour);
star297 0:0a76610c48a1 199 /**
star297 0:0a76610c48a1 200 * Draw a Vertical line one pixel wide in the pixel buffer.
star297 0:0a76610c48a1 201 *
star297 0:0a76610c48a1 202 * @param x0 X-Position.
star297 0:0a76610c48a1 203 * @param y0 Y-Start position.
star297 0:0a76610c48a1 204 * @param y1 Y-End position.
star297 0:0a76610c48a1 205 * @param colour Colour to set pixel to. White or Black.
star297 0:0a76610c48a1 206 */
star297 0:0a76610c48a1 207 void vline(int x0, int y0, int y1, int colour);
star297 0:0a76610c48a1 208 /**
star297 0:0a76610c48a1 209 * Draw a Line in the pixel buffer.
star297 0:0a76610c48a1 210 *
star297 0:0a76610c48a1 211 * @param x0 X-Start position.
star297 0:0a76610c48a1 212 * @param y0 Y-Start position.
star297 0:0a76610c48a1 213 * @param x1 X-End position.
star297 0:0a76610c48a1 214 * @param y1 Y-End position.
star297 0:0a76610c48a1 215 * @param colour Colour to fill rectangle to. White or Black.
star297 0:0a76610c48a1 216 */
star297 0:0a76610c48a1 217 void line(int x0, int y0, int x1, int y1, int colour);
star297 0:0a76610c48a1 218 /**
star297 0:0a76610c48a1 219 * Toggles the EXTCOM connection to the display if used.
star297 0:0a76610c48a1 220 * Call this function at 55 ~ 65 Hz to keep the display from losing contrast.
star297 0:0a76610c48a1 221 */
star297 0:0a76610c48a1 222 void toggle();
star297 0:0a76610c48a1 223 /**
star297 0:0a76610c48a1 224 * Transfers pixel buffer to the display
star297 0:0a76610c48a1 225 */
star297 0:0a76610c48a1 226 void update();
star297 0:0a76610c48a1 227 /**
star297 0:0a76610c48a1 228 * Clear the display & set pixel buffer to zero.
star297 0:0a76610c48a1 229 */
star297 0:0a76610c48a1 230 void clearImmediate();
star297 0:0a76610c48a1 231 /**
star297 0:0a76610c48a1 232 * Move bitmap into pixel buffer
star297 0:0a76610c48a1 233 *
star297 0:0a76610c48a1 234 * @param bitmap pointer to uint8 array containing horizontal pixel data
star297 0:0a76610c48a1 235 * @param bmpWidth width of the bitmap in pixels (must be byte multiple, eg 8,16,32,...)
star297 0:0a76610c48a1 236 * @param bmpHeight height of the bitmap in pixels (must be byte multiple, eg 8,16,32,...)
star297 0:0a76610c48a1 237 * @param startX starting position to apply bitmap in horizontal direction (0 = leftmost) (pixel resolution)
star297 0:0a76610c48a1 238 * @param startY starting position to apply bitmap in vertical direction (0 = topmost) (pixel resolution)
star297 0:0a76610c48a1 239 */
star297 0:0a76610c48a1 240 void showBMP(const uint8_t* bitmap, const uint32_t bmpWidth, const uint32_t bmpHeight, const uint32_t startX, const uint32_t startY);
star297 0:0a76610c48a1 241 /** Output a character at the given position
star297 0:0a76610c48a1 242 *
star297 0:0a76610c48a1 243 * @param column column where charater must be written
star297 0:0a76610c48a1 244 * @param row where character must be written
star297 0:0a76610c48a1 245 * @param c the character to be written to the TextDisplay
star297 0:0a76610c48a1 246 */
star297 0:0a76610c48a1 247 void character(int column, int row, int c);
star297 0:0a76610c48a1 248
star297 0:0a76610c48a1 249 /** return number if rows on TextDisplay
star297 0:0a76610c48a1 250 * @result number of rows
star297 0:0a76610c48a1 251 */
star297 0:0a76610c48a1 252 int rows();
star297 0:0a76610c48a1 253 /** return number if columns on TextDisplay
star297 0:0a76610c48a1 254 * @result number of rows
star297 0:0a76610c48a1 255 */
star297 0:0a76610c48a1 256 int columns();
star297 0:0a76610c48a1 257
star297 0:0a76610c48a1 258 private:
star297 0:0a76610c48a1 259 void window(int x, int y, int w, int h);
star297 0:0a76610c48a1 260 void putp(int colour);
star297 0:0a76610c48a1 261 void blit(int x, int y, int w, int h, const int *colour);
star297 0:0a76610c48a1 262 void blitbit(int x, int y, int w, int h, const char* colour);
star297 0:0a76610c48a1 263 protected:
star297 0:0a76610c48a1 264 //SPI spi; // standard SPI
star297 0:0a76610c48a1 265 BurstSPI spi; // Faster SPI pixel buffer transfer
star297 0:0a76610c48a1 266 DigitalOut chipSelect;
star297 0:0a76610c48a1 267 DigitalOut Enable;
star297 0:0a76610c48a1 268 DigitalOut ExtCom;
star297 0:0a76610c48a1 269
star297 0:0a76610c48a1 270 uint8_t lcdPolarity;
star297 0:0a76610c48a1 271 volatile uint32_t rowCount;
star297 0:0a76610c48a1 272 uint8_t cmd[2 + (DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE))];
star297 0:0a76610c48a1 273 volatile DISPLAY_BUFFER_TYPE pixelBuffer[DISPLAY_BUFFER_ELEMENTS]; // one full frame buffer
star297 0:0a76610c48a1 274 volatile DISPLAY_BUFFER_TYPE RowState[DISPLAY_HEIGHT/DISPLAY_BUFFER_TYPE_SIZE]; // 1 bit per row to indicate row change status
star297 0:0a76610c48a1 275
star297 0:0a76610c48a1 276 // transfers pixel buffer to display
star297 0:0a76610c48a1 277 void display_write();
star297 0:0a76610c48a1 278
star297 0:0a76610c48a1 279 // pixel location
star297 0:0a76610c48a1 280 short _x;
star297 0:0a76610c48a1 281 short _y;
star297 0:0a76610c48a1 282
star297 0:0a76610c48a1 283 // window location
star297 0:0a76610c48a1 284 short _x1;
star297 0:0a76610c48a1 285 short _x2;
star297 0:0a76610c48a1 286 short _y1;
star297 0:0a76610c48a1 287 short _y2;
star297 0:0a76610c48a1 288
star297 0:0a76610c48a1 289 int _putc(int value);
star297 0:0a76610c48a1 290 int _getc();
star297 0:0a76610c48a1 291 int width();
star297 0:0a76610c48a1 292 int height();
star297 0:0a76610c48a1 293
star297 0:0a76610c48a1 294 // external font functions
star297 0:0a76610c48a1 295 const unsigned char* font;
star297 0:0a76610c48a1 296 int externalfont;
star297 0:0a76610c48a1 297
star297 0:0a76610c48a1 298 // character location
star297 0:0a76610c48a1 299 int _column;
star297 0:0a76610c48a1 300 int _row;
star297 0:0a76610c48a1 301 unsigned int char_x;
star297 0:0a76610c48a1 302 unsigned int char_y;
star297 0:0a76610c48a1 303
star297 0:0a76610c48a1 304 };
star297 0:0a76610c48a1 305 #endif