StopwatchLCD

Dependencies:   mbed

Committer:
martwerl
Date:
Thu Nov 15 18:10:36 2018 +0000
Revision:
0:3b4026dea19d
StopwatchLCD

Who changed what in which revision?

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