Example program printing text to the LCD on the mbed application board

Dependencies:   C12832 mbed

Fork of lab1 by Peter Drescher

Committer:
dreschpe
Date:
Mon Oct 15 21:48:28 2012 +0000
Revision:
0:f6a57b843f79
first test of the lcd driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:f6a57b843f79 1 /* mbed library for the mbed Lab Board 128*32 pixel LCD
dreschpe 0:f6a57b843f79 2 * use C12832 controller
dreschpe 0:f6a57b843f79 3 * Copyright (c) 2012 Peter Drescher - DC2PD
dreschpe 0:f6a57b843f79 4 * Released under the MIT License: http://mbed.org/license/mit
dreschpe 0:f6a57b843f79 5 *
dreschpe 0:f6a57b843f79 6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 0:f6a57b843f79 7 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 0:f6a57b843f79 8 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 0:f6a57b843f79 9 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 0:f6a57b843f79 10 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 0:f6a57b843f79 11 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 0:f6a57b843f79 12 * THE SOFTWARE.
dreschpe 0:f6a57b843f79 13 */
dreschpe 0:f6a57b843f79 14
dreschpe 0:f6a57b843f79 15 // 13.10.12 initial design
dreschpe 0:f6a57b843f79 16 // optional defines :
dreschpe 0:f6a57b843f79 17 #define debug_lcd 1
dreschpe 0:f6a57b843f79 18 #define use_rtos 1
dreschpe 0:f6a57b843f79 19
dreschpe 0:f6a57b843f79 20 #include "C12832_lcd.h"
dreschpe 0:f6a57b843f79 21 #include "mbed.h"
dreschpe 0:f6a57b843f79 22 #include "stdio.h"
dreschpe 0:f6a57b843f79 23
dreschpe 0:f6a57b843f79 24 #define BPP 1 // Bits per pixel
dreschpe 0:f6a57b843f79 25
dreschpe 0:f6a57b843f79 26 #ifdef debug_lcd
dreschpe 0:f6a57b843f79 27 extern Serial pc;
dreschpe 0:f6a57b843f79 28 #endif
dreschpe 0:f6a57b843f79 29
dreschpe 0:f6a57b843f79 30 #ifdef use_rtos
dreschpe 0:f6a57b843f79 31 #include "rtos.h"
dreschpe 0:f6a57b843f79 32 #endif
dreschpe 0:f6a57b843f79 33
dreschpe 0:f6a57b843f79 34
dreschpe 0:f6a57b843f79 35
dreschpe 0:f6a57b843f79 36 C12832_LCD::C12832_LCD(const char* name)
dreschpe 0:f6a57b843f79 37 : _spi(p5,NC,p7),_reset(p6),_A0(p8),_CS(p11),GraphicsDisplay(name)
dreschpe 0:f6a57b843f79 38 {
dreschpe 0:f6a57b843f79 39 orientation = 1;
dreschpe 0:f6a57b843f79 40 draw_mode = NORMAL;
dreschpe 0:f6a57b843f79 41 char_x = 0;
dreschpe 0:f6a57b843f79 42 lcd_reset();
dreschpe 0:f6a57b843f79 43 }
dreschpe 0:f6a57b843f79 44
dreschpe 0:f6a57b843f79 45 int C12832_LCD::width()
dreschpe 0:f6a57b843f79 46 {
dreschpe 0:f6a57b843f79 47 if (orientation == 0 || orientation == 2) return 32;
dreschpe 0:f6a57b843f79 48 else return 128;
dreschpe 0:f6a57b843f79 49 }
dreschpe 0:f6a57b843f79 50
dreschpe 0:f6a57b843f79 51 int C12832_LCD::height()
dreschpe 0:f6a57b843f79 52 {
dreschpe 0:f6a57b843f79 53 if (orientation == 0 || orientation == 2) return 128;
dreschpe 0:f6a57b843f79 54 else return 32;
dreschpe 0:f6a57b843f79 55 }
dreschpe 0:f6a57b843f79 56
dreschpe 0:f6a57b843f79 57
dreschpe 0:f6a57b843f79 58 void C12832_LCD::set_orientation(unsigned int o)
dreschpe 0:f6a57b843f79 59 {
dreschpe 0:f6a57b843f79 60 orientation = o;
dreschpe 0:f6a57b843f79 61 switch (o) {
dreschpe 0:f6a57b843f79 62 case (0):
dreschpe 0:f6a57b843f79 63 wr_cmd(0xA0);
dreschpe 0:f6a57b843f79 64 wr_cmd(0xC0);
dreschpe 0:f6a57b843f79 65 break;
dreschpe 0:f6a57b843f79 66 case (1):
dreschpe 0:f6a57b843f79 67 wr_cmd(0xA0);
dreschpe 0:f6a57b843f79 68 wr_cmd(0xC8);
dreschpe 0:f6a57b843f79 69 break;
dreschpe 0:f6a57b843f79 70 case (2):
dreschpe 0:f6a57b843f79 71 wr_cmd(0xA1);
dreschpe 0:f6a57b843f79 72 wr_cmd(0xC8);
dreschpe 0:f6a57b843f79 73 break;
dreschpe 0:f6a57b843f79 74 case (3):
dreschpe 0:f6a57b843f79 75 wr_cmd(0xA1);
dreschpe 0:f6a57b843f79 76 wr_cmd(0xC0);
dreschpe 0:f6a57b843f79 77 break;
dreschpe 0:f6a57b843f79 78 }
dreschpe 0:f6a57b843f79 79 }
dreschpe 0:f6a57b843f79 80
dreschpe 0:f6a57b843f79 81 void C12832_LCD::invert(unsigned int o)
dreschpe 0:f6a57b843f79 82 {
dreschpe 0:f6a57b843f79 83 if(o == 0) wr_cmd(0xA6);
dreschpe 0:f6a57b843f79 84 else wr_cmd(0xA7);
dreschpe 0:f6a57b843f79 85 }
dreschpe 0:f6a57b843f79 86
dreschpe 0:f6a57b843f79 87
dreschpe 0:f6a57b843f79 88 void C12832_LCD::set_contrast(unsigned int o)
dreschpe 0:f6a57b843f79 89 {
dreschpe 0:f6a57b843f79 90 wr_cmd(0x81); // set volume
dreschpe 0:f6a57b843f79 91 wr_cmd(o & 0x2F);
dreschpe 0:f6a57b843f79 92 }
dreschpe 0:f6a57b843f79 93
dreschpe 0:f6a57b843f79 94
dreschpe 0:f6a57b843f79 95 // write command to lcd controller
dreschpe 0:f6a57b843f79 96
dreschpe 0:f6a57b843f79 97 void C12832_LCD::wr_cmd(unsigned char cmd)
dreschpe 0:f6a57b843f79 98 {
dreschpe 0:f6a57b843f79 99 _A0 = 0;
dreschpe 0:f6a57b843f79 100 _CS = 0;
dreschpe 0:f6a57b843f79 101 LPC_SSP1->DR = cmd;
dreschpe 0:f6a57b843f79 102 do {
dreschpe 0:f6a57b843f79 103 } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
dreschpe 0:f6a57b843f79 104 _CS = 1;
dreschpe 0:f6a57b843f79 105 }
dreschpe 0:f6a57b843f79 106
dreschpe 0:f6a57b843f79 107 // write data to lcd controller
dreschpe 0:f6a57b843f79 108
dreschpe 0:f6a57b843f79 109 void C12832_LCD::wr_dat(unsigned char dat)
dreschpe 0:f6a57b843f79 110 {
dreschpe 0:f6a57b843f79 111 _A0 = 1;
dreschpe 0:f6a57b843f79 112 _CS = 0;
dreschpe 0:f6a57b843f79 113 LPC_SSP1->DR = dat;
dreschpe 0:f6a57b843f79 114 do {
dreschpe 0:f6a57b843f79 115 } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
dreschpe 0:f6a57b843f79 116 _CS = 1;
dreschpe 0:f6a57b843f79 117 }
dreschpe 0:f6a57b843f79 118
dreschpe 0:f6a57b843f79 119 // reset and init the lcd controller
dreschpe 0:f6a57b843f79 120
dreschpe 0:f6a57b843f79 121 void C12832_LCD::lcd_reset()
dreschpe 0:f6a57b843f79 122 {
dreschpe 0:f6a57b843f79 123
dreschpe 0:f6a57b843f79 124 #ifdef debug_lcd
dreschpe 0:f6a57b843f79 125 pc.printf("init LCD \n\r");
dreschpe 0:f6a57b843f79 126 #endif
dreschpe 0:f6a57b843f79 127
dreschpe 0:f6a57b843f79 128 _spi.format(8,3); // 8 bit spi mode 3
dreschpe 0:f6a57b843f79 129 _spi.frequency(20000000); // 19,2 Mhz SPI clock
dreschpe 0:f6a57b843f79 130 DigitalOut _reset(p6);
dreschpe 0:f6a57b843f79 131 _A0 = 0;
dreschpe 0:f6a57b843f79 132 _CS = 1;
dreschpe 0:f6a57b843f79 133 _reset = 0; // display reset
dreschpe 0:f6a57b843f79 134 wait_us(50);
dreschpe 0:f6a57b843f79 135 _reset = 1; // end reset
dreschpe 0:f6a57b843f79 136 wait_ms(5);
dreschpe 0:f6a57b843f79 137
dreschpe 0:f6a57b843f79 138 /* Start Initial Sequence ----------------------------------------------------*/
dreschpe 0:f6a57b843f79 139
dreschpe 0:f6a57b843f79 140 wr_cmd(0xAE); // display off
dreschpe 0:f6a57b843f79 141 wr_cmd(0xA2); // bias voltage
dreschpe 0:f6a57b843f79 142
dreschpe 0:f6a57b843f79 143 wr_cmd(0xA0);
dreschpe 0:f6a57b843f79 144 wr_cmd(0xC8); // colum normal
dreschpe 0:f6a57b843f79 145
dreschpe 0:f6a57b843f79 146 wr_cmd(0x22); // voltage resistor ratio
dreschpe 0:f6a57b843f79 147 wr_cmd(0x2F); // power on
dreschpe 0:f6a57b843f79 148 wr_cmd(0xA4); // LCD display ram
dreschpe 0:f6a57b843f79 149 wr_cmd(0x40); // start line = 0
dreschpe 0:f6a57b843f79 150
dreschpe 0:f6a57b843f79 151 memset(buffer,0x00,sizeof(buffer)); // clear display buffer
dreschpe 0:f6a57b843f79 152
dreschpe 0:f6a57b843f79 153 wr_cmd(0xAF); // display ON
dreschpe 0:f6a57b843f79 154
dreschpe 0:f6a57b843f79 155 wr_cmd(0x81); // set contrast
dreschpe 0:f6a57b843f79 156 wr_cmd(0x17); // set contrast
dreschpe 0:f6a57b843f79 157
dreschpe 0:f6a57b843f79 158 wr_cmd(0xA6); // display normal
dreschpe 0:f6a57b843f79 159
dreschpe 0:f6a57b843f79 160 //setup DMA channel 0
dreschpe 0:f6a57b843f79 161 LPC_SC->PCONP |= (1UL << 29); // Power up the GPDMA
dreschpe 0:f6a57b843f79 162 LPC_GPDMA->DMACConfig = 1; // enable DMA controller
dreschpe 0:f6a57b843f79 163 LPC_GPDMA->DMACIntTCClear = 0x1;
dreschpe 0:f6a57b843f79 164 LPC_GPDMA->DMACIntErrClr = 0x1;
dreschpe 0:f6a57b843f79 165 LPC_GPDMACH0->DMACCLLI = 0;
dreschpe 0:f6a57b843f79 166
dreschpe 0:f6a57b843f79 167 copy_to_lcd();
dreschpe 0:f6a57b843f79 168
dreschpe 0:f6a57b843f79 169 }
dreschpe 0:f6a57b843f79 170
dreschpe 0:f6a57b843f79 171 // set one pixel in buffer
dreschpe 0:f6a57b843f79 172
dreschpe 0:f6a57b843f79 173 void C12832_LCD::pixel(int x, int y, int color)
dreschpe 0:f6a57b843f79 174 {
dreschpe 0:f6a57b843f79 175
dreschpe 0:f6a57b843f79 176 // first check paameter
dreschpe 0:f6a57b843f79 177 if(x >= width() || y >= height() || x < 0 || y < 0) return;
dreschpe 0:f6a57b843f79 178
dreschpe 0:f6a57b843f79 179 if(draw_mode == NORMAL) {
dreschpe 0:f6a57b843f79 180 if(color == 0)
dreschpe 0:f6a57b843f79 181 buffer[x + (y/8) * 128] &= ~(1 << (y%8));
dreschpe 0:f6a57b843f79 182 else
dreschpe 0:f6a57b843f79 183 buffer[x + (y/8) * 128] |= (1 << (y%8));
dreschpe 0:f6a57b843f79 184 } else { // XOR mode
dreschpe 0:f6a57b843f79 185 if(color == 1)
dreschpe 0:f6a57b843f79 186 buffer[x + (y/8) * 128] ^= (1 << (y%8));
dreschpe 0:f6a57b843f79 187 }
dreschpe 0:f6a57b843f79 188 }
dreschpe 0:f6a57b843f79 189
dreschpe 0:f6a57b843f79 190 // update lcd
dreschpe 0:f6a57b843f79 191
dreschpe 0:f6a57b843f79 192 void C12832_LCD::copy_to_lcd(void)
dreschpe 0:f6a57b843f79 193 {
dreschpe 0:f6a57b843f79 194 //int i;
dreschpe 0:f6a57b843f79 195
dreschpe 0:f6a57b843f79 196 wr_cmd(0x00); // set column low nibble 0
dreschpe 0:f6a57b843f79 197 wr_cmd(0x10); // set column hi nibble 0
dreschpe 0:f6a57b843f79 198
dreschpe 0:f6a57b843f79 199 wr_cmd(0xB0); // set page address 0
dreschpe 0:f6a57b843f79 200 _CS = 0;
dreschpe 0:f6a57b843f79 201 _A0 = 1;
dreschpe 0:f6a57b843f79 202 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
dreschpe 0:f6a57b843f79 203 LPC_SSP1->DMACR = 0x2; // Enable SSP1 for DMA.
dreschpe 0:f6a57b843f79 204 LPC_GPDMA->DMACIntTCClear = 0x1;
dreschpe 0:f6a57b843f79 205 LPC_GPDMA->DMACIntErrClr = 0x1;
dreschpe 0:f6a57b843f79 206 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer);
dreschpe 0:f6a57b843f79 207 LPC_GPDMACH0->DMACCControl = 128 | (1UL << 18) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
dreschpe 0:f6a57b843f79 208 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
dreschpe 0:f6a57b843f79 209 LPC_GPDMA->DMACSoftSReq = 0x1;
dreschpe 0:f6a57b843f79 210 do {
dreschpe 0:f6a57b843f79 211 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
dreschpe 0:f6a57b843f79 212
dreschpe 0:f6a57b843f79 213 wr_cmd(0x00); // set column low nibble 0
dreschpe 0:f6a57b843f79 214 wr_cmd(0x10); // set column hi nibble 0
dreschpe 0:f6a57b843f79 215 do {
dreschpe 0:f6a57b843f79 216 } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
dreschpe 0:f6a57b843f79 217 _CS = 1;
dreschpe 0:f6a57b843f79 218 wr_cmd(0xB1); // set page address 1
dreschpe 0:f6a57b843f79 219
dreschpe 0:f6a57b843f79 220 _CS = 0;
dreschpe 0:f6a57b843f79 221 _A0 = 1;
dreschpe 0:f6a57b843f79 222 LPC_GPDMA->DMACIntTCClear = 0x1;
dreschpe 0:f6a57b843f79 223 LPC_GPDMA->DMACIntErrClr = 0x1;
dreschpe 0:f6a57b843f79 224 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 128);
dreschpe 0:f6a57b843f79 225 LPC_GPDMACH0->DMACCControl = 128 | (1UL << 18) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
dreschpe 0:f6a57b843f79 226 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
dreschpe 0:f6a57b843f79 227 LPC_GPDMA->DMACSoftSReq = 0x1;
dreschpe 0:f6a57b843f79 228 do {
dreschpe 0:f6a57b843f79 229 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
dreschpe 0:f6a57b843f79 230
dreschpe 0:f6a57b843f79 231 wr_cmd(0x00); // set column low nibble 0
dreschpe 0:f6a57b843f79 232 wr_cmd(0x10); // set column hi nibble 0
dreschpe 0:f6a57b843f79 233 do {
dreschpe 0:f6a57b843f79 234 } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
dreschpe 0:f6a57b843f79 235 _CS = 1;
dreschpe 0:f6a57b843f79 236 wr_cmd(0xB2); // set page address 2
dreschpe 0:f6a57b843f79 237 _CS = 0;
dreschpe 0:f6a57b843f79 238 _A0 = 1;
dreschpe 0:f6a57b843f79 239 LPC_GPDMA->DMACIntTCClear = 0x1;
dreschpe 0:f6a57b843f79 240 LPC_GPDMA->DMACIntErrClr = 0x1;
dreschpe 0:f6a57b843f79 241 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 256);
dreschpe 0:f6a57b843f79 242 LPC_GPDMACH0->DMACCControl = 128 | (1UL << 18) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
dreschpe 0:f6a57b843f79 243 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX ;
dreschpe 0:f6a57b843f79 244 LPC_GPDMA->DMACSoftSReq = 0x1;
dreschpe 0:f6a57b843f79 245 do {
dreschpe 0:f6a57b843f79 246 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
dreschpe 0:f6a57b843f79 247
dreschpe 0:f6a57b843f79 248 wr_cmd(0x00); // set column low nibble 0
dreschpe 0:f6a57b843f79 249 wr_cmd(0x10); // set column hi nibble 0
dreschpe 0:f6a57b843f79 250 do {
dreschpe 0:f6a57b843f79 251 } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
dreschpe 0:f6a57b843f79 252 _CS = 1;
dreschpe 0:f6a57b843f79 253 wr_cmd(0xB3); // set page address 3
dreschpe 0:f6a57b843f79 254 _CS = 0;
dreschpe 0:f6a57b843f79 255 _A0 = 1;
dreschpe 0:f6a57b843f79 256 LPC_GPDMA->DMACIntTCClear = 0x1;
dreschpe 0:f6a57b843f79 257 LPC_GPDMA->DMACIntErrClr = 0x1;
dreschpe 0:f6a57b843f79 258 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + 384);
dreschpe 0:f6a57b843f79 259 LPC_GPDMACH0->DMACCControl = 128 | (1UL << 18) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 8 bit transfer , address increment, interrupt
dreschpe 0:f6a57b843f79 260 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | DMA_DEST_SSP1_TX;
dreschpe 0:f6a57b843f79 261 LPC_GPDMA->DMACSoftSReq = 0x1;
dreschpe 0:f6a57b843f79 262 do {
dreschpe 0:f6a57b843f79 263 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
dreschpe 0:f6a57b843f79 264
dreschpe 0:f6a57b843f79 265 do {
dreschpe 0:f6a57b843f79 266 } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
dreschpe 0:f6a57b843f79 267 _CS = 1;
dreschpe 0:f6a57b843f79 268 }
dreschpe 0:f6a57b843f79 269
dreschpe 0:f6a57b843f79 270 void C12832_LCD::cls(void)
dreschpe 0:f6a57b843f79 271 {
dreschpe 0:f6a57b843f79 272 memset(buffer,0x00,sizeof(buffer)); // clear display buffer
dreschpe 0:f6a57b843f79 273 copy_to_lcd();
dreschpe 0:f6a57b843f79 274 }
dreschpe 0:f6a57b843f79 275
dreschpe 0:f6a57b843f79 276
dreschpe 0:f6a57b843f79 277 void C12832_LCD::line(int x0, int y0, int x1, int y1, int color)
dreschpe 0:f6a57b843f79 278 {
dreschpe 0:f6a57b843f79 279 int dx = 0, dy = 0;
dreschpe 0:f6a57b843f79 280 int dx_sym = 0, dy_sym = 0;
dreschpe 0:f6a57b843f79 281 int dx_x2 = 0, dy_x2 = 0;
dreschpe 0:f6a57b843f79 282 int di = 0;
dreschpe 0:f6a57b843f79 283
dreschpe 0:f6a57b843f79 284 dx = x1-x0;
dreschpe 0:f6a57b843f79 285 dy = y1-y0;
dreschpe 0:f6a57b843f79 286
dreschpe 0:f6a57b843f79 287 // if (dx == 0) { /* vertical line */
dreschpe 0:f6a57b843f79 288 // if (y1 > y0) vline(x0,y0,y1,color);
dreschpe 0:f6a57b843f79 289 // else vline(x0,y1,y0,color);
dreschpe 0:f6a57b843f79 290 // return;
dreschpe 0:f6a57b843f79 291 // }
dreschpe 0:f6a57b843f79 292
dreschpe 0:f6a57b843f79 293 if (dx > 0) {
dreschpe 0:f6a57b843f79 294 dx_sym = 1;
dreschpe 0:f6a57b843f79 295 } else {
dreschpe 0:f6a57b843f79 296 dx_sym = -1;
dreschpe 0:f6a57b843f79 297 }
dreschpe 0:f6a57b843f79 298 // if (dy == 0) { /* horizontal line */
dreschpe 0:f6a57b843f79 299 // if (x1 > x0) hline(x0,x1,y0,color);
dreschpe 0:f6a57b843f79 300 // else hline(x1,x0,y0,color);
dreschpe 0:f6a57b843f79 301 // return;
dreschpe 0:f6a57b843f79 302 // }
dreschpe 0:f6a57b843f79 303
dreschpe 0:f6a57b843f79 304 if (dy > 0) {
dreschpe 0:f6a57b843f79 305 dy_sym = 1;
dreschpe 0:f6a57b843f79 306 } else {
dreschpe 0:f6a57b843f79 307 dy_sym = -1;
dreschpe 0:f6a57b843f79 308 }
dreschpe 0:f6a57b843f79 309
dreschpe 0:f6a57b843f79 310 dx = dx_sym*dx;
dreschpe 0:f6a57b843f79 311 dy = dy_sym*dy;
dreschpe 0:f6a57b843f79 312
dreschpe 0:f6a57b843f79 313 dx_x2 = dx*2;
dreschpe 0:f6a57b843f79 314 dy_x2 = dy*2;
dreschpe 0:f6a57b843f79 315
dreschpe 0:f6a57b843f79 316 if (dx >= dy) {
dreschpe 0:f6a57b843f79 317 di = dy_x2 - dx;
dreschpe 0:f6a57b843f79 318 while (x0 != x1) {
dreschpe 0:f6a57b843f79 319
dreschpe 0:f6a57b843f79 320 pixel(x0, y0, color);
dreschpe 0:f6a57b843f79 321 x0 += dx_sym;
dreschpe 0:f6a57b843f79 322 if (di<0) {
dreschpe 0:f6a57b843f79 323 di += dy_x2;
dreschpe 0:f6a57b843f79 324 } else {
dreschpe 0:f6a57b843f79 325 di += dy_x2 - dx_x2;
dreschpe 0:f6a57b843f79 326 y0 += dy_sym;
dreschpe 0:f6a57b843f79 327 }
dreschpe 0:f6a57b843f79 328 }
dreschpe 0:f6a57b843f79 329 pixel(x0, y0, color);
dreschpe 0:f6a57b843f79 330 } else {
dreschpe 0:f6a57b843f79 331 di = dx_x2 - dy;
dreschpe 0:f6a57b843f79 332 while (y0 != y1) {
dreschpe 0:f6a57b843f79 333 pixel(x0, y0, color);
dreschpe 0:f6a57b843f79 334 y0 += dy_sym;
dreschpe 0:f6a57b843f79 335 if (di < 0) {
dreschpe 0:f6a57b843f79 336 di += dx_x2;
dreschpe 0:f6a57b843f79 337 } else {
dreschpe 0:f6a57b843f79 338 di += dx_x2 - dy_x2;
dreschpe 0:f6a57b843f79 339 x0 += dx_sym;
dreschpe 0:f6a57b843f79 340 }
dreschpe 0:f6a57b843f79 341 }
dreschpe 0:f6a57b843f79 342 pixel(x0, y0, color);
dreschpe 0:f6a57b843f79 343 }
dreschpe 0:f6a57b843f79 344 return;
dreschpe 0:f6a57b843f79 345 }
dreschpe 0:f6a57b843f79 346
dreschpe 0:f6a57b843f79 347 void C12832_LCD::rect(int x0, int y0, int x1, int y1, int color)
dreschpe 0:f6a57b843f79 348 {
dreschpe 0:f6a57b843f79 349
dreschpe 0:f6a57b843f79 350 if (x1 > x0) line(x0,y0,x1,y0,color);
dreschpe 0:f6a57b843f79 351 else line(x1,y0,x0,y0,color);
dreschpe 0:f6a57b843f79 352
dreschpe 0:f6a57b843f79 353 if (y1 > y0) line(x0,y0,x0,y1,color);
dreschpe 0:f6a57b843f79 354 else line(x0,y1,x0,y0,color);
dreschpe 0:f6a57b843f79 355
dreschpe 0:f6a57b843f79 356 if (x1 > x0) line(x0,y1,x1,y1,color);
dreschpe 0:f6a57b843f79 357 else line(x1,y1,x0,y1,color);
dreschpe 0:f6a57b843f79 358
dreschpe 0:f6a57b843f79 359 if (y1 > y0) line(x1,y0,x1,y1,color);
dreschpe 0:f6a57b843f79 360 else line(x1,y1,x1,y0,color);
dreschpe 0:f6a57b843f79 361
dreschpe 0:f6a57b843f79 362 return;
dreschpe 0:f6a57b843f79 363 }
dreschpe 0:f6a57b843f79 364
dreschpe 0:f6a57b843f79 365 void C12832_LCD::fillrect(int x0, int y0, int x1, int y1, int color)
dreschpe 0:f6a57b843f79 366 {
dreschpe 0:f6a57b843f79 367 int l,c,i;
dreschpe 0:f6a57b843f79 368 if(x0 > x1) {
dreschpe 0:f6a57b843f79 369 i = x0;
dreschpe 0:f6a57b843f79 370 x0 = x1;
dreschpe 0:f6a57b843f79 371 x1 = i;
dreschpe 0:f6a57b843f79 372 }
dreschpe 0:f6a57b843f79 373
dreschpe 0:f6a57b843f79 374 if(y0 > y1) {
dreschpe 0:f6a57b843f79 375 i = y0;
dreschpe 0:f6a57b843f79 376 y0 = y1;
dreschpe 0:f6a57b843f79 377 y1 = i;
dreschpe 0:f6a57b843f79 378 }
dreschpe 0:f6a57b843f79 379
dreschpe 0:f6a57b843f79 380 for(l = x0; l<= x1; l ++) {
dreschpe 0:f6a57b843f79 381 for(c = y0; c<= y1; c++) {
dreschpe 0:f6a57b843f79 382 pixel(l,c,color);
dreschpe 0:f6a57b843f79 383 }
dreschpe 0:f6a57b843f79 384 }
dreschpe 0:f6a57b843f79 385 }
dreschpe 0:f6a57b843f79 386
dreschpe 0:f6a57b843f79 387
dreschpe 0:f6a57b843f79 388
dreschpe 0:f6a57b843f79 389 void C12832_LCD::circle(int x0, int y0, int r, int color)
dreschpe 0:f6a57b843f79 390 {
dreschpe 0:f6a57b843f79 391
dreschpe 0:f6a57b843f79 392 int draw_x0, draw_y0;
dreschpe 0:f6a57b843f79 393 int draw_x1, draw_y1;
dreschpe 0:f6a57b843f79 394 int draw_x2, draw_y2;
dreschpe 0:f6a57b843f79 395 int draw_x3, draw_y3;
dreschpe 0:f6a57b843f79 396 int draw_x4, draw_y4;
dreschpe 0:f6a57b843f79 397 int draw_x5, draw_y5;
dreschpe 0:f6a57b843f79 398 int draw_x6, draw_y6;
dreschpe 0:f6a57b843f79 399 int draw_x7, draw_y7;
dreschpe 0:f6a57b843f79 400 int xx, yy;
dreschpe 0:f6a57b843f79 401 int di;
dreschpe 0:f6a57b843f79 402 //WindowMax();
dreschpe 0:f6a57b843f79 403 if (r == 0) { /* no radius */
dreschpe 0:f6a57b843f79 404 return;
dreschpe 0:f6a57b843f79 405 }
dreschpe 0:f6a57b843f79 406
dreschpe 0:f6a57b843f79 407 draw_x0 = draw_x1 = x0;
dreschpe 0:f6a57b843f79 408 draw_y0 = draw_y1 = y0 + r;
dreschpe 0:f6a57b843f79 409 if (draw_y0 < height()) {
dreschpe 0:f6a57b843f79 410 pixel(draw_x0, draw_y0, color); /* 90 degree */
dreschpe 0:f6a57b843f79 411 }
dreschpe 0:f6a57b843f79 412
dreschpe 0:f6a57b843f79 413 draw_x2 = draw_x3 = x0;
dreschpe 0:f6a57b843f79 414 draw_y2 = draw_y3 = y0 - r;
dreschpe 0:f6a57b843f79 415 if (draw_y2 >= 0) {
dreschpe 0:f6a57b843f79 416 pixel(draw_x2, draw_y2, color); /* 270 degree */
dreschpe 0:f6a57b843f79 417 }
dreschpe 0:f6a57b843f79 418
dreschpe 0:f6a57b843f79 419 draw_x4 = draw_x6 = x0 + r;
dreschpe 0:f6a57b843f79 420 draw_y4 = draw_y6 = y0;
dreschpe 0:f6a57b843f79 421 if (draw_x4 < width()) {
dreschpe 0:f6a57b843f79 422 pixel(draw_x4, draw_y4, color); /* 0 degree */
dreschpe 0:f6a57b843f79 423 }
dreschpe 0:f6a57b843f79 424
dreschpe 0:f6a57b843f79 425 draw_x5 = draw_x7 = x0 - r;
dreschpe 0:f6a57b843f79 426 draw_y5 = draw_y7 = y0;
dreschpe 0:f6a57b843f79 427 if (draw_x5>=0) {
dreschpe 0:f6a57b843f79 428 pixel(draw_x5, draw_y5, color); /* 180 degree */
dreschpe 0:f6a57b843f79 429 }
dreschpe 0:f6a57b843f79 430
dreschpe 0:f6a57b843f79 431 if (r == 1) {
dreschpe 0:f6a57b843f79 432 return;
dreschpe 0:f6a57b843f79 433 }
dreschpe 0:f6a57b843f79 434
dreschpe 0:f6a57b843f79 435 di = 3 - 2*r;
dreschpe 0:f6a57b843f79 436 xx = 0;
dreschpe 0:f6a57b843f79 437 yy = r;
dreschpe 0:f6a57b843f79 438 while (xx < yy) {
dreschpe 0:f6a57b843f79 439
dreschpe 0:f6a57b843f79 440 if (di < 0) {
dreschpe 0:f6a57b843f79 441 di += 4*xx + 6;
dreschpe 0:f6a57b843f79 442 } else {
dreschpe 0:f6a57b843f79 443 di += 4*(xx - yy) + 10;
dreschpe 0:f6a57b843f79 444 yy--;
dreschpe 0:f6a57b843f79 445 draw_y0--;
dreschpe 0:f6a57b843f79 446 draw_y1--;
dreschpe 0:f6a57b843f79 447 draw_y2++;
dreschpe 0:f6a57b843f79 448 draw_y3++;
dreschpe 0:f6a57b843f79 449 draw_x4--;
dreschpe 0:f6a57b843f79 450 draw_x5++;
dreschpe 0:f6a57b843f79 451 draw_x6--;
dreschpe 0:f6a57b843f79 452 draw_x7++;
dreschpe 0:f6a57b843f79 453 }
dreschpe 0:f6a57b843f79 454 xx++;
dreschpe 0:f6a57b843f79 455 draw_x0++;
dreschpe 0:f6a57b843f79 456 draw_x1--;
dreschpe 0:f6a57b843f79 457 draw_x2++;
dreschpe 0:f6a57b843f79 458 draw_x3--;
dreschpe 0:f6a57b843f79 459 draw_y4++;
dreschpe 0:f6a57b843f79 460 draw_y5++;
dreschpe 0:f6a57b843f79 461 draw_y6--;
dreschpe 0:f6a57b843f79 462 draw_y7--;
dreschpe 0:f6a57b843f79 463
dreschpe 0:f6a57b843f79 464 if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
dreschpe 0:f6a57b843f79 465 pixel(draw_x0, draw_y0, color);
dreschpe 0:f6a57b843f79 466 }
dreschpe 0:f6a57b843f79 467
dreschpe 0:f6a57b843f79 468 if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
dreschpe 0:f6a57b843f79 469 pixel(draw_x1, draw_y1, color);
dreschpe 0:f6a57b843f79 470 }
dreschpe 0:f6a57b843f79 471
dreschpe 0:f6a57b843f79 472 if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
dreschpe 0:f6a57b843f79 473 pixel(draw_x2, draw_y2, color);
dreschpe 0:f6a57b843f79 474 }
dreschpe 0:f6a57b843f79 475
dreschpe 0:f6a57b843f79 476 if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
dreschpe 0:f6a57b843f79 477 pixel(draw_x3, draw_y3, color);
dreschpe 0:f6a57b843f79 478 }
dreschpe 0:f6a57b843f79 479
dreschpe 0:f6a57b843f79 480 if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
dreschpe 0:f6a57b843f79 481 pixel(draw_x4, draw_y4, color);
dreschpe 0:f6a57b843f79 482 }
dreschpe 0:f6a57b843f79 483
dreschpe 0:f6a57b843f79 484 if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
dreschpe 0:f6a57b843f79 485 pixel(draw_x5, draw_y5, color);
dreschpe 0:f6a57b843f79 486 }
dreschpe 0:f6a57b843f79 487 if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
dreschpe 0:f6a57b843f79 488 pixel(draw_x6, draw_y6, color);
dreschpe 0:f6a57b843f79 489 }
dreschpe 0:f6a57b843f79 490 if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
dreschpe 0:f6a57b843f79 491 pixel(draw_x7, draw_y7, color);
dreschpe 0:f6a57b843f79 492 }
dreschpe 0:f6a57b843f79 493 }
dreschpe 0:f6a57b843f79 494 return;
dreschpe 0:f6a57b843f79 495 }
dreschpe 0:f6a57b843f79 496
dreschpe 0:f6a57b843f79 497 void C12832_LCD::fillcircle(int x, int y, int r, int color)
dreschpe 0:f6a57b843f79 498 {
dreschpe 0:f6a57b843f79 499 int i;
dreschpe 0:f6a57b843f79 500 for (i = 0; i <= r; i++)
dreschpe 0:f6a57b843f79 501 circle(x,y,i,color);
dreschpe 0:f6a57b843f79 502 }
dreschpe 0:f6a57b843f79 503
dreschpe 0:f6a57b843f79 504 void C12832_LCD::setmode(int mode)
dreschpe 0:f6a57b843f79 505 {
dreschpe 0:f6a57b843f79 506 draw_mode = mode;
dreschpe 0:f6a57b843f79 507 }
dreschpe 0:f6a57b843f79 508
dreschpe 0:f6a57b843f79 509 void C12832_LCD::locate(int x, int y)
dreschpe 0:f6a57b843f79 510 {
dreschpe 0:f6a57b843f79 511 char_x = x;
dreschpe 0:f6a57b843f79 512 char_y = y;
dreschpe 0:f6a57b843f79 513 }
dreschpe 0:f6a57b843f79 514
dreschpe 0:f6a57b843f79 515
dreschpe 0:f6a57b843f79 516
dreschpe 0:f6a57b843f79 517 int C12832_LCD::columns()
dreschpe 0:f6a57b843f79 518 {
dreschpe 0:f6a57b843f79 519 return width() / font[1];
dreschpe 0:f6a57b843f79 520 }
dreschpe 0:f6a57b843f79 521
dreschpe 0:f6a57b843f79 522
dreschpe 0:f6a57b843f79 523
dreschpe 0:f6a57b843f79 524 int C12832_LCD::rows()
dreschpe 0:f6a57b843f79 525 {
dreschpe 0:f6a57b843f79 526 return height() / font[2];
dreschpe 0:f6a57b843f79 527 }
dreschpe 0:f6a57b843f79 528
dreschpe 0:f6a57b843f79 529
dreschpe 0:f6a57b843f79 530
dreschpe 0:f6a57b843f79 531 int C12832_LCD::_putc(int value)
dreschpe 0:f6a57b843f79 532 {
dreschpe 0:f6a57b843f79 533 if (value == '\n') { // new line
dreschpe 0:f6a57b843f79 534 char_x = 0;
dreschpe 0:f6a57b843f79 535 char_y = char_y + font[2];
dreschpe 0:f6a57b843f79 536 if (char_y >= height() - font[2]) {
dreschpe 0:f6a57b843f79 537 char_y = 0;
dreschpe 0:f6a57b843f79 538 }
dreschpe 0:f6a57b843f79 539 } else {
dreschpe 0:f6a57b843f79 540 character(char_x, char_y, value);
dreschpe 0:f6a57b843f79 541 }
dreschpe 0:f6a57b843f79 542 return value;
dreschpe 0:f6a57b843f79 543 }
dreschpe 0:f6a57b843f79 544
dreschpe 0:f6a57b843f79 545 void C12832_LCD::character(int x, int y, int c)
dreschpe 0:f6a57b843f79 546 {
dreschpe 0:f6a57b843f79 547 unsigned int hor,vert,offset,bpl,j,i,b;
dreschpe 0:f6a57b843f79 548 unsigned char* zeichen;
dreschpe 0:f6a57b843f79 549 unsigned char z,w;
dreschpe 0:f6a57b843f79 550
dreschpe 0:f6a57b843f79 551 if ((c < 31) || (c > 127)) return; // test char range
dreschpe 0:f6a57b843f79 552
dreschpe 0:f6a57b843f79 553 // read font parameter from start of array
dreschpe 0:f6a57b843f79 554 offset = font[0]; // bytes / char
dreschpe 0:f6a57b843f79 555 hor = font[1]; // get hor size of font
dreschpe 0:f6a57b843f79 556 vert = font[2]; // get vert size of font
dreschpe 0:f6a57b843f79 557 bpl = font[3]; // bytes per line
dreschpe 0:f6a57b843f79 558
dreschpe 0:f6a57b843f79 559 if (char_x + hor > width()) {
dreschpe 0:f6a57b843f79 560 char_x = 0;
dreschpe 0:f6a57b843f79 561 char_y = char_y + vert;
dreschpe 0:f6a57b843f79 562 if (char_y >= height() - font[2]) {
dreschpe 0:f6a57b843f79 563 char_y = 0;
dreschpe 0:f6a57b843f79 564 }
dreschpe 0:f6a57b843f79 565 }
dreschpe 0:f6a57b843f79 566
dreschpe 0:f6a57b843f79 567 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
dreschpe 0:f6a57b843f79 568 w = zeichen[0]; // width of actual char
dreschpe 0:f6a57b843f79 569 // construct the char into the buffer
dreschpe 0:f6a57b843f79 570 for (j=0; j<vert; j++) { // vert line
dreschpe 0:f6a57b843f79 571 for (i=0; i<hor; i++) { // horz line
dreschpe 0:f6a57b843f79 572 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
dreschpe 0:f6a57b843f79 573 b = 1 << (j & 0x07);
dreschpe 0:f6a57b843f79 574 if (( z & b ) == 0x00) {
dreschpe 0:f6a57b843f79 575 pixel(x+i,y+j,0);
dreschpe 0:f6a57b843f79 576 } else {
dreschpe 0:f6a57b843f79 577 pixel(x+i,y+j,1);
dreschpe 0:f6a57b843f79 578 }
dreschpe 0:f6a57b843f79 579
dreschpe 0:f6a57b843f79 580 }
dreschpe 0:f6a57b843f79 581 }
dreschpe 0:f6a57b843f79 582
dreschpe 0:f6a57b843f79 583 char_x += w;
dreschpe 0:f6a57b843f79 584 }
dreschpe 0:f6a57b843f79 585
dreschpe 0:f6a57b843f79 586
dreschpe 0:f6a57b843f79 587 void C12832_LCD::set_font(unsigned char* f)
dreschpe 0:f6a57b843f79 588 {
dreschpe 0:f6a57b843f79 589 font = f;
dreschpe 0:f6a57b843f79 590 }
dreschpe 0:f6a57b843f79 591
dreschpe 0:f6a57b843f79 592
dreschpe 0:f6a57b843f79 593