for TFT2P0327 aitendo.com 128*160 TFT LCD. LCD driver is S6D0151 Sumsung.
Dependents: FRDM_tocos_x2_FIXED
S6D0151_TFT.cpp@3:f6feda54df80, 2015-10-25 (annotated)
- Committer:
- king33jp
- Date:
- Sun Oct 25 10:49:35 2015 +0000
- Revision:
- 3:f6feda54df80
- Parent:
- 2:1e3ac0621559
fillrect update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
king33jp | 1:a3651128e297 | 1 | /* mbed library for 128*160 pixel display TFT based on S6D0151 LCD Controller |
king33jp | 1:a3651128e297 | 2 | * |
king33jp | 1:a3651128e297 | 3 | */ |
king33jp | 0:de7db46990d0 | 4 | |
king33jp | 0:de7db46990d0 | 5 | #include "S6D0151_TFT.h" |
king33jp | 0:de7db46990d0 | 6 | #include "mbed.h" |
king33jp | 0:de7db46990d0 | 7 | |
king33jp | 0:de7db46990d0 | 8 | #define BPP 16 // Bits per pixel |
king33jp | 0:de7db46990d0 | 9 | |
king33jp | 0:de7db46990d0 | 10 | S6D0151_TFT::S6D0151_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name) |
king33jp | 0:de7db46990d0 | 11 | : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) { |
king33jp | 0:de7db46990d0 | 12 | tft_reset(); |
king33jp | 0:de7db46990d0 | 13 | orientation = 2; |
king33jp | 0:de7db46990d0 | 14 | char_x = 0; |
king33jp | 0:de7db46990d0 | 15 | } |
king33jp | 0:de7db46990d0 | 16 | |
king33jp | 0:de7db46990d0 | 17 | |
king33jp | 0:de7db46990d0 | 18 | int S6D0151_TFT::width() { |
king33jp | 0:de7db46990d0 | 19 | if (orientation == 0 || orientation == 2) return 128; |
king33jp | 0:de7db46990d0 | 20 | else return 160; |
king33jp | 0:de7db46990d0 | 21 | } |
king33jp | 0:de7db46990d0 | 22 | |
king33jp | 0:de7db46990d0 | 23 | |
king33jp | 0:de7db46990d0 | 24 | int S6D0151_TFT::height() { |
king33jp | 0:de7db46990d0 | 25 | if (orientation == 0 || orientation == 2) return 160; |
king33jp | 0:de7db46990d0 | 26 | else return 128; |
king33jp | 0:de7db46990d0 | 27 | } |
king33jp | 0:de7db46990d0 | 28 | |
king33jp | 0:de7db46990d0 | 29 | |
king33jp | 0:de7db46990d0 | 30 | void S6D0151_TFT::set_orientation(unsigned int o) { |
king33jp | 0:de7db46990d0 | 31 | orientation = o; |
king33jp | 1:a3651128e297 | 32 | /* I do NOT understand rotation methods. */ |
king33jp | 0:de7db46990d0 | 33 | #if 0 |
king33jp | 0:de7db46990d0 | 34 | switch (orientation) { |
king33jp | 0:de7db46990d0 | 35 | case 0: |
king33jp | 0:de7db46990d0 | 36 | wr_reg(ST7735_MADCTL, 0x0000); |
king33jp | 0:de7db46990d0 | 37 | break; |
king33jp | 0:de7db46990d0 | 38 | case 1: |
king33jp | 0:de7db46990d0 | 39 | wr_reg(ST7735_MADCTL, 0x0060); |
king33jp | 0:de7db46990d0 | 40 | break; |
king33jp | 0:de7db46990d0 | 41 | case 2: |
king33jp | 0:de7db46990d0 | 42 | wr_reg(ST7735_MADCTL, 0x00C0); |
king33jp | 0:de7db46990d0 | 43 | break; |
king33jp | 0:de7db46990d0 | 44 | case 3: |
king33jp | 0:de7db46990d0 | 45 | wr_reg(ST7735_MADCTL, 0x00A0); |
king33jp | 0:de7db46990d0 | 46 | break; |
king33jp | 0:de7db46990d0 | 47 | } |
king33jp | 0:de7db46990d0 | 48 | #endif |
king33jp | 0:de7db46990d0 | 49 | } |
king33jp | 0:de7db46990d0 | 50 | |
king33jp | 0:de7db46990d0 | 51 | void S6D0151_TFT::wr_dat_start(void) { |
king33jp | 0:de7db46990d0 | 52 | // _rs = 1; // rs high, cs low for transmitting data |
king33jp | 0:de7db46990d0 | 53 | _cs = 0; |
king33jp | 0:de7db46990d0 | 54 | _spi.write( 0x72 ); // Start byte |
king33jp | 0:de7db46990d0 | 55 | } |
king33jp | 0:de7db46990d0 | 56 | |
king33jp | 0:de7db46990d0 | 57 | |
king33jp | 0:de7db46990d0 | 58 | |
king33jp | 0:de7db46990d0 | 59 | void S6D0151_TFT::wr_dat_stop (void) { |
king33jp | 0:de7db46990d0 | 60 | _cs = 1; |
king33jp | 0:de7db46990d0 | 61 | } |
king33jp | 0:de7db46990d0 | 62 | |
king33jp | 0:de7db46990d0 | 63 | |
king33jp | 0:de7db46990d0 | 64 | void S6D0151_TFT::set_start_address(int left,int top){ |
king33jp | 0:de7db46990d0 | 65 | unsigned short addr; |
king33jp | 0:de7db46990d0 | 66 | addr = (((top&0xff)<<8)|(left&0xff)); |
king33jp | 0:de7db46990d0 | 67 | |
king33jp | 0:de7db46990d0 | 68 | regwr( 0x0021 , addr ); |
king33jp | 0:de7db46990d0 | 69 | } |
king33jp | 0:de7db46990d0 | 70 | |
king33jp | 2:1e3ac0621559 | 71 | void S6D0151_TFT::reverse(int rev){ |
king33jp | 2:1e3ac0621559 | 72 | if(rev == TFT_REVERSE_ON){ |
king33jp | 2:1e3ac0621559 | 73 | regwr(0x0007,0x0033); //DISPLAY CONTROL (R07h) |
king33jp | 2:1e3ac0621559 | 74 | } else { |
king33jp | 2:1e3ac0621559 | 75 | regwr(0x0007,0x0037); //DISPLAY CONTROL (R07h) |
king33jp | 2:1e3ac0621559 | 76 | } |
king33jp | 2:1e3ac0621559 | 77 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 2:1e3ac0621559 | 78 | //GON=1,DTE=1 VGH/VGL Normal operation |
king33jp | 2:1e3ac0621559 | 79 | //Rev bit mean Color reverse. |
king33jp | 2:1e3ac0621559 | 80 | //REV=1 Displays all character and graphics display sections with Reverse |
king33jp | 2:1e3ac0621559 | 81 | //D[1:0]=11 Display is RAM data |
king33jp | 2:1e3ac0621559 | 82 | } |
king33jp | 0:de7db46990d0 | 83 | |
king33jp | 0:de7db46990d0 | 84 | /** |
king33jp | 0:de7db46990d0 | 85 | */ |
king33jp | 0:de7db46990d0 | 86 | void S6D0151_TFT::regwr(unsigned short reg,unsigned short dat){ |
king33jp | 0:de7db46990d0 | 87 | int data; |
king33jp | 0:de7db46990d0 | 88 | _cs = 0; |
king33jp | 0:de7db46990d0 | 89 | _spi.write(0x70); // RW=0,ID=0,RS=0(Index Register write) |
king33jp | 0:de7db46990d0 | 90 | data = (reg>>8)&0x00ff; |
king33jp | 0:de7db46990d0 | 91 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 92 | data = (reg )&0x00ff; |
king33jp | 0:de7db46990d0 | 93 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 94 | _cs = 1; |
king33jp | 1:a3651128e297 | 95 | wait_us(1); // cs=high Time has not been defined. |
king33jp | 0:de7db46990d0 | 96 | _cs = 0; |
king33jp | 0:de7db46990d0 | 97 | _spi.write(0x72); // RW=0,ID=0,RS=1(data write) |
king33jp | 0:de7db46990d0 | 98 | data = (dat>>8)&0x00ff; |
king33jp | 0:de7db46990d0 | 99 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 100 | data = (dat )&0x00ff; |
king33jp | 0:de7db46990d0 | 101 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 102 | _cs = 1; |
king33jp | 0:de7db46990d0 | 103 | } |
king33jp | 0:de7db46990d0 | 104 | void S6D0151_TFT::reg0(unsigned short reg){ |
king33jp | 0:de7db46990d0 | 105 | int data; |
king33jp | 0:de7db46990d0 | 106 | _cs = 0; |
king33jp | 0:de7db46990d0 | 107 | _spi.write(0x70); // RW=0,ID=0,RS=0(Index Register write) |
king33jp | 0:de7db46990d0 | 108 | data = (reg>>8)&0x00ff; |
king33jp | 0:de7db46990d0 | 109 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 110 | data = (reg )&0x00ff; |
king33jp | 0:de7db46990d0 | 111 | _spi.write( data ); |
king33jp | 0:de7db46990d0 | 112 | _cs = 1; |
king33jp | 0:de7db46990d0 | 113 | } |
king33jp | 0:de7db46990d0 | 114 | |
king33jp | 0:de7db46990d0 | 115 | void S6D0151_TFT::tft_reset() { |
king33jp | 0:de7db46990d0 | 116 | // init SPI |
king33jp | 0:de7db46990d0 | 117 | _spi.format(8,3); // 8 bit spi mode 3 |
king33jp | 1:a3651128e297 | 118 | _spi.frequency(6000000); // 6Mhz SPI clock ... 10Mhz is maximum for display |
king33jp | 0:de7db46990d0 | 119 | |
king33jp | 0:de7db46990d0 | 120 | _cs = 0; |
king33jp | 1:a3651128e297 | 121 | _reset = 1; |
king33jp | 0:de7db46990d0 | 122 | wait_ms(500); |
king33jp | 0:de7db46990d0 | 123 | _reset = 0; // reset |
king33jp | 0:de7db46990d0 | 124 | wait_ms(500); |
king33jp | 1:a3651128e297 | 125 | _reset = 1; |
king33jp | 0:de7db46990d0 | 126 | wait_ms(500); |
king33jp | 0:de7db46990d0 | 127 | |
king33jp | 0:de7db46990d0 | 128 | /* Start Initial Sequence ----------------------------------------------------*/ |
king33jp | 1:a3651128e297 | 129 | /* follow code is aitendo demo-code...........................................*/ |
king33jp | 1:a3651128e297 | 130 | /* I have changed the part in the demo code to reference----------------------*/ |
king33jp | 0:de7db46990d0 | 131 | regwr(0x0007,0x0020); //DISPLAY CONTROL (R07h) |
king33jp | 0:de7db46990d0 | 132 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 0:de7db46990d0 | 133 | //PT[1:0]=00 V63/V0 Normal Drive |
king33jp | 0:de7db46990d0 | 134 | //SPT=0 Split Screen Driving Function is Not performed |
king33jp | 0:de7db46990d0 | 135 | //GON=1,DTE=0 Normal operation |
king33jp | 0:de7db46990d0 | 136 | //CL=0 262,144 colors / 65,536 colors |
king33jp | 0:de7db46990d0 | 137 | //REV=0 Displays all character and graphics display sections with Normal |
king33jp | 0:de7db46990d0 | 138 | //D[1:0]=00 the internal display operation halts and the display is off. |
king33jp | 0:de7db46990d0 | 139 | |
king33jp | 0:de7db46990d0 | 140 | regwr(0x00b6,0x013f); //Module Vendor (RB6h) |
king33jp | 0:de7db46990d0 | 141 | // TEST_IN4 TEST_IN3 X X X X PSMD1(0) PSM D0(1) X X 1 1 1 1 1 1 |
king33jp | 0:de7db46990d0 | 142 | //TEST_IN4,TEST_IN3 User can know the Module Vendor through this register is accessed. |
king33jp | 0:de7db46990d0 | 143 | //PSMD[1:0]=01 5680*(1/fosc) Select the power on time delay of step-up circuit. |
king33jp | 0:de7db46990d0 | 144 | |
king33jp | 0:de7db46990d0 | 145 | regwr(0x00b4,0x0010); //MTP CONTROL (RB4h) |
king33jp | 0:de7db46990d0 | 146 | // X X X MTP_SEL X X X MTP_INIT X X X MTP_WRB X X X MTP_LOAD |
king33jp | 0:de7db46990d0 | 147 | //MTP_SEL=0 VCOMH Control Data is VCM Register |
king33jp | 0:de7db46990d0 | 148 | //MTP_INIT=0 |
king33jp | 0:de7db46990d0 | 149 | //MTP_WRB=1 Cannot write MTP data |
king33jp | 0:de7db46990d0 | 150 | //MTP_LOAD=0 |
king33jp | 0:de7db46990d0 | 151 | |
king33jp | 0:de7db46990d0 | 152 | regwr(0x0012,0x00b2); //POWER CONTROL 2 (R12h) |
king33jp | 0:de7db46990d0 | 153 | // X X X X X X X X SVC3 SVC2 SVC1 SVC0 X 0 VRH5 VRH4 |
king33jp | 0:de7db46990d0 | 154 | //SVC Adjust reference voltage of AVDD, VGH, VGL and VCL |
king33jp | 0:de7db46990d0 | 155 | // SVC[3:0]=1011 VCI1=2.76V |
king33jp | 0:de7db46990d0 | 156 | |
king33jp | 0:de7db46990d0 | 157 | regwr(0x0013,0x080e); //POWER CONTROL 3 (R13h) |
king33jp | 0:de7db46990d0 | 158 | // X X X X VCMR X X X X X X PON VRH3 VRH2 VRH1 VRH0 |
king33jp | 0:de7db46990d0 | 159 | //PON=0 The operational amplifier is stop. |
king33jp | 0:de7db46990d0 | 160 | //VCMR=1 VCOMH voltage=Internal electronic volume |
king33jp | 0:de7db46990d0 | 161 | //VRH[5:0]=1e=011110 VCIR_EXIN X 2.250 = 4.50V |
king33jp | 0:de7db46990d0 | 162 | |
king33jp | 0:de7db46990d0 | 163 | regwr(0x0014,0x5bca); //POWER CONTROL 4 (R14h) |
king33jp | 0:de7db46990d0 | 164 | // X VDV6 VDV5 VDV4 VDV3 VDV2 VDV1 VDV0 VCOMG VCM6 VCM5 VCM4 VCM3 VCM2 VCM1 VCM0 |
king33jp | 0:de7db46990d0 | 165 | //VDV Set the alternating amplitudes of Vcom at the Vcom alternating drive. |
king33jp | 0:de7db46990d0 | 166 | // VDV[6:0]=5b=1011011 GVDD x 0.984 |
king33jp | 0:de7db46990d0 | 167 | //VCOMG=1 VcomL voltage can output to negative voltage. |
king33jp | 0:de7db46990d0 | 168 | //Set the VcomH voltage (a high-level voltage at the Vcom alternating drive). |
king33jp | 0:de7db46990d0 | 169 | // VCM[6:0]=1001010 GVDD x 0.8085 |
king33jp | 0:de7db46990d0 | 170 | |
king33jp | 0:de7db46990d0 | 171 | regwr(0x0061,0x0018); //OSCILLATOR CONTROL (R61h) |
king33jp | 0:de7db46990d0 | 172 | // X X X X X X X X X X X RADJ4 RADJ3 RADJ2 RADJ1 RADJ0 |
king33jp | 0:de7db46990d0 | 173 | //RADJ[4:0]=11000 Oscillation Speed=x 1.000 Default |
king33jp | 0:de7db46990d0 | 174 | |
king33jp | 0:de7db46990d0 | 175 | regwr(0x0010,0x190c); //POWER CONTROL 1 (R10h) |
king33jp | 0:de7db46990d0 | 176 | // DSTB X SAP2 SAP1 SAP0 BT2 BT1 BT0 DC2 DC1 DC0 AP2 AP1 AP0 SLP STB |
king33jp | 0:de7db46990d0 | 177 | //DSTB=0 Noraml |
king33jp | 0:de7db46990d0 | 178 | //Adjust the slew-rate of the operational amplifier for the source driver. |
king33jp | 0:de7db46990d0 | 179 | // SAP[2:0]=011 Medium Medium |
king33jp | 0:de7db46990d0 | 180 | //BT[2:0]=001 VGH=AVDDx3 VGL=-(AVDDx2) VGH = Vci1 X six times |
king33jp | 0:de7db46990d0 | 181 | //DC[2:0]=000 AVDD,VCL=DCCLK/1 VGH,VGL=DCCLK/2 |
king33jp | 0:de7db46990d0 | 182 | //AP[2:0]=011 Amount of Current in Operational Amplifier=Medium |
king33jp | 0:de7db46990d0 | 183 | //SLP=0 setting Sleep. Normal mode |
king33jp | 0:de7db46990d0 | 184 | //STB=0 setting standby. normal mode |
king33jp | 0:de7db46990d0 | 185 | wait_ms(80); |
king33jp | 0:de7db46990d0 | 186 | |
king33jp | 0:de7db46990d0 | 187 | regwr(0x0013,0x081e); //POWER CONTROL 3 (R13h) |
king33jp | 0:de7db46990d0 | 188 | // X X X X VCMR X X X X X X PON VRH3 VRH2 VRH1 VRH0 |
king33jp | 0:de7db46990d0 | 189 | //PON=1 The operational amplifier is Start. |
king33jp | 0:de7db46990d0 | 190 | //VCMR=1 VCOMH voltage=Internal electronic volume |
king33jp | 0:de7db46990d0 | 191 | //VRH[5:0]=1e=011110 VCIR_EXIN X 2.250 = 4.50V |
king33jp | 0:de7db46990d0 | 192 | wait_ms(20); |
king33jp | 0:de7db46990d0 | 193 | |
king33jp | 0:de7db46990d0 | 194 | regwr(0x0001,0x0014); //DRIVER OUTPUT CONTROL (R01h) |
king33jp | 0:de7db46990d0 | 195 | // X X X DPL EPL SM GS SS X X X NL4 NL3 NL2 NL1 NL0 |
king33jp | 1:a3651128e297 | 196 | // demo code was 0x0114,SS bit=1 is reverse horizontal. |
king33jp | 0:de7db46990d0 | 197 | //DPL=0,EPL=0 |
king33jp | 0:de7db46990d0 | 198 | //SM=0 even/odd division is selected |
king33jp | 0:de7db46990d0 | 199 | //GS=0 G1 is output first and G160 is finally output. |
king33jp | 0:de7db46990d0 | 200 | //SS=1 Select the direction of the source driver channel in pixel unit. |
king33jp | 0:de7db46990d0 | 201 | //NL[4:0]=10100 Drive Duty=384 X 160 dots 160 G1 to G160 |
king33jp | 0:de7db46990d0 | 202 | |
king33jp | 0:de7db46990d0 | 203 | regwr(0x0002,0x0100); //LCD INVERSION CONTROL (R02h) |
king33jp | 0:de7db46990d0 | 204 | // X X X X X X FL1 FL0 X X X FLD X X X X |
king33jp | 0:de7db46990d0 | 205 | //FL[1:0]=01 FLD=0 Line Inversion-1 field interlace |
king33jp | 0:de7db46990d0 | 206 | |
king33jp | 0:de7db46990d0 | 207 | regwr(0x0003,0x0030); //ENTRY MODE (R03h) |
king33jp | 0:de7db46990d0 | 208 | // X X X BGR X X MDT1 MDT0 X X ID1 ID0 AM X X X |
king33jp | 0:de7db46990d0 | 209 | //BGR=0 assigned to {R, G, B}.When 18-bit data is written to GRAM through DB bus |
king33jp | 0:de7db46990d0 | 210 | //MDT[1:0]=00 260k color data is transferred by 3or2-times(setting for IM) Data Transfer. |
king33jp | 0:de7db46990d0 | 211 | // When user wants to transfer 260k color data on 8/16-bit parallel bus |
king33jp | 0:de7db46990d0 | 212 | //ID[1:0]=11 When ID[1], ID[0] = 1, the address counter (AC) is automatically increased by 1 after the data is written to the GRAM |
king33jp | 0:de7db46990d0 | 213 | //AM=0 the data is continuously written in horizontally. |
king33jp | 0:de7db46990d0 | 214 | |
king33jp | 0:de7db46990d0 | 215 | regwr(0x0008,0x0202); //BLANK PERIOD CONTROL 1 (R08h) |
king33jp | 0:de7db46990d0 | 216 | // X X X X FP3 FP2 FP1 FP0 X X X X BP3 BP2 BP1 BP0 |
king33jp | 0:de7db46990d0 | 217 | //FP[3:0]=0010 |
king33jp | 0:de7db46990d0 | 218 | //BP[3:0]=0010 |
king33jp | 0:de7db46990d0 | 219 | //Number of Raster Periods In Front (Back) Porch Default=2; |
king33jp | 0:de7db46990d0 | 220 | |
king33jp | 0:de7db46990d0 | 221 | regwr(0x000b,0x0000); //FRAME CYCLE CONTROL (R0Bh) |
king33jp | 0:de7db46990d0 | 222 | // X X X X X X DIV1 DIV0 X X X X RTN3 RTN2 RTN1 RTN0 |
king33jp | 0:de7db46990d0 | 223 | //DIV[1:0]=00 Division Ratio=1 Internal operation clock frequency=fosc/1 |
king33jp | 0:de7db46990d0 | 224 | //RTN[3:0]=Clock Cycles per horizontal Line= 16 (INCLKs) |
king33jp | 0:de7db46990d0 | 225 | |
king33jp | 0:de7db46990d0 | 226 | regwr(0x000c,0x0000); //EXTERNAL DISPLAY INTERFACE CONTROL (R0Ch) |
king33jp | 0:de7db46990d0 | 227 | // X X X X X X X RM X X DM1 DM0 X X RIM1 RIM0 |
king33jp | 0:de7db46990d0 | 228 | //RM=0 GRAM Access Interface=System interface |
king33jp | 0:de7db46990d0 | 229 | //DM[1:0]=00 Display operation mode = Internal clock operation |
king33jp | 0:de7db46990d0 | 230 | |
king33jp | 0:de7db46990d0 | 231 | regwr(0x0061,0x0018); //OSCILLATOR CONTROL (R61h) |
king33jp | 0:de7db46990d0 | 232 | // X X X X X X X X X X X RADJ4 RADJ3 RADJ2 RADJ1 RADJ0 |
king33jp | 0:de7db46990d0 | 233 | //RADJ[4:0]=11000 Oscillation Speed=x 1.000 Default |
king33jp | 0:de7db46990d0 | 234 | |
king33jp | 0:de7db46990d0 | 235 | regwr(0x0069,0x0000); //DC/DC CONVERT LOW POWER MODE SETTING (R69h) |
king33jp | 0:de7db46990d0 | 236 | // 0 0 0 0 0 0 0 0 0 0 0 NLDC3 NLDC2 NLDC1 NLDC0 NLPM |
king33jp | 0:de7db46990d0 | 237 | //NLPM=0 Normal operation mode |
king33jp | 0:de7db46990d0 | 238 | //NLDC[1:0]=00 DCCLK/1 |
king33jp | 0:de7db46990d0 | 239 | //NLDC[3:2]=00 DCCLK/2 |
king33jp | 0:de7db46990d0 | 240 | |
king33jp | 0:de7db46990d0 | 241 | regwr(0x0070,0x0000); //SOURCE DRIVER PRE-DRIVING PERIOD SETTING (R70h) |
king33jp | 0:de7db46990d0 | 242 | // X X X X X X X X SDT1 SDT0 X X X X EQ1 EQ0 |
king33jp | 0:de7db46990d0 | 243 | //STD[1:0]=00 Source Output Delay Control=1 DISP_CK |
king33jp | 0:de7db46990d0 | 244 | //EQ=00 No Equalization Control |
king33jp | 0:de7db46990d0 | 245 | regwr(0x0071,0x0000); //GATE OUTPUT PERIOD CONTROL (R71h) |
king33jp | 0:de7db46990d0 | 246 | // X X X X GNO1 GNO0 X X X X X X X X X X |
king33jp | 0:de7db46990d0 | 247 | //GN)=00 Non-Overlap Period Control =2 DISP_CKs |
king33jp | 0:de7db46990d0 | 248 | |
king33jp | 0:de7db46990d0 | 249 | regwr(0x0011,0x0000); //GAMMA CONTROL 1 (R11h) |
king33jp | 0:de7db46990d0 | 250 | // VR1C X X VRN14 VRN13 VRN12 VRN11 VRN10 X X X VRP14 VRP13 VRP12 VRP11 VRP10 |
king33jp | 0:de7db46990d0 | 251 | //VR1C=0 Control step of amplitude positive and negative of 64-grayscale. |
king33jp | 0:de7db46990d0 | 252 | //VRP1[4:0]=00000 Control amplitude positive polarity of 64-grayscale. |
king33jp | 0:de7db46990d0 | 253 | //VRN1[4:0]=00000 Control amplitude negative polarity of 64-grayscale. |
king33jp | 0:de7db46990d0 | 254 | |
king33jp | 0:de7db46990d0 | 255 | /* Gamma settings -----------------------------------------------------------*/ |
king33jp | 0:de7db46990d0 | 256 | ///////r CONTROL |
king33jp | 0:de7db46990d0 | 257 | regwr(0x0030,0x0303); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 258 | regwr(0x0031,0x0303); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 259 | regwr(0x0032,0x0303); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 260 | regwr(0x0033,0x0000); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 261 | regwr(0x0034,0x0404); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 262 | regwr(0x0035,0x0404); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 263 | regwr(0x0036,0x0404); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 264 | regwr(0x0037,0x0000); //GAMMA CONTROL 2 (R30h to R37h) |
king33jp | 0:de7db46990d0 | 265 | regwr(0x0038,0x0707); //GAMMA CONTROL 2 (R38h) |
king33jp | 0:de7db46990d0 | 266 | ///////Coordinatioontrol setting |
king33jp | 0:de7db46990d0 | 267 | regwr(0x0040,0x0000); // GATE SCAN POSITION (R40h) |
king33jp | 0:de7db46990d0 | 268 | // 1 X X X X X X X X X X X SCN4 SCN3 SCN2 SCN1 SCN0 |
king33jp | 0:de7db46990d0 | 269 | //Set the scanning starting position of the gate driver. |
king33jp | 0:de7db46990d0 | 270 | |
king33jp | 0:de7db46990d0 | 271 | regwr(0x0042,0x9f00); //1st SCREEN DRIVING POSITION (R42h) |
king33jp | 0:de7db46990d0 | 272 | // SE17 SE16 SE15 SE14 SE13 SE12 SE11 SE10 SS17 SS16 SS15 SS14 SS13 SS12 SS11 SS10 |
king33jp | 0:de7db46990d0 | 273 | |
king33jp | 0:de7db46990d0 | 274 | regwr(0x0043,0x0000); //2nd SCREEN DRIVING POSITION (R43h) |
king33jp | 0:de7db46990d0 | 275 | // SE27 SE26 SE25 SE24 SE23 SE22 SE21 SE20 SS27 SS26 SS25 SS24 SS23 SS22 SS21 SS20 |
king33jp | 0:de7db46990d0 | 276 | regwr(0x0044,0x7f00); //HORIZONTAL RAM ADDRESS POSITION (R44h) |
king33jp | 0:de7db46990d0 | 277 | |
king33jp | 0:de7db46990d0 | 278 | regwr(0x0045,0x9f00); //VERTICAL RAM ADDRESS POSITION (R45h) |
king33jp | 0:de7db46990d0 | 279 | |
king33jp | 0:de7db46990d0 | 280 | regwr(0x0069,0x0000); //DC/DC CONVERT LOW POWER MODE SETTING (R69h) |
king33jp | 0:de7db46990d0 | 281 | // 0 0 0 0 0 0 0 0 0 0 0 NLDC3 NLDC2 NLDC1 NLDC0 NLPM |
king33jp | 0:de7db46990d0 | 282 | //NLPM=0 Normal operation mode |
king33jp | 0:de7db46990d0 | 283 | //NLDC[1:0]=00 DCCLK/1 |
king33jp | 0:de7db46990d0 | 284 | //NLDC[3:2]=00 DCCLK/2 |
king33jp | 0:de7db46990d0 | 285 | |
king33jp | 0:de7db46990d0 | 286 | regwr(0x0070,0x0000); //SOURCE DRIVER PRE-DRIVING PERIOD SETTING (R70h) |
king33jp | 0:de7db46990d0 | 287 | // X X X X X X X X SDT1 SDT0 X X X X EQ1 EQ0 |
king33jp | 0:de7db46990d0 | 288 | //STD[1:0]=00 Source Output Delay Control=1 DISP_CK |
king33jp | 0:de7db46990d0 | 289 | //EQ=00 No Equalization Control |
king33jp | 0:de7db46990d0 | 290 | |
king33jp | 0:de7db46990d0 | 291 | regwr(0x0071,0x0000); //GATE OUTPUT PERIOD CONTROL (R71h) |
king33jp | 0:de7db46990d0 | 292 | // X X X X GNO1 GNO0 X X X X X X X X X X |
king33jp | 0:de7db46990d0 | 293 | //GNO=00 Non-Overlap Period Control =2 DISP_CKs |
king33jp | 0:de7db46990d0 | 294 | |
king33jp | 0:de7db46990d0 | 295 | regwr(0x0073,0x0000); //TEST_KEY (R73h) |
king33jp | 0:de7db46990d0 | 296 | // X X X X X X X X TEST_KEY[7:0] |
king33jp | 0:de7db46990d0 | 297 | //When you want to update MTP data, “A5” should be written to this register. |
king33jp | 0:de7db46990d0 | 298 | |
king33jp | 0:de7db46990d0 | 299 | regwr(0x00B3,0x0000); //PUMPING CLOCK SOURCE SELECTION (RB3h) |
king33jp | 0:de7db46990d0 | 300 | // X 0 0 0 X 0 1 0 X X X DCR_EX X X X 1 |
king33jp | 0:de7db46990d0 | 301 | //Select the source of pumping clock. |
king33jp | 0:de7db46990d0 | 302 | //In RGB mode, DCR_EX should be set before power setting. |
king33jp | 0:de7db46990d0 | 303 | |
king33jp | 0:de7db46990d0 | 304 | regwr(0x00BD,0x0000); //MTP DATA READ (RBDh) |
king33jp | 0:de7db46990d0 | 305 | // X X X X X X X DISEN X MTP_DOUT[6:0] |
king33jp | 0:de7db46990d0 | 306 | //DISEN=0 Standby mode discharge circuit operation stop. |
king33jp | 0:de7db46990d0 | 307 | //MTP_DOUT MTP data read using MTP_READ register. |
king33jp | 0:de7db46990d0 | 308 | |
king33jp | 0:de7db46990d0 | 309 | regwr(0x00BE,0x0000); //INTERFACE MODE SELECTION (RBEh) |
king33jp | 0:de7db46990d0 | 310 | // X X X X X X X X X X X IM_SEL IM_3 X X FLM_MSK |
king33jp | 0:de7db46990d0 | 311 | //IM_SEL register selects interface mode. |
king33jp | 0:de7db46990d0 | 312 | //IM_SEL=0 is IM[3:0] setting |
king33jp | 0:de7db46990d0 | 313 | |
king33jp | 0:de7db46990d0 | 314 | regwr(0x0021,0x0000); //GRAM ADDRESS SET (R21h) |
king33jp | 0:de7db46990d0 | 315 | // AD15 AD14 AD13 AD12 AD11 AD10 AD9 AD8 AD7 AD6 AD5 AD4 AD3 AD2 AD1 AD0 |
king33jp | 0:de7db46990d0 | 316 | // You can write initial GRAM address into internal Address Counter (AC). |
king33jp | 0:de7db46990d0 | 317 | |
king33jp | 0:de7db46990d0 | 318 | reg0(0x0022); //WRITE DATA TO GRAM (R22h) |
king33jp | 0:de7db46990d0 | 319 | // RAM write data (WD17 ~ WB0). Interface mode controls the width of WD |
king33jp | 0:de7db46990d0 | 320 | // Data on DB bus is expanded to 18-bits before being written to GRAM and the data determines grayscale level of S6D0151’s source output. |
king33jp | 0:de7db46990d0 | 321 | wait_ms(20); |
king33jp | 0:de7db46990d0 | 322 | |
king33jp | 0:de7db46990d0 | 323 | regwr(0x0007,0x0020); //DISPLAY CONTROL (R07h) |
king33jp | 0:de7db46990d0 | 324 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 0:de7db46990d0 | 325 | //PT[1:0]=00 V63/V0 Normal Drive |
king33jp | 0:de7db46990d0 | 326 | //SPT=0 Split Screen Driving Function is Not performed |
king33jp | 0:de7db46990d0 | 327 | //GON=1,DTE=0 Normal operation |
king33jp | 0:de7db46990d0 | 328 | //CL=0 262,144 colors / 65,536 colors |
king33jp | 0:de7db46990d0 | 329 | //REV=0 Displays all character and graphics display sections with Normal |
king33jp | 0:de7db46990d0 | 330 | //D[1:0]=00 the internal display operation halts and the display is off. |
king33jp | 0:de7db46990d0 | 331 | wait_ms(5); |
king33jp | 0:de7db46990d0 | 332 | |
king33jp | 0:de7db46990d0 | 333 | regwr(0x0007,0x0021); //DISPLAY CONTROL (R07h) |
king33jp | 0:de7db46990d0 | 334 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 0:de7db46990d0 | 335 | //D[1:0]=01 the internal display operation halts and the display is off. |
king33jp | 0:de7db46990d0 | 336 | |
king33jp | 0:de7db46990d0 | 337 | regwr(0x0007,0x0027); //DISPLAY CONTROL (R07h) |
king33jp | 0:de7db46990d0 | 338 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 0:de7db46990d0 | 339 | //REV=1 Displays all character and graphics display sections with Reverse |
king33jp | 0:de7db46990d0 | 340 | //D[1:0]=11 the internal display operation halts and the display is off. |
king33jp | 0:de7db46990d0 | 341 | |
king33jp | 0:de7db46990d0 | 342 | wait_ms(50); |
king33jp | 0:de7db46990d0 | 343 | regwr(0x0007,0x0037); //DISPLAY CONTROL (R07h) |
king33jp | 0:de7db46990d0 | 344 | // X X X PT1 PT0 X X SPT X X GON DTE CL REV D1 D0 |
king33jp | 0:de7db46990d0 | 345 | //GON=1,DTE=1 VGH/VGL Normal operation |
king33jp | 1:a3651128e297 | 346 | //Rev bit mean Color reverse. |
king33jp | 0:de7db46990d0 | 347 | //REV=1 Displays all character and graphics display sections with Reverse |
king33jp | 0:de7db46990d0 | 348 | //D[1:0]=11 Display is RAM data |
king33jp | 0:de7db46990d0 | 349 | |
king33jp | 1:a3651128e297 | 350 | /* Up to here, demo code reference partsas ------------------------------*/ |
king33jp | 1:a3651128e297 | 351 | |
king33jp | 0:de7db46990d0 | 352 | WindowMax (); |
king33jp | 0:de7db46990d0 | 353 | } |
king33jp | 0:de7db46990d0 | 354 | |
king33jp | 0:de7db46990d0 | 355 | |
king33jp | 0:de7db46990d0 | 356 | void S6D0151_TFT::pixel(int x, int y, int color) { |
king33jp | 0:de7db46990d0 | 357 | if ((x >= width()) || (y >= height())) return; |
king33jp | 0:de7db46990d0 | 358 | |
king33jp | 0:de7db46990d0 | 359 | window(x,y,1,1); |
king33jp | 0:de7db46990d0 | 360 | |
king33jp | 0:de7db46990d0 | 361 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 362 | _spi.write(color >> 8); |
king33jp | 0:de7db46990d0 | 363 | _spi.write(color & 0xFF); |
king33jp | 0:de7db46990d0 | 364 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 365 | |
king33jp | 0:de7db46990d0 | 366 | } |
king33jp | 0:de7db46990d0 | 367 | |
king33jp | 0:de7db46990d0 | 368 | void S6D0151_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) { |
king33jp | 0:de7db46990d0 | 369 | unsigned int hea,hsa,vea,vsa,dd; |
king33jp | 0:de7db46990d0 | 370 | hea=x+w-1; |
king33jp | 0:de7db46990d0 | 371 | hsa=x; |
king33jp | 0:de7db46990d0 | 372 | vea=y+h-1; |
king33jp | 0:de7db46990d0 | 373 | vsa=y; |
king33jp | 0:de7db46990d0 | 374 | |
king33jp | 0:de7db46990d0 | 375 | dd=((hea&0xff)<<8)|(hsa&0xff); |
king33jp | 0:de7db46990d0 | 376 | regwr(0x0044, dd ); //HORIZONTAL RAM ADDRESS POSITION (R44h) |
king33jp | 0:de7db46990d0 | 377 | dd=((vea&0xff)<<8)|(vsa&0xff); |
king33jp | 0:de7db46990d0 | 378 | regwr(0x0045, dd ); //VERTICAL RAM ADDRESS POSITION (R45h) |
king33jp | 0:de7db46990d0 | 379 | |
king33jp | 1:a3651128e297 | 380 | // set_start_address(hea,vea); |
king33jp | 1:a3651128e297 | 381 | set_start_address(hsa,vsa); |
king33jp | 0:de7db46990d0 | 382 | reg0(0x0022); //Write data to GRAM |
king33jp | 0:de7db46990d0 | 383 | } |
king33jp | 0:de7db46990d0 | 384 | |
king33jp | 0:de7db46990d0 | 385 | |
king33jp | 0:de7db46990d0 | 386 | void S6D0151_TFT::WindowMax (void) { |
king33jp | 0:de7db46990d0 | 387 | window (0, 0, width(), height()); |
king33jp | 0:de7db46990d0 | 388 | } |
king33jp | 0:de7db46990d0 | 389 | |
king33jp | 0:de7db46990d0 | 390 | |
king33jp | 0:de7db46990d0 | 391 | void S6D0151_TFT::cls (void) { |
king33jp | 0:de7db46990d0 | 392 | unsigned int i; |
king33jp | 0:de7db46990d0 | 393 | WindowMax(); |
king33jp | 0:de7db46990d0 | 394 | |
king33jp | 0:de7db46990d0 | 395 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 396 | for (i = 0; i < ( (width()+1) * (height()+3)); i++) { |
king33jp | 0:de7db46990d0 | 397 | _spi.write(_background >> 8); |
king33jp | 0:de7db46990d0 | 398 | _spi.write(_background & 0xFF); |
king33jp | 0:de7db46990d0 | 399 | } |
king33jp | 0:de7db46990d0 | 400 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 401 | } |
king33jp | 0:de7db46990d0 | 402 | |
king33jp | 0:de7db46990d0 | 403 | |
king33jp | 0:de7db46990d0 | 404 | void S6D0151_TFT::circle(int x0, int y0, int r, int color) { |
king33jp | 0:de7db46990d0 | 405 | |
king33jp | 0:de7db46990d0 | 406 | int draw_x0, draw_y0; |
king33jp | 0:de7db46990d0 | 407 | int draw_x1, draw_y1; |
king33jp | 0:de7db46990d0 | 408 | int draw_x2, draw_y2; |
king33jp | 0:de7db46990d0 | 409 | int draw_x3, draw_y3; |
king33jp | 0:de7db46990d0 | 410 | int draw_x4, draw_y4; |
king33jp | 0:de7db46990d0 | 411 | int draw_x5, draw_y5; |
king33jp | 0:de7db46990d0 | 412 | int draw_x6, draw_y6; |
king33jp | 0:de7db46990d0 | 413 | int draw_x7, draw_y7; |
king33jp | 0:de7db46990d0 | 414 | int xx, yy; |
king33jp | 0:de7db46990d0 | 415 | int di; |
king33jp | 0:de7db46990d0 | 416 | WindowMax(); |
king33jp | 0:de7db46990d0 | 417 | if (r == 0) { /* no radius */ |
king33jp | 0:de7db46990d0 | 418 | return; |
king33jp | 0:de7db46990d0 | 419 | } |
king33jp | 0:de7db46990d0 | 420 | |
king33jp | 0:de7db46990d0 | 421 | draw_x0 = draw_x1 = x0; |
king33jp | 0:de7db46990d0 | 422 | draw_y0 = draw_y1 = y0 + r; |
king33jp | 0:de7db46990d0 | 423 | if (draw_y0 < height()) { |
king33jp | 0:de7db46990d0 | 424 | pixel(draw_x0, draw_y0, color); /* 90 degree */ |
king33jp | 0:de7db46990d0 | 425 | } |
king33jp | 0:de7db46990d0 | 426 | |
king33jp | 0:de7db46990d0 | 427 | draw_x2 = draw_x3 = x0; |
king33jp | 0:de7db46990d0 | 428 | draw_y2 = draw_y3 = y0 - r; |
king33jp | 0:de7db46990d0 | 429 | if (draw_y2 >= 0) { |
king33jp | 0:de7db46990d0 | 430 | pixel(draw_x2, draw_y2, color); /* 270 degree */ |
king33jp | 0:de7db46990d0 | 431 | } |
king33jp | 0:de7db46990d0 | 432 | |
king33jp | 0:de7db46990d0 | 433 | draw_x4 = draw_x6 = x0 + r; |
king33jp | 0:de7db46990d0 | 434 | draw_y4 = draw_y6 = y0; |
king33jp | 0:de7db46990d0 | 435 | if (draw_x4 < width()) { |
king33jp | 0:de7db46990d0 | 436 | pixel(draw_x4, draw_y4, color); /* 0 degree */ |
king33jp | 0:de7db46990d0 | 437 | } |
king33jp | 0:de7db46990d0 | 438 | |
king33jp | 0:de7db46990d0 | 439 | draw_x5 = draw_x7 = x0 - r; |
king33jp | 0:de7db46990d0 | 440 | draw_y5 = draw_y7 = y0; |
king33jp | 0:de7db46990d0 | 441 | if (draw_x5>=0) { |
king33jp | 0:de7db46990d0 | 442 | pixel(draw_x5, draw_y5, color); /* 180 degree */ |
king33jp | 0:de7db46990d0 | 443 | } |
king33jp | 0:de7db46990d0 | 444 | |
king33jp | 0:de7db46990d0 | 445 | if (r == 1) { |
king33jp | 0:de7db46990d0 | 446 | return; |
king33jp | 0:de7db46990d0 | 447 | } |
king33jp | 0:de7db46990d0 | 448 | |
king33jp | 0:de7db46990d0 | 449 | di = 3 - 2*r; |
king33jp | 0:de7db46990d0 | 450 | xx = 0; |
king33jp | 0:de7db46990d0 | 451 | yy = r; |
king33jp | 0:de7db46990d0 | 452 | while (xx < yy) { |
king33jp | 0:de7db46990d0 | 453 | |
king33jp | 0:de7db46990d0 | 454 | if (di < 0) { |
king33jp | 0:de7db46990d0 | 455 | di += 4*xx + 6; |
king33jp | 0:de7db46990d0 | 456 | } else { |
king33jp | 0:de7db46990d0 | 457 | di += 4*(xx - yy) + 10; |
king33jp | 0:de7db46990d0 | 458 | yy--; |
king33jp | 0:de7db46990d0 | 459 | draw_y0--; |
king33jp | 0:de7db46990d0 | 460 | draw_y1--; |
king33jp | 0:de7db46990d0 | 461 | draw_y2++; |
king33jp | 0:de7db46990d0 | 462 | draw_y3++; |
king33jp | 0:de7db46990d0 | 463 | draw_x4--; |
king33jp | 0:de7db46990d0 | 464 | draw_x5++; |
king33jp | 0:de7db46990d0 | 465 | draw_x6--; |
king33jp | 0:de7db46990d0 | 466 | draw_x7++; |
king33jp | 0:de7db46990d0 | 467 | } |
king33jp | 0:de7db46990d0 | 468 | xx++; |
king33jp | 0:de7db46990d0 | 469 | draw_x0++; |
king33jp | 0:de7db46990d0 | 470 | draw_x1--; |
king33jp | 0:de7db46990d0 | 471 | draw_x2++; |
king33jp | 0:de7db46990d0 | 472 | draw_x3--; |
king33jp | 0:de7db46990d0 | 473 | draw_y4++; |
king33jp | 0:de7db46990d0 | 474 | draw_y5++; |
king33jp | 0:de7db46990d0 | 475 | draw_y6--; |
king33jp | 0:de7db46990d0 | 476 | draw_y7--; |
king33jp | 0:de7db46990d0 | 477 | |
king33jp | 0:de7db46990d0 | 478 | if ( (draw_x0 <= width()) && (draw_y0>=0) ) { |
king33jp | 0:de7db46990d0 | 479 | pixel(draw_x0, draw_y0, color); |
king33jp | 0:de7db46990d0 | 480 | } |
king33jp | 0:de7db46990d0 | 481 | |
king33jp | 0:de7db46990d0 | 482 | if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { |
king33jp | 0:de7db46990d0 | 483 | pixel(draw_x1, draw_y1, color); |
king33jp | 0:de7db46990d0 | 484 | } |
king33jp | 0:de7db46990d0 | 485 | |
king33jp | 0:de7db46990d0 | 486 | if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { |
king33jp | 0:de7db46990d0 | 487 | pixel(draw_x2, draw_y2, color); |
king33jp | 0:de7db46990d0 | 488 | } |
king33jp | 0:de7db46990d0 | 489 | |
king33jp | 0:de7db46990d0 | 490 | if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) { |
king33jp | 0:de7db46990d0 | 491 | pixel(draw_x3, draw_y3, color); |
king33jp | 0:de7db46990d0 | 492 | } |
king33jp | 0:de7db46990d0 | 493 | |
king33jp | 0:de7db46990d0 | 494 | if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { |
king33jp | 0:de7db46990d0 | 495 | pixel(draw_x4, draw_y4, color); |
king33jp | 0:de7db46990d0 | 496 | } |
king33jp | 0:de7db46990d0 | 497 | |
king33jp | 0:de7db46990d0 | 498 | if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { |
king33jp | 0:de7db46990d0 | 499 | pixel(draw_x5, draw_y5, color); |
king33jp | 0:de7db46990d0 | 500 | } |
king33jp | 0:de7db46990d0 | 501 | if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) { |
king33jp | 0:de7db46990d0 | 502 | pixel(draw_x6, draw_y6, color); |
king33jp | 0:de7db46990d0 | 503 | } |
king33jp | 0:de7db46990d0 | 504 | if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { |
king33jp | 0:de7db46990d0 | 505 | pixel(draw_x7, draw_y7, color); |
king33jp | 0:de7db46990d0 | 506 | } |
king33jp | 0:de7db46990d0 | 507 | } |
king33jp | 0:de7db46990d0 | 508 | return; |
king33jp | 0:de7db46990d0 | 509 | } |
king33jp | 0:de7db46990d0 | 510 | |
king33jp | 0:de7db46990d0 | 511 | void S6D0151_TFT::fillcircle(int x, int y, int r, int color) { |
king33jp | 0:de7db46990d0 | 512 | int i; |
king33jp | 0:de7db46990d0 | 513 | for (i = 0; i <= r; i++) |
king33jp | 0:de7db46990d0 | 514 | circle(x,y,i,color); |
king33jp | 0:de7db46990d0 | 515 | } |
king33jp | 0:de7db46990d0 | 516 | |
king33jp | 0:de7db46990d0 | 517 | void S6D0151_TFT::fillcircle2(int x0, int y0, int r, int color){ |
king33jp | 0:de7db46990d0 | 518 | int x = -r, y = 0, err = 2-2*r, e2; |
king33jp | 0:de7db46990d0 | 519 | do { |
king33jp | 0:de7db46990d0 | 520 | vline(x0-x, y0-y, y0+y, color); |
king33jp | 0:de7db46990d0 | 521 | vline(x0+x, y0-y, y0+y, color); |
king33jp | 0:de7db46990d0 | 522 | e2 = err; |
king33jp | 0:de7db46990d0 | 523 | if (e2 <= y) { |
king33jp | 0:de7db46990d0 | 524 | err += ++y*2+1; |
king33jp | 0:de7db46990d0 | 525 | if (-x == y && e2 <= x) e2 = 0; |
king33jp | 0:de7db46990d0 | 526 | } |
king33jp | 0:de7db46990d0 | 527 | if (e2 > x) err += ++x*2+1; |
king33jp | 0:de7db46990d0 | 528 | } while (x <= 0); |
king33jp | 0:de7db46990d0 | 529 | } |
king33jp | 0:de7db46990d0 | 530 | |
king33jp | 0:de7db46990d0 | 531 | void S6D0151_TFT::hline(int x0, int x1, int y, int color) { |
king33jp | 0:de7db46990d0 | 532 | int w; |
king33jp | 0:de7db46990d0 | 533 | w = x1 - x0 + 1; |
king33jp | 0:de7db46990d0 | 534 | window(x0,y,w,1); |
king33jp | 0:de7db46990d0 | 535 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 536 | for (int x=0; x<w; x++) { |
king33jp | 0:de7db46990d0 | 537 | _spi.write(color >> 8); |
king33jp | 0:de7db46990d0 | 538 | _spi.write(color); |
king33jp | 0:de7db46990d0 | 539 | } |
king33jp | 0:de7db46990d0 | 540 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 541 | return; |
king33jp | 0:de7db46990d0 | 542 | } |
king33jp | 0:de7db46990d0 | 543 | |
king33jp | 0:de7db46990d0 | 544 | |
king33jp | 0:de7db46990d0 | 545 | |
king33jp | 0:de7db46990d0 | 546 | void S6D0151_TFT::vline(int x, int y0, int y1, int color) { |
king33jp | 0:de7db46990d0 | 547 | int h; |
king33jp | 0:de7db46990d0 | 548 | h = y1 - y0 + 1; |
king33jp | 0:de7db46990d0 | 549 | window(x,y0,1,h); |
king33jp | 0:de7db46990d0 | 550 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 551 | for (int y=0; y<h; y++) { |
king33jp | 0:de7db46990d0 | 552 | _spi.write(color >> 8); |
king33jp | 0:de7db46990d0 | 553 | _spi.write(color); |
king33jp | 0:de7db46990d0 | 554 | } |
king33jp | 0:de7db46990d0 | 555 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 556 | return; |
king33jp | 0:de7db46990d0 | 557 | } |
king33jp | 0:de7db46990d0 | 558 | |
king33jp | 0:de7db46990d0 | 559 | |
king33jp | 0:de7db46990d0 | 560 | |
king33jp | 0:de7db46990d0 | 561 | void S6D0151_TFT::line(int x0, int y0, int x1, int y1, int color) { |
king33jp | 0:de7db46990d0 | 562 | WindowMax(); |
king33jp | 0:de7db46990d0 | 563 | int dx = 0, dy = 0; |
king33jp | 0:de7db46990d0 | 564 | int dx_sym = 0, dy_sym = 0; |
king33jp | 0:de7db46990d0 | 565 | int dx_x2 = 0, dy_x2 = 0; |
king33jp | 0:de7db46990d0 | 566 | int di = 0; |
king33jp | 0:de7db46990d0 | 567 | |
king33jp | 0:de7db46990d0 | 568 | dx = x1-x0; |
king33jp | 0:de7db46990d0 | 569 | dy = y1-y0; |
king33jp | 0:de7db46990d0 | 570 | |
king33jp | 0:de7db46990d0 | 571 | if (dx == 0) { /* vertical line */ |
king33jp | 0:de7db46990d0 | 572 | if (y1 > y0) vline(x0,y0,y1,color); |
king33jp | 0:de7db46990d0 | 573 | else vline(x0,y1,y0,color); |
king33jp | 0:de7db46990d0 | 574 | return; |
king33jp | 0:de7db46990d0 | 575 | } |
king33jp | 0:de7db46990d0 | 576 | |
king33jp | 0:de7db46990d0 | 577 | if (dx > 0) { |
king33jp | 0:de7db46990d0 | 578 | dx_sym = 1; |
king33jp | 0:de7db46990d0 | 579 | } else { |
king33jp | 0:de7db46990d0 | 580 | dx_sym = -1; |
king33jp | 0:de7db46990d0 | 581 | } |
king33jp | 0:de7db46990d0 | 582 | if (dy == 0) { /* horizontal line */ |
king33jp | 0:de7db46990d0 | 583 | if (x1 > x0) hline(x0,x1,y0,color); |
king33jp | 0:de7db46990d0 | 584 | else hline(x1,x0,y0,color); |
king33jp | 0:de7db46990d0 | 585 | return; |
king33jp | 0:de7db46990d0 | 586 | } |
king33jp | 0:de7db46990d0 | 587 | |
king33jp | 0:de7db46990d0 | 588 | if (dy > 0) { |
king33jp | 0:de7db46990d0 | 589 | dy_sym = 1; |
king33jp | 0:de7db46990d0 | 590 | } else { |
king33jp | 0:de7db46990d0 | 591 | dy_sym = -1; |
king33jp | 0:de7db46990d0 | 592 | } |
king33jp | 0:de7db46990d0 | 593 | |
king33jp | 0:de7db46990d0 | 594 | dx = dx_sym*dx; |
king33jp | 0:de7db46990d0 | 595 | dy = dy_sym*dy; |
king33jp | 0:de7db46990d0 | 596 | |
king33jp | 0:de7db46990d0 | 597 | dx_x2 = dx*2; |
king33jp | 0:de7db46990d0 | 598 | dy_x2 = dy*2; |
king33jp | 0:de7db46990d0 | 599 | |
king33jp | 0:de7db46990d0 | 600 | if (dx >= dy) { |
king33jp | 0:de7db46990d0 | 601 | di = dy_x2 - dx; |
king33jp | 0:de7db46990d0 | 602 | while (x0 != x1) { |
king33jp | 0:de7db46990d0 | 603 | |
king33jp | 0:de7db46990d0 | 604 | pixel(x0, y0, color); |
king33jp | 0:de7db46990d0 | 605 | x0 += dx_sym; |
king33jp | 0:de7db46990d0 | 606 | if (di<0) { |
king33jp | 0:de7db46990d0 | 607 | di += dy_x2; |
king33jp | 0:de7db46990d0 | 608 | } else { |
king33jp | 0:de7db46990d0 | 609 | di += dy_x2 - dx_x2; |
king33jp | 0:de7db46990d0 | 610 | y0 += dy_sym; |
king33jp | 0:de7db46990d0 | 611 | } |
king33jp | 0:de7db46990d0 | 612 | } |
king33jp | 0:de7db46990d0 | 613 | pixel(x0, y0, color); |
king33jp | 0:de7db46990d0 | 614 | } else { |
king33jp | 0:de7db46990d0 | 615 | di = dx_x2 - dy; |
king33jp | 0:de7db46990d0 | 616 | while (y0 != y1) { |
king33jp | 0:de7db46990d0 | 617 | pixel(x0, y0, color); |
king33jp | 0:de7db46990d0 | 618 | y0 += dy_sym; |
king33jp | 0:de7db46990d0 | 619 | if (di < 0) { |
king33jp | 0:de7db46990d0 | 620 | di += dx_x2; |
king33jp | 0:de7db46990d0 | 621 | } else { |
king33jp | 0:de7db46990d0 | 622 | di += dx_x2 - dy_x2; |
king33jp | 0:de7db46990d0 | 623 | x0 += dx_sym; |
king33jp | 0:de7db46990d0 | 624 | } |
king33jp | 0:de7db46990d0 | 625 | } |
king33jp | 0:de7db46990d0 | 626 | pixel(x0, y0, color); |
king33jp | 0:de7db46990d0 | 627 | } |
king33jp | 0:de7db46990d0 | 628 | return; |
king33jp | 0:de7db46990d0 | 629 | } |
king33jp | 0:de7db46990d0 | 630 | |
king33jp | 0:de7db46990d0 | 631 | |
king33jp | 0:de7db46990d0 | 632 | |
king33jp | 0:de7db46990d0 | 633 | |
king33jp | 0:de7db46990d0 | 634 | void S6D0151_TFT::rect(int x0, int y0, int x1, int y1, int color) { |
king33jp | 0:de7db46990d0 | 635 | |
king33jp | 0:de7db46990d0 | 636 | if (x1 > x0) hline(x0,x1,y0,color); |
king33jp | 0:de7db46990d0 | 637 | else hline(x1,x0,y0,color); |
king33jp | 0:de7db46990d0 | 638 | |
king33jp | 0:de7db46990d0 | 639 | if (y1 > y0) vline(x0,y0,y1,color); |
king33jp | 0:de7db46990d0 | 640 | else vline(x0,y1,y0,color); |
king33jp | 0:de7db46990d0 | 641 | |
king33jp | 0:de7db46990d0 | 642 | if (x1 > x0) hline(x0,x1,y1,color); |
king33jp | 0:de7db46990d0 | 643 | else hline(x1,x0,y1,color); |
king33jp | 0:de7db46990d0 | 644 | |
king33jp | 0:de7db46990d0 | 645 | if (y1 > y0) vline(x1,y0,y1,color); |
king33jp | 0:de7db46990d0 | 646 | else vline(x1,y1,y0,color); |
king33jp | 0:de7db46990d0 | 647 | |
king33jp | 0:de7db46990d0 | 648 | return; |
king33jp | 0:de7db46990d0 | 649 | } |
king33jp | 0:de7db46990d0 | 650 | |
king33jp | 0:de7db46990d0 | 651 | |
king33jp | 0:de7db46990d0 | 652 | |
king33jp | 0:de7db46990d0 | 653 | void S6D0151_TFT::fillrect(int x0, int y0, int x1, int y1, int color) { |
king33jp | 0:de7db46990d0 | 654 | |
king33jp | 0:de7db46990d0 | 655 | int h = y1 - y0 + 1; |
king33jp | 0:de7db46990d0 | 656 | int w = x1 - x0 + 1; |
king33jp | 0:de7db46990d0 | 657 | int pixel = h * w; |
king33jp | 0:de7db46990d0 | 658 | window(x0,y0,w,h); |
king33jp | 0:de7db46990d0 | 659 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 660 | for (int p=0; p<pixel; p++) { |
king33jp | 0:de7db46990d0 | 661 | _spi.write(color >> 8); |
king33jp | 3:f6feda54df80 | 662 | _spi.write(color & 0xFF); |
king33jp | 0:de7db46990d0 | 663 | } |
king33jp | 0:de7db46990d0 | 664 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 665 | return; |
king33jp | 0:de7db46990d0 | 666 | } |
king33jp | 0:de7db46990d0 | 667 | |
king33jp | 0:de7db46990d0 | 668 | |
king33jp | 0:de7db46990d0 | 669 | |
king33jp | 0:de7db46990d0 | 670 | void S6D0151_TFT::locate(int x, int y) { |
king33jp | 0:de7db46990d0 | 671 | char_x = x; |
king33jp | 0:de7db46990d0 | 672 | char_y = y; |
king33jp | 0:de7db46990d0 | 673 | } |
king33jp | 0:de7db46990d0 | 674 | |
king33jp | 0:de7db46990d0 | 675 | |
king33jp | 0:de7db46990d0 | 676 | |
king33jp | 0:de7db46990d0 | 677 | int S6D0151_TFT::columns() { |
king33jp | 0:de7db46990d0 | 678 | return width() / font[1]; |
king33jp | 0:de7db46990d0 | 679 | } |
king33jp | 0:de7db46990d0 | 680 | |
king33jp | 0:de7db46990d0 | 681 | |
king33jp | 0:de7db46990d0 | 682 | |
king33jp | 0:de7db46990d0 | 683 | int S6D0151_TFT::rows() { |
king33jp | 0:de7db46990d0 | 684 | return height() / font[2]; |
king33jp | 0:de7db46990d0 | 685 | } |
king33jp | 0:de7db46990d0 | 686 | |
king33jp | 0:de7db46990d0 | 687 | |
king33jp | 0:de7db46990d0 | 688 | |
king33jp | 0:de7db46990d0 | 689 | int S6D0151_TFT::_putc(int value) { |
king33jp | 0:de7db46990d0 | 690 | if (value == '\n') { // new line |
king33jp | 0:de7db46990d0 | 691 | char_x = 0; |
king33jp | 0:de7db46990d0 | 692 | char_y = char_y + font[2]; |
king33jp | 0:de7db46990d0 | 693 | if (char_y >= height() - font[2]) { |
king33jp | 0:de7db46990d0 | 694 | char_y = 0; |
king33jp | 0:de7db46990d0 | 695 | } |
king33jp | 0:de7db46990d0 | 696 | } else { |
king33jp | 0:de7db46990d0 | 697 | character(char_x, char_y, value); |
king33jp | 0:de7db46990d0 | 698 | } |
king33jp | 0:de7db46990d0 | 699 | return value; |
king33jp | 0:de7db46990d0 | 700 | } |
king33jp | 0:de7db46990d0 | 701 | |
king33jp | 0:de7db46990d0 | 702 | |
king33jp | 0:de7db46990d0 | 703 | |
king33jp | 0:de7db46990d0 | 704 | |
king33jp | 0:de7db46990d0 | 705 | void S6D0151_TFT::character(int x, int y, int c) { |
king33jp | 0:de7db46990d0 | 706 | unsigned int hor,vert,offset,bpl,j,i,b; |
king33jp | 0:de7db46990d0 | 707 | unsigned char* zeichen; |
king33jp | 0:de7db46990d0 | 708 | unsigned char z,w; |
king33jp | 0:de7db46990d0 | 709 | |
king33jp | 0:de7db46990d0 | 710 | if ((c < 31) || (c > 156)) return; // test char range |
king33jp | 0:de7db46990d0 | 711 | |
king33jp | 0:de7db46990d0 | 712 | // read font parameter from start of array |
king33jp | 0:de7db46990d0 | 713 | offset = font[0]; // bytes / char |
king33jp | 0:de7db46990d0 | 714 | hor = font[1]; // get hor size of font |
king33jp | 0:de7db46990d0 | 715 | vert = font[2]; // get vert size of font |
king33jp | 0:de7db46990d0 | 716 | bpl = font[3]; // bytes per line |
king33jp | 0:de7db46990d0 | 717 | |
king33jp | 0:de7db46990d0 | 718 | if (char_x + hor > width()) { |
king33jp | 0:de7db46990d0 | 719 | char_x = 0; |
king33jp | 0:de7db46990d0 | 720 | char_y = char_y + vert; |
king33jp | 0:de7db46990d0 | 721 | if (char_y >= height() - font[2]) { |
king33jp | 0:de7db46990d0 | 722 | char_y = 0; |
king33jp | 0:de7db46990d0 | 723 | } |
king33jp | 0:de7db46990d0 | 724 | } |
king33jp | 0:de7db46990d0 | 725 | |
king33jp | 0:de7db46990d0 | 726 | window(char_x, char_y,hor,vert); // char box |
king33jp | 0:de7db46990d0 | 727 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 728 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
king33jp | 0:de7db46990d0 | 729 | w = zeichen[0]; // width of actual char |
king33jp | 0:de7db46990d0 | 730 | |
king33jp | 0:de7db46990d0 | 731 | for (j=0; j<vert; j++) { // vert line |
king33jp | 0:de7db46990d0 | 732 | for (i=0; i<hor; i++) { // horz line |
king33jp | 0:de7db46990d0 | 733 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
king33jp | 0:de7db46990d0 | 734 | b = 1 << (j & 0x07); |
king33jp | 0:de7db46990d0 | 735 | if (( z & b ) == 0x00) { |
king33jp | 0:de7db46990d0 | 736 | _spi.write(_background >> 8); |
king33jp | 0:de7db46990d0 | 737 | _spi.write(_background & 0xff); |
king33jp | 0:de7db46990d0 | 738 | } else { |
king33jp | 0:de7db46990d0 | 739 | _spi.write(_foreground >> 8); |
king33jp | 0:de7db46990d0 | 740 | _spi.write(_foreground & 0xff); |
king33jp | 0:de7db46990d0 | 741 | } |
king33jp | 0:de7db46990d0 | 742 | } |
king33jp | 0:de7db46990d0 | 743 | } |
king33jp | 0:de7db46990d0 | 744 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 745 | if ((w + 2) < hor) { // x offset to next char |
king33jp | 0:de7db46990d0 | 746 | char_x += w + 2; |
king33jp | 0:de7db46990d0 | 747 | } else char_x += hor; |
king33jp | 0:de7db46990d0 | 748 | } |
king33jp | 0:de7db46990d0 | 749 | |
king33jp | 0:de7db46990d0 | 750 | void S6D0151_TFT::set_font(unsigned char* f) { |
king33jp | 0:de7db46990d0 | 751 | font = f; |
king33jp | 0:de7db46990d0 | 752 | } |
king33jp | 0:de7db46990d0 | 753 | |
king33jp | 0:de7db46990d0 | 754 | |
king33jp | 0:de7db46990d0 | 755 | |
king33jp | 0:de7db46990d0 | 756 | void S6D0151_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) { |
king33jp | 0:de7db46990d0 | 757 | unsigned int i,j; |
king33jp | 0:de7db46990d0 | 758 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
king33jp | 0:de7db46990d0 | 759 | window(x, y, w, h); |
king33jp | 0:de7db46990d0 | 760 | wr_dat_start(); |
king33jp | 0:de7db46990d0 | 761 | for (j = 0; j < h; j++) { //Lines |
king33jp | 0:de7db46990d0 | 762 | for (i = 0; i < w; i++) { // copy pixel data to TFT |
king33jp | 0:de7db46990d0 | 763 | |
king33jp | 0:de7db46990d0 | 764 | _spi.write(*bitmap_ptr >> 8); |
king33jp | 0:de7db46990d0 | 765 | _spi.write(*bitmap_ptr); // one line |
king33jp | 0:de7db46990d0 | 766 | |
king33jp | 0:de7db46990d0 | 767 | bitmap_ptr++; |
king33jp | 0:de7db46990d0 | 768 | } |
king33jp | 0:de7db46990d0 | 769 | } |
king33jp | 0:de7db46990d0 | 770 | wr_dat_stop(); |
king33jp | 0:de7db46990d0 | 771 | } |
king33jp | 0:de7db46990d0 | 772 | |
king33jp | 0:de7db46990d0 | 773 |