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@4:90dce6032a37, 2013-03-24 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Sun Mar 24 19:21:51 2013 +0000
- Revision:
- 4:90dce6032a37
- Parent:
- 3:9808f63fd2fe
- Child:
- 5:1fd7af32e521
updates;
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 | 3:9808f63fd2fe | 238 | |
SomeRandomBloke | 3:9808f63fd2fe | 239 | |
SomeRandomBloke | 0:7efa6655d94b | 240 | } |
SomeRandomBloke | 0:7efa6655d94b | 241 | |
SomeRandomBloke | 0:7efa6655d94b | 242 | void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode) |
SomeRandomBloke | 0:7efa6655d94b | 243 | { |
SomeRandomBloke | 0:7efa6655d94b | 244 | BYTE sendByte; |
SomeRandomBloke | 3:9808f63fd2fe | 245 | int8_t colsUsed = 12; |
SomeRandomBloke | 0:7efa6655d94b | 246 | unsigned char* pFont = (unsigned char *) big_number; |
SomeRandomBloke | 1:51961974fe55 | 247 | |
SomeRandomBloke | 3:9808f63fd2fe | 248 | |
SomeRandomBloke | 3:9808f63fd2fe | 249 | if(ch == '.') { |
SomeRandomBloke | 0:7efa6655d94b | 250 | ch = 10; |
SomeRandomBloke | 3:9808f63fd2fe | 251 | colsUsed=5; |
SomeRandomBloke | 3:9808f63fd2fe | 252 | } else if (ch == '+') |
SomeRandomBloke | 0:7efa6655d94b | 253 | ch = 11; |
SomeRandomBloke | 3:9808f63fd2fe | 254 | else if (ch == '-') |
SomeRandomBloke | 0:7efa6655d94b | 255 | ch = 12; |
SomeRandomBloke | 3:9808f63fd2fe | 256 | else if (ch == ':') |
SomeRandomBloke | 3:9808f63fd2fe | 257 | ch = 13; |
SomeRandomBloke | 3:9808f63fd2fe | 258 | else if (ch == '/') |
SomeRandomBloke | 3:9808f63fd2fe | 259 | ch = 14; |
SomeRandomBloke | 0:7efa6655d94b | 260 | else |
SomeRandomBloke | 0:7efa6655d94b | 261 | ch = ch & 0x0f; |
SomeRandomBloke | 1:51961974fe55 | 262 | |
SomeRandomBloke | 3:9808f63fd2fe | 263 | /* |
SomeRandomBloke | 3:9808f63fd2fe | 264 | for(BYTE i = 0; i < 3; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 265 | locate(xPos, yPos + i); |
SomeRandomBloke | 1:51961974fe55 | 266 | |
SomeRandomBloke | 3:9808f63fd2fe | 267 | for(BYTE j = 0; j < 16; j++) { |
SomeRandomBloke | 3:9808f63fd2fe | 268 | sendByte = *(pFont + ch*48 + i*16 + j); |
SomeRandomBloke | 3:9808f63fd2fe | 269 | writeData((mode == NORMAL)? sendByte : (sendByte^0xff)); |
SomeRandomBloke | 3:9808f63fd2fe | 270 | } |
SomeRandomBloke | 3:9808f63fd2fe | 271 | } |
SomeRandomBloke | 3:9808f63fd2fe | 272 | */ |
SomeRandomBloke | 3:9808f63fd2fe | 273 | if (xPos > LCDCOLMAX - colsUsed) xPos = LCDCOLMAX - colsUsed ; // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 274 | if (yPos > LCDROWMAX - 3) yPos = LCDROWMAX - 3 ; // ensure space is available for the character |
SomeRandomBloke | 3:9808f63fd2fe | 275 | |
SomeRandomBloke | 3:9808f63fd2fe | 276 | for(int8_t i=0; i<3; i++) { |
SomeRandomBloke | 3:9808f63fd2fe | 277 | locate( xPos, yPos + i); |
SomeRandomBloke | 3:9808f63fd2fe | 278 | |
SomeRandomBloke | 3:9808f63fd2fe | 279 | for(int8_t j=0; j<colsUsed; j++) { |
SomeRandomBloke | 0:7efa6655d94b | 280 | sendByte = *(pFont + ch*48 + i*16 + j); |
SomeRandomBloke | 3:9808f63fd2fe | 281 | lcd_buffer[cursor_row][cursor_col + j] = (mode == NORMAL)? sendByte : (sendByte^0xff); |
SomeRandomBloke | 4:90dce6032a37 | 282 | writeData( lcd_buffer[cursor_row][cursor_col + j] ); |
SomeRandomBloke | 0:7efa6655d94b | 283 | } |
SomeRandomBloke | 0:7efa6655d94b | 284 | } |
SomeRandomBloke | 0:7efa6655d94b | 285 | } |
SomeRandomBloke | 0:7efa6655d94b | 286 | |
SomeRandomBloke | 0:7efa6655d94b | 287 | /* |
SomeRandomBloke | 0:7efa6655d94b | 288 | * Name : setPixel |
SomeRandomBloke | 0:7efa6655d94b | 289 | * Description : Set a single pixel either on or off, update display buffer. |
SomeRandomBloke | 0:7efa6655d94b | 290 | * Argument(s) : x,y - position, x = 0-83, y = 0-6 |
SomeRandomBloke | 0:7efa6655d94b | 291 | * c - colour, either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 292 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 293 | */ |
SomeRandomBloke | 1:51961974fe55 | 294 | void N3310LCD::setPixel( unsigned char x, unsigned char y, unsigned char c ) |
SomeRandomBloke | 1:51961974fe55 | 295 | { |
SomeRandomBloke | 1:51961974fe55 | 296 | unsigned char value; |
SomeRandomBloke | 1:51961974fe55 | 297 | unsigned char row; |
SomeRandomBloke | 1:51961974fe55 | 298 | |
SomeRandomBloke | 0:7efa6655d94b | 299 | // if( x < 0 || x >= LCDCOLMAX || y < 0 || y >= LCDPIXELROWMAX ) return; |
SomeRandomBloke | 0:7efa6655d94b | 300 | if( x >= LCDCOLMAX || y >= LCDPIXELROWMAX ) return; |
SomeRandomBloke | 0:7efa6655d94b | 301 | |
SomeRandomBloke | 0:7efa6655d94b | 302 | row = y / 8; |
SomeRandomBloke | 0:7efa6655d94b | 303 | |
SomeRandomBloke | 0:7efa6655d94b | 304 | value = lcd_buffer[row][x]; |
SomeRandomBloke | 0:7efa6655d94b | 305 | if( c == PIXEL_ON ) { |
SomeRandomBloke | 0:7efa6655d94b | 306 | value |= (1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 307 | } else if( c == PIXEL_XOR ) { |
SomeRandomBloke | 0:7efa6655d94b | 308 | value ^= (1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 309 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 310 | value &= ~(1 << (y % 8)); |
SomeRandomBloke | 0:7efa6655d94b | 311 | } |
SomeRandomBloke | 0:7efa6655d94b | 312 | |
SomeRandomBloke | 0:7efa6655d94b | 313 | lcd_buffer[row][x] = value; |
SomeRandomBloke | 0:7efa6655d94b | 314 | locate (x,row); |
SomeRandomBloke | 3:9808f63fd2fe | 315 | writeData(value); |
SomeRandomBloke | 0:7efa6655d94b | 316 | } |
SomeRandomBloke | 0:7efa6655d94b | 317 | |
SomeRandomBloke | 0:7efa6655d94b | 318 | |
SomeRandomBloke | 0:7efa6655d94b | 319 | /* |
SomeRandomBloke | 0:7efa6655d94b | 320 | * Name : drawLine |
SomeRandomBloke | 0:7efa6655d94b | 321 | * Description : Draws a line between two points on the display. |
SomeRandomBloke | 0:7efa6655d94b | 322 | * Argument(s) : x1, y1 - Absolute pixel coordinates for line origin. |
SomeRandomBloke | 0:7efa6655d94b | 323 | * x2, y2 - Absolute pixel coordinates for line end. |
SomeRandomBloke | 0:7efa6655d94b | 324 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 325 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 326 | */ |
SomeRandomBloke | 0:7efa6655d94b | 327 | void N3310LCD::drawLine(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 328 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 329 | { |
SomeRandomBloke | 0:7efa6655d94b | 330 | int dx, dy, stepx, stepy, fraction; |
SomeRandomBloke | 0:7efa6655d94b | 331 | |
SomeRandomBloke | 0:7efa6655d94b | 332 | /* Calculate differential form */ |
SomeRandomBloke | 0:7efa6655d94b | 333 | /* dy y2 - y1 */ |
SomeRandomBloke | 0:7efa6655d94b | 334 | /* -- = ------- */ |
SomeRandomBloke | 0:7efa6655d94b | 335 | /* dx x2 - x1 */ |
SomeRandomBloke | 0:7efa6655d94b | 336 | |
SomeRandomBloke | 0:7efa6655d94b | 337 | /* Take differences */ |
SomeRandomBloke | 0:7efa6655d94b | 338 | dy = y2 - y1; |
SomeRandomBloke | 0:7efa6655d94b | 339 | dx = x2 - x1; |
SomeRandomBloke | 0:7efa6655d94b | 340 | |
SomeRandomBloke | 0:7efa6655d94b | 341 | /* dy is negative */ |
SomeRandomBloke | 0:7efa6655d94b | 342 | if ( dy < 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 343 | dy = -dy; |
SomeRandomBloke | 0:7efa6655d94b | 344 | stepy = -1; |
SomeRandomBloke | 0:7efa6655d94b | 345 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 346 | stepy = 1; |
SomeRandomBloke | 0:7efa6655d94b | 347 | } |
SomeRandomBloke | 0:7efa6655d94b | 348 | |
SomeRandomBloke | 0:7efa6655d94b | 349 | /* dx is negative */ |
SomeRandomBloke | 0:7efa6655d94b | 350 | if ( dx < 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 351 | dx = -dx; |
SomeRandomBloke | 0:7efa6655d94b | 352 | stepx = -1; |
SomeRandomBloke | 0:7efa6655d94b | 353 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 354 | stepx = 1; |
SomeRandomBloke | 0:7efa6655d94b | 355 | } |
SomeRandomBloke | 0:7efa6655d94b | 356 | |
SomeRandomBloke | 0:7efa6655d94b | 357 | dx <<= 1; |
SomeRandomBloke | 0:7efa6655d94b | 358 | dy <<= 1; |
SomeRandomBloke | 0:7efa6655d94b | 359 | |
SomeRandomBloke | 0:7efa6655d94b | 360 | /* Draw initial position */ |
SomeRandomBloke | 0:7efa6655d94b | 361 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 362 | |
SomeRandomBloke | 0:7efa6655d94b | 363 | /* Draw next positions until end */ |
SomeRandomBloke | 0:7efa6655d94b | 364 | if ( dx > dy ) { |
SomeRandomBloke | 0:7efa6655d94b | 365 | /* Take fraction */ |
SomeRandomBloke | 0:7efa6655d94b | 366 | fraction = dy - ( dx >> 1); |
SomeRandomBloke | 0:7efa6655d94b | 367 | while ( x1 != x2 ) { |
SomeRandomBloke | 0:7efa6655d94b | 368 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 369 | y1 += stepy; |
SomeRandomBloke | 0:7efa6655d94b | 370 | fraction -= dx; |
SomeRandomBloke | 0:7efa6655d94b | 371 | } |
SomeRandomBloke | 0:7efa6655d94b | 372 | x1 += stepx; |
SomeRandomBloke | 0:7efa6655d94b | 373 | fraction += dy; |
SomeRandomBloke | 0:7efa6655d94b | 374 | |
SomeRandomBloke | 0:7efa6655d94b | 375 | /* Draw calculated point */ |
SomeRandomBloke | 0:7efa6655d94b | 376 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 377 | } |
SomeRandomBloke | 0:7efa6655d94b | 378 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 379 | /* Take fraction */ |
SomeRandomBloke | 0:7efa6655d94b | 380 | fraction = dx - ( dy >> 1); |
SomeRandomBloke | 0:7efa6655d94b | 381 | while ( y1 != y2 ) { |
SomeRandomBloke | 0:7efa6655d94b | 382 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:7efa6655d94b | 383 | x1 += stepx; |
SomeRandomBloke | 0:7efa6655d94b | 384 | fraction -= dy; |
SomeRandomBloke | 0:7efa6655d94b | 385 | } |
SomeRandomBloke | 0:7efa6655d94b | 386 | y1 += stepy; |
SomeRandomBloke | 0:7efa6655d94b | 387 | fraction += dx; |
SomeRandomBloke | 0:7efa6655d94b | 388 | |
SomeRandomBloke | 0:7efa6655d94b | 389 | /* Draw calculated point */ |
SomeRandomBloke | 0:7efa6655d94b | 390 | setPixel( x1, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 391 | } |
SomeRandomBloke | 0:7efa6655d94b | 392 | } |
SomeRandomBloke | 0:7efa6655d94b | 393 | } |
SomeRandomBloke | 0:7efa6655d94b | 394 | |
SomeRandomBloke | 0:7efa6655d94b | 395 | |
SomeRandomBloke | 0:7efa6655d94b | 396 | /* |
SomeRandomBloke | 0:7efa6655d94b | 397 | * Name : drawRectangle |
SomeRandomBloke | 0:7efa6655d94b | 398 | * Description : Draw a rectangle given to top left and bottom right points |
SomeRandomBloke | 0:7efa6655d94b | 399 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:7efa6655d94b | 400 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:7efa6655d94b | 401 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 402 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 403 | */ |
SomeRandomBloke | 0:7efa6655d94b | 404 | void N3310LCD::drawRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 405 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 406 | { |
SomeRandomBloke | 0:7efa6655d94b | 407 | drawLine( x1, y1, x2, y1, c ); |
SomeRandomBloke | 0:7efa6655d94b | 408 | drawLine( x1, y1, x1, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 409 | drawLine( x1, y2, x2, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 410 | drawLine( x2, y1, x2, y2, c ); |
SomeRandomBloke | 0:7efa6655d94b | 411 | } |
SomeRandomBloke | 0:7efa6655d94b | 412 | |
SomeRandomBloke | 0:7efa6655d94b | 413 | |
SomeRandomBloke | 0:7efa6655d94b | 414 | /* |
SomeRandomBloke | 0:7efa6655d94b | 415 | * Name : drawFilledRectangle |
SomeRandomBloke | 0:7efa6655d94b | 416 | * Description : Draw a filled rectangle given to top left and bottom right points |
SomeRandomBloke | 0:7efa6655d94b | 417 | * just simply draws horizontal lines where the rectangle would be |
SomeRandomBloke | 0:7efa6655d94b | 418 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:7efa6655d94b | 419 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:7efa6655d94b | 420 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 0:7efa6655d94b | 421 | * Return value : none |
SomeRandomBloke | 0:7efa6655d94b | 422 | */ |
SomeRandomBloke | 0:7efa6655d94b | 423 | void N3310LCD::drawFilledRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 1:51961974fe55 | 424 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 425 | { |
SomeRandomBloke | 0:7efa6655d94b | 426 | for(int i=y1; i <= y2; i++ ) { |
SomeRandomBloke | 0:7efa6655d94b | 427 | drawLine( x1, i, x2, i, c ); |
SomeRandomBloke | 0:7efa6655d94b | 428 | } |
SomeRandomBloke | 0:7efa6655d94b | 429 | } |
SomeRandomBloke | 0:7efa6655d94b | 430 | |
SomeRandomBloke | 0:7efa6655d94b | 431 | |
SomeRandomBloke | 0:7efa6655d94b | 432 | /* |
SomeRandomBloke | 0:7efa6655d94b | 433 | * Name : drawCircle |
SomeRandomBloke | 1:51961974fe55 | 434 | * Description : Draw a circle using Bresenham's algorithm. |
SomeRandomBloke | 0:7efa6655d94b | 435 | * Some small circles will look like squares!! |
SomeRandomBloke | 0:7efa6655d94b | 436 | * Argument(s) : xc, yc - Centre of circle |
SomeRandomBloke | 0:7efa6655d94b | 437 | * r - Radius |
SomeRandomBloke | 0:7efa6655d94b | 438 | * c - either PIXEL_ON, PIXEL_OFF or PIXEL_XOR |
SomeRandomBloke | 1:51961974fe55 | 439 | * Return value : None |
SomeRandomBloke | 0:7efa6655d94b | 440 | */ |
SomeRandomBloke | 0:7efa6655d94b | 441 | void N3310LCD::drawCircle(unsigned char xc, unsigned char yc, |
SomeRandomBloke | 1:51961974fe55 | 442 | unsigned char r, unsigned char c) |
SomeRandomBloke | 1:51961974fe55 | 443 | { |
SomeRandomBloke | 0:7efa6655d94b | 444 | int x=0; |
SomeRandomBloke | 0:7efa6655d94b | 445 | int y=r; |
SomeRandomBloke | 0:7efa6655d94b | 446 | int p=3-(2*r); |
SomeRandomBloke | 0:7efa6655d94b | 447 | |
SomeRandomBloke | 1:51961974fe55 | 448 | setPixel( (uint8_t)(xc+x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 449 | |
SomeRandomBloke | 1:51961974fe55 | 450 | for(x=0; x<=y; x++) { |
SomeRandomBloke | 0:7efa6655d94b | 451 | if (p<0) { |
SomeRandomBloke | 0:7efa6655d94b | 452 | y=y; |
SomeRandomBloke | 0:7efa6655d94b | 453 | p=(p+(4*x)+6); |
SomeRandomBloke | 0:7efa6655d94b | 454 | } else { |
SomeRandomBloke | 0:7efa6655d94b | 455 | y=y-1; |
SomeRandomBloke | 0:7efa6655d94b | 456 | p=p+((4*(x-y)+10)); |
SomeRandomBloke | 0:7efa6655d94b | 457 | } |
SomeRandomBloke | 0:7efa6655d94b | 458 | |
SomeRandomBloke | 0:7efa6655d94b | 459 | setPixel((uint8_t)(xc+x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 460 | setPixel((uint8_t)(xc-x),(uint8_t)(yc-y), c); |
SomeRandomBloke | 0:7efa6655d94b | 461 | setPixel((uint8_t)(xc+x),(uint8_t)(yc+y), c); |
SomeRandomBloke | 0:7efa6655d94b | 462 | setPixel((uint8_t)(xc-x),(uint8_t)(yc+y), c); |
SomeRandomBloke | 0:7efa6655d94b | 463 | setPixel((uint8_t)(xc+y),(uint8_t)(yc-x), c); |
SomeRandomBloke | 0:7efa6655d94b | 464 | setPixel((uint8_t)(xc-y),(uint8_t)(yc-x), c); |
SomeRandomBloke | 0:7efa6655d94b | 465 | setPixel((uint8_t)(xc+y),(uint8_t)(yc+x), c); |
SomeRandomBloke | 0:7efa6655d94b | 466 | setPixel((uint8_t)(xc-y),(uint8_t)(yc+x), c); |
SomeRandomBloke | 0:7efa6655d94b | 467 | } |
SomeRandomBloke | 0:7efa6655d94b | 468 | } |