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