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