Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.
Fork of N3310LCD by
Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.
N3310LCD.cpp@5:1fd7af32e521, 2013-03-27 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed Mar 27 21:09:58 2013 +0000
- Revision:
- 5:1fd7af32e521
- Parent:
- 4:90dce6032a37
- Child:
- 6:46bcc4e584c4
Used Stream class for printf output
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:7efa6655d94b | 1 | /* |
SomeRandomBloke | 0:7efa6655d94b | 2 | * N3310LCD. A program to interface mbed with the nuelectronics |
SomeRandomBloke | 0:7efa6655d94b | 3 | * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from |
SomeRandomBloke | 0:7efa6655d94b | 4 | * the nuelectronics Arduino code. |
SomeRandomBloke | 0:7efa6655d94b | 5 | * |
SomeRandomBloke | 0:7efa6655d94b | 6 | * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> |
SomeRandomBloke | 0:7efa6655d94b | 7 | * |
SomeRandomBloke | 0:7efa6655d94b | 8 | * Converted to a mbed library by Andrew D. Lindsay |
SomeRandomBloke | 0:7efa6655d94b | 9 | * |
SomeRandomBloke | 0:7efa6655d94b | 10 | * This file is part of N3310LCD. |
SomeRandomBloke | 0:7efa6655d94b | 11 | * |
SomeRandomBloke | 0:7efa6655d94b | 12 | * N3310LCD is free software: you can redistribute it and/or modify |
SomeRandomBloke | 0:7efa6655d94b | 13 | * it under the terms of the GNU General Public License as published by |
SomeRandomBloke | 0:7efa6655d94b | 14 | * the Free Software Foundation, either version 3 of the License, or |
SomeRandomBloke | 0:7efa6655d94b | 15 | * (at your option) any later version. |
SomeRandomBloke | 1:51961974fe55 | 16 | * |
SomeRandomBloke | 0:7efa6655d94b | 17 | * N3310LCD is distributed in the hope that it will be useful, |
SomeRandomBloke | 0:7efa6655d94b | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
SomeRandomBloke | 0:7efa6655d94b | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
SomeRandomBloke | 0:7efa6655d94b | 20 | * GNU General Public License for more details. |
SomeRandomBloke | 0:7efa6655d94b | 21 | * |
SomeRandomBloke | 0:7efa6655d94b | 22 | * You should have received a copy of the GNU General Public License |
SomeRandomBloke | 0:7efa6655d94b | 23 | * along with N3310LCD. If not, see <http://www.gnu.org/licenses/>. |
SomeRandomBloke | 0:7efa6655d94b | 24 | */ |
SomeRandomBloke | 0:7efa6655d94b | 25 | |
SomeRandomBloke | 0:7efa6655d94b | 26 | #include "N3310LCD.h" |
SomeRandomBloke | 0:7efa6655d94b | 27 | #include "N3310Fonts.h" |
SomeRandomBloke | 0:7efa6655d94b | 28 | |
SomeRandomBloke | 0:7efa6655d94b | 29 | static unsigned char lcd_buffer[LCDROWMAX][LCDCOLMAX]; |
SomeRandomBloke | 3:9808f63fd2fe | 30 | // current cursor postition |
SomeRandomBloke | 3:9808f63fd2fe | 31 | static unsigned char cursor_row = 0; /* 0-5 */ |
SomeRandomBloke | 3:9808f63fd2fe | 32 | static unsigned char cursor_col = 0; /* 0-83 */ |
SomeRandomBloke | 3:9808f63fd2fe | 33 | unsigned char *fontStart; // Start of font data |
SomeRandomBloke | 3:9808f63fd2fe | 34 | int8_t fontWidth; // Font width |
SomeRandomBloke | 3:9808f63fd2fe | 35 | int8_t fontHeight; // Font height |
SomeRandomBloke | 4:90dce6032a37 | 36 | int16_t fontStartChar; // Start, usually 32 |
SomeRandomBloke | 4:90dce6032a37 | 37 | int16_t fontEndChar; // End character, usually 127 or 128 |
SomeRandomBloke | 0:7efa6655d94b | 38 | |
SomeRandomBloke | 1:51961974fe55 | 39 | N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck, |
SomeRandomBloke | 1:51961974fe55 | 40 | PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) : |
SomeRandomBloke | 1:51961974fe55 | 41 | lcdPort(mosi, miso, sck), |
SomeRandomBloke | 4:90dce6032a37 | 42 | ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on) { |
SomeRandomBloke | 0:7efa6655d94b | 43 | } |
SomeRandomBloke | 0:7efa6655d94b | 44 | |
SomeRandomBloke | 0:7efa6655d94b | 45 | void N3310LCD::init() |
SomeRandomBloke | 0:7efa6655d94b | 46 | { |
SomeRandomBloke | 0:7efa6655d94b | 47 | // use default SPI format |
SomeRandomBloke | 0:7efa6655d94b | 48 | lcdPort.format(8,0); |
SomeRandomBloke | 4:90dce6032a37 | 49 | lcdPort.frequency(8000000); // Lets try 8MHz |
SomeRandomBloke | 1:51961974fe55 | 50 | |
SomeRandomBloke | 0:7efa6655d94b | 51 | // lcd reset |
SomeRandomBloke | 0:7efa6655d94b | 52 | wait_ms(1); |
SomeRandomBloke | 0:7efa6655d94b | 53 | rstWire = 0; |
SomeRandomBloke | 0:7efa6655d94b | 54 | wait_ms(1); |
SomeRandomBloke | 0:7efa6655d94b | 55 | rstWire = 1; |
SomeRandomBloke | 1:51961974fe55 | 56 | |
SomeRandomBloke | 3:9808f63fd2fe | 57 | writeCommand(0x21); // LCD Extended Commands |
SomeRandomBloke | 3:9808f63fd2fe | 58 | writeCommand(0xC0); // Set LCD Vop (Contrast) |
SomeRandomBloke | 3:9808f63fd2fe | 59 | writeCommand(0x06); // Set temp coefficient |
SomeRandomBloke | 3:9808f63fd2fe | 60 | writeCommand(0x13); // LCD bias mode1:48 |
SomeRandomBloke | 3:9808f63fd2fe | 61 | writeCommand(0x20); // LCD Standard Commands, Horizontal addressing mode |
SomeRandomBloke | 3:9808f63fd2fe | 62 | writeCommand(0x0c); // LCD in normal mode |
SomeRandomBloke | 1:51961974fe55 | 63 | cls(); |
SomeRandomBloke | 3:9808f63fd2fe | 64 | setFont( FONT_5x7 ); |
SomeRandomBloke | 3:9808f63fd2fe | 65 | // setFont( FONT_6x8 ); |
SomeRandomBloke | 3:9808f63fd2fe | 66 | |
SomeRandomBloke | 3:9808f63fd2fe | 67 | } |
SomeRandomBloke | 3:9808f63fd2fe | 68 | |
SomeRandomBloke | 3:9808f63fd2fe | 69 | void N3310LCD::setFont(BYTE font ) |
SomeRandomBloke | 3:9808f63fd2fe | 70 | { |
SomeRandomBloke | 3:9808f63fd2fe | 71 | |
SomeRandomBloke | 3:9808f63fd2fe | 72 | switch( font ) { |
SomeRandomBloke | 3:9808f63fd2fe | 73 | case FONT_6x8: |
SomeRandomBloke | 3:9808f63fd2fe | 74 | fontWidth = 6; |
SomeRandomBloke | 3:9808f63fd2fe | 75 | fontHeight = 8; |
SomeRandomBloke | 4:90dce6032a37 | 76 | fontStartChar = 32; |
SomeRandomBloke | 4:90dce6032a37 | 77 | fontEndChar = 128; |
SomeRandomBloke | 3:9808f63fd2fe | 78 | fontStart = font6_8; |
SomeRandomBloke | 3:9808f63fd2fe | 79 | break; |
SomeRandomBloke | 4:90dce6032a37 | 80 | |
SomeRandomBloke | 4:90dce6032a37 | 81 | /* case FONT_ALPHA_17x17: |
SomeRandomBloke | 4:90dce6032a37 | 82 | fontWidth = 17; |
SomeRandomBloke | 4:90dce6032a37 | 83 | fontHeight = 17; |
SomeRandomBloke | 4:90dce6032a37 | 84 | fontStartChar = 65; |
SomeRandomBloke | 4:90dce6032a37 | 85 | fontEndChar = 91; |
SomeRandomBloke | 4:90dce6032a37 | 86 | fontStart = Liberation_Sans17x17_Alpha; |
SomeRandomBloke | 4:90dce6032a37 | 87 | break; |
SomeRandomBloke | 4:90dce6032a37 | 88 | */ |
SomeRandomBloke | 3:9808f63fd2fe | 89 | default: |
SomeRandomBloke | 3:9808f63fd2fe | 90 | fontWidth = 5; |
SomeRandomBloke | 3:9808f63fd2fe | 91 | fontHeight = 7; |
SomeRandomBloke | 4:90dce6032a37 | 92 | fontStartChar = 32; |
SomeRandomBloke | 4:90dce6032a37 | 93 | fontEndChar = 128; |
SomeRandomBloke | 3:9808f63fd2fe | 94 | fontStart = font5_7; |
SomeRandomBloke | 3:9808f63fd2fe | 95 | break; |
SomeRandomBloke | 3:9808f63fd2fe | 96 | } |
SomeRandomBloke | 0:7efa6655d94b | 97 | } |
SomeRandomBloke | 0:7efa6655d94b | 98 | |
SomeRandomBloke | 0:7efa6655d94b | 99 | void N3310LCD::cls() |
SomeRandomBloke | 0:7efa6655d94b | 100 | { |
SomeRandomBloke | 3:9808f63fd2fe | 101 | writeCommand(0x40); // column |
SomeRandomBloke | 3:9808f63fd2fe | 102 | writeCommand(0x80); // row |
SomeRandomBloke | 3:9808f63fd2fe | 103 | for(int i=0; i< LCDROWMAX; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 104 | for(int j=0; j< LCDCOLMAX; j++) { |
SomeRandomBloke | 3:9808f63fd2fe | 105 | writeData( 0x00 ); |
SomeRandomBloke | 3:9808f63fd2fe | 106 | lcd_buffer[i][j] = 0x00; |
SomeRandomBloke | 3:9808f63fd2fe | 107 | } |
SomeRandomBloke | 0:7efa6655d94b | 108 | } |
SomeRandomBloke | 0:7efa6655d94b | 109 | } |
SomeRandomBloke | 0:7efa6655d94b | 110 | |
SomeRandomBloke | 0:7efa6655d94b | 111 | void N3310LCD::backlight(eBacklight state) |
SomeRandomBloke | 0:7efa6655d94b | 112 | { |
SomeRandomBloke | 0:7efa6655d94b | 113 | // switch off/on back light |
SomeRandomBloke | 0:7efa6655d94b | 114 | blWire = state; |
SomeRandomBloke | 0:7efa6655d94b | 115 | } |
SomeRandomBloke | 0:7efa6655d94b | 116 | |
SomeRandomBloke | 3:9808f63fd2fe | 117 | void N3310LCD::writeCommand(BYTE data) |
SomeRandomBloke | 0:7efa6655d94b | 118 | { |
SomeRandomBloke | 0:7efa6655d94b | 119 | // bring CS low for write |
SomeRandomBloke | 0:7efa6655d94b | 120 | ceWire = 0; |
SomeRandomBloke | 3:9808f63fd2fe | 121 | dcWire = 0; |
SomeRandomBloke | 1:51961974fe55 | 122 | |
SomeRandomBloke | 3:9808f63fd2fe | 123 | lcdPort.write(data); |
SomeRandomBloke | 3:9808f63fd2fe | 124 | |
SomeRandomBloke | 3:9808f63fd2fe | 125 | // write finished |
SomeRandomBloke | 3:9808f63fd2fe | 126 | ceWire = 1; |
SomeRandomBloke | 3:9808f63fd2fe | 127 | } |
SomeRandomBloke | 3:9808f63fd2fe | 128 | |
SomeRandomBloke | 3:9808f63fd2fe | 129 | void N3310LCD::writeData(BYTE data) |
SomeRandomBloke | 3:9808f63fd2fe | 130 | { |
SomeRandomBloke | 3:9808f63fd2fe | 131 | // bring CS low for write |
SomeRandomBloke | 3:9808f63fd2fe | 132 | ceWire = 0; |
SomeRandomBloke | 3:9808f63fd2fe | 133 | dcWire = 1; |
SomeRandomBloke | 1:51961974fe55 | 134 | |
SomeRandomBloke | 0:7efa6655d94b | 135 | lcdPort.write(data); |
SomeRandomBloke | 1:51961974fe55 | 136 | |
SomeRandomBloke | 0:7efa6655d94b | 137 | // write finished |
SomeRandomBloke | 0:7efa6655d94b | 138 | ceWire = 1; |
SomeRandomBloke | 0:7efa6655d94b | 139 | } |
SomeRandomBloke | 0:7efa6655d94b | 140 | |
SomeRandomBloke | 0:7efa6655d94b | 141 | void N3310LCD::locate(BYTE xPos, BYTE yPos) |
SomeRandomBloke | 0:7efa6655d94b | 142 | { |
SomeRandomBloke | 3:9808f63fd2fe | 143 | writeCommand(0x40 | yPos); // column |
SomeRandomBloke | 3:9808f63fd2fe | 144 | writeCommand(0x80 | xPos); // row |
SomeRandomBloke | 3:9808f63fd2fe | 145 | cursor_row = yPos; |
SomeRandomBloke | 3:9808f63fd2fe | 146 | cursor_col = xPos; |
SomeRandomBloke | 0:7efa6655d94b | 147 | } |
SomeRandomBloke | 0:7efa6655d94b | 148 | |
SomeRandomBloke | 0:7efa6655d94b | 149 | void N3310LCD::drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize) |
SomeRandomBloke | 0:7efa6655d94b | 150 | { |
SomeRandomBloke | 0:7efa6655d94b | 151 | BYTE row; |
SomeRandomBloke | 1:51961974fe55 | 152 | |
SomeRandomBloke | 0:7efa6655d94b | 153 | if (0 == bmpYSize % 8) |
SomeRandomBloke | 1:51961974fe55 | 154 | row = bmpYSize/8; |
SomeRandomBloke | 0:7efa6655d94b | 155 | else |
SomeRandomBloke | 0:7efa6655d94b | 156 | row = bmpYSize/8 + 1; |
SomeRandomBloke | 1:51961974fe55 | 157 | |
SomeRandomBloke | 1:51961974fe55 | 158 | for (BYTE n = 0; n < row; n++) { |
SomeRandomBloke | 0:7efa6655d94b | 159 | locate(xPos, yPos); |
SomeRandomBloke | 1:51961974fe55 | 160 | for(BYTE i = 0; i < bmpXSize; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 161 | writeData(bitmap[i + (n * bmpXSize)]); |
SomeRandomBloke | 0:7efa6655d94b | 162 | } |
SomeRandomBloke | 1:51961974fe55 | 163 | yPos++; |
SomeRandomBloke | 0:7efa6655d94b | 164 | } |
SomeRandomBloke | 0:7efa6655d94b | 165 | } |
SomeRandomBloke | 0:7efa6655d94b | 166 | |
SomeRandomBloke | 0:7efa6655d94b | 167 | /* |
SomeRandomBloke | 1:51961974fe55 | 168 | * Name : clearBitmap |
SomeRandomBloke | 0:7efa6655d94b | 169 | * Description : Clear an area of the screen, usually to blank out a |
SomeRandomBloke | 0:7efa6655d94b | 170 | * previously drawn image or part of image. |
SomeRandomBloke | 0:7efa6655d94b | 171 | * Argument(s) : x, y - Position on screen, x 0-83, y 1-6 |
SomeRandomBloke | 0:7efa6655d94b | 172 | * size_x,size_y - Size of the image in pixels, |
SomeRandomBloke | 0:7efa6655d94b | 173 | * size_y is multiple of 8 |
SomeRandomBloke | 1:51961974fe55 | 174 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 175 | */ |
SomeRandomBloke | 0:7efa6655d94b | 176 | void N3310LCD::clearBitmap( unsigned char x,unsigned char y, |
SomeRandomBloke | 1:51961974fe55 | 177 | unsigned char size_x,unsigned char size_y) |
SomeRandomBloke | 1:51961974fe55 | 178 | { |
SomeRandomBloke | 0:7efa6655d94b | 179 | unsigned int i,n; |
SomeRandomBloke | 0:7efa6655d94b | 180 | unsigned char row; |
SomeRandomBloke | 1:51961974fe55 | 181 | |
SomeRandomBloke | 0:7efa6655d94b | 182 | row = (size_y % 8 == 0 ) ? size_y / 8 : size_y / 8 + 1; |
SomeRandomBloke | 1:51961974fe55 | 183 | |
SomeRandomBloke | 1:51961974fe55 | 184 | for (n=0; n<row; n++) { |
SomeRandomBloke | 0:7efa6655d94b | 185 | locate(x,y); |
SomeRandomBloke | 0:7efa6655d94b | 186 | for(i=0; i<size_x; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 187 | writeData( 0x00 ); |
SomeRandomBloke | 0:7efa6655d94b | 188 | } |
SomeRandomBloke | 1:51961974fe55 | 189 | y++; |
SomeRandomBloke | 1:51961974fe55 | 190 | } |
SomeRandomBloke | 0:7efa6655d94b | 191 | } |
SomeRandomBloke | 0:7efa6655d94b | 192 | |
SomeRandomBloke | 0:7efa6655d94b | 193 | void N3310LCD::writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode) |
SomeRandomBloke | 0:7efa6655d94b | 194 | { |
SomeRandomBloke | 0:7efa6655d94b | 195 | locate(xPos, yPos); |
SomeRandomBloke | 1:51961974fe55 | 196 | |
SomeRandomBloke | 1:51961974fe55 | 197 | while (*string) { |
SomeRandomBloke | 0:7efa6655d94b | 198 | writeChar(*string++, mode); |
SomeRandomBloke | 0:7efa6655d94b | 199 | } |
SomeRandomBloke | 0:7efa6655d94b | 200 | } |
SomeRandomBloke | 1:51961974fe55 | 201 | |
SomeRandomBloke | 0:7efa6655d94b | 202 | void N3310LCD::writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode) |
SomeRandomBloke | 0:7efa6655d94b | 203 | { |
SomeRandomBloke | 1:51961974fe55 | 204 | while (*string) { |
SomeRandomBloke | 0:7efa6655d94b | 205 | writeCharBig(xPos, yPos, *string , mode); |
SomeRandomBloke | 1:51961974fe55 | 206 | |
SomeRandomBloke | 0:7efa6655d94b | 207 | if('.' == *string++) |
SomeRandomBloke | 0:7efa6655d94b | 208 | xPos += 5; |
SomeRandomBloke | 0:7efa6655d94b | 209 | else |
SomeRandomBloke | 0:7efa6655d94b | 210 | xPos += 12; |
SomeRandomBloke | 0:7efa6655d94b | 211 | } |
SomeRandomBloke | 0:7efa6655d94b | 212 | } |
SomeRandomBloke | 0:7efa6655d94b | 213 | |
SomeRandomBloke | 0:7efa6655d94b | 214 | void N3310LCD::writeChar(BYTE ch, eDisplayMode mode) |
SomeRandomBloke | 0:7efa6655d94b | 215 | { |
SomeRandomBloke | 4:90dce6032a37 | 216 | ch -= fontStartChar; |
SomeRandomBloke | 0:7efa6655d94b | 217 | |
SomeRandomBloke | 3:9808f63fd2fe | 218 | if (cursor_col > LCDCOLMAX - (fontWidth+1)) cursor_col = LCDCOLMAX - (fontWidth+1); // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 219 | if (cursor_row > LCDROWMAX - 1) cursor_row = LCDROWMAX - 1; // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 220 | lcd_buffer[cursor_row][cursor_col] = 0x00; |
SomeRandomBloke | 3:9808f63fd2fe | 221 | for(int8_t j=0; j< fontHeight; j++) { |
SomeRandomBloke | 4:90dce6032a37 | 222 | lcd_buffer[cursor_row][cursor_col + j] = fontStart[(ch)*fontWidth + j]; |
SomeRandomBloke | 0:7efa6655d94b | 223 | } |
SomeRandomBloke | 3:9808f63fd2fe | 224 | |
SomeRandomBloke | 3:9808f63fd2fe | 225 | lcd_buffer[cursor_row][cursor_col + fontWidth] = 0x00; |
SomeRandomBloke | 3:9808f63fd2fe | 226 | |
SomeRandomBloke | 3:9808f63fd2fe | 227 | for(int8_t j=0; j< (fontWidth+1); j++) { |
SomeRandomBloke | 3:9808f63fd2fe | 228 | if( mode == NORMAL ) |
SomeRandomBloke | 3:9808f63fd2fe | 229 | writeData(lcd_buffer[cursor_row][cursor_col++]); |
SomeRandomBloke | 3:9808f63fd2fe | 230 | else |
SomeRandomBloke | 3:9808f63fd2fe | 231 | writeData(lcd_buffer[cursor_row][cursor_col++] ^ 0xff); |
SomeRandomBloke | 3:9808f63fd2fe | 232 | if (cursor_col >= LCDCOLMAX) { |
SomeRandomBloke | 3:9808f63fd2fe | 233 | cursor_col=0; |
SomeRandomBloke | 3:9808f63fd2fe | 234 | cursor_row++; |
SomeRandomBloke | 3:9808f63fd2fe | 235 | if (cursor_row >= LCDROWMAX) cursor_row=0; |
SomeRandomBloke | 3:9808f63fd2fe | 236 | } |
SomeRandomBloke | 3:9808f63fd2fe | 237 | } |
SomeRandomBloke | 5:1fd7af32e521 | 238 | } |
SomeRandomBloke | 3:9808f63fd2fe | 239 | |
SomeRandomBloke | 5:1fd7af32e521 | 240 | int N3310LCD::_putc(int c) { |
SomeRandomBloke | 5:1fd7af32e521 | 241 | writeChar(c, NORMAL); |
SomeRandomBloke | 5:1fd7af32e521 | 242 | return c; |
SomeRandomBloke | 5:1fd7af32e521 | 243 | } |
SomeRandomBloke | 5:1fd7af32e521 | 244 | int N3310LCD::_getc() { |
SomeRandomBloke | 5:1fd7af32e521 | 245 | return -1; |
SomeRandomBloke | 0:7efa6655d94b | 246 | } |
SomeRandomBloke | 0:7efa6655d94b | 247 | |
SomeRandomBloke | 0:7efa6655d94b | 248 | void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode) |
SomeRandomBloke | 0:7efa6655d94b | 249 | { |
SomeRandomBloke | 0:7efa6655d94b | 250 | BYTE sendByte; |
SomeRandomBloke | 3:9808f63fd2fe | 251 | int8_t colsUsed = 12; |
SomeRandomBloke | 0:7efa6655d94b | 252 | unsigned char* pFont = (unsigned char *) big_number; |
SomeRandomBloke | 1:51961974fe55 | 253 | |
SomeRandomBloke | 3:9808f63fd2fe | 254 | |
SomeRandomBloke | 3:9808f63fd2fe | 255 | if(ch == '.') { |
SomeRandomBloke | 0:7efa6655d94b | 256 | ch = 10; |
SomeRandomBloke | 3:9808f63fd2fe | 257 | colsUsed=5; |
SomeRandomBloke | 3:9808f63fd2fe | 258 | } else if (ch == '+') |
SomeRandomBloke | 0:7efa6655d94b | 259 | ch = 11; |
SomeRandomBloke | 3:9808f63fd2fe | 260 | else if (ch == '-') |
SomeRandomBloke | 0:7efa6655d94b | 261 | ch = 12; |
SomeRandomBloke | 3:9808f63fd2fe | 262 | else if (ch == ':') |
SomeRandomBloke | 3:9808f63fd2fe | 263 | ch = 13; |
SomeRandomBloke | 3:9808f63fd2fe | 264 | else if (ch == '/') |
SomeRandomBloke | 3:9808f63fd2fe | 265 | ch = 14; |
SomeRandomBloke | 0:7efa6655d94b | 266 | else |
SomeRandomBloke | 0:7efa6655d94b | 267 | ch = ch & 0x0f; |
SomeRandomBloke | 1:51961974fe55 | 268 | |
SomeRandomBloke | 3:9808f63fd2fe | 269 | /* |
SomeRandomBloke | 3:9808f63fd2fe | 270 | for(BYTE i = 0; i < 3; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 271 | locate(xPos, yPos + i); |
SomeRandomBloke | 1:51961974fe55 | 272 | |
SomeRandomBloke | 3:9808f63fd2fe | 273 | for(BYTE j = 0; j < 16; j++) { |
SomeRandomBloke | 3:9808f63fd2fe | 274 | sendByte = *(pFont + ch*48 + i*16 + j); |
SomeRandomBloke | 3:9808f63fd2fe | 275 | writeData((mode == NORMAL)? sendByte : (sendByte^0xff)); |
SomeRandomBloke | 3:9808f63fd2fe | 276 | } |
SomeRandomBloke | 3:9808f63fd2fe | 277 | } |
SomeRandomBloke | 3:9808f63fd2fe | 278 | */ |
SomeRandomBloke | 3:9808f63fd2fe | 279 | if (xPos > LCDCOLMAX - colsUsed) xPos = LCDCOLMAX - colsUsed ; // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 280 | if (yPos > LCDROWMAX - 3) yPos = LCDROWMAX - 3 ; // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 281 | |
SomeRandomBloke | 3:9808f63fd2fe | 282 | for(int8_t i=0; i<3; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 283 | locate( xPos, yPos + i); |
SomeRandomBloke | 3:9808f63fd2fe | 284 | |
SomeRandomBloke | 3:9808f63fd2fe | 285 | for(int8_t j=0; j<colsUsed; j++) { |
SomeRandomBloke | 0:7efa6655d94b | 286 | sendByte = *(pFont + ch*48 + i*16 + j); |
SomeRandomBloke | 3:9808f63fd2fe | 287 | lcd_buffer[cursor_row][cursor_col + j] = (mode == NORMAL)? sendByte : (sendByte^0xff); |
SomeRandomBloke | 4:90dce6032a37 | 288 | writeData( lcd_buffer[cursor_row][cursor_col + j] ); |
SomeRandomBloke | 0:7efa6655d94b | 289 | } |
SomeRandomBloke | 0:7efa6655d94b | 290 | } |
SomeRandomBloke | 0:7efa6655d94b | 291 | } |
SomeRandomBloke | 0:7efa6655d94b | 292 | |
SomeRandomBloke | 0:7efa6655d94b | 293 | /* |
SomeRandomBloke | 0:7efa6655d94b | 294 | * Name : setPixel |
SomeRandomBloke | 0:7efa6655d94b | 295 | * Description : Set a single pixel either on or off, update display buffer. |
SomeRandomBloke | 0:7efa6655d94b | 296 | * Argument(s) : x,y - position, x = 0-83, y = 0-6 |
SomeRandomBloke | 0:7efa6655d94b | 297 | * c - colour, either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 298 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 299 | */ |
SomeRandomBloke | 1:51961974fe55 | 300 | void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c ) |
SomeRandomBloke | 1:51961974fe55 | 301 | { |
SomeRandomBloke | 1:51961974fe55 | 302 | unsigned char value; |
SomeRandomBloke | 1:51961974fe55 | 303 | unsigned char row; |
SomeRandomBloke | 1:51961974fe55 | 304 | |
SomeRandomBloke | 0:7efa6655d94b | 305 | // if( x < 0 || x >= LCDCOLMAX || y < 0 || y >= LCDPIXELROWMAX ) return; |
SomeRandomBloke | 0:7efa6655d94b | 306 | if( x >= LCDCOLMAX || y >= LCDPIXELROWMAX ) return; |
SomeRandomBloke | 0:7efa6655d94b | 307 | |
SomeRandomBloke | 0:7efa6655d94b | 308 | row = y / 8; |
SomeRandomBloke | 0:7efa6655d94b | 309 | |
SomeRandomBloke | 0:7efa6655d94b | 310 | value = lcd_buffer[row][x]; |
SomeRandomBloke | 0:7efa6655d94b | 311 | if( c == PIXEL_ON ) { |
SomeRandomBloke | 0:7efa6655d94b | 312 | value |= (1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 313 | } else if( c == PIXEL_XOR ) { |
SomeRandomBloke | 0:7efa6655d94b | 314 | value ^= (1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 315 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 316 | value &= ~(1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 317 | } |
SomeRandomBloke | 0:7efa6655d94b | 318 | |
SomeRandomBloke | 0:7efa6655d94b | 319 | lcd_buffer[row][x] = value; |
SomeRandomBloke | 0:7efa6655d94b | 320 | locate (x,row); |
SomeRandomBloke | 3:9808f63fd2fe | 321 | writeData(value); |
SomeRandomBloke | 0:7efa6655d94b | 322 | } |
SomeRandomBloke | 0:7efa6655d94b | 323 | |
SomeRandomBloke | 0:7efa6655d94b | 324 | |
SomeRandomBloke | 0:7efa6655d94b | 325 | /* |
SomeRandomBloke | 0:7efa6655d94b | 326 | * Name : drawLine |
SomeRandomBloke | 0:7efa6655d94b | 327 | * Description : Draws a line between two points on the display. |
SomeRandomBloke | 0:7efa6655d94b | 328 | * Argument(s) : x1, y1 - Absolute pixel coordinates for line origin. |
SomeRandomBloke | 0:7efa6655d94b | 329 | * x2, y2 - Absolute pixel coordinates for line end. |
SomeRandomBloke | 0:7efa6655d94b | 330 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 331 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 332 | */ |
SomeRandomBloke | 0:7efa6655d94b | 333 | void N3310LCD::drawLine(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 334 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 335 | { |
SomeRandomBloke | 0:7efa6655d94b | 336 | int dx, dy, stepx, stepy, fraction; |
SomeRandomBloke | 0:7efa6655d94b | 337 | |
SomeRandomBloke | 0:7efa6655d94b | 338 | /* Calculate differential form */ |
SomeRandomBloke | 0:7efa6655d94b | 339 | /* dy y2 - y1 */ |
SomeRandomBloke | 0:7efa6655d94b | 340 | /* -- = ------- */ |
SomeRandomBloke | 0:7efa6655d94b | 341 | /* dx x2 - x1 */ |
SomeRandomBloke | 0:7efa6655d94b | 342 | |
SomeRandomBloke | 0:7efa6655d94b | 343 | /* Take differences */ |
SomeRandomBloke | 0:7efa6655d94b | 344 | dy = y2 - y1; |
SomeRandomBloke | 0:7efa6655d94b | 345 | dx = x2 - x1; |
SomeRandomBloke | 0:7efa6655d94b | 346 | |
SomeRandomBloke | 0:7efa6655d94b | 347 | /* dy is negative */ |
SomeRandomBloke | 0:7efa6655d94b | 348 | if ( dy < 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 349 | dy = -dy; |
SomeRandomBloke | 0:7efa6655d94b | 350 | stepy = -1; |
SomeRandomBloke | 0:7efa6655d94b | 351 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 352 | stepy = 1; |
SomeRandomBloke | 0:7efa6655d94b | 353 | } |
SomeRandomBloke | 0:7efa6655d94b | 354 | |
SomeRandomBloke | 0:7efa6655d94b | 355 | /* dx is negative */ |
SomeRandomBloke | 0:7efa6655d94b | 356 | if ( dx < 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 357 | dx = -dx; |
SomeRandomBloke | 0:7efa6655d94b | 358 | stepx = -1; |
SomeRandomBloke | 0:7efa6655d94b | 359 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 360 | stepx = 1; |
SomeRandomBloke | 0:7efa6655d94b | 361 | } |
SomeRandomBloke | 0:7efa6655d94b | 362 | |
SomeRandomBloke | 0:7efa6655d94b | 363 | dx <<= 1; |
SomeRandomBloke | 0:7efa6655d94b | 364 | dy <<= 1; |
SomeRandomBloke | 0:7efa6655d94b | 365 | |
SomeRandomBloke | 0:7efa6655d94b | 366 | /* Draw initial position */ |
SomeRandomBloke | 0:7efa6655d94b | 367 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 368 | |
SomeRandomBloke | 0:7efa6655d94b | 369 | /* Draw next positions until end */ |
SomeRandomBloke | 0:7efa6655d94b | 370 | if ( dx > dy ) { |
SomeRandomBloke | 0:7efa6655d94b | 371 | /* Take fraction */ |
SomeRandomBloke | 0:7efa6655d94b | 372 | fraction = dy - ( dx >> 1); |
SomeRandomBloke | 0:7efa6655d94b | 373 | while ( x1 != x2 ) { |
SomeRandomBloke | 0:7efa6655d94b | 374 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 375 | y1 += stepy; |
SomeRandomBloke | 0:7efa6655d94b | 376 | fraction -= dx; |
SomeRandomBloke | 0:7efa6655d94b | 377 | } |
SomeRandomBloke | 0:7efa6655d94b | 378 | x1 += stepx; |
SomeRandomBloke | 0:7efa6655d94b | 379 | fraction += dy; |
SomeRandomBloke | 0:7efa6655d94b | 380 | |
SomeRandomBloke | 0:7efa6655d94b | 381 | /* Draw calculated point */ |
SomeRandomBloke | 0:7efa6655d94b | 382 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 383 | } |
SomeRandomBloke | 0:7efa6655d94b | 384 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 385 | /* Take fraction */ |
SomeRandomBloke | 0:7efa6655d94b | 386 | fraction = dx - ( dy >> 1); |
SomeRandomBloke | 0:7efa6655d94b | 387 | while ( y1 != y2 ) { |
SomeRandomBloke | 0:7efa6655d94b | 388 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 389 | x1 += stepx; |
SomeRandomBloke | 0:7efa6655d94b | 390 | fraction -= dy; |
SomeRandomBloke | 0:7efa6655d94b | 391 | } |
SomeRandomBloke | 0:7efa6655d94b | 392 | y1 += stepy; |
SomeRandomBloke | 0:7efa6655d94b | 393 | fraction += dx; |
SomeRandomBloke | 0:7efa6655d94b | 394 | |
SomeRandomBloke | 0:7efa6655d94b | 395 | /* Draw calculated point */ |
SomeRandomBloke | 0:7efa6655d94b | 396 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 397 | } |
SomeRandomBloke | 0:7efa6655d94b | 398 | } |
SomeRandomBloke | 0:7efa6655d94b | 399 | } |
SomeRandomBloke | 0:7efa6655d94b | 400 | |
SomeRandomBloke | 0:7efa6655d94b | 401 | |
SomeRandomBloke | 0:7efa6655d94b | 402 | /* |
SomeRandomBloke | 0:7efa6655d94b | 403 | * Name : drawRectangle |
SomeRandomBloke | 0:7efa6655d94b | 404 | * Description : Draw a rectangle given to top left and bottom right points |
SomeRandomBloke | 0:7efa6655d94b | 405 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:7efa6655d94b | 406 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:7efa6655d94b | 407 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 408 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 409 | */ |
SomeRandomBloke | 0:7efa6655d94b | 410 | void N3310LCD::drawRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 411 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 412 | { |
SomeRandomBloke | 0:7efa6655d94b | 413 | drawLine( x1, y1, x2, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 414 | drawLine( x1, y1, x1, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 415 | drawLine( x1, y2, x2, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 416 | drawLine( x2, y1, x2, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 417 | } |
SomeRandomBloke | 0:7efa6655d94b | 418 | |
SomeRandomBloke | 0:7efa6655d94b | 419 | |
SomeRandomBloke | 0:7efa6655d94b | 420 | /* |
SomeRandomBloke | 0:7efa6655d94b | 421 | * Name : drawFilledRectangle |
SomeRandomBloke | 0:7efa6655d94b | 422 | * Description : Draw a filled rectangle given to top left and bottom right points |
SomeRandomBloke | 0:7efa6655d94b | 423 | * just simply draws horizontal lines where the rectangle would be |
SomeRandomBloke | 0:7efa6655d94b | 424 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:7efa6655d94b | 425 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:7efa6655d94b | 426 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 427 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 428 | */ |
SomeRandomBloke | 0:7efa6655d94b | 429 | void N3310LCD::drawFilledRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 430 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 431 | { |
SomeRandomBloke | 0:7efa6655d94b | 432 | for(int i=y1; i <= y2; i++ ) { |
SomeRandomBloke | 0:7efa6655d94b | 433 | drawLine( x1, i, x2, i, c ); |
SomeRandomBloke | 0:7efa6655d94b | 434 | } |
SomeRandomBloke | 0:7efa6655d94b | 435 | } |
SomeRandomBloke | 0:7efa6655d94b | 436 | |
SomeRandomBloke | 0:7efa6655d94b | 437 | |
SomeRandomBloke | 0:7efa6655d94b | 438 | /* |
SomeRandomBloke | 0:7efa6655d94b | 439 | * Name : drawCircle |
SomeRandomBloke | 1:51961974fe55 | 440 | * Description : Draw a circle using Bresenham's algorithm. |
SomeRandomBloke | 0:7efa6655d94b | 441 | * Some small circles will look like squares!! |
SomeRandomBloke | 0:7efa6655d94b | 442 | * Argument(s) : xc, yc - Centre of circle |
SomeRandomBloke | 0:7efa6655d94b | 443 | * r - Radius |
SomeRandomBloke | 0:7efa6655d94b | 444 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 1:51961974fe55 | 445 | * Return value : None |
SomeRandomBloke | 0:7efa6655d94b | 446 | */ |
SomeRandomBloke | 0:7efa6655d94b | 447 | void N3310LCD::drawCircle(unsigned char xc, unsigned char yc, |
SomeRandomBloke | 1:51961974fe55 | 448 | unsigned char r, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 449 | { |
SomeRandomBloke | 0:7efa6655d94b | 450 | int x=0; |
SomeRandomBloke | 0:7efa6655d94b | 451 | int y=r; |
SomeRandomBloke | 0:7efa6655d94b | 452 | int p=3-(2*r); |
SomeRandomBloke | 0:7efa6655d94b | 453 | |
SomeRandomBloke | 1:51961974fe55 | 454 | setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 455 | |
SomeRandomBloke | 1:51961974fe55 | 456 | for(x=0; x<=y; x++) { |
SomeRandomBloke | 0:7efa6655d94b | 457 | if (p<0) { |
SomeRandomBloke | 0:7efa6655d94b | 458 | y=y; |
SomeRandomBloke | 0:7efa6655d94b | 459 | p=(p+(4*x)+6); |
SomeRandomBloke | 0:7efa6655d94b | 460 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 461 | y=y-1; |
SomeRandomBloke | 0:7efa6655d94b | 462 | p=p+((4*(x-y)+10)); |
SomeRandomBloke | 0:7efa6655d94b | 463 | } |
SomeRandomBloke | 0:7efa6655d94b | 464 | |
SomeRandomBloke | 0:7efa6655d94b | 465 | setPixel((uint8_t)(xc+x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 466 | setPixel((uint8_t)(xc-x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 467 | setPixel((uint8_t)(xc+x),(uint8_t)(yc+y), c); |
SomeRandomBloke | 0:7efa6655d94b | 468 | setPixel((uint8_t)(xc-x),(uint8_t)(yc+y), c); |
SomeRandomBloke | 0:7efa6655d94b | 469 | setPixel((uint8_t)(xc+y),(uint8_t)(yc-x), c); |
SomeRandomBloke | 0:7efa6655d94b | 470 | setPixel((uint8_t)(xc-y),(uint8_t)(yc-x), c); |
SomeRandomBloke | 0:7efa6655d94b | 471 | setPixel((uint8_t)(xc+y),(uint8_t)(yc+x), c); |
SomeRandomBloke | 0:7efa6655d94b | 472 | setPixel((uint8_t)(xc-y),(uint8_t)(yc+x), c); |
SomeRandomBloke | 0:7efa6655d94b | 473 | } |
SomeRandomBloke | 0:7efa6655d94b | 474 | } |