Fork of Chris Styles' C12832 LCD driver

Dependents:   co657_lcdplay co657_nrf52_beacons door_lock co657_IoT

Fork of C12832 by Chris Styles

Committer:
co657_frmb
Date:
Fri Nov 27 22:45:20 2015 +0000
Revision:
22:7accdf0bfc18
Parent:
21:f5ad9d768b91
Some updates for drawing optimisation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:4bbc531be6e2 1 /* mbed library for the mbed Lab Board 128*32 pixel LCD
dreschpe 0:4bbc531be6e2 2 * use C12832 controller
dreschpe 0:4bbc531be6e2 3 * Copyright (c) 2012 Peter Drescher - DC2PD
dreschpe 0:4bbc531be6e2 4 * Released under the MIT License: http://mbed.org/license/mit
dreschpe 0:4bbc531be6e2 5 *
dreschpe 0:4bbc531be6e2 6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 0:4bbc531be6e2 7 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 0:4bbc531be6e2 8 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 0:4bbc531be6e2 9 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 0:4bbc531be6e2 10 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 0:4bbc531be6e2 11 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 0:4bbc531be6e2 12 * THE SOFTWARE.
dreschpe 0:4bbc531be6e2 13 */
dreschpe 0:4bbc531be6e2 14
dreschpe 0:4bbc531be6e2 15 #ifndef C12832_H
dreschpe 0:4bbc531be6e2 16 #define C12832_H
dreschpe 0:4bbc531be6e2 17
dreschpe 0:4bbc531be6e2 18 #include "mbed.h"
dreschpe 0:4bbc531be6e2 19 #include "GraphicsDisplay.h"
dreschpe 0:4bbc531be6e2 20
dreschpe 0:4bbc531be6e2 21
dreschpe 1:66dd8afbfd06 22 /** optional Defines :
dreschpe 0:4bbc531be6e2 23 * #define debug_lcd 1 enable infos to PC_USB
dreschpe 0:4bbc531be6e2 24 */
dreschpe 0:4bbc531be6e2 25
dreschpe 0:4bbc531be6e2 26 // some defines for the DMA use
dreschpe 0:4bbc531be6e2 27 #define DMA_CHANNEL_ENABLE 1
dreschpe 0:4bbc531be6e2 28 #define DMA_TRANSFER_TYPE_M2P (1UL << 11)
dreschpe 0:4bbc531be6e2 29 #define DMA_CHANNEL_TCIE (1UL << 31)
dreschpe 0:4bbc531be6e2 30 #define DMA_CHANNEL_SRC_INC (1UL << 26)
dreschpe 0:4bbc531be6e2 31 #define DMA_MASK_IE (1UL << 14)
dreschpe 0:4bbc531be6e2 32 #define DMA_MASK_ITC (1UL << 15)
dreschpe 0:4bbc531be6e2 33 #define DMA_SSP1_TX (1UL << 2)
dreschpe 0:4bbc531be6e2 34 #define DMA_SSP0_TX (0)
dreschpe 0:4bbc531be6e2 35 #define DMA_DEST_SSP1_TX (2UL << 6)
dreschpe 0:4bbc531be6e2 36 #define DMA_DEST_SSP0_TX (0UL << 6)
dreschpe 0:4bbc531be6e2 37
dreschpe 1:66dd8afbfd06 38 /** Draw mode
dreschpe 1:66dd8afbfd06 39 * NORMAl
dreschpe 1:66dd8afbfd06 40 * XOR set pixel by xor the screen
dreschpe 1:66dd8afbfd06 41 */
dreschpe 0:4bbc531be6e2 42 enum {NORMAL,XOR};
dreschpe 0:4bbc531be6e2 43
dreschpe 7:0f5a3b0f3cab 44 /** Bitmap
dreschpe 7:0f5a3b0f3cab 45 */
dreschpe 7:0f5a3b0f3cab 46 struct Bitmap{
dreschpe 7:0f5a3b0f3cab 47 int xSize;
dreschpe 7:0f5a3b0f3cab 48 int ySize;
dreschpe 7:0f5a3b0f3cab 49 int Byte_in_Line;
dreschpe 7:0f5a3b0f3cab 50 char* data;
dreschpe 7:0f5a3b0f3cab 51 };
dreschpe 7:0f5a3b0f3cab 52
chris 16:7de323fa46fe 53 class C12832 : public GraphicsDisplay
dreschpe 0:4bbc531be6e2 54 {
dreschpe 0:4bbc531be6e2 55 public:
chris 16:7de323fa46fe 56 /** Create a C12832 object connected to SPI1
dreschpe 0:4bbc531be6e2 57 *
dreschpe 0:4bbc531be6e2 58 */
dreschpe 0:4bbc531be6e2 59
chris 16:7de323fa46fe 60 C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name = "LCD");
dreschpe 0:4bbc531be6e2 61
chris 13:8a9cad03124b 62
dreschpe 0:4bbc531be6e2 63 /** Get the width of the screen in pixel
dreschpe 0:4bbc531be6e2 64 *
dreschpe 0:4bbc531be6e2 65 * @param
dreschpe 0:4bbc531be6e2 66 * @returns width of screen in pixel
dreschpe 0:4bbc531be6e2 67 *
dreschpe 0:4bbc531be6e2 68 */
dreschpe 0:4bbc531be6e2 69 virtual int width();
dreschpe 0:4bbc531be6e2 70
dreschpe 0:4bbc531be6e2 71 /** Get the height of the screen in pixel
dreschpe 0:4bbc531be6e2 72 *
dreschpe 0:4bbc531be6e2 73 * @returns height of screen in pixel
dreschpe 0:4bbc531be6e2 74 *
dreschpe 0:4bbc531be6e2 75 */
dreschpe 0:4bbc531be6e2 76 virtual int height();
dreschpe 0:4bbc531be6e2 77
dreschpe 0:4bbc531be6e2 78 /** Draw a pixel at x,y black or white
dreschpe 0:4bbc531be6e2 79 *
dreschpe 0:4bbc531be6e2 80 * @param x horizontal position
dreschpe 0:4bbc531be6e2 81 * @param y vertical position
dreschpe 0:4bbc531be6e2 82 * @param colour ,1 set pixel ,0 erase pixel
dreschpe 0:4bbc531be6e2 83 */
co657_frmb 18:8c294697c901 84 virtual void pixel (int x, int y, int colour);
dreschpe 0:4bbc531be6e2 85
dreschpe 0:4bbc531be6e2 86 /** draw a circle
dreschpe 0:4bbc531be6e2 87 *
dreschpe 0:4bbc531be6e2 88 * @param x0,y0 center
dreschpe 0:4bbc531be6e2 89 * @param r radius
dreschpe 0:4bbc531be6e2 90 * @param colour ,1 set pixel ,0 erase pixel
dreschpe 0:4bbc531be6e2 91 *
dreschpe 0:4bbc531be6e2 92 */
dreschpe 0:4bbc531be6e2 93 void circle(int x, int y, int r, int colour);
dreschpe 0:4bbc531be6e2 94
dreschpe 0:4bbc531be6e2 95 /** draw a filled circle
dreschpe 0:4bbc531be6e2 96 *
dreschpe 0:4bbc531be6e2 97 * @param x0,y0 center
dreschpe 0:4bbc531be6e2 98 * @param r radius
dreschpe 0:4bbc531be6e2 99 * @param color ,1 set pixel ,0 erase pixel
dreschpe 0:4bbc531be6e2 100 *
dreschpe 0:4bbc531be6e2 101 * use circle with different radius,
dreschpe 0:4bbc531be6e2 102 * can miss some pixel
dreschpe 0:4bbc531be6e2 103 */
dreschpe 0:4bbc531be6e2 104 void fillcircle(int x, int y, int r, int colour);
dreschpe 0:4bbc531be6e2 105
dreschpe 0:4bbc531be6e2 106 /** draw a 1 pixel line
dreschpe 0:4bbc531be6e2 107 *
dreschpe 0:4bbc531be6e2 108 * @param x0,y0 start point
dreschpe 0:4bbc531be6e2 109 * @param x1,y1 stop point
co657_frmb 17:67f9ca828270 110 * @param colour 1 set pixel, 0 erase pixel
dreschpe 0:4bbc531be6e2 111 *
dreschpe 0:4bbc531be6e2 112 */
co657_frmb 18:8c294697c901 113 void line (int x0, int y0, int x1, int y1, int colour);
dreschpe 1:66dd8afbfd06 114
co657_frmb 17:67f9ca828270 115
co657_frmb 18:8c294697c901 116 /** draw a horizontal line. Note x1 must be >= x0.
co657_frmb 17:67f9ca828270 117 *
co657_frmb 17:67f9ca828270 118 * @param x0 horizontal start
co657_frmb 17:67f9ca828270 119 * @param y0 vertical position
co657_frmb 17:67f9ca828270 120 * @param x1 horizontal stop
co657_frmb 17:67f9ca828270 121 * @param colour 1 set pixel, 0 erase pixel
co657_frmb 17:67f9ca828270 122 *
co657_frmb 17:67f9ca828270 123 */
co657_frmb 18:8c294697c901 124 void hline (int x0, int y0, int x1, int colour);
co657_frmb 17:67f9ca828270 125
co657_frmb 17:67f9ca828270 126
co657_frmb 18:8c294697c901 127 /** draw a vertical line. Note y1 must be >= y0.
co657_frmb 17:67f9ca828270 128 *
co657_frmb 17:67f9ca828270 129 * @param x0 horizontal position
co657_frmb 17:67f9ca828270 130 * @param y0 vertical start
co657_frmb 17:67f9ca828270 131 * @param y1 vertical stop
co657_frmb 17:67f9ca828270 132 * @param colour 1 set pixel, 0 erase pixel
co657_frmb 17:67f9ca828270 133 *
co657_frmb 17:67f9ca828270 134 */
co657_frmb 18:8c294697c901 135 void vline (int x0, int y0, int y1, int colour);
co657_frmb 17:67f9ca828270 136
co657_frmb 17:67f9ca828270 137
co657_frmb 18:8c294697c901 138 /** draw a rectangle
dreschpe 1:66dd8afbfd06 139 *
dreschpe 1:66dd8afbfd06 140 * @param x0,y0 top left corner
dreschpe 1:66dd8afbfd06 141 * @param x1,y1 down right corner
dreschpe 1:66dd8afbfd06 142 * @param color 1 set pixel ,0 erase pixel
dreschpe 1:66dd8afbfd06 143 * *
dreschpe 1:66dd8afbfd06 144 */
dreschpe 1:66dd8afbfd06 145 void rect(int x0, int y0, int x1, int y1, int colour);
dreschpe 1:66dd8afbfd06 146
co657_frmb 18:8c294697c901 147 /** draw a filled rectangle
dreschpe 1:66dd8afbfd06 148 *
dreschpe 1:66dd8afbfd06 149 * @param x0,y0 top left corner
dreschpe 1:66dd8afbfd06 150 * @param x1,y1 down right corner
dreschpe 1:66dd8afbfd06 151 * @param color 1 set pixel ,0 erase pixel
dreschpe 0:4bbc531be6e2 152 *
dreschpe 0:4bbc531be6e2 153 */
dreschpe 1:66dd8afbfd06 154 void fillrect(int x0, int y0, int x1, int y1, int colour);
dreschpe 1:66dd8afbfd06 155
dreschpe 1:66dd8afbfd06 156 /** copy display buffer to lcd
dreschpe 1:66dd8afbfd06 157 *
dreschpe 1:66dd8afbfd06 158 */
dreschpe 1:66dd8afbfd06 159
dreschpe 0:4bbc531be6e2 160 void copy_to_lcd(void);
dreschpe 0:4bbc531be6e2 161
dreschpe 0:4bbc531be6e2 162 /** set the orienation of the screen
dreschpe 1:66dd8afbfd06 163 *
dreschpe 1:66dd8afbfd06 164 */
dreschpe 1:66dd8afbfd06 165
dreschpe 0:4bbc531be6e2 166
dreschpe 0:4bbc531be6e2 167 void set_contrast(unsigned int o);
dreschpe 1:66dd8afbfd06 168
dreschpe 1:66dd8afbfd06 169 /** read the contrast level
dreschpe 1:66dd8afbfd06 170 *
dreschpe 1:66dd8afbfd06 171 */
dreschpe 1:66dd8afbfd06 172 unsigned int get_contrast(void);
dreschpe 1:66dd8afbfd06 173
dreschpe 1:66dd8afbfd06 174
dreschpe 1:66dd8afbfd06 175 /** invert the screen
dreschpe 1:66dd8afbfd06 176 *
dreschpe 1:66dd8afbfd06 177 * @param o = 0 normal, 1 invert
dreschpe 1:66dd8afbfd06 178 */
dreschpe 0:4bbc531be6e2 179 void invert(unsigned int o);
dreschpe 1:66dd8afbfd06 180
dreschpe 1:66dd8afbfd06 181 /** clear the screen
dreschpe 1:66dd8afbfd06 182 *
dreschpe 1:66dd8afbfd06 183 */
dreschpe 1:66dd8afbfd06 184 virtual void cls(void);
dreschpe 1:66dd8afbfd06 185
dreschpe 1:66dd8afbfd06 186 /** set the drawing mode
dreschpe 1:66dd8afbfd06 187 *
dreschpe 1:66dd8afbfd06 188 * @param mode NORMAl or XOR
dreschpe 1:66dd8afbfd06 189 */
dreschpe 1:66dd8afbfd06 190
dreschpe 1:66dd8afbfd06 191 void setmode(int mode);
dreschpe 1:66dd8afbfd06 192
dreschpe 6:6b96b16aad47 193 virtual int columns(void);
dreschpe 1:66dd8afbfd06 194
dreschpe 1:66dd8afbfd06 195 /** calculate the max number of columns
dreschpe 1:66dd8afbfd06 196 *
dreschpe 1:66dd8afbfd06 197 * @returns max column
dreschpe 1:66dd8afbfd06 198 * depends on actual font size
dreschpe 1:66dd8afbfd06 199 *
dreschpe 1:66dd8afbfd06 200 */
dreschpe 6:6b96b16aad47 201 virtual int rows(void);
dreschpe 1:66dd8afbfd06 202
dreschpe 1:66dd8afbfd06 203 /** put a char on the screen
dreschpe 1:66dd8afbfd06 204 *
dreschpe 1:66dd8afbfd06 205 * @param value char to print
dreschpe 1:66dd8afbfd06 206 * @returns printed char
dreschpe 1:66dd8afbfd06 207 *
dreschpe 1:66dd8afbfd06 208 */
dreschpe 6:6b96b16aad47 209 virtual int _putc(int value);
dreschpe 1:66dd8afbfd06 210
dreschpe 1:66dd8afbfd06 211 /** draw a character on given position out of the active font to the LCD
dreschpe 1:66dd8afbfd06 212 *
dreschpe 1:66dd8afbfd06 213 * @param x x-position of char (top left)
dreschpe 1:66dd8afbfd06 214 * @param y y-position
dreschpe 1:66dd8afbfd06 215 * @param c char to print
dreschpe 1:66dd8afbfd06 216 *
dreschpe 1:66dd8afbfd06 217 */
dreschpe 1:66dd8afbfd06 218 virtual void character(int x, int y, int c);
dreschpe 1:66dd8afbfd06 219
dreschpe 1:66dd8afbfd06 220 /** setup cursor position
dreschpe 1:66dd8afbfd06 221 *
dreschpe 1:66dd8afbfd06 222 * @param x x-position (top left)
dreschpe 1:66dd8afbfd06 223 * @param y y-position
dreschpe 1:66dd8afbfd06 224 */
dreschpe 6:6b96b16aad47 225 virtual void locate(int x, int y);
dreschpe 3:468cdccff7af 226
dreschpe 3:468cdccff7af 227 /** setup auto update of screen
dreschpe 3:468cdccff7af 228 *
dreschpe 3:468cdccff7af 229 * @param up 1 = on , 0 = off
dreschpe 3:468cdccff7af 230 * if switched off the program has to call copy_to_lcd()
dreschpe 3:468cdccff7af 231 * to update screen from framebuffer
dreschpe 3:468cdccff7af 232 */
dreschpe 5:0f53e522a2bf 233 void set_auto_up(unsigned int up);
dreschpe 1:66dd8afbfd06 234
dreschpe 3:468cdccff7af 235 /** get status of the auto update function
dreschpe 3:468cdccff7af 236 *
dreschpe 3:468cdccff7af 237 * @returns if auto update is on
dreschpe 3:468cdccff7af 238 */
dreschpe 5:0f53e522a2bf 239 unsigned int get_auto_up(void);
dreschpe 0:4bbc531be6e2 240
dreschpe 0:4bbc531be6e2 241 /** Vars */
dreschpe 0:4bbc531be6e2 242 SPI _spi;
dreschpe 0:4bbc531be6e2 243 DigitalOut _reset;
dreschpe 0:4bbc531be6e2 244 DigitalOut _A0;
dreschpe 0:4bbc531be6e2 245 DigitalOut _CS;
dreschpe 0:4bbc531be6e2 246 unsigned char* font;
dreschpe 0:4bbc531be6e2 247 unsigned int draw_mode;
dreschpe 0:4bbc531be6e2 248
dreschpe 0:4bbc531be6e2 249
dreschpe 1:66dd8afbfd06 250 /** select the font to use
dreschpe 1:66dd8afbfd06 251 *
dreschpe 1:66dd8afbfd06 252 * @param f pointer to font array
dreschpe 1:66dd8afbfd06 253 *
dreschpe 1:66dd8afbfd06 254 * font array can created with GLCD Font Creator from http://www.mikroe.com
dreschpe 1:66dd8afbfd06 255 * you have to add 4 parameter at the beginning of the font array to use:
dreschpe 1:66dd8afbfd06 256 * - the number of byte / char
dreschpe 1:66dd8afbfd06 257 * - the vertial size in pixel
dreschpe 1:66dd8afbfd06 258 * - the horizontal size in pixel
dreschpe 1:66dd8afbfd06 259 * - the number of byte per vertical line
dreschpe 1:66dd8afbfd06 260 * you also have to change the array to char[]
dreschpe 1:66dd8afbfd06 261 *
dreschpe 1:66dd8afbfd06 262 */
dreschpe 1:66dd8afbfd06 263 void set_font(unsigned char* f);
dreschpe 7:0f5a3b0f3cab 264
dreschpe 7:0f5a3b0f3cab 265 /** print bitmap to buffer
dreschpe 7:0f5a3b0f3cab 266 *
dreschpe 7:0f5a3b0f3cab 267 * @param bm Bitmap in flash
dreschpe 7:0f5a3b0f3cab 268 * @param x x start
dreschpe 7:0f5a3b0f3cab 269 * @param y y start
dreschpe 7:0f5a3b0f3cab 270 *
dreschpe 7:0f5a3b0f3cab 271 */
dreschpe 0:4bbc531be6e2 272
dreschpe 7:0f5a3b0f3cab 273 void print_bm(Bitmap bm, int x, int y);
dreschpe 0:4bbc531be6e2 274
co657_frmb 18:8c294697c901 275 /** Plot a pixel without bounds checking
co657_frmb 18:8c294697c901 276 *
co657_frmb 18:8c294697c901 277 * @param x X position [0..127]
co657_frmb 18:8c294697c901 278 * @param y Y position [0..31]
co657_frmb 18:8c294697c901 279 * @param colour colour (1 = set, 0 = clear)
co657_frmb 18:8c294697c901 280 */
co657_frmb 18:8c294697c901 281 void pixel_nochk (int x, int y, int colour);
dreschpe 0:4bbc531be6e2 282
co657_frmb 22:7accdf0bfc18 283 /** Plots a pixel, but without bounds checking (assumed to be done elsewhere). Fixed normal-mode. */
co657_frmb 22:7accdf0bfc18 284 inline void pixel_nochk_norm (int x, int y, int colour)
co657_frmb 22:7accdf0bfc18 285 {
co657_frmb 22:7accdf0bfc18 286 if (colour == 0) {
co657_frmb 22:7accdf0bfc18 287 buffer[x + ((y & ~7) << 4)] &= ~(1 << (y & 0x07)); // erase pixel
co657_frmb 22:7accdf0bfc18 288 } else {
co657_frmb 22:7accdf0bfc18 289 buffer[x + ((y & ~7) << 4)] |= (1 << (y & 0x07)); // set pixel
co657_frmb 22:7accdf0bfc18 290 }
co657_frmb 22:7accdf0bfc18 291 }
co657_frmb 22:7accdf0bfc18 292
co657_frmb 22:7accdf0bfc18 293 /** Plots a pixel, but without bounds checking (assumed to be done elsewhere). Fixed XOR mode. */
co657_frmb 22:7accdf0bfc18 294 inline void pixel_nochk_xor (int x, int y, int colour)
co657_frmb 22:7accdf0bfc18 295 {
co657_frmb 22:7accdf0bfc18 296 if (colour == 1) {
co657_frmb 22:7accdf0bfc18 297 buffer[x + ((y & ~7) << 4)] ^= (1 << (y & 0x07)); // xor pixel
co657_frmb 22:7accdf0bfc18 298 }
co657_frmb 22:7accdf0bfc18 299 }
co657_frmb 22:7accdf0bfc18 300
co657_frmb 21:f5ad9d768b91 301 protected:
co657_frmb 21:f5ad9d768b91 302
dreschpe 0:4bbc531be6e2 303 /** Init the C12832 LCD controller
dreschpe 0:4bbc531be6e2 304 *
dreschpe 0:4bbc531be6e2 305 */
dreschpe 0:4bbc531be6e2 306 void lcd_reset();
dreschpe 0:4bbc531be6e2 307
dreschpe 0:4bbc531be6e2 308 /** Write data to the LCD controller
dreschpe 0:4bbc531be6e2 309 *
dreschpe 0:4bbc531be6e2 310 * @param dat data written to LCD controller
dreschpe 0:4bbc531be6e2 311 *
dreschpe 0:4bbc531be6e2 312 */
dreschpe 0:4bbc531be6e2 313 void wr_dat(unsigned char value);
dreschpe 0:4bbc531be6e2 314
dreschpe 0:4bbc531be6e2 315 /** Write a command the LCD controller
dreschpe 0:4bbc531be6e2 316 *
dreschpe 0:4bbc531be6e2 317 * @param cmd: command to be written
dreschpe 0:4bbc531be6e2 318 *
dreschpe 0:4bbc531be6e2 319 */
dreschpe 0:4bbc531be6e2 320 void wr_cmd(unsigned char value);
dreschpe 1:66dd8afbfd06 321
dreschpe 0:4bbc531be6e2 322 void wr_cnt(unsigned char cmd);
dreschpe 0:4bbc531be6e2 323
dreschpe 0:4bbc531be6e2 324 unsigned int orientation;
dreschpe 0:4bbc531be6e2 325 unsigned int char_x;
dreschpe 0:4bbc531be6e2 326 unsigned int char_y;
dreschpe 0:4bbc531be6e2 327 unsigned char buffer[512];
dreschpe 1:66dd8afbfd06 328 unsigned int contrast;
dreschpe 3:468cdccff7af 329 unsigned int auto_up;
dreschpe 0:4bbc531be6e2 330
dreschpe 0:4bbc531be6e2 331 };
dreschpe 0:4bbc531be6e2 332
dreschpe 0:4bbc531be6e2 333
dreschpe 0:4bbc531be6e2 334
dreschpe 0:4bbc531be6e2 335
dreschpe 0:4bbc531be6e2 336 #endif