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