MBED library for LCD-TFT display: https://www.waveshare.com/wiki/2.8inch_TFT_Touch_Shield

Dependents:   DiffPressureMeter

Committer:
igbt6
Date:
Tue Apr 03 20:08:18 2018 +0000
Revision:
0:b036dfb76a75
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igbt6 0:b036dfb76a75 1 #ifndef LCD_h
igbt6 0:b036dfb76a75 2 #define LCD_h
igbt6 0:b036dfb76a75 3
igbt6 0:b036dfb76a75 4 #include "mbed.h"
igbt6 0:b036dfb76a75 5 #include "system.h"
igbt6 0:b036dfb76a75 6 #include "interfaces.h"
igbt6 0:b036dfb76a75 7
igbt6 0:b036dfb76a75 8 #include "font.h"
igbt6 0:b036dfb76a75 9
igbt6 0:b036dfb76a75 10 //Basic Colors
igbt6 0:b036dfb76a75 11 #define WHITE 0xFFFF
igbt6 0:b036dfb76a75 12 #define BLACK 0x0000
igbt6 0:b036dfb76a75 13 #define BLUE 0x001F
igbt6 0:b036dfb76a75 14 #define BRED 0XF81F
igbt6 0:b036dfb76a75 15 #define GRED 0XFFE0
igbt6 0:b036dfb76a75 16 #define GBLUE 0X07FF
igbt6 0:b036dfb76a75 17 #define RED 0xF800
igbt6 0:b036dfb76a75 18 #define MAGENTA 0xF81F
igbt6 0:b036dfb76a75 19 #define GREEN 0x07E0
igbt6 0:b036dfb76a75 20 #define CYAN 0x7FFF
igbt6 0:b036dfb76a75 21 #define YELLOW 0xFFE0
igbt6 0:b036dfb76a75 22 #define BROWN 0XBC40
igbt6 0:b036dfb76a75 23 #define BRRED 0XFC07
igbt6 0:b036dfb76a75 24 #define GRAY 0X8430
igbt6 0:b036dfb76a75 25
igbt6 0:b036dfb76a75 26 #define LCD_CMD 0
igbt6 0:b036dfb76a75 27 #define LCD_DATA 1
igbt6 0:b036dfb76a75 28
igbt6 0:b036dfb76a75 29 #define FONT_1206 12
igbt6 0:b036dfb76a75 30 #define FONT_1608 16
igbt6 0:b036dfb76a75 31
igbt6 0:b036dfb76a75 32 #define LCD_WIDTH 240
igbt6 0:b036dfb76a75 33 #define LCD_HEIGHT 320
igbt6 0:b036dfb76a75 34
igbt6 0:b036dfb76a75 35 #define LCD_DC_PIN PA_8
igbt6 0:b036dfb76a75 36 #define LCD_BKL_PIN PC_7
igbt6 0:b036dfb76a75 37 #define LCD_CS_PIN PB_6
igbt6 0:b036dfb76a75 38
igbt6 0:b036dfb76a75 39 #define __LCD_CS_OUT() lcd_cs_pin(LCD_CS_PIN)
igbt6 0:b036dfb76a75 40 #define __LCD_CS_CLR() lcd_cs_pin.write(LOW)
igbt6 0:b036dfb76a75 41 #define __LCD_CS_SET() lcd_cs_pin.write(HIGH)
igbt6 0:b036dfb76a75 42
igbt6 0:b036dfb76a75 43 #define __LCD_DC_OUT() lcd_dc_pin(LCD_DC_PIN)
igbt6 0:b036dfb76a75 44 #define __LCD_DC_CLR() lcd_dc_pin.write(LOW)
igbt6 0:b036dfb76a75 45 #define __LCD_DC_SET() lcd_dc_pin.write(HIGH)
igbt6 0:b036dfb76a75 46
igbt6 0:b036dfb76a75 47 #define __LCD_BKL_OUT() lcd_bkl_pin(LCD_BKL_PIN)
igbt6 0:b036dfb76a75 48 #define __LCD_BKL_OFF() lcd_bkl_pin.write(LOW)
igbt6 0:b036dfb76a75 49 #define __LCD_BKL_ON() lcd_bkl_pin.write(HIGH)
igbt6 0:b036dfb76a75 50
igbt6 0:b036dfb76a75 51
igbt6 0:b036dfb76a75 52 #define __LCD_WRITE_BYTE(__DATA) HwInterfaces::instance()::getHwInterface<SPI__1>().handle().transfer(__DATA)
igbt6 0:b036dfb76a75 53
igbt6 0:b036dfb76a75 54
igbt6 0:b036dfb76a75 55
igbt6 0:b036dfb76a75 56
igbt6 0:b036dfb76a75 57 class LCD
igbt6 0:b036dfb76a75 58 {
igbt6 0:b036dfb76a75 59
igbt6 0:b036dfb76a75 60 public:
igbt6 0:b036dfb76a75 61
igbt6 0:b036dfb76a75 62 LCD();
igbt6 0:b036dfb76a75 63
igbt6 0:b036dfb76a75 64 void lcd_write_byte(uint8_t chByte, uint8_t chCmd)
igbt6 0:b036dfb76a75 65 {
igbt6 0:b036dfb76a75 66 if (chCmd) {
igbt6 0:b036dfb76a75 67 __LCD_DC_SET();
igbt6 0:b036dfb76a75 68 } else {
igbt6 0:b036dfb76a75 69 __LCD_DC_CLR();
igbt6 0:b036dfb76a75 70 }
igbt6 0:b036dfb76a75 71
igbt6 0:b036dfb76a75 72 __LCD_CS_CLR();
igbt6 0:b036dfb76a75 73 __LCD_WRITE_BYTE(chByte);
igbt6 0:b036dfb76a75 74 __LCD_CS_SET();
igbt6 0:b036dfb76a75 75 }
igbt6 0:b036dfb76a75 76
igbt6 0:b036dfb76a75 77 inline void lcd_write_word(uint16_t hwData)
igbt6 0:b036dfb76a75 78 {
igbt6 0:b036dfb76a75 79 __LCD_DC_SET();
igbt6 0:b036dfb76a75 80 __LCD_CS_CLR();
igbt6 0:b036dfb76a75 81 __LCD_WRITE_BYTE(hwData >> 8);
igbt6 0:b036dfb76a75 82 __LCD_WRITE_BYTE(hwData & 0xFF);
igbt6 0:b036dfb76a75 83 __LCD_CS_SET();
igbt6 0:b036dfb76a75 84 }
igbt6 0:b036dfb76a75 85
igbt6 0:b036dfb76a75 86
igbt6 0:b036dfb76a75 87 //write a word(two bytes) to the specified register of lcd.
igbt6 0:b036dfb76a75 88 //chRegister address of the register of lcd.
igbt6 0:b036dfb76a75 89 //hwValue value is written to the specified register.
igbt6 0:b036dfb76a75 90 void lcd_write_register(uint8_t chRegister, uint8_t chValue)
igbt6 0:b036dfb76a75 91 {
igbt6 0:b036dfb76a75 92 lcd_write_byte(chRegister, LCD_CMD);
igbt6 0:b036dfb76a75 93 lcd_write_byte(chValue, LCD_DATA);
igbt6 0:b036dfb76a75 94 }
igbt6 0:b036dfb76a75 95
igbt6 0:b036dfb76a75 96 //set the specified position of cursor on lcd.
igbt6 0:b036dfb76a75 97 //hwXpos specify x position
igbt6 0:b036dfb76a75 98 //hwYpos specify y position
igbt6 0:b036dfb76a75 99 void lcd_set_cursor(uint16_t hwXpos, uint16_t hwYpos)
igbt6 0:b036dfb76a75 100 {
igbt6 0:b036dfb76a75 101 if (hwXpos >= LCD_WIDTH|| hwYpos >= LCD_HEIGHT) {
igbt6 0:b036dfb76a75 102 return;
igbt6 0:b036dfb76a75 103 }
igbt6 0:b036dfb76a75 104
igbt6 0:b036dfb76a75 105 lcd_write_register(0x02, hwXpos >> 8);
igbt6 0:b036dfb76a75 106 lcd_write_register(0x03, hwXpos & 0xFF); //Column Start
igbt6 0:b036dfb76a75 107 lcd_write_register(0x06, hwYpos >> 8);
igbt6 0:b036dfb76a75 108 lcd_write_register(0x07, hwYpos & 0xFF); //Row Start
igbt6 0:b036dfb76a75 109 }
igbt6 0:b036dfb76a75 110
igbt6 0:b036dfb76a75 111 //clear the lcd with the specified color.
igbt6 0:b036dfb76a75 112 void lcd_clear_screen(uint16_t hwColor)
igbt6 0:b036dfb76a75 113 {
igbt6 0:b036dfb76a75 114 uint32_t i, wCount = LCD_WIDTH;
igbt6 0:b036dfb76a75 115
igbt6 0:b036dfb76a75 116 wCount *= LCD_HEIGHT;
igbt6 0:b036dfb76a75 117
igbt6 0:b036dfb76a75 118 lcd_set_cursor(0, 0);
igbt6 0:b036dfb76a75 119 lcd_write_byte(0x22, LCD_CMD);
igbt6 0:b036dfb76a75 120
igbt6 0:b036dfb76a75 121 __LCD_DC_SET();
igbt6 0:b036dfb76a75 122 __LCD_CS_CLR();
igbt6 0:b036dfb76a75 123 for (i = 0; i < wCount; i ++) {
igbt6 0:b036dfb76a75 124 __LCD_WRITE_BYTE(hwColor >> 8);
igbt6 0:b036dfb76a75 125 __LCD_WRITE_BYTE(hwColor & 0xFF);
igbt6 0:b036dfb76a75 126 }
igbt6 0:b036dfb76a75 127 __LCD_CS_SET();
igbt6 0:b036dfb76a75 128 }
igbt6 0:b036dfb76a75 129
igbt6 0:b036dfb76a75 130 void lcd_init ();
igbt6 0:b036dfb76a75 131 void lcd_draw_point(uint16_t hwXpos, uint16_t hwYpos, uint16_t hwColor);
igbt6 0:b036dfb76a75 132 void lcd_display_char(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 133 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 134 uint8_t chChr, //a char is display.
igbt6 0:b036dfb76a75 135 uint8_t chSize, //specify the size of the char
igbt6 0:b036dfb76a75 136 uint16_t hwColor); //specify the color of the char
igbt6 0:b036dfb76a75 137 //display a number at the specified position on lcd.
igbt6 0:b036dfb76a75 138 void lcd_display_num(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 139 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 140 uint32_t chNum, //a number is display.
igbt6 0:b036dfb76a75 141 uint8_t chLen, //length ot the number
igbt6 0:b036dfb76a75 142 uint8_t chSize, //specify the size of the number
igbt6 0:b036dfb76a75 143 uint16_t hwColor); //specify the color of the number
igbt6 0:b036dfb76a75 144 //display a string at the specified position on lcd.
igbt6 0:b036dfb76a75 145 void lcd_display_string(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 146 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 147 const uint8_t *pchString, //a pointer to string
igbt6 0:b036dfb76a75 148 uint8_t chSize, // the size of the string
igbt6 0:b036dfb76a75 149 uint16_t hwColor); // specify the color of the string
igbt6 0:b036dfb76a75 150 void lcd_draw_line(uint16_t hwXpos0, //specify x0 position.
igbt6 0:b036dfb76a75 151 uint16_t hwYpos0, //specify y0 position.
igbt6 0:b036dfb76a75 152 uint16_t hwXpos1, //specify x1 position.
igbt6 0:b036dfb76a75 153 uint16_t hwYpos1, //specify y1 position.
igbt6 0:b036dfb76a75 154 uint16_t hwColor); //specify the color of the line
igbt6 0:b036dfb76a75 155 void lcd_draw_circle(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 156 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 157 uint16_t hwRadius, //specify the radius of the circle.
igbt6 0:b036dfb76a75 158 uint16_t hwColor); //specify the color of the circle.
igbt6 0:b036dfb76a75 159 void lcd_fill_rect(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 160 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 161 uint16_t hwWidth, //specify the width of the rectangle.
igbt6 0:b036dfb76a75 162 uint16_t hwHeight, //specify the height of the rectangle.
igbt6 0:b036dfb76a75 163 uint16_t hwColor); //specify the color of rectangle.
igbt6 0:b036dfb76a75 164 void lcd_draw_v_line(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 165 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 166 uint16_t hwHeight, //specify the height of the vertical line.
igbt6 0:b036dfb76a75 167 uint16_t hwColor); //specify the color of the vertical line.
igbt6 0:b036dfb76a75 168 void lcd_draw_h_line(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 169 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 170 uint16_t hwWidth, //specify the width of the horizonal line.
igbt6 0:b036dfb76a75 171 uint16_t hwColor); //specify the color of the horizonal line.
igbt6 0:b036dfb76a75 172 void lcd_draw_rect(uint16_t hwXpos, //specify x position.
igbt6 0:b036dfb76a75 173 uint16_t hwYpos, //specify y position.
igbt6 0:b036dfb76a75 174 uint16_t hwWidth, //specify the width of the rectangle.
igbt6 0:b036dfb76a75 175 uint16_t hwHeight, //specify the height of the rectangle.
igbt6 0:b036dfb76a75 176 uint16_t hwColor); //specify the color of rectangle.
igbt6 0:b036dfb76a75 177
igbt6 0:b036dfb76a75 178 private:
igbt6 0:b036dfb76a75 179 DigitalOut lcd_cs_pin;
igbt6 0:b036dfb76a75 180 DigitalOut lcd_dc_pin;
igbt6 0:b036dfb76a75 181 DigitalOut lcd_bkl_pin;
igbt6 0:b036dfb76a75 182
igbt6 0:b036dfb76a75 183
igbt6 0:b036dfb76a75 184 };
igbt6 0:b036dfb76a75 185
igbt6 0:b036dfb76a75 186 extern LCD lcd;
igbt6 0:b036dfb76a75 187
igbt6 0:b036dfb76a75 188 #endif
igbt6 0:b036dfb76a75 189
igbt6 0:b036dfb76a75 190 /*********************************************************************************************************
igbt6 0:b036dfb76a75 191 END FILE
igbt6 0:b036dfb76a75 192 *********************************************************************************************************/
igbt6 0:b036dfb76a75 193