BayEOS-Arduino  1.8.0_0.0.4
UTFT.cpp
1 /*
2  UTFT.cpp - Multi-Platform library support for Color TFT LCD Boards
3  Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
4 
5  This library is the continuation of my ITDB02_Graph, ITDB02_Graph16
6  and RGB_GLCD libraries for Arduino and chipKit. As the number of
7  supported display modules and controllers started to increase I felt
8  it was time to make a single, universal library as it will be much
9  easier to maintain in the future.
10 
11  Basic functionality of this library was origianlly based on the
12  demo-code provided by ITead studio (for the ITDB02 modules) and
13  NKC Electronics (for the RGB GLCD module/shield).
14 
15  This library supports a number of 8bit, 16bit and serial graphic
16  displays, and will work with both Arduino, chipKit boards and select
17  TI LaunchPads. For a full list of tested display modules and controllers,
18  see the document UTFT_Supported_display_modules_&_controllers.pdf.
19 
20  When using 8bit and 16bit display modules there are some
21  requirements you must adhere to. These requirements can be found
22  in the document UTFT_Requirements.pdf.
23  There are no special requirements when using serial displays.
24 
25  You can find the latest version of the library at
26  http://www.RinkyDinkElectronics.com/
27 
28  This library is free software; you can redistribute it and/or
29  modify it under the terms of the CC BY-NC-SA 3.0 license.
30  Please see the included documents for further information.
31 
32  Commercial use of this library requires you to buy a license that
33  will allow commercial use. This includes using the library,
34  modified or not, as a tool to sell products.
35 
36  The license applies to all part of the library including the
37  examples and tools supplied with the library.
38 */
39 
40 #include "UTFT.h"
41 
42 // Include hardware-specific functions for the correct MCU
43 #if defined(__AVR__)
44  #include <avr/pgmspace.h>
45  #include "hardware/avr/HW_AVR.h"
46  #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
47  #include "hardware/avr/HW_ATmega1280.h"
48  #elif defined(__AVR_ATmega328P__)
49  #include "hardware/avr/HW_ATmega328P.h"
50  #elif defined(__AVR_ATmega32U4__)
51  #include "hardware/avr/HW_ATmega32U4.h"
52  #elif defined(__AVR_ATmega168__)
53  #error "ATmega168 MCUs are not supported because they have too little flash memory!"
54  #elif defined(__AVR_ATmega1284P__)
55  #include "hardware/avr/HW_ATmega1284P.h"
56  #else
57  #error "Unsupported AVR MCU!"
58  #endif
59 #elif defined(__PIC32MX__)
60  #include "hardware/pic32/HW_PIC32.h"
61  #if defined(__32MX320F128H__)
62  #pragma message("Compiling for chipKIT UNO32 (PIC32MX320F128H)")
63  #include "hardware/pic32/HW_PIC32MX320F128H.h"
64  #elif defined(__32MX340F512H__)
65  #pragma message("Compiling for chipKIT uC32 (PIC32MX340F512H)")
66  #include "hardware/pic32/HW_PIC32MX340F512H.h"
67  #elif defined(__32MX795F512L__)
68  #pragma message("Compiling for chipKIT MAX32 (PIC32MX795F512L)")
69  #include "hardware/pic32/HW_PIC32MX795F512L.h"
70  #else
71  #error "Unsupported PIC32 MCU!"
72  #endif
73 #elif defined(__arm__)
74  #include "hardware/arm/HW_ARM.h"
75  #if defined(__SAM3X8E__)
76  #pragma message("Compiling for Arduino Due (AT91SAM3X8E)...")
77  #include "hardware/arm/HW_SAM3X8E.h"
78  #elif defined(__MK20DX128__) || defined(__MK20DX256__)
79  #pragma message("Compiling for Teensy 3.x (MK20DX128VLH7 / MK20DX256VLH7)...")
80  #include "hardware/arm/HW_MX20DX256.h"
81  #elif defined(__CC3200R1M1RGC__)
82  #pragma message("Compiling for TI CC3200 LaunchPad...")
83  #include "hardware/arm/HW_CC3200.h"
84  #else
85  #error "Unsupported ARM MCU!"
86  #endif
87 #endif
88 #include "memorysaver.h"
89 
90 UTFT::UTFT()
91 {
92 }
93 
94 UTFT::UTFT(byte model, int RS, int WR, int CS, int RST, int SER)
95 {
96  word dsx[] = {239, 239, 239, 239, 239, 239, 175, 175, 239, 127, 127, 239, 271, 479, 239, 239, 239, 0, 0, 239, 479, 319, 239, 175, 127, 239, 239, 319, 319, 799, 127, 127};
97  word dsy[] = {319, 399, 319, 319, 319, 319, 219, 219, 399, 159, 127, 319, 479, 799, 319, 319, 319, 0, 0, 319, 799, 479, 319, 219, 159, 319, 319, 479, 479, 479, 159, 159};
98  byte dtm[] = {16, 16, 16, 8, 8, 16, 8, SERIAL_4PIN, 16, SERIAL_5PIN, SERIAL_5PIN, 16, 16, 16, 8, 16, LATCHED_16, 0, 0, 8, 16, 16, 16, 8, SERIAL_5PIN, SERIAL_5PIN, SERIAL_4PIN, 16, 16, 16, SERIAL_5PIN, SERIAL_5PIN};
99 
100  disp_x_size = dsx[model];
101  disp_y_size = dsy[model];
102  display_transfer_mode = dtm[model];
103  display_model = model;
104 
105  __p1 = RS;
106  __p2 = WR;
107  __p3 = CS;
108  __p4 = RST;
109  __p5 = SER;
110 
111  if (display_transfer_mode == SERIAL_4PIN)
112  {
113  display_transfer_mode=1;
114  display_serial_mode=SERIAL_4PIN;
115  }
116  if (display_transfer_mode == SERIAL_5PIN)
117  {
118  display_transfer_mode=1;
119  display_serial_mode=SERIAL_5PIN;
120  }
121 
122  if (display_transfer_mode!=1)
123  {
124  _set_direction_registers(display_transfer_mode);
125  P_RS = portOutputRegister(digitalPinToPort(RS));
126  B_RS = digitalPinToBitMask(RS);
127  P_WR = portOutputRegister(digitalPinToPort(WR));
128  B_WR = digitalPinToBitMask(WR);
129  P_CS = portOutputRegister(digitalPinToPort(CS));
130  B_CS = digitalPinToBitMask(CS);
131  P_RST = portOutputRegister(digitalPinToPort(RST));
132  B_RST = digitalPinToBitMask(RST);
133  if (display_transfer_mode==LATCHED_16)
134  {
135  P_ALE = portOutputRegister(digitalPinToPort(SER));
136  B_ALE = digitalPinToBitMask(SER);
137  cbi(P_ALE, B_ALE);
138  pinMode(8,OUTPUT);
139  digitalWrite(8, LOW);
140  }
141  }
142  else
143  {
144  P_SDA = portOutputRegister(digitalPinToPort(RS));
145  B_SDA = digitalPinToBitMask(RS);
146  P_SCL = portOutputRegister(digitalPinToPort(WR));
147  B_SCL = digitalPinToBitMask(WR);
148  P_CS = portOutputRegister(digitalPinToPort(CS));
149  B_CS = digitalPinToBitMask(CS);
150  if (RST != NOTINUSE)
151  {
152  P_RST = portOutputRegister(digitalPinToPort(RST));
153  B_RST = digitalPinToBitMask(RST);
154  }
155  if (display_serial_mode!=SERIAL_4PIN)
156  {
157  P_RS = portOutputRegister(digitalPinToPort(SER));
158  B_RS = digitalPinToBitMask(SER);
159  }
160  }
161 }
162 
163 void UTFT::LCD_Write_COM(char VL)
164 {
165  if (display_transfer_mode!=1)
166  {
167  cbi(P_RS, B_RS);
168  LCD_Writ_Bus(0x00,VL,display_transfer_mode);
169  }
170  else
171  LCD_Writ_Bus(0x00,VL,display_transfer_mode);
172 }
173 
174 void UTFT::LCD_Write_DATA(char VH,char VL)
175 {
176  if (display_transfer_mode!=1)
177  {
178  sbi(P_RS, B_RS);
179  LCD_Writ_Bus(VH,VL,display_transfer_mode);
180  }
181  else
182  {
183  LCD_Writ_Bus(0x01,VH,display_transfer_mode);
184  LCD_Writ_Bus(0x01,VL,display_transfer_mode);
185  }
186 }
187 
188 void UTFT::LCD_Write_DATA(char VL)
189 {
190  if (display_transfer_mode!=1)
191  {
192  sbi(P_RS, B_RS);
193  LCD_Writ_Bus(0x00,VL,display_transfer_mode);
194  }
195  else
196  LCD_Writ_Bus(0x01,VL,display_transfer_mode);
197 }
198 
199 void UTFT::LCD_Write_COM_DATA(char com1,int dat1)
200 {
201  LCD_Write_COM(com1);
202  LCD_Write_DATA(dat1>>8,dat1);
203 }
204 
205 void UTFT::InitLCD(byte orientation)
206 {
207  orient=orientation;
209 
210  pinMode(__p1,OUTPUT);
211  pinMode(__p2,OUTPUT);
212  pinMode(__p3,OUTPUT);
213  if (__p4 != NOTINUSE)
214  pinMode(__p4,OUTPUT);
215  if ((display_transfer_mode==LATCHED_16) or ((display_transfer_mode==1) and (display_serial_mode==SERIAL_5PIN)))
216  pinMode(__p5,OUTPUT);
217  if (display_transfer_mode!=1)
218  _set_direction_registers(display_transfer_mode);
219 
220  sbi(P_RST, B_RST);
221  delay(5);
222  cbi(P_RST, B_RST);
223  delay(15);
224  sbi(P_RST, B_RST);
225  delay(15);
226 
227  cbi(P_CS, B_CS);
228 
229  switch(display_model)
230  {
231 #ifndef DISABLE_HX8347A
232  #include "tft_drivers/hx8347a/initlcd.h"
233 #endif
234 #ifndef DISABLE_ILI9327
235  #include "tft_drivers/ili9327/initlcd.h"
236 #endif
237 #ifndef DISABLE_SSD1289
238  #include "tft_drivers/ssd1289/initlcd.h"
239 #endif
240 #ifndef DISABLE_ILI9325C
241  #include "tft_drivers/ili9325c/initlcd.h"
242 #endif
243 #ifndef DISABLE_ILI9325D
244  #include "tft_drivers/ili9325d/default/initlcd.h"
245 #endif
246 #ifndef DISABLE_ILI9325D_ALT
247  #include "tft_drivers/ili9325d/alt/initlcd.h"
248 #endif
249 #ifndef DISABLE_HX8340B_8
250  #include "tft_drivers/hx8340b/8/initlcd.h"
251 #endif
252 #ifndef DISABLE_HX8340B_S
253  #include "tft_drivers/hx8340b/s/initlcd.h"
254 #endif
255 #ifndef DISABLE_ST7735
256  #include "tft_drivers/st7735/std/initlcd.h"
257 #endif
258 #ifndef DISABLE_ST7735_ALT
259  #include "tft_drivers/st7735/alt/initlcd.h"
260 #endif
261 #ifndef DISABLE_PCF8833
262  #include "tft_drivers/pcf8833/initlcd.h"
263 #endif
264 #ifndef DISABLE_S1D19122
265  #include "tft_drivers/s1d19122/initlcd.h"
266 #endif
267 #ifndef DISABLE_HX8352A
268  #include "tft_drivers/hx8352a/initlcd.h"
269 #endif
270 #ifndef DISABLE_SSD1963_480
271  #include "tft_drivers/ssd1963/480/initlcd.h"
272 #endif
273 #ifndef DISABLE_SSD1963_800
274  #include "tft_drivers/ssd1963/800/initlcd.h"
275 #endif
276 #ifndef DISABLE_SSD1963_800_ALT
277  #include "tft_drivers/ssd1963/800alt/initlcd.h"
278 #endif
279 #ifndef DISABLE_S6D1121
280  #include "tft_drivers/s6d1121/initlcd.h"
281 #endif
282 #ifndef DISABLE_ILI9481
283  #include "tft_drivers/ili9481/initlcd.h"
284 #endif
285 #ifndef DISABLE_S6D0164
286  #include "tft_drivers/s6d0164/initlcd.h"
287 #endif
288 #ifndef DISABLE_ST7735S
289  #include "tft_drivers/st7735s/initlcd.h"
290 #endif
291 #ifndef DISABLE_ILI9341_S4P
292  #include "tft_drivers/ili9341/s4p/initlcd.h"
293 #endif
294 #ifndef DISABLE_ILI9341_S5P
295  #include "tft_drivers/ili9341/s5p/initlcd.h"
296 #endif
297 #ifndef DISABLE_R61581
298  #include "tft_drivers/r61581/initlcd.h"
299 #endif
300 #ifndef DISABLE_ILI9486
301  #include "tft_drivers/ili9486/initlcd.h"
302 #endif
303 #ifndef DISABLE_CPLD
304  #include "tft_drivers/cpld/initlcd.h"
305 #endif
306 #ifndef DISABLE_HX8353C
307  #include "tft_drivers/hx8353c/initlcd.h"
308 #endif
309  }
310 
311  sbi (P_CS, B_CS);
312 
313  setColor(255, 255, 255);
314  setBackColor(0, 0, 0);
315  cfont.font=0;
316  _transparent = false;
317 }
318 
319 void UTFT::setXY(word x1, word y1, word x2, word y2)
320 {
321  if (orient==LANDSCAPE)
322  {
323  swap(word, x1, y1);
324  swap(word, x2, y2)
325  y1=disp_y_size-y1;
326  y2=disp_y_size-y2;
327  swap(word, y1, y2)
328  }
329 
330  switch(display_model)
331  {
332 #ifndef DISABLE_HX8347A
333  #include "tft_drivers/hx8347a/setxy.h"
334 #endif
335 #ifndef DISABLE_HX8352A
336  #include "tft_drivers/hx8352a/setxy.h"
337 #endif
338 #ifndef DISABLE_ILI9327
339  #include "tft_drivers/ili9327/setxy.h"
340 #endif
341 #ifndef DISABLE_SSD1289
342  #include "tft_drivers/ssd1289/setxy.h"
343 #endif
344 #ifndef DISABLE_ILI9325C
345  #include "tft_drivers/ili9325c/setxy.h"
346 #endif
347 #ifndef DISABLE_ILI9325D
348  #include "tft_drivers/ili9325d/default/setxy.h"
349 #endif
350 #ifndef DISABLE_ILI9325D_ALT
351  #include "tft_drivers/ili9325d/alt/setxy.h"
352 #endif
353 #ifndef DISABLE_HX8340B_8
354  #include "tft_drivers/hx8340b/8/setxy.h"
355 #endif
356 #ifndef DISABLE_HX8340B_S
357  #include "tft_drivers/hx8340b/s/setxy.h"
358 #endif
359 #ifndef DISABLE_ST7735
360  #include "tft_drivers/st7735/std/setxy.h"
361 #endif
362 #ifndef DISABLE_ST7735_ALT
363  #include "tft_drivers/st7735/alt/setxy.h"
364 #endif
365 #ifndef DISABLE_S1D19122
366  #include "tft_drivers/s1d19122/setxy.h"
367 #endif
368 #ifndef DISABLE_PCF8833
369  #include "tft_drivers/pcf8833/setxy.h"
370 #endif
371 #ifndef DISABLE_SSD1963_480
372  #include "tft_drivers/ssd1963/480/setxy.h"
373 #endif
374 #ifndef DISABLE_SSD1963_800
375  #include "tft_drivers/ssd1963/800/setxy.h"
376 #endif
377 #ifndef DISABLE_SSD1963_800_ALT
378  #include "tft_drivers/ssd1963/800alt/setxy.h"
379 #endif
380 #ifndef DISABLE_S6D1121
381  #include "tft_drivers/s6d1121/setxy.h"
382 #endif
383 #ifndef DISABLE_ILI9481
384  #include "tft_drivers/ili9481/setxy.h"
385 #endif
386 #ifndef DISABLE_S6D0164
387  #include "tft_drivers/s6d0164/setxy.h"
388 #endif
389 #ifndef DISABLE_ST7735S
390  #include "tft_drivers/st7735s/setxy.h"
391 #endif
392 #ifndef DISABLE_ILI9341_S4P
393  #include "tft_drivers/ili9341/s4p/setxy.h"
394 #endif
395 #ifndef DISABLE_ILI9341_S5P
396  #include "tft_drivers/ili9341/s5p/setxy.h"
397 #endif
398 #ifndef DISABLE_R61581
399  #include "tft_drivers/r61581/setxy.h"
400 #endif
401 #ifndef DISABLE_ILI9486
402  #include "tft_drivers/ili9486/setxy.h"
403 #endif
404 #ifndef DISABLE_CPLD
405  #include "tft_drivers/cpld/setxy.h"
406 #endif
407 #ifndef DISABLE_HX8353C
408  #include "tft_drivers/hx8353c/setxy.h"
409 #endif
410  }
411 }
412 
413 void UTFT::clrXY()
414 {
415  if (orient==PORTRAIT)
416  setXY(0,0,disp_x_size,disp_y_size);
417  else
418  setXY(0,0,disp_y_size,disp_x_size);
419 }
420 
421 void UTFT::drawRect(int x1, int y1, int x2, int y2)
422 {
423  if (x1>x2)
424  {
425  swap(int, x1, x2);
426  }
427  if (y1>y2)
428  {
429  swap(int, y1, y2);
430  }
431 
432  drawHLine(x1, y1, x2-x1);
433  drawHLine(x1, y2, x2-x1);
434  drawVLine(x1, y1, y2-y1);
435  drawVLine(x2, y1, y2-y1);
436 }
437 
438 void UTFT::drawRoundRect(int x1, int y1, int x2, int y2)
439 {
440  if (x1>x2)
441  {
442  swap(int, x1, x2);
443  }
444  if (y1>y2)
445  {
446  swap(int, y1, y2);
447  }
448  if ((x2-x1)>4 && (y2-y1)>4)
449  {
450  drawPixel(x1+1,y1+1);
451  drawPixel(x2-1,y1+1);
452  drawPixel(x1+1,y2-1);
453  drawPixel(x2-1,y2-1);
454  drawHLine(x1+2, y1, x2-x1-4);
455  drawHLine(x1+2, y2, x2-x1-4);
456  drawVLine(x1, y1+2, y2-y1-4);
457  drawVLine(x2, y1+2, y2-y1-4);
458  }
459 }
460 
461 void UTFT::fillRect(int x1, int y1, int x2, int y2)
462 {
463  if (x1>x2)
464  {
465  swap(int, x1, x2);
466  }
467  if (y1>y2)
468  {
469  swap(int, y1, y2);
470  }
471  if (display_transfer_mode==16)
472  {
473  cbi(P_CS, B_CS);
474  setXY(x1, y1, x2, y2);
475  sbi(P_RS, B_RS);
476  _fast_fill_16(fch,fcl,((long(x2-x1)+1)*(long(y2-y1)+1)));
477  sbi(P_CS, B_CS);
478  }
479  else if ((display_transfer_mode==8) and (fch==fcl))
480  {
481  cbi(P_CS, B_CS);
482  setXY(x1, y1, x2, y2);
483  sbi(P_RS, B_RS);
484  _fast_fill_8(fch,((long(x2-x1)+1)*(long(y2-y1)+1)));
485  sbi(P_CS, B_CS);
486  }
487  else
488  {
489  if (orient==PORTRAIT)
490  {
491  for (int i=0; i<((y2-y1)/2)+1; i++)
492  {
493  drawHLine(x1, y1+i, x2-x1);
494  drawHLine(x1, y2-i, x2-x1);
495  }
496  }
497  else
498  {
499  for (int i=0; i<((x2-x1)/2)+1; i++)
500  {
501  drawVLine(x1+i, y1, y2-y1);
502  drawVLine(x2-i, y1, y2-y1);
503  }
504  }
505  }
506 }
507 
508 void UTFT::fillRoundRect(int x1, int y1, int x2, int y2)
509 {
510  if (x1>x2)
511  {
512  swap(int, x1, x2);
513  }
514  if (y1>y2)
515  {
516  swap(int, y1, y2);
517  }
518 
519  if ((x2-x1)>4 && (y2-y1)>4)
520  {
521  for (int i=0; i<((y2-y1)/2)+1; i++)
522  {
523  switch(i)
524  {
525  case 0:
526  drawHLine(x1+2, y1+i, x2-x1-4);
527  drawHLine(x1+2, y2-i, x2-x1-4);
528  break;
529  case 1:
530  drawHLine(x1+1, y1+i, x2-x1-2);
531  drawHLine(x1+1, y2-i, x2-x1-2);
532  break;
533  default:
534  drawHLine(x1, y1+i, x2-x1);
535  drawHLine(x1, y2-i, x2-x1);
536  }
537  }
538  }
539 }
540 
541 void UTFT::drawCircle(int x, int y, int radius)
542 {
543  int f = 1 - radius;
544  int ddF_x = 1;
545  int ddF_y = -2 * radius;
546  int x1 = 0;
547  int y1 = radius;
548 
549  cbi(P_CS, B_CS);
550  setXY(x, y + radius, x, y + radius);
551  LCD_Write_DATA(fch,fcl);
552  setXY(x, y - radius, x, y - radius);
553  LCD_Write_DATA(fch,fcl);
554  setXY(x + radius, y, x + radius, y);
555  LCD_Write_DATA(fch,fcl);
556  setXY(x - radius, y, x - radius, y);
557  LCD_Write_DATA(fch,fcl);
558 
559  while(x1 < y1)
560  {
561  if(f >= 0)
562  {
563  y1--;
564  ddF_y += 2;
565  f += ddF_y;
566  }
567  x1++;
568  ddF_x += 2;
569  f += ddF_x;
570  setXY(x + x1, y + y1, x + x1, y + y1);
571  LCD_Write_DATA(fch,fcl);
572  setXY(x - x1, y + y1, x - x1, y + y1);
573  LCD_Write_DATA(fch,fcl);
574  setXY(x + x1, y - y1, x + x1, y - y1);
575  LCD_Write_DATA(fch,fcl);
576  setXY(x - x1, y - y1, x - x1, y - y1);
577  LCD_Write_DATA(fch,fcl);
578  setXY(x + y1, y + x1, x + y1, y + x1);
579  LCD_Write_DATA(fch,fcl);
580  setXY(x - y1, y + x1, x - y1, y + x1);
581  LCD_Write_DATA(fch,fcl);
582  setXY(x + y1, y - x1, x + y1, y - x1);
583  LCD_Write_DATA(fch,fcl);
584  setXY(x - y1, y - x1, x - y1, y - x1);
585  LCD_Write_DATA(fch,fcl);
586  }
587  sbi(P_CS, B_CS);
588  clrXY();
589 }
590 
591 void UTFT::fillCircle(int x, int y, int radius)
592 {
593  for(int y1=-radius; y1<=0; y1++)
594  for(int x1=-radius; x1<=0; x1++)
595  if(x1*x1+y1*y1 <= radius*radius)
596  {
597  drawHLine(x+x1, y+y1, 2*(-x1));
598  drawHLine(x+x1, y-y1, 2*(-x1));
599  break;
600  }
601 }
602 
603 void UTFT::clrScr()
604 {
605  long i;
606 
607  cbi(P_CS, B_CS);
608  clrXY();
609  if (display_transfer_mode!=1)
610  sbi(P_RS, B_RS);
611  if (display_transfer_mode==16)
612  _fast_fill_16(0,0,((disp_x_size+1)*(disp_y_size+1)));
613  else if (display_transfer_mode==8)
614  _fast_fill_8(0,((disp_x_size+1)*(disp_y_size+1)));
615  else
616  {
617  for (i=0; i<((disp_x_size+1)*(disp_y_size+1)); i++)
618  {
619  if (display_transfer_mode!=1)
620  LCD_Writ_Bus(0,0,display_transfer_mode);
621  else
622  {
623  LCD_Writ_Bus(1,0,display_transfer_mode);
624  LCD_Writ_Bus(1,0,display_transfer_mode);
625  }
626  }
627  }
628  sbi(P_CS, B_CS);
629 }
630 
631 void UTFT::fillScr(byte r, byte g, byte b)
632 {
633  word color = ((r&248)<<8 | (g&252)<<3 | (b&248)>>3);
634  fillScr(color);
635 }
636 
637 void UTFT::fillScr(word color)
638 {
639  long i;
640  char ch, cl;
641 
642  ch=byte(color>>8);
643  cl=byte(color & 0xFF);
644 
645  cbi(P_CS, B_CS);
646  clrXY();
647  if (display_transfer_mode!=1)
648  sbi(P_RS, B_RS);
649  if (display_transfer_mode==16)
650  _fast_fill_16(ch,cl,((disp_x_size+1)*(disp_y_size+1)));
651  else if ((display_transfer_mode==8) and (ch==cl))
652  _fast_fill_8(ch,((disp_x_size+1)*(disp_y_size+1)));
653  else
654  {
655  for (i=0; i<((disp_x_size+1)*(disp_y_size+1)); i++)
656  {
657  if (display_transfer_mode!=1)
658  LCD_Writ_Bus(ch,cl,display_transfer_mode);
659  else
660  {
661  LCD_Writ_Bus(1,ch,display_transfer_mode);
662  LCD_Writ_Bus(1,cl,display_transfer_mode);
663  }
664  }
665  }
666  sbi(P_CS, B_CS);
667 }
668 
669 void UTFT::setColor(byte r, byte g, byte b)
670 {
671  fch=((r&248)|g>>5);
672  fcl=((g&28)<<3|b>>3);
673 }
674 
675 void UTFT::setColor(word color)
676 {
677  fch=byte(color>>8);
678  fcl=byte(color & 0xFF);
679 }
680 
681 word UTFT::getColor()
682 {
683  return (fch<<8) | fcl;
684 }
685 
686 void UTFT::setBackColor(byte r, byte g, byte b)
687 {
688  bch=((r&248)|g>>5);
689  bcl=((g&28)<<3|b>>3);
690  _transparent=false;
691 }
692 
693 void UTFT::setBackColor(uint32_t color)
694 {
695  if (color==VGA_TRANSPARENT)
696  _transparent=true;
697  else
698  {
699  bch=byte(color>>8);
700  bcl=byte(color & 0xFF);
701  _transparent=false;
702  }
703 }
704 
705 word UTFT::getBackColor()
706 {
707  return (bch<<8) | bcl;
708 }
709 
710 void UTFT::setPixel(word color)
711 {
712  LCD_Write_DATA((color>>8),(color&0xFF)); // rrrrrggggggbbbbb
713 }
714 
715 void UTFT::drawPixel(int x, int y)
716 {
717  cbi(P_CS, B_CS);
718  setXY(x, y, x, y);
719  setPixel((fch<<8)|fcl);
720  sbi(P_CS, B_CS);
721  clrXY();
722 }
723 
724 void UTFT::drawLine(int x1, int y1, int x2, int y2)
725 {
726  if (y1==y2)
727  drawHLine(x1, y1, x2-x1);
728  else if (x1==x2)
729  drawVLine(x1, y1, y2-y1);
730  else
731  {
732  unsigned int dx = (x2 > x1 ? x2 - x1 : x1 - x2);
733  short xstep = x2 > x1 ? 1 : -1;
734  unsigned int dy = (y2 > y1 ? y2 - y1 : y1 - y2);
735  short ystep = y2 > y1 ? 1 : -1;
736  int col = x1, row = y1;
737 
738  cbi(P_CS, B_CS);
739  if (dx < dy)
740  {
741  int t = - (dy >> 1);
742  while (true)
743  {
744  setXY (col, row, col, row);
745  LCD_Write_DATA (fch, fcl);
746  if (row == y2)
747  return;
748  row += ystep;
749  t += dx;
750  if (t >= 0)
751  {
752  col += xstep;
753  t -= dy;
754  }
755  }
756  }
757  else
758  {
759  int t = - (dx >> 1);
760  while (true)
761  {
762  setXY (col, row, col, row);
763  LCD_Write_DATA (fch, fcl);
764  if (col == x2)
765  return;
766  col += xstep;
767  t += dy;
768  if (t >= 0)
769  {
770  row += ystep;
771  t -= dx;
772  }
773  }
774  }
775  sbi(P_CS, B_CS);
776  }
777  clrXY();
778 }
779 
780 void UTFT::drawHLine(int x, int y, int l)
781 {
782  if (l<0)
783  {
784  l = -l;
785  x -= l;
786  }
787  cbi(P_CS, B_CS);
788  setXY(x, y, x+l, y);
789  if (display_transfer_mode == 16)
790  {
791  sbi(P_RS, B_RS);
792  _fast_fill_16(fch,fcl,l);
793  }
794  else if ((display_transfer_mode==8) and (fch==fcl))
795  {
796  sbi(P_RS, B_RS);
797  _fast_fill_8(fch,l);
798  }
799  else
800  {
801  for (int i=0; i<l+1; i++)
802  {
803  LCD_Write_DATA(fch, fcl);
804  }
805  }
806  sbi(P_CS, B_CS);
807  clrXY();
808 }
809 
810 void UTFT::drawVLine(int x, int y, int l)
811 {
812  if (l<0)
813  {
814  l = -l;
815  y -= l;
816  }
817  cbi(P_CS, B_CS);
818  setXY(x, y, x, y+l);
819  if (display_transfer_mode == 16)
820  {
821  sbi(P_RS, B_RS);
822  _fast_fill_16(fch,fcl,l);
823  }
824  else if ((display_transfer_mode==8) and (fch==fcl))
825  {
826  sbi(P_RS, B_RS);
827  _fast_fill_8(fch,l);
828  }
829  else
830  {
831  for (int i=0; i<l+1; i++)
832  {
833  LCD_Write_DATA(fch, fcl);
834  }
835  }
836  sbi(P_CS, B_CS);
837  clrXY();
838 }
839 
840 void UTFT::printChar(byte c, int x, int y)
841 {
842  byte i,ch;
843  word j;
844  word temp;
845 
846  cbi(P_CS, B_CS);
847 
848  if (!_transparent)
849  {
850  if (orient==PORTRAIT)
851  {
852  setXY(x,y,x+cfont.x_size-1,y+cfont.y_size-1);
853 
854  temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
855  for(j=0;j<((cfont.x_size/8)*cfont.y_size);j++)
856  {
857  ch=pgm_read_byte(&cfont.font[temp]);
858  for(i=0;i<8;i++)
859  {
860  if((ch&(1<<(7-i)))!=0)
861  {
862  setPixel((fch<<8)|fcl);
863  }
864  else
865  {
866  setPixel((bch<<8)|bcl);
867  }
868  }
869  temp++;
870  }
871  }
872  else
873  {
874  temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
875 
876  for(j=0;j<((cfont.x_size/8)*cfont.y_size);j+=(cfont.x_size/8))
877  {
878  setXY(x,y+(j/(cfont.x_size/8)),x+cfont.x_size-1,y+(j/(cfont.x_size/8)));
879  for (int zz=(cfont.x_size/8)-1; zz>=0; zz--)
880  {
881  ch=pgm_read_byte(&cfont.font[temp+zz]);
882  for(i=0;i<8;i++)
883  {
884  if((ch&(1<<i))!=0)
885  {
886  setPixel((fch<<8)|fcl);
887  }
888  else
889  {
890  setPixel((bch<<8)|bcl);
891  }
892  }
893  }
894  temp+=(cfont.x_size/8);
895  }
896  }
897  }
898  else
899  {
900  temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
901  for(j=0;j<cfont.y_size;j++)
902  {
903  for (int zz=0; zz<(cfont.x_size/8); zz++)
904  {
905  ch=pgm_read_byte(&cfont.font[temp+zz]);
906  for(i=0;i<8;i++)
907  {
908 
909  if((ch&(1<<(7-i)))!=0)
910  {
911  setXY(x+i+(zz*8),y+j,x+i+(zz*8)+1,y+j+1);
912  setPixel((fch<<8)|fcl);
913  }
914  }
915  }
916  temp+=(cfont.x_size/8);
917  }
918  }
919 
920  sbi(P_CS, B_CS);
921  clrXY();
922 }
923 
924 void UTFT::rotateChar(byte c, int x, int y, int pos, int deg)
925 {
926  byte i,j,ch;
927  word temp;
928  int newx,newy;
929  double radian;
930  radian=deg*0.0175;
931 
932  cbi(P_CS, B_CS);
933 
934  temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
935  for(j=0;j<cfont.y_size;j++)
936  {
937  for (int zz=0; zz<(cfont.x_size/8); zz++)
938  {
939  ch=pgm_read_byte(&cfont.font[temp+zz]);
940  for(i=0;i<8;i++)
941  {
942  newx=x+(((i+(zz*8)+(pos*cfont.x_size))*cos(radian))-((j)*sin(radian)));
943  newy=y+(((j)*cos(radian))+((i+(zz*8)+(pos*cfont.x_size))*sin(radian)));
944 
945  setXY(newx,newy,newx+1,newy+1);
946 
947  if((ch&(1<<(7-i)))!=0)
948  {
949  setPixel((fch<<8)|fcl);
950  }
951  else
952  {
953  if (!_transparent)
954  setPixel((bch<<8)|bcl);
955  }
956  }
957  }
958  temp+=(cfont.x_size/8);
959  }
960  sbi(P_CS, B_CS);
961  clrXY();
962 }
963 
964 void UTFT::print(char *st, int x, int y, int deg)
965 {
966  int stl, i;
967 
968  stl = strlen(st);
969 
970  if (orient==PORTRAIT)
971  {
972  if (x==RIGHT)
973  x=(disp_x_size+1)-(stl*cfont.x_size);
974  if (x==CENTER)
975  x=((disp_x_size+1)-(stl*cfont.x_size))/2;
976  }
977  else
978  {
979  if (x==RIGHT)
980  x=(disp_y_size+1)-(stl*cfont.x_size);
981  if (x==CENTER)
982  x=((disp_y_size+1)-(stl*cfont.x_size))/2;
983  }
984 
985  for (i=0; i<stl; i++)
986  if (deg==0)
987  printChar(*st++, x + (i*(cfont.x_size)), y);
988  else
989  rotateChar(*st++, x, y, i, deg);
990 }
991 
992 void UTFT::print(String st, int x, int y, int deg)
993 {
994  char buf[st.length()+1];
995 
996  st.toCharArray(buf, st.length()+1);
997  print(buf, x, y, deg);
998 }
999 
1000 void UTFT::printNumI(long num, int x, int y, int length, char filler)
1001 {
1002  char buf[25];
1003  char st[27];
1004  boolean neg=false;
1005  int c=0, f=0;
1006 
1007  if (num==0)
1008  {
1009  if (length!=0)
1010  {
1011  for (c=0; c<(length-1); c++)
1012  st[c]=filler;
1013  st[c]=48;
1014  st[c+1]=0;
1015  }
1016  else
1017  {
1018  st[0]=48;
1019  st[1]=0;
1020  }
1021  }
1022  else
1023  {
1024  if (num<0)
1025  {
1026  neg=true;
1027  num=-num;
1028  }
1029 
1030  while (num>0)
1031  {
1032  buf[c]=48+(num % 10);
1033  c++;
1034  num=(num-(num % 10))/10;
1035  }
1036  buf[c]=0;
1037 
1038  if (neg)
1039  {
1040  st[0]=45;
1041  }
1042 
1043  if (length>(c+neg))
1044  {
1045  for (int i=0; i<(length-c-neg); i++)
1046  {
1047  st[i+neg]=filler;
1048  f++;
1049  }
1050  }
1051 
1052  for (int i=0; i<c; i++)
1053  {
1054  st[i+neg+f]=buf[c-i-1];
1055  }
1056  st[c+neg+f]=0;
1057 
1058  }
1059 
1060  print(st,x,y);
1061 }
1062 
1063 void UTFT::printNumF(double num, byte dec, int x, int y, char divider, int length, char filler)
1064 {
1065  char st[27];
1066  boolean neg=false;
1067 
1068  if (dec<1)
1069  dec=1;
1070  else if (dec>5)
1071  dec=5;
1072 
1073  if (num<0)
1074  neg = true;
1075 
1076  _convert_float(st, num, length, dec);
1077 
1078  if (divider != '.')
1079  {
1080  for (int i=0; i<sizeof(st); i++)
1081  if (st[i]=='.')
1082  st[i]=divider;
1083  }
1084 
1085  if (filler != ' ')
1086  {
1087  if (neg)
1088  {
1089  st[0]='-';
1090  for (int i=1; i<sizeof(st); i++)
1091  if ((st[i]==' ') || (st[i]=='-'))
1092  st[i]=filler;
1093  }
1094  else
1095  {
1096  for (int i=0; i<sizeof(st); i++)
1097  if (st[i]==' ')
1098  st[i]=filler;
1099  }
1100  }
1101 
1102  print(st,x,y);
1103 }
1104 
1105 void UTFT::setFont(uint8_t* font)
1106 {
1107  cfont.font=font;
1108  cfont.x_size=fontbyte(0);
1109  cfont.y_size=fontbyte(1);
1110  cfont.offset=fontbyte(2);
1111  cfont.numchars=fontbyte(3);
1112 }
1113 
1114 uint8_t* UTFT::getFont()
1115 {
1116  return cfont.font;
1117 }
1118 
1119 uint8_t UTFT::getFontXsize()
1120 {
1121  return cfont.x_size;
1122 }
1123 
1124 uint8_t UTFT::getFontYsize()
1125 {
1126  return cfont.y_size;
1127 }
1128 
1129 void UTFT::drawBitmap(int x, int y, int sx, int sy, bitmapdatatype data, int scale)
1130 {
1131  unsigned int col;
1132  int tx, ty, tc, tsx, tsy;
1133 
1134  if (scale==1)
1135  {
1136  if (orient==PORTRAIT)
1137  {
1138  cbi(P_CS, B_CS);
1139  setXY(x, y, x+sx-1, y+sy-1);
1140  for (tc=0; tc<(sx*sy); tc++)
1141  {
1142  col=pgm_read_word(&data[tc]);
1143  LCD_Write_DATA(col>>8,col & 0xff);
1144  }
1145  sbi(P_CS, B_CS);
1146  }
1147  else
1148  {
1149  cbi(P_CS, B_CS);
1150  for (ty=0; ty<sy; ty++)
1151  {
1152  setXY(x, y+ty, x+sx-1, y+ty);
1153  for (tx=sx-1; tx>=0; tx--)
1154  {
1155  col=pgm_read_word(&data[(ty*sx)+tx]);
1156  LCD_Write_DATA(col>>8,col & 0xff);
1157  }
1158  }
1159  sbi(P_CS, B_CS);
1160  }
1161  }
1162  else
1163  {
1164  if (orient==PORTRAIT)
1165  {
1166  cbi(P_CS, B_CS);
1167  for (ty=0; ty<sy; ty++)
1168  {
1169  setXY(x, y+(ty*scale), x+((sx*scale)-1), y+(ty*scale)+scale);
1170  for (tsy=0; tsy<scale; tsy++)
1171  for (tx=0; tx<sx; tx++)
1172  {
1173  col=pgm_read_word(&data[(ty*sx)+tx]);
1174  for (tsx=0; tsx<scale; tsx++)
1175  LCD_Write_DATA(col>>8,col & 0xff);
1176  }
1177  }
1178  sbi(P_CS, B_CS);
1179  }
1180  else
1181  {
1182  cbi(P_CS, B_CS);
1183  for (ty=0; ty<sy; ty++)
1184  {
1185  for (tsy=0; tsy<scale; tsy++)
1186  {
1187  setXY(x, y+(ty*scale)+tsy, x+((sx*scale)-1), y+(ty*scale)+tsy);
1188  for (tx=sx-1; tx>=0; tx--)
1189  {
1190  col=pgm_read_word(&data[(ty*sx)+tx]);
1191  for (tsx=0; tsx<scale; tsx++)
1192  LCD_Write_DATA(col>>8,col & 0xff);
1193  }
1194  }
1195  }
1196  sbi(P_CS, B_CS);
1197  }
1198  }
1199  clrXY();
1200 }
1201 
1202 void UTFT::drawBitmap(int x, int y, int sx, int sy, bitmapdatatype data, int deg, int rox, int roy)
1203 {
1204  unsigned int col;
1205  int tx, ty, newx, newy;
1206  double radian;
1207  radian=deg*0.0175;
1208 
1209  if (deg==0)
1210  drawBitmap(x, y, sx, sy, data);
1211  else
1212  {
1213  cbi(P_CS, B_CS);
1214  for (ty=0; ty<sy; ty++)
1215  for (tx=0; tx<sx; tx++)
1216  {
1217  col=pgm_read_word(&data[(ty*sx)+tx]);
1218 
1219  newx=x+rox+(((tx-rox)*cos(radian))-((ty-roy)*sin(radian)));
1220  newy=y+roy+(((ty-roy)*cos(radian))+((tx-rox)*sin(radian)));
1221 
1222  setXY(newx, newy, newx, newy);
1223  LCD_Write_DATA(col>>8,col & 0xff);
1224  }
1225  sbi(P_CS, B_CS);
1226  }
1227  clrXY();
1228 }
1229 
1230 void UTFT::lcdOff()
1231 {
1232  cbi(P_CS, B_CS);
1233  switch (display_model)
1234  {
1235  case PCF8833:
1236  LCD_Write_COM(0x28);
1237  break;
1238  case S6D1121_8:
1239  //turn OFF
1240  LCD_Write_COM_DATA(0x07,0x0000);
1241  LCD_Write_COM_DATA(0x00,0x0000);
1242  // LCD_Write_COM_DATA(0x10,0x0010); //Sleep mode
1243 
1244  //only standby mode ON : no visible difference (?)
1245  LCD_Write_COM_DATA(0x10,0x0001);
1246  break;
1247  case CPLD:
1248  LCD_Write_COM_DATA(0x01,0x0000);
1249  LCD_Write_COM(0x0F);
1250  break;
1251  }
1252  sbi(P_CS, B_CS);
1253 }
1254 
1255 void UTFT::lcdOn()
1256 {
1257  cbi(P_CS, B_CS);
1258  switch (display_model)
1259  {
1260  case PCF8833:
1261  LCD_Write_COM(0x29);
1262  break;
1263  case S6D1121_8:
1264  //turn ON
1265  LCD_Write_COM_DATA(0x00,0x0001);
1266  LCD_Write_COM_DATA(0x07,0x0023);
1267  LCD_Write_COM_DATA(0x10,0x0000);
1268  delay(30);
1269  LCD_Write_COM_DATA(0x07,0x0033);
1270  LCD_Write_COM_DATA(0x11,0x60B0);
1271  LCD_Write_COM_DATA(0x02,0x0600);
1272  LCD_Write_COM(0x22);
1273 
1274  //only standby mode OFF : no visible difference (?)
1275  //LCD_Write_COM_DATA(0x10,0x0000);
1276  break;
1277  case CPLD:
1278  LCD_Write_COM_DATA(0x01,0x0010);
1279  LCD_Write_COM(0x0F);
1280  break;
1281  }
1282  sbi(P_CS, B_CS);
1283 }
1284 
1285 void UTFT::setContrast(char c)
1286 {
1287  cbi(P_CS, B_CS);
1288  switch (display_model)
1289  {
1290  case PCF8833:
1291  if (c>64) c=64;
1292  LCD_Write_COM(0x25);
1293  LCD_Write_DATA(c);
1294  break;
1295  }
1296  sbi(P_CS, B_CS);
1297 }
1298 
1299 int UTFT::getDisplayXSize()
1300 {
1301  if (orient==PORTRAIT)
1302  return disp_x_size+1;
1303  else
1304  return disp_y_size+1;
1305 }
1306 
1307 int UTFT::getDisplayYSize()
1308 {
1309  if (orient==PORTRAIT)
1310  return disp_y_size+1;
1311  else
1312  return disp_x_size+1;
1313 }
1314 
1315 void UTFT::setBrightness(byte br)
1316 {
1317  cbi(P_CS, B_CS);
1318  switch (display_model)
1319  {
1320  case CPLD:
1321  if (br>16) br=16;
1322  LCD_Write_COM_DATA(0x01,br);
1323  LCD_Write_COM(0x0F);
1324  break;
1325  }
1326  sbi(P_CS, B_CS);
1327 }
1328 
1329 void UTFT::setDisplayPage(byte page)
1330 {
1331  cbi(P_CS, B_CS);
1332  switch (display_model)
1333  {
1334  case CPLD:
1335  if (page>7) page=7;
1336  LCD_Write_COM_DATA(0x04,page);
1337  LCD_Write_COM(0x0F);
1338  break;
1339  }
1340  sbi(P_CS, B_CS);
1341 }
1342 
1343 void UTFT::setWritePage(byte page)
1344 {
1345  cbi(P_CS, B_CS);
1346  switch (display_model)
1347  {
1348  case CPLD:
1349  if (page>7) page=7;
1350  LCD_Write_COM_DATA(0x05,page);
1351  LCD_Write_COM(0x0F);
1352  break;
1353  }
1354  sbi(P_CS, B_CS);
1355 }
void _hw_special_init()
Definition: HW_CC3200.h:2