test

Dependencies:   mbed

Committer:
chris77774
Date:
Sat Apr 30 17:57:29 2016 +0000
Revision:
0:32a06703946f
test

Who changed what in which revision?

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