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