Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Dependents:   LEDFun NetTester

Fork of N3310LCD by Andrew Lindsay

Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Committer:
SomeRandomBloke
Date:
Fri Jul 19 13:42:00 2013 +0000
Revision:
6:46bcc4e584c4
Parent:
5:1fd7af32e521
Child:
8:0380b34047ad
code comments

Who changed what in which revision?

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