KS0108 LCD LIB with I2C I/O expander PCF8574 for Databus

Dependencies:   BusEnums

Dependents:   Menu

Committer:
GuiTwo
Date:
Sat Apr 21 20:52:05 2012 +0000
Revision:
0:f2f71eab6aef
Child:
1:8bc625378e38
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GuiTwo 0:f2f71eab6aef 1 #include "KS0108.h"
GuiTwo 0:f2f71eab6aef 2 #include "PCF8574_DataBus.h"
GuiTwo 0:f2f71eab6aef 3
GuiTwo 0:f2f71eab6aef 4 /*KS0108::KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS2, PinName _CS1, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7)
GuiTwo 0:f2f71eab6aef 5 : DB(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7),RST (_RST),DI(_DI), RW(_RW), E(_E), CS2(_CS2), CS1(_CS1) {*/
GuiTwo 0:f2f71eab6aef 6
GuiTwo 0:f2f71eab6aef 7
GuiTwo 0:f2f71eab6aef 8
GuiTwo 0:f2f71eab6aef 9 KS0108::KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS2, PinName _CS1,PCF8574_DataBus &_DB)
GuiTwo 0:f2f71eab6aef 10 :
GuiTwo 0:f2f71eab6aef 11 RST(_RST),
GuiTwo 0:f2f71eab6aef 12 DI(_DI),
GuiTwo 0:f2f71eab6aef 13 RW(_RW),
GuiTwo 0:f2f71eab6aef 14 E(_E),
GuiTwo 0:f2f71eab6aef 15 CS2(_CS2),
GuiTwo 0:f2f71eab6aef 16 CS1(_CS1),
GuiTwo 0:f2f71eab6aef 17 DB(_DB)
GuiTwo 0:f2f71eab6aef 18 {
GuiTwo 0:f2f71eab6aef 19
GuiTwo 0:f2f71eab6aef 20
GuiTwo 0:f2f71eab6aef 21 RST.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 22 DI.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 23 RW.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 24 E.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 25 CS2.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 26 CS1.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 27
GuiTwo 0:f2f71eab6aef 28 //RST.output();
GuiTwo 0:f2f71eab6aef 29 DI.output();
GuiTwo 0:f2f71eab6aef 30 RW.output();
GuiTwo 0:f2f71eab6aef 31 E.output();
GuiTwo 0:f2f71eab6aef 32 CS2.output();
GuiTwo 0:f2f71eab6aef 33 CS1.output();
GuiTwo 0:f2f71eab6aef 34 DB.output();
GuiTwo 0:f2f71eab6aef 35
GuiTwo 0:f2f71eab6aef 36
GuiTwo 0:f2f71eab6aef 37
GuiTwo 0:f2f71eab6aef 38
GuiTwo 0:f2f71eab6aef 39
GuiTwo 0:f2f71eab6aef 40
GuiTwo 0:f2f71eab6aef 41
GuiTwo 0:f2f71eab6aef 42 CS1.output();
GuiTwo 0:f2f71eab6aef 43 CS2.output();
GuiTwo 0:f2f71eab6aef 44 RST.write(0);
GuiTwo 0:f2f71eab6aef 45 wait_us(10);
GuiTwo 0:f2f71eab6aef 46 RST.write(1); //reset screen
GuiTwo 0:f2f71eab6aef 47 E.write(0);
GuiTwo 0:f2f71eab6aef 48 //ClearScreen(); //clear display
GuiTwo 0:f2f71eab6aef 49 WriteInstruction(LCD_ON, BOTH); //turn on lcd
GuiTwo 0:f2f71eab6aef 50 Inverted = 0;
GuiTwo 0:f2f71eab6aef 51 }
GuiTwo 0:f2f71eab6aef 52
GuiTwo 0:f2f71eab6aef 53
GuiTwo 0:f2f71eab6aef 54
GuiTwo 0:f2f71eab6aef 55 void KS0108::WriteInstruction(unsigned int Command,unsigned int side){
GuiTwo 0:f2f71eab6aef 56 E.write(0);
GuiTwo 0:f2f71eab6aef 57 DI.write(0);
GuiTwo 0:f2f71eab6aef 58 RW.write(0);
GuiTwo 0:f2f71eab6aef 59
GuiTwo 0:f2f71eab6aef 60 SelectSide(side); //select controller
GuiTwo 0:f2f71eab6aef 61
GuiTwo 0:f2f71eab6aef 62 wait(0.0000005); //wait 300ns
GuiTwo 0:f2f71eab6aef 63 E.write(1);
GuiTwo 0:f2f71eab6aef 64 DB.output();
GuiTwo 0:f2f71eab6aef 65 DB.write(Command);
GuiTwo 0:f2f71eab6aef 66 wait(0.0000005);
GuiTwo 0:f2f71eab6aef 67 E.write(0);
GuiTwo 0:f2f71eab6aef 68 }
GuiTwo 0:f2f71eab6aef 69
GuiTwo 0:f2f71eab6aef 70
GuiTwo 0:f2f71eab6aef 71 void KS0108::WriteData(unsigned int data,unsigned char side){
GuiTwo 0:f2f71eab6aef 72 E.write(0);
GuiTwo 0:f2f71eab6aef 73 DI.write(1);
GuiTwo 0:f2f71eab6aef 74 RW.write(0);
GuiTwo 0:f2f71eab6aef 75
GuiTwo 0:f2f71eab6aef 76 SelectSide(side);
GuiTwo 0:f2f71eab6aef 77
GuiTwo 0:f2f71eab6aef 78 wait(0.0000005); // 300ns
GuiTwo 0:f2f71eab6aef 79 E.write(1);
GuiTwo 0:f2f71eab6aef 80 DB.output();
GuiTwo 0:f2f71eab6aef 81 DB.write(data);
GuiTwo 0:f2f71eab6aef 82 wait(0.0000005);
GuiTwo 0:f2f71eab6aef 83 E.write(0);
GuiTwo 0:f2f71eab6aef 84 }
GuiTwo 0:f2f71eab6aef 85
GuiTwo 0:f2f71eab6aef 86 void KS0108::WriteData(unsigned int data) {
GuiTwo 0:f2f71eab6aef 87 unsigned int displayData, yOffset, chip;
GuiTwo 0:f2f71eab6aef 88
GuiTwo 0:f2f71eab6aef 89 if(Coord.x >= SCREEN_WIDTH)
GuiTwo 0:f2f71eab6aef 90 return;
GuiTwo 0:f2f71eab6aef 91 chip = Coord.x/CHIP_WIDTH;
GuiTwo 0:f2f71eab6aef 92 wait(0.0000007); // 300ns
GuiTwo 0:f2f71eab6aef 93
GuiTwo 0:f2f71eab6aef 94 if(Coord.x % CHIP_WIDTH == 0 && chip > 0){
GuiTwo 0:f2f71eab6aef 95 GotoXY(Coord.x, Coord.y);
GuiTwo 0:f2f71eab6aef 96 }
GuiTwo 0:f2f71eab6aef 97
GuiTwo 0:f2f71eab6aef 98 DI.write(1); // D/I = 1
GuiTwo 0:f2f71eab6aef 99 RW.write(0); // R/W = 0
GuiTwo 0:f2f71eab6aef 100 DB.output(); // data port is output
GuiTwo 0:f2f71eab6aef 101
GuiTwo 0:f2f71eab6aef 102 yOffset = Coord.y%8;
GuiTwo 0:f2f71eab6aef 103
GuiTwo 0:f2f71eab6aef 104 if(yOffset != 0) { // first page
GuiTwo 0:f2f71eab6aef 105
GuiTwo 0:f2f71eab6aef 106 displayData = ReadData();
GuiTwo 0:f2f71eab6aef 107
GuiTwo 0:f2f71eab6aef 108 DI.write(1); // D/I = 1
GuiTwo 0:f2f71eab6aef 109 RW.write(0); // R/W = 0
GuiTwo 0:f2f71eab6aef 110 SelectSide(chip);
GuiTwo 0:f2f71eab6aef 111 DB.output();
GuiTwo 0:f2f71eab6aef 112 // data port is output
GuiTwo 0:f2f71eab6aef 113 displayData |= data << yOffset;
GuiTwo 0:f2f71eab6aef 114 if(Inverted) displayData = ~displayData;
GuiTwo 0:f2f71eab6aef 115 DB.write(displayData); // write data
GuiTwo 0:f2f71eab6aef 116 wait(0.0000005); // 300ns
GuiTwo 0:f2f71eab6aef 117 E.write(1);
GuiTwo 0:f2f71eab6aef 118 wait(0.0000005);
GuiTwo 0:f2f71eab6aef 119 E.write(0);
GuiTwo 0:f2f71eab6aef 120
GuiTwo 0:f2f71eab6aef 121 // second page
GuiTwo 0:f2f71eab6aef 122 GotoXY(Coord.x, Coord.y+8);
GuiTwo 0:f2f71eab6aef 123
GuiTwo 0:f2f71eab6aef 124 displayData = ReadData();
GuiTwo 0:f2f71eab6aef 125
GuiTwo 0:f2f71eab6aef 126 DI.write(1); // D/I = 1
GuiTwo 0:f2f71eab6aef 127 RW.write(0); // R/W = 0
GuiTwo 0:f2f71eab6aef 128 SelectSide(chip);
GuiTwo 0:f2f71eab6aef 129
GuiTwo 0:f2f71eab6aef 130 DB.output(); // data port is output
GuiTwo 0:f2f71eab6aef 131
GuiTwo 0:f2f71eab6aef 132 displayData |= data >> (8-yOffset);
GuiTwo 0:f2f71eab6aef 133
GuiTwo 0:f2f71eab6aef 134 if(Inverted)
GuiTwo 0:f2f71eab6aef 135 displayData = ~displayData;
GuiTwo 0:f2f71eab6aef 136 DB.write(displayData); // write data
GuiTwo 0:f2f71eab6aef 137
GuiTwo 0:f2f71eab6aef 138 wait(0.0000005); // 300ns
GuiTwo 0:f2f71eab6aef 139 E.write(1);
GuiTwo 0:f2f71eab6aef 140 wait(0.0000005);
GuiTwo 0:f2f71eab6aef 141 E.write(0);
GuiTwo 0:f2f71eab6aef 142
GuiTwo 0:f2f71eab6aef 143 GotoXY(Coord.x+1, Coord.y-8);
GuiTwo 0:f2f71eab6aef 144 }else {
GuiTwo 0:f2f71eab6aef 145
GuiTwo 0:f2f71eab6aef 146 // just this code gets executed if the write is on a single page
GuiTwo 0:f2f71eab6aef 147 if(Inverted)
GuiTwo 0:f2f71eab6aef 148 data = ~data;
GuiTwo 0:f2f71eab6aef 149 wait(0.0000005); // 300nsEN_DELAY();
GuiTwo 0:f2f71eab6aef 150 DB.write(data); // write data
GuiTwo 0:f2f71eab6aef 151 wait(0.0000005); // 300ns
GuiTwo 0:f2f71eab6aef 152 E = 1;
GuiTwo 0:f2f71eab6aef 153 wait(0.0000005);
GuiTwo 0:f2f71eab6aef 154 E = 0;
GuiTwo 0:f2f71eab6aef 155 Coord.x++;
GuiTwo 0:f2f71eab6aef 156 }
GuiTwo 0:f2f71eab6aef 157 }
GuiTwo 0:f2f71eab6aef 158
GuiTwo 0:f2f71eab6aef 159
GuiTwo 0:f2f71eab6aef 160 void KS0108::WriteDataColPag(unsigned int page, unsigned int col, unsigned int data){
GuiTwo 0:f2f71eab6aef 161
GuiTwo 0:f2f71eab6aef 162 SelectSide(NONE);
GuiTwo 0:f2f71eab6aef 163 col = col%SCREEN_WIDTH;
GuiTwo 0:f2f71eab6aef 164 page = page%8;
GuiTwo 0:f2f71eab6aef 165
GuiTwo 0:f2f71eab6aef 166 if(col<(SCREEN_WIDTH/2)){
GuiTwo 0:f2f71eab6aef 167 SelectSide(LEFT);
GuiTwo 0:f2f71eab6aef 168 WriteInstruction(LCD_SET_PAGE|page,LEFT);
GuiTwo 0:f2f71eab6aef 169 WriteInstruction(LCD_SET_ADD|col,LEFT); //set page and column position
GuiTwo 0:f2f71eab6aef 170 WriteData(data,LEFT); //output data to D0-D7
GuiTwo 0:f2f71eab6aef 171 }else{
GuiTwo 0:f2f71eab6aef 172
GuiTwo 0:f2f71eab6aef 173 SelectSide(RIGHT);
GuiTwo 0:f2f71eab6aef 174 col -= (SCREEN_WIDTH/2);
GuiTwo 0:f2f71eab6aef 175 WriteInstruction(LCD_SET_PAGE|page,RIGHT);
GuiTwo 0:f2f71eab6aef 176 WriteInstruction(LCD_SET_ADD|col,RIGHT); //set page and column position
GuiTwo 0:f2f71eab6aef 177 WriteData(data,RIGHT); //output data to D0-D7
GuiTwo 0:f2f71eab6aef 178 }
GuiTwo 0:f2f71eab6aef 179
GuiTwo 0:f2f71eab6aef 180 SelectSide(NONE);
GuiTwo 0:f2f71eab6aef 181 }
GuiTwo 0:f2f71eab6aef 182
GuiTwo 0:f2f71eab6aef 183
GuiTwo 0:f2f71eab6aef 184
GuiTwo 0:f2f71eab6aef 185 unsigned int KS0108::ReadData(){
GuiTwo 0:f2f71eab6aef 186 unsigned int data;
GuiTwo 0:f2f71eab6aef 187 DB.input();
GuiTwo 0:f2f71eab6aef 188
GuiTwo 0:f2f71eab6aef 189
GuiTwo 0:f2f71eab6aef 190 DI.write(1);
GuiTwo 0:f2f71eab6aef 191 RW.write(1);
GuiTwo 0:f2f71eab6aef 192
GuiTwo 0:f2f71eab6aef 193 E.write(1);
GuiTwo 0:f2f71eab6aef 194 wait(0.0000007);
GuiTwo 0:f2f71eab6aef 195
GuiTwo 0:f2f71eab6aef 196 data = DB.read();
GuiTwo 0:f2f71eab6aef 197 wait(0.0000007);
GuiTwo 0:f2f71eab6aef 198 E.write(0);
GuiTwo 0:f2f71eab6aef 199 DB.output();
GuiTwo 0:f2f71eab6aef 200 // DB.mode(OpenDrain);
GuiTwo 0:f2f71eab6aef 201 return data;
GuiTwo 0:f2f71eab6aef 202 }
GuiTwo 0:f2f71eab6aef 203
GuiTwo 0:f2f71eab6aef 204 unsigned int KS0108::ReadStatus(){
GuiTwo 0:f2f71eab6aef 205 unsigned int status;
GuiTwo 0:f2f71eab6aef 206 DB.input();
GuiTwo 0:f2f71eab6aef 207
GuiTwo 0:f2f71eab6aef 208 DI.write(0);
GuiTwo 0:f2f71eab6aef 209
GuiTwo 0:f2f71eab6aef 210 RW.write(1);
GuiTwo 0:f2f71eab6aef 211 E.write(1);
GuiTwo 0:f2f71eab6aef 212 wait(0.0000007);
GuiTwo 0:f2f71eab6aef 213
GuiTwo 0:f2f71eab6aef 214 status = DB.read();
GuiTwo 0:f2f71eab6aef 215 E.write(0);
GuiTwo 0:f2f71eab6aef 216 wait(0.0000007);
GuiTwo 0:f2f71eab6aef 217 DB.output();
GuiTwo 0:f2f71eab6aef 218
GuiTwo 0:f2f71eab6aef 219 return status;
GuiTwo 0:f2f71eab6aef 220 }
GuiTwo 0:f2f71eab6aef 221
GuiTwo 0:f2f71eab6aef 222
GuiTwo 0:f2f71eab6aef 223
GuiTwo 0:f2f71eab6aef 224 void KS0108::SelectSide(unsigned char side){
GuiTwo 0:f2f71eab6aef 225 if(side==LEFT)
GuiTwo 0:f2f71eab6aef 226 {CS1.write(1);CS2.write(0);}
GuiTwo 0:f2f71eab6aef 227 else if(side==RIGHT)
GuiTwo 0:f2f71eab6aef 228 {CS1.write(0);CS2.write(1);}
GuiTwo 0:f2f71eab6aef 229 else if (side==BOTH)
GuiTwo 0:f2f71eab6aef 230 {CS1.write(1);CS2.write(1);}
GuiTwo 0:f2f71eab6aef 231 else if (side==NONE)
GuiTwo 0:f2f71eab6aef 232 {CS1.write(0);CS2.write(0);}
GuiTwo 0:f2f71eab6aef 233 }
GuiTwo 0:f2f71eab6aef 234
GuiTwo 0:f2f71eab6aef 235
GuiTwo 0:f2f71eab6aef 236 void KS0108::ClearScreen(){
GuiTwo 0:f2f71eab6aef 237 for (int col=0;col<128;col++) {
GuiTwo 0:f2f71eab6aef 238 for (int page=0;page<8;page++)
GuiTwo 0:f2f71eab6aef 239 {
GuiTwo 0:f2f71eab6aef 240 WriteDataColPag(page,col,WHITE);
GuiTwo 0:f2f71eab6aef 241 }
GuiTwo 0:f2f71eab6aef 242 }
GuiTwo 0:f2f71eab6aef 243 }
GuiTwo 0:f2f71eab6aef 244
GuiTwo 0:f2f71eab6aef 245
GuiTwo 0:f2f71eab6aef 246 void KS0108::TurnOn(){
GuiTwo 0:f2f71eab6aef 247 WriteInstruction(LCD_ON,BOTH);
GuiTwo 0:f2f71eab6aef 248 }
GuiTwo 0:f2f71eab6aef 249
GuiTwo 0:f2f71eab6aef 250
GuiTwo 0:f2f71eab6aef 251 void KS0108::TurnOff(){
GuiTwo 0:f2f71eab6aef 252 WriteInstruction(LCD_OFF,BOTH);
GuiTwo 0:f2f71eab6aef 253 }
GuiTwo 0:f2f71eab6aef 254
GuiTwo 0:f2f71eab6aef 255
GuiTwo 0:f2f71eab6aef 256 /*******************************************************************************************/
GuiTwo 0:f2f71eab6aef 257
GuiTwo 0:f2f71eab6aef 258
GuiTwo 0:f2f71eab6aef 259 void KS0108::SetPixel(unsigned int x, unsigned int y, unsigned int color){
GuiTwo 0:f2f71eab6aef 260
GuiTwo 0:f2f71eab6aef 261 unsigned int position;
GuiTwo 0:f2f71eab6aef 262
GuiTwo 0:f2f71eab6aef 263 SelectSide(NONE);
GuiTwo 0:f2f71eab6aef 264 WriteInstruction(LCD_SET_ADD,NONE);
GuiTwo 0:f2f71eab6aef 265
GuiTwo 0:f2f71eab6aef 266 if(x>=64){
GuiTwo 0:f2f71eab6aef 267 WriteInstruction(LCD_SET_PAGE|(y/8),RIGHT);
GuiTwo 0:f2f71eab6aef 268 WriteInstruction(LCD_SET_ADD|x,RIGHT);
GuiTwo 0:f2f71eab6aef 269 position = ReadData(); //dummy read
GuiTwo 0:f2f71eab6aef 270 position = ReadData(); //actual read
GuiTwo 0:f2f71eab6aef 271 WriteInstruction(LCD_SET_ADD|x,RIGHT);
GuiTwo 0:f2f71eab6aef 272 if(color==WHITE)
GuiTwo 0:f2f71eab6aef 273 WriteData(position&(~(1<<(y%8))),RIGHT); // draw a white pixel
GuiTwo 0:f2f71eab6aef 274 else
GuiTwo 0:f2f71eab6aef 275 WriteData(position|(1<<(y%8)),RIGHT);
GuiTwo 0:f2f71eab6aef 276 wait_us(450);
GuiTwo 0:f2f71eab6aef 277 }else{
GuiTwo 0:f2f71eab6aef 278 WriteInstruction(LCD_SET_PAGE|(y/8),LEFT);
GuiTwo 0:f2f71eab6aef 279 WriteInstruction(LCD_SET_ADD|x,LEFT);
GuiTwo 0:f2f71eab6aef 280 position = ReadData(); //dummy read
GuiTwo 0:f2f71eab6aef 281 position = ReadData(); //actual read
GuiTwo 0:f2f71eab6aef 282 WriteInstruction(LCD_SET_ADD|x,LEFT);
GuiTwo 0:f2f71eab6aef 283 if(color==WHITE)
GuiTwo 0:f2f71eab6aef 284 WriteData(position&(~(1<<(y%8))),LEFT);
GuiTwo 0:f2f71eab6aef 285 else
GuiTwo 0:f2f71eab6aef 286 WriteData(position|(1<<(y%8)),LEFT);
GuiTwo 0:f2f71eab6aef 287
GuiTwo 0:f2f71eab6aef 288 wait_us(450);
GuiTwo 0:f2f71eab6aef 289
GuiTwo 0:f2f71eab6aef 290 }
GuiTwo 0:f2f71eab6aef 291
GuiTwo 0:f2f71eab6aef 292 }
GuiTwo 0:f2f71eab6aef 293
GuiTwo 0:f2f71eab6aef 294
GuiTwo 0:f2f71eab6aef 295
GuiTwo 0:f2f71eab6aef 296 void KS0108::FullRectangle(unsigned int Xaxis1, unsigned int Yaxis1, unsigned int Xaxis2 ,unsigned int Yaxis2,unsigned int color){
GuiTwo 0:f2f71eab6aef 297
GuiTwo 0:f2f71eab6aef 298 for(unsigned int i=Xaxis1;i<=Xaxis2;i++){
GuiTwo 0:f2f71eab6aef 299 for(unsigned int j=Yaxis1;j<=Yaxis2;j++){
GuiTwo 0:f2f71eab6aef 300 SetPixel(i,j,color);
GuiTwo 0:f2f71eab6aef 301
GuiTwo 0:f2f71eab6aef 302 }
GuiTwo 0:f2f71eab6aef 303 }
GuiTwo 0:f2f71eab6aef 304 }
GuiTwo 0:f2f71eab6aef 305
GuiTwo 0:f2f71eab6aef 306
GuiTwo 0:f2f71eab6aef 307 void KS0108::EmptyRectangle(unsigned int Xaxis1,unsigned int Yaxis1, unsigned int Xaxis2,unsigned int Yaxis2,unsigned int color){
GuiTwo 0:f2f71eab6aef 308 unsigned int CurrentValue;
GuiTwo 0:f2f71eab6aef 309
GuiTwo 0:f2f71eab6aef 310 /* Draw the two horizontal lines */
GuiTwo 0:f2f71eab6aef 311 for (CurrentValue = 0; CurrentValue < Xaxis2 - Xaxis1+ 1; CurrentValue++)
GuiTwo 0:f2f71eab6aef 312 {
GuiTwo 0:f2f71eab6aef 313 SetPixel(Xaxis1 + CurrentValue, Yaxis1,color);
GuiTwo 0:f2f71eab6aef 314 SetPixel(Xaxis1 + CurrentValue, Yaxis2,color);
GuiTwo 0:f2f71eab6aef 315 }
GuiTwo 0:f2f71eab6aef 316
GuiTwo 0:f2f71eab6aef 317 /* draw the two vertical lines */
GuiTwo 0:f2f71eab6aef 318 for (CurrentValue = 0; CurrentValue < Yaxis2 - Yaxis1 + 1; CurrentValue++)
GuiTwo 0:f2f71eab6aef 319 {
GuiTwo 0:f2f71eab6aef 320 SetPixel(Xaxis1, Yaxis1 + CurrentValue,color);
GuiTwo 0:f2f71eab6aef 321 SetPixel(Xaxis2, Yaxis1 + CurrentValue,color);
GuiTwo 0:f2f71eab6aef 322 }
GuiTwo 0:f2f71eab6aef 323 }
GuiTwo 0:f2f71eab6aef 324
GuiTwo 0:f2f71eab6aef 325
GuiTwo 0:f2f71eab6aef 326 void KS0108::RoundRectangle(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int radius, unsigned int color) {
GuiTwo 0:f2f71eab6aef 327 int tSwitch, x1 = 0, y1 = radius;
GuiTwo 0:f2f71eab6aef 328 tSwitch = 3 - 2 * radius;
GuiTwo 0:f2f71eab6aef 329
GuiTwo 0:f2f71eab6aef 330 while (x1 <= y1) {
GuiTwo 0:f2f71eab6aef 331 SetPixel(x+radius - x1, y+radius - y1, color);
GuiTwo 0:f2f71eab6aef 332 SetPixel(x+radius - y1, y+radius - x1, color);
GuiTwo 0:f2f71eab6aef 333
GuiTwo 0:f2f71eab6aef 334 SetPixel(x+width-radius + x1, y+radius - y1, color);
GuiTwo 0:f2f71eab6aef 335 SetPixel(x+width-radius + y1, y+radius - x1, color);
GuiTwo 0:f2f71eab6aef 336
GuiTwo 0:f2f71eab6aef 337 SetPixel(x+width-radius + x1, y+height-radius + y1, color);
GuiTwo 0:f2f71eab6aef 338 SetPixel(x+width-radius + y1, y+height-radius + x1, color);
GuiTwo 0:f2f71eab6aef 339
GuiTwo 0:f2f71eab6aef 340 SetPixel(x+radius - x1, y+height-radius + y1, color);
GuiTwo 0:f2f71eab6aef 341 SetPixel(x+radius - y1, y+height-radius + x1, color);
GuiTwo 0:f2f71eab6aef 342
GuiTwo 0:f2f71eab6aef 343 if (tSwitch < 0) {
GuiTwo 0:f2f71eab6aef 344 tSwitch += (4 * x1 + 6);
GuiTwo 0:f2f71eab6aef 345 } else {
GuiTwo 0:f2f71eab6aef 346 tSwitch += (4 * (x1 - y1) + 10);
GuiTwo 0:f2f71eab6aef 347 y1--;
GuiTwo 0:f2f71eab6aef 348 }
GuiTwo 0:f2f71eab6aef 349 x1++;
GuiTwo 0:f2f71eab6aef 350 }
GuiTwo 0:f2f71eab6aef 351
GuiTwo 0:f2f71eab6aef 352 HLineShort(x+radius,y, width-(2*radius), color); // top
GuiTwo 0:f2f71eab6aef 353 HLineShort(x+radius,y+height, width-(2*radius), color); // bottom
GuiTwo 0:f2f71eab6aef 354 VLineShort(x,y+radius,height-(2*radius), color); // left
GuiTwo 0:f2f71eab6aef 355 VLineShort(x+width, y+radius,height-(2*radius), color); // right
GuiTwo 0:f2f71eab6aef 356 }
GuiTwo 0:f2f71eab6aef 357
GuiTwo 0:f2f71eab6aef 358
GuiTwo 0:f2f71eab6aef 359 void KS0108::HLine(unsigned int Xaxis1, unsigned int Xaxis2 ,unsigned int Yaxis,unsigned int color){
GuiTwo 0:f2f71eab6aef 360 FullRectangle(Xaxis1,Yaxis,Xaxis2,Yaxis,color);
GuiTwo 0:f2f71eab6aef 361
GuiTwo 0:f2f71eab6aef 362 }
GuiTwo 0:f2f71eab6aef 363
GuiTwo 0:f2f71eab6aef 364
GuiTwo 0:f2f71eab6aef 365 void KS0108::HLineShort(unsigned int Xaxis, unsigned int Yaxis,unsigned int width ,unsigned int color){
GuiTwo 0:f2f71eab6aef 366 FullRectangle(Xaxis,Yaxis,Xaxis+width,Yaxis,color);
GuiTwo 0:f2f71eab6aef 367
GuiTwo 0:f2f71eab6aef 368 }
GuiTwo 0:f2f71eab6aef 369
GuiTwo 0:f2f71eab6aef 370
GuiTwo 0:f2f71eab6aef 371 void KS0108::VLine(unsigned int Xaxis, unsigned int Yaxis1 ,unsigned int Yaxis2,unsigned int color){
GuiTwo 0:f2f71eab6aef 372 FullRectangle(Xaxis,Yaxis1,Xaxis,Yaxis2,color);
GuiTwo 0:f2f71eab6aef 373 }
GuiTwo 0:f2f71eab6aef 374
GuiTwo 0:f2f71eab6aef 375
GuiTwo 0:f2f71eab6aef 376 void KS0108::VLineShort(unsigned int Xaxis,unsigned int Yaxis, unsigned int height ,unsigned int color){
GuiTwo 0:f2f71eab6aef 377 FullRectangle(Xaxis,Yaxis,Xaxis,Yaxis+height,color);
GuiTwo 0:f2f71eab6aef 378
GuiTwo 0:f2f71eab6aef 379 }
GuiTwo 0:f2f71eab6aef 380
GuiTwo 0:f2f71eab6aef 381
GuiTwo 0:f2f71eab6aef 382 void KS0108::DegreeLine(unsigned int x, int y,unsigned int degree,unsigned int inner_radius,unsigned int outer_radius, unsigned int color){
GuiTwo 0:f2f71eab6aef 383 int fx,fy,tx,ty;
GuiTwo 0:f2f71eab6aef 384 fx = x + dfloor(inner_radius * sin(degree * 3.14 / 180));
GuiTwo 0:f2f71eab6aef 385 fy = y - dfloor(inner_radius * cos(degree * 3.14 / 180));
GuiTwo 0:f2f71eab6aef 386 tx = x + dfloor(outer_radius * sin(degree * 3.14 / 180));
GuiTwo 0:f2f71eab6aef 387 ty = y - dfloor(outer_radius * cos(degree * 3.14 / 180));
GuiTwo 0:f2f71eab6aef 388 SlantyLine(fx,fy,tx,ty,color);
GuiTwo 0:f2f71eab6aef 389 }
GuiTwo 0:f2f71eab6aef 390
GuiTwo 0:f2f71eab6aef 391
GuiTwo 0:f2f71eab6aef 392 double KS0108::dfloor( double value ) {
GuiTwo 0:f2f71eab6aef 393
GuiTwo 0:f2f71eab6aef 394 if (value < 0.0)
GuiTwo 0:f2f71eab6aef 395 return ceil( value );
GuiTwo 0:f2f71eab6aef 396 else
GuiTwo 0:f2f71eab6aef 397 return floor( value );
GuiTwo 0:f2f71eab6aef 398
GuiTwo 0:f2f71eab6aef 399 }
GuiTwo 0:f2f71eab6aef 400
GuiTwo 0:f2f71eab6aef 401
GuiTwo 0:f2f71eab6aef 402 void KS0108::SlantyLine(unsigned int lX1, unsigned int lY1, unsigned int lX2,unsigned int lY2,unsigned int color){
GuiTwo 0:f2f71eab6aef 403 long lError, lDeltaX, lDeltaY, lYStep, bSteep;
GuiTwo 0:f2f71eab6aef 404
GuiTwo 0:f2f71eab6aef 405 // A steep line has a bigger ordinate.
GuiTwo 0:f2f71eab6aef 406
GuiTwo 0:f2f71eab6aef 407 if(((lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2)) > ((lX2 > lX1) ? (lX2 - lX1) : (lX1 - lX2))){
GuiTwo 0:f2f71eab6aef 408 bSteep = 1;
GuiTwo 0:f2f71eab6aef 409 }else {
GuiTwo 0:f2f71eab6aef 410 bSteep = 0;
GuiTwo 0:f2f71eab6aef 411 }
GuiTwo 0:f2f71eab6aef 412
GuiTwo 0:f2f71eab6aef 413 // If line is steep, swap the X and Y coordinates.
GuiTwo 0:f2f71eab6aef 414 if(bSteep){
GuiTwo 0:f2f71eab6aef 415 lError = lX1;
GuiTwo 0:f2f71eab6aef 416 lX1 = lY1;
GuiTwo 0:f2f71eab6aef 417 lY1 = lError;
GuiTwo 0:f2f71eab6aef 418 lError = lX2;
GuiTwo 0:f2f71eab6aef 419 lX2 = lY2;
GuiTwo 0:f2f71eab6aef 420 lY2 = lError;
GuiTwo 0:f2f71eab6aef 421 }
GuiTwo 0:f2f71eab6aef 422
GuiTwo 0:f2f71eab6aef 423
GuiTwo 0:f2f71eab6aef 424 // If the starting X coordinate is larger than the ending X coordinate,
GuiTwo 0:f2f71eab6aef 425 // swap coordinates.
GuiTwo 0:f2f71eab6aef 426 if(lX1 > lX2){
GuiTwo 0:f2f71eab6aef 427 lError = lX1;
GuiTwo 0:f2f71eab6aef 428 lX1 = lX2;
GuiTwo 0:f2f71eab6aef 429 lX2 = lError;
GuiTwo 0:f2f71eab6aef 430 lError = lY1;
GuiTwo 0:f2f71eab6aef 431 lY1 = lY2;
GuiTwo 0:f2f71eab6aef 432 lY2 = lError;
GuiTwo 0:f2f71eab6aef 433 }
GuiTwo 0:f2f71eab6aef 434
GuiTwo 0:f2f71eab6aef 435 // Compute the difference between the start and end coordinates.
GuiTwo 0:f2f71eab6aef 436 lDeltaX = lX2 - lX1;
GuiTwo 0:f2f71eab6aef 437 lDeltaY = (lY2 > lY1) ? (lY2 - lY1) : (lY1 - lY2);
GuiTwo 0:f2f71eab6aef 438
GuiTwo 0:f2f71eab6aef 439 lError = -lDeltaX / 2; // Initialize the error term to negative half the X delta.
GuiTwo 0:f2f71eab6aef 440
GuiTwo 0:f2f71eab6aef 441 if(lY1 < lY2){ // Determine the direction to step in the Y axis when required.
GuiTwo 0:f2f71eab6aef 442 lYStep = 1;
GuiTwo 0:f2f71eab6aef 443 }else{
GuiTwo 0:f2f71eab6aef 444 lYStep = -1;
GuiTwo 0:f2f71eab6aef 445 }
GuiTwo 0:f2f71eab6aef 446
GuiTwo 0:f2f71eab6aef 447 for(; lX1 <= lX2; lX1++){ // Loop through all the points along the X axis of the line.
GuiTwo 0:f2f71eab6aef 448
GuiTwo 0:f2f71eab6aef 449 // See if this is a steep line.
GuiTwo 0:f2f71eab6aef 450
GuiTwo 0:f2f71eab6aef 451 if(bSteep){
GuiTwo 0:f2f71eab6aef 452
GuiTwo 0:f2f71eab6aef 453 // Plot this point of the line, swapping the X and Y coordinates.
GuiTwo 0:f2f71eab6aef 454
GuiTwo 0:f2f71eab6aef 455 SetPixel(lY1, lX1,color);
GuiTwo 0:f2f71eab6aef 456 }
GuiTwo 0:f2f71eab6aef 457 else { // Plot this point of the line, using the coordinates as is.
GuiTwo 0:f2f71eab6aef 458 SetPixel(lX1, lY1,color);
GuiTwo 0:f2f71eab6aef 459 }
GuiTwo 0:f2f71eab6aef 460
GuiTwo 0:f2f71eab6aef 461 // Increment the error term by the Y delta.
GuiTwo 0:f2f71eab6aef 462
GuiTwo 0:f2f71eab6aef 463 lError += lDeltaY;
GuiTwo 0:f2f71eab6aef 464
GuiTwo 0:f2f71eab6aef 465 if(lError > 0){ // See if the error term is now greater than zero.
GuiTwo 0:f2f71eab6aef 466
GuiTwo 0:f2f71eab6aef 467 lY1 += lYStep; // Take a step in the Y axis.
GuiTwo 0:f2f71eab6aef 468
GuiTwo 0:f2f71eab6aef 469 lError -= lDeltaX; // Decrement the error term by the X delta.
GuiTwo 0:f2f71eab6aef 470 }
GuiTwo 0:f2f71eab6aef 471 }
GuiTwo 0:f2f71eab6aef 472 }
GuiTwo 0:f2f71eab6aef 473
GuiTwo 0:f2f71eab6aef 474
GuiTwo 0:f2f71eab6aef 475
GuiTwo 0:f2f71eab6aef 476 void KS0108::Line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int color){
GuiTwo 0:f2f71eab6aef 477 unsigned int deltax, deltay, x,y, steep;
GuiTwo 0:f2f71eab6aef 478 int lerror, ystep;
GuiTwo 0:f2f71eab6aef 479
GuiTwo 0:f2f71eab6aef 480 steep = absDiff(y1,y2) > absDiff(x1,x2); //check slope
GuiTwo 0:f2f71eab6aef 481
GuiTwo 0:f2f71eab6aef 482 if ( steep ){
GuiTwo 0:f2f71eab6aef 483 swap(x1, y1);
GuiTwo 0:f2f71eab6aef 484 swap(x2, y2);
GuiTwo 0:f2f71eab6aef 485 }
GuiTwo 0:f2f71eab6aef 486
GuiTwo 0:f2f71eab6aef 487 if (x1 > x2){
GuiTwo 0:f2f71eab6aef 488 swap(x1, x2);
GuiTwo 0:f2f71eab6aef 489 swap(y1, y2);
GuiTwo 0:f2f71eab6aef 490 }
GuiTwo 0:f2f71eab6aef 491
GuiTwo 0:f2f71eab6aef 492 deltax = x2 - x1;
GuiTwo 0:f2f71eab6aef 493 deltay = absDiff(y2,y1);
GuiTwo 0:f2f71eab6aef 494 lerror = deltax / 2;
GuiTwo 0:f2f71eab6aef 495 y = y1;
GuiTwo 0:f2f71eab6aef 496 if(y1 < y2) ystep = 1; else ystep = -1;
GuiTwo 0:f2f71eab6aef 497
GuiTwo 0:f2f71eab6aef 498 for(x = x1; x <= x2; x++){
GuiTwo 0:f2f71eab6aef 499 if (steep) SetPixel(y,x, color); else SetPixel(x,y, color);
GuiTwo 0:f2f71eab6aef 500 lerror -= deltay;
GuiTwo 0:f2f71eab6aef 501 if (lerror < 0){
GuiTwo 0:f2f71eab6aef 502 y = y + ystep;
GuiTwo 0:f2f71eab6aef 503 lerror += deltax;
GuiTwo 0:f2f71eab6aef 504 }
GuiTwo 0:f2f71eab6aef 505 }
GuiTwo 0:f2f71eab6aef 506 }
GuiTwo 0:f2f71eab6aef 507
GuiTwo 0:f2f71eab6aef 508
GuiTwo 0:f2f71eab6aef 509 void KS0108::EmptyCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color){
GuiTwo 0:f2f71eab6aef 510 unsigned int y=0, x=0, d = 0;
GuiTwo 0:f2f71eab6aef 511 int part;
GuiTwo 0:f2f71eab6aef 512 d = CenterY - CenterX;
GuiTwo 0:f2f71eab6aef 513 y = Radius;
GuiTwo 0:f2f71eab6aef 514 part = 3 - 2 * Radius;
GuiTwo 0:f2f71eab6aef 515
GuiTwo 0:f2f71eab6aef 516 while (x <= y) {
GuiTwo 0:f2f71eab6aef 517 SetPixel(CenterX + x, CenterY + y,color);
GuiTwo 0:f2f71eab6aef 518 SetPixel(CenterX + x, CenterY - y,color);
GuiTwo 0:f2f71eab6aef 519 SetPixel(CenterX - x, CenterY + y,color);
GuiTwo 0:f2f71eab6aef 520 SetPixel(CenterX - x, CenterY - y,color);
GuiTwo 0:f2f71eab6aef 521 SetPixel(CenterY + y - d, CenterY + x,color);
GuiTwo 0:f2f71eab6aef 522 SetPixel(CenterY + y - d, CenterY - x,color);
GuiTwo 0:f2f71eab6aef 523 SetPixel(CenterY - y - d, CenterY + x,color);
GuiTwo 0:f2f71eab6aef 524 SetPixel(CenterY - y - d, CenterY - x,color);
GuiTwo 0:f2f71eab6aef 525
GuiTwo 0:f2f71eab6aef 526 if (part < 0) part += (4 * x + 6);
GuiTwo 0:f2f71eab6aef 527 else {
GuiTwo 0:f2f71eab6aef 528 part += (4 * (x - y) + 10);
GuiTwo 0:f2f71eab6aef 529 y--;
GuiTwo 0:f2f71eab6aef 530 }
GuiTwo 0:f2f71eab6aef 531 x++;
GuiTwo 0:f2f71eab6aef 532 }
GuiTwo 0:f2f71eab6aef 533
GuiTwo 0:f2f71eab6aef 534 }
GuiTwo 0:f2f71eab6aef 535
GuiTwo 0:f2f71eab6aef 536
GuiTwo 0:f2f71eab6aef 537 void KS0108::FullCircle(unsigned int CenterX, unsigned int CenterY, unsigned int Radius,unsigned int color){
GuiTwo 0:f2f71eab6aef 538
GuiTwo 0:f2f71eab6aef 539 int f = 1 - Radius;
GuiTwo 0:f2f71eab6aef 540 int ddF_x = 1;
GuiTwo 0:f2f71eab6aef 541 int ddF_y = 2 * Radius; //changed sign of -2
GuiTwo 0:f2f71eab6aef 542 unsigned int x = 0;
GuiTwo 0:f2f71eab6aef 543 unsigned int y = Radius;
GuiTwo 0:f2f71eab6aef 544
GuiTwo 0:f2f71eab6aef 545 //Fill in the center between the two halves
GuiTwo 0:f2f71eab6aef 546
GuiTwo 0:f2f71eab6aef 547 Line(CenterX, CenterY-Radius, CenterX, CenterY+Radius, color);
GuiTwo 0:f2f71eab6aef 548
GuiTwo 0:f2f71eab6aef 549 while(x < y){
GuiTwo 0:f2f71eab6aef 550 if(f >= 0)
GuiTwo 0:f2f71eab6aef 551 {
GuiTwo 0:f2f71eab6aef 552 y--;
GuiTwo 0:f2f71eab6aef 553 ddF_y += 2;
GuiTwo 0:f2f71eab6aef 554 f += ddF_y;
GuiTwo 0:f2f71eab6aef 555 }
GuiTwo 0:f2f71eab6aef 556 x++;
GuiTwo 0:f2f71eab6aef 557 ddF_x += 2;
GuiTwo 0:f2f71eab6aef 558 f += ddF_x;
GuiTwo 0:f2f71eab6aef 559
GuiTwo 0:f2f71eab6aef 560 /*
GuiTwo 0:f2f71eab6aef 561 * Now draw vertical lines between the points on the circle rather than
GuiTwo 0:f2f71eab6aef 562 * draw the points of the circle. This draws lines between the
GuiTwo 0:f2f71eab6aef 563 * perimeter points on the upper and lower quadrants of the 2 halves of the circle.
GuiTwo 0:f2f71eab6aef 564 */
GuiTwo 0:f2f71eab6aef 565
GuiTwo 0:f2f71eab6aef 566 Line(CenterX+x, CenterY+y, CenterX+x, CenterY-y, color);
GuiTwo 0:f2f71eab6aef 567 Line(CenterX-x, CenterY+y, CenterX-x, CenterY-y, color);
GuiTwo 0:f2f71eab6aef 568 Line(CenterX+y, CenterY+x, y+CenterX, CenterY-x, color);
GuiTwo 0:f2f71eab6aef 569 Line(CenterX-y, CenterY+x, CenterX-y, CenterY-x, color);
GuiTwo 0:f2f71eab6aef 570 }
GuiTwo 0:f2f71eab6aef 571 }
GuiTwo 0:f2f71eab6aef 572
GuiTwo 0:f2f71eab6aef 573
GuiTwo 0:f2f71eab6aef 574
GuiTwo 0:f2f71eab6aef 575 void KS0108::PlotEllipse(long CX, long CY, long XRadius,long YRadius, int color) {
GuiTwo 0:f2f71eab6aef 576
GuiTwo 0:f2f71eab6aef 577
GuiTwo 0:f2f71eab6aef 578 long X, Y;
GuiTwo 0:f2f71eab6aef 579 long XChange, YChange;
GuiTwo 0:f2f71eab6aef 580 long EllipseError;
GuiTwo 0:f2f71eab6aef 581 long TwoASquare,TwoBSquare;
GuiTwo 0:f2f71eab6aef 582 long StoppingX, StoppingY;
GuiTwo 0:f2f71eab6aef 583 TwoASquare = 2*XRadius*XRadius;
GuiTwo 0:f2f71eab6aef 584 TwoBSquare = 2*YRadius*YRadius;
GuiTwo 0:f2f71eab6aef 585 X = XRadius;
GuiTwo 0:f2f71eab6aef 586 Y = 0;
GuiTwo 0:f2f71eab6aef 587 XChange = YRadius*YRadius*(1-2*XRadius);
GuiTwo 0:f2f71eab6aef 588 YChange = XRadius*XRadius;
GuiTwo 0:f2f71eab6aef 589 EllipseError = 0;
GuiTwo 0:f2f71eab6aef 590 StoppingX = TwoBSquare*XRadius;
GuiTwo 0:f2f71eab6aef 591 StoppingY = 0;
GuiTwo 0:f2f71eab6aef 592
GuiTwo 0:f2f71eab6aef 593 while ( StoppingX >=StoppingY ) //first set of points,y'>-1
GuiTwo 0:f2f71eab6aef 594 {
GuiTwo 0:f2f71eab6aef 595 Plot4EllipsePoints(CX,CY,X,Y,color);
GuiTwo 0:f2f71eab6aef 596 Y++;
GuiTwo 0:f2f71eab6aef 597 StoppingY=StoppingY+ TwoASquare;
GuiTwo 0:f2f71eab6aef 598 EllipseError = EllipseError+ YChange;
GuiTwo 0:f2f71eab6aef 599 YChange=YChange+TwoASquare;
GuiTwo 0:f2f71eab6aef 600 if ((2*EllipseError + XChange) > 0 ) {
GuiTwo 0:f2f71eab6aef 601 X--;
GuiTwo 0:f2f71eab6aef 602 StoppingX=StoppingX- TwoBSquare;
GuiTwo 0:f2f71eab6aef 603 EllipseError=EllipseError+ XChange;
GuiTwo 0:f2f71eab6aef 604 XChange=XChange+TwoBSquare;
GuiTwo 0:f2f71eab6aef 605 }
GuiTwo 0:f2f71eab6aef 606 }
GuiTwo 0:f2f71eab6aef 607
GuiTwo 0:f2f71eab6aef 608 Y = YRadius;
GuiTwo 0:f2f71eab6aef 609 X = 0;
GuiTwo 0:f2f71eab6aef 610 YChange = XRadius*XRadius*(1-2*YRadius);
GuiTwo 0:f2f71eab6aef 611 XChange = YRadius*YRadius;
GuiTwo 0:f2f71eab6aef 612 EllipseError = 0;
GuiTwo 0:f2f71eab6aef 613 StoppingY = TwoASquare*YRadius;
GuiTwo 0:f2f71eab6aef 614 StoppingX = 0;
GuiTwo 0:f2f71eab6aef 615
GuiTwo 0:f2f71eab6aef 616 while ( StoppingY >=StoppingX ) //{2nd set of points, y'< -1}
GuiTwo 0:f2f71eab6aef 617 {
GuiTwo 0:f2f71eab6aef 618 Plot4EllipsePoints(CX,CY,X,Y,color);
GuiTwo 0:f2f71eab6aef 619 X++;
GuiTwo 0:f2f71eab6aef 620 StoppingX=StoppingX + TwoBSquare;
GuiTwo 0:f2f71eab6aef 621 EllipseError=EllipseError+ XChange;
GuiTwo 0:f2f71eab6aef 622 XChange=XChange+TwoBSquare;
GuiTwo 0:f2f71eab6aef 623 if ((2*EllipseError + YChange) > 0 ) {
GuiTwo 0:f2f71eab6aef 624 Y--;
GuiTwo 0:f2f71eab6aef 625 StoppingY=StoppingY- TwoASquare;
GuiTwo 0:f2f71eab6aef 626 EllipseError=EllipseError+ YChange;
GuiTwo 0:f2f71eab6aef 627 YChange=YChange+TwoASquare;
GuiTwo 0:f2f71eab6aef 628 }
GuiTwo 0:f2f71eab6aef 629 }
GuiTwo 0:f2f71eab6aef 630 }
GuiTwo 0:f2f71eab6aef 631
GuiTwo 0:f2f71eab6aef 632
GuiTwo 0:f2f71eab6aef 633
GuiTwo 0:f2f71eab6aef 634 void KS0108::Plot4EllipsePoints(long CX,long CY, long X, long Y, int color){
GuiTwo 0:f2f71eab6aef 635 SetPixel(CX+X, CY+Y, color); //{point in quadrant 1}
GuiTwo 0:f2f71eab6aef 636 SetPixel(CX-X, CY+Y, color); //{point in quadrant 2}
GuiTwo 0:f2f71eab6aef 637 SetPixel(CX-X, CY-Y, color); //{point in quadrant 3}
GuiTwo 0:f2f71eab6aef 638 SetPixel(CX+X, CY-Y, color); //{point in quadrant 4}
GuiTwo 0:f2f71eab6aef 639 }
GuiTwo 0:f2f71eab6aef 640
GuiTwo 0:f2f71eab6aef 641
GuiTwo 0:f2f71eab6aef 642 void KS0108::RightTriangle ( int topx, int topy, int rightx, int righty) {
GuiTwo 0:f2f71eab6aef 643
GuiTwo 0:f2f71eab6aef 644 //draw rectangle one line at a time
GuiTwo 0:f2f71eab6aef 645 Line( topx,topy, rightx,righty,BLACK ); //draw hypotenuse
GuiTwo 0:f2f71eab6aef 646 Line ( topx,righty,topx,topy,BLACK); //draw perpendicular
GuiTwo 0:f2f71eab6aef 647 Line (topx,righty, rightx,righty,BLACK); // draw base
GuiTwo 0:f2f71eab6aef 648
GuiTwo 0:f2f71eab6aef 649 }
GuiTwo 0:f2f71eab6aef 650
GuiTwo 0:f2f71eab6aef 651 void KS0108::Triangle ( int topx, int topy, int rightx, int righty) {
GuiTwo 0:f2f71eab6aef 652 int base =0;
GuiTwo 0:f2f71eab6aef 653 base = 2* rightx-topx;
GuiTwo 0:f2f71eab6aef 654 //draw rectangle one line at a time
GuiTwo 0:f2f71eab6aef 655 Line( topx,topy, rightx,righty,BLACK ); //draw hypotenuse
GuiTwo 0:f2f71eab6aef 656 Line ( topx,righty,topx,topy,BLACK); //draw perpendicular
GuiTwo 0:f2f71eab6aef 657 Line(topx-base/2,righty, rightx,righty,BLACK); // draw base
GuiTwo 0:f2f71eab6aef 658 Line(topx-base/2, righty, topx,topy,BLACK); // draw hypotenuse
GuiTwo 0:f2f71eab6aef 659
GuiTwo 0:f2f71eab6aef 660 }
GuiTwo 0:f2f71eab6aef 661
GuiTwo 0:f2f71eab6aef 662
GuiTwo 0:f2f71eab6aef 663
GuiTwo 0:f2f71eab6aef 664
GuiTwo 0:f2f71eab6aef 665 /***********************************************************************************/
GuiTwo 0:f2f71eab6aef 666
GuiTwo 0:f2f71eab6aef 667
GuiTwo 0:f2f71eab6aef 668 void KS0108::FullScreenBMP (unsigned char *PictureData){
GuiTwo 0:f2f71eab6aef 669 unsigned int Page=0;
GuiTwo 0:f2f71eab6aef 670 unsigned int Column=0;
GuiTwo 0:f2f71eab6aef 671
GuiTwo 0:f2f71eab6aef 672 // Draw left side of the picture
GuiTwo 0:f2f71eab6aef 673 SelectSide(LEFT);
GuiTwo 0:f2f71eab6aef 674 for (Page = 0; Page < 8; Page++){ /* loop on the 8 pages */
GuiTwo 0:f2f71eab6aef 675 WriteInstruction(LCD_SET_PAGE | Page,LEFT); /* Set the page */
GuiTwo 0:f2f71eab6aef 676 for (Column = 0; Column < 64; Column++)
GuiTwo 0:f2f71eab6aef 677 WriteData(PictureData[(128*Page)+Column],LEFT);
GuiTwo 0:f2f71eab6aef 678 }
GuiTwo 0:f2f71eab6aef 679
GuiTwo 0:f2f71eab6aef 680 // Draw right side of the picture
GuiTwo 0:f2f71eab6aef 681 SelectSide(RIGHT);
GuiTwo 0:f2f71eab6aef 682 for (Page = 0; Page < 8; Page++){ /* loop on the 8 pages */
GuiTwo 0:f2f71eab6aef 683
GuiTwo 0:f2f71eab6aef 684 WriteInstruction(LCD_SET_PAGE| Page,RIGHT); /* Set the page */
GuiTwo 0:f2f71eab6aef 685 for (Column = 64; Column < 128; Column++)
GuiTwo 0:f2f71eab6aef 686 WriteData(PictureData[(128*Page)+Column],RIGHT);
GuiTwo 0:f2f71eab6aef 687 }
GuiTwo 0:f2f71eab6aef 688 }
GuiTwo 0:f2f71eab6aef 689
GuiTwo 0:f2f71eab6aef 690 unsigned int KS0108::ReadArrayData(const unsigned int* ptr) {
GuiTwo 0:f2f71eab6aef 691 return (*ptr);
GuiTwo 0:f2f71eab6aef 692 }
GuiTwo 0:f2f71eab6aef 693
GuiTwo 0:f2f71eab6aef 694 void KS0108::DrawBitmap(const unsigned int * bitmap, unsigned int x, unsigned int y, unsigned int color){
GuiTwo 0:f2f71eab6aef 695 unsigned int width, height;
GuiTwo 0:f2f71eab6aef 696 unsigned int i, j;
GuiTwo 0:f2f71eab6aef 697
GuiTwo 0:f2f71eab6aef 698 width = ReadArrayData(bitmap++);
GuiTwo 0:f2f71eab6aef 699 height = ReadArrayData(bitmap++);
GuiTwo 0:f2f71eab6aef 700 for(j = 0; j < height / 8; j++) {
GuiTwo 0:f2f71eab6aef 701 GotoXY(x, y + (j*8) );
GuiTwo 0:f2f71eab6aef 702 for(i = 0; i < width; i++) {
GuiTwo 0:f2f71eab6aef 703 unsigned int displayData = ReadArrayData(bitmap++);
GuiTwo 0:f2f71eab6aef 704 if(color == BLACK)
GuiTwo 0:f2f71eab6aef 705 WriteData(displayData);
GuiTwo 0:f2f71eab6aef 706 else
GuiTwo 0:f2f71eab6aef 707 WriteData(~displayData);
GuiTwo 0:f2f71eab6aef 708 }
GuiTwo 0:f2f71eab6aef 709 }
GuiTwo 0:f2f71eab6aef 710 }
GuiTwo 0:f2f71eab6aef 711 /******************************************************************************************/
GuiTwo 0:f2f71eab6aef 712
GuiTwo 0:f2f71eab6aef 713
GuiTwo 0:f2f71eab6aef 714 void KS0108::GotoXY(unsigned int x, unsigned int y) {
GuiTwo 0:f2f71eab6aef 715 unsigned int chip, cmd;
GuiTwo 0:f2f71eab6aef 716
GuiTwo 0:f2f71eab6aef 717 if( (x > SCREEN_WIDTH-1) || (y > SCREEN_HEIGHT-1) ) // exit if coordinates are not legal
GuiTwo 0:f2f71eab6aef 718 return;
GuiTwo 0:f2f71eab6aef 719 Coord.x = x; // save new coordinates
GuiTwo 0:f2f71eab6aef 720 Coord.y = y;
GuiTwo 0:f2f71eab6aef 721
GuiTwo 0:f2f71eab6aef 722 if(y/8 != Coord.page) {
GuiTwo 0:f2f71eab6aef 723 Coord.page = y/8;
GuiTwo 0:f2f71eab6aef 724 cmd = LCD_SET_PAGE | Coord.page; // set y address on all chips
GuiTwo 0:f2f71eab6aef 725 for(chip=0; chip < 2; chip++){
GuiTwo 0:f2f71eab6aef 726 WriteInstruction(cmd, chip);
GuiTwo 0:f2f71eab6aef 727 }
GuiTwo 0:f2f71eab6aef 728 }
GuiTwo 0:f2f71eab6aef 729 chip = Coord.x/64; //select chip
GuiTwo 0:f2f71eab6aef 730 x = x % 64; // valeur de X quelque soit le CS affich&#65533;
GuiTwo 0:f2f71eab6aef 731 cmd = LCD_SET_ADD | x;
GuiTwo 0:f2f71eab6aef 732 WriteInstruction(cmd, chip); // set x address on active chip
GuiTwo 0:f2f71eab6aef 733
GuiTwo 0:f2f71eab6aef 734 }
GuiTwo 0:f2f71eab6aef 735
GuiTwo 0:f2f71eab6aef 736
GuiTwo 0:f2f71eab6aef 737 /*****************************************************************************************/
GuiTwo 0:f2f71eab6aef 738
GuiTwo 0:f2f71eab6aef 739
GuiTwo 0:f2f71eab6aef 740
GuiTwo 0:f2f71eab6aef 741 void KS0108::Putchar (int page, int col,unsigned char c) {
GuiTwo 0:f2f71eab6aef 742 if (c>31 && c<127){
GuiTwo 0:f2f71eab6aef 743 for(int i=0;i<5;i++){
GuiTwo 0:f2f71eab6aef 744 WriteDataColPag(page,col+i,System5x7[((c-32)*5+i)+6]);
GuiTwo 0:f2f71eab6aef 745 }
GuiTwo 0:f2f71eab6aef 746 }
GuiTwo 0:f2f71eab6aef 747 }
GuiTwo 0:f2f71eab6aef 748
GuiTwo 0:f2f71eab6aef 749
GuiTwo 0:f2f71eab6aef 750
GuiTwo 0:f2f71eab6aef 751 void KS0108::PutString(unsigned int x, unsigned int y,char* str){
GuiTwo 0:f2f71eab6aef 752
GuiTwo 0:f2f71eab6aef 753 while(*str != 0){
GuiTwo 0:f2f71eab6aef 754 Putchar(x,y,*str);
GuiTwo 0:f2f71eab6aef 755 str++;
GuiTwo 0:f2f71eab6aef 756 y+=System5x7[2];
GuiTwo 0:f2f71eab6aef 757 }
GuiTwo 0:f2f71eab6aef 758
GuiTwo 0:f2f71eab6aef 759 }
GuiTwo 0:f2f71eab6aef 760
GuiTwo 0:f2f71eab6aef 761 void KS0108::PrintFloat(float val, unsigned int x,unsigned int y){
GuiTwo 0:f2f71eab6aef 762 char buf[20] = {}; // prints up to 20 digits
GuiTwo 0:f2f71eab6aef 763 sprintf(buf,"%f",val);
GuiTwo 0:f2f71eab6aef 764 PutString(x,y,buf);
GuiTwo 0:f2f71eab6aef 765
GuiTwo 0:f2f71eab6aef 766 }
GuiTwo 0:f2f71eab6aef 767
GuiTwo 0:f2f71eab6aef 768
GuiTwo 0:f2f71eab6aef 769 void KS0108::PrintInteger(int val,unsigned int x,unsigned int y){
GuiTwo 0:f2f71eab6aef 770 char buf[20] = {}; // prints up to 20 digits
GuiTwo 0:f2f71eab6aef 771 sprintf(buf,"%d",val);
GuiTwo 0:f2f71eab6aef 772 PutString(x,y,buf);
GuiTwo 0:f2f71eab6aef 773 }
GuiTwo 0:f2f71eab6aef 774
GuiTwo 0:f2f71eab6aef 775 void KS0108::SelectFont(unsigned int* font,unsigned int color, FontCallback callback) {
GuiTwo 0:f2f71eab6aef 776 Font = font;
GuiTwo 0:f2f71eab6aef 777 FontRead = callback;
GuiTwo 0:f2f71eab6aef 778 FontColor = color;
GuiTwo 0:f2f71eab6aef 779 }
GuiTwo 0:f2f71eab6aef 780
GuiTwo 0:f2f71eab6aef 781
GuiTwo 0:f2f71eab6aef 782 int KS0108::PrintChar(char c) {
GuiTwo 0:f2f71eab6aef 783 unsigned int width = 0;
GuiTwo 0:f2f71eab6aef 784 unsigned int height = FontRead(Font+FONT_HEIGHT);
GuiTwo 0:f2f71eab6aef 785 unsigned int bytes = (height+7)/8;
GuiTwo 0:f2f71eab6aef 786
GuiTwo 0:f2f71eab6aef 787 unsigned int firstChar = FontRead(Font+FONT_FIRST_CHAR);
GuiTwo 0:f2f71eab6aef 788 unsigned int charCount = FontRead(Font+FONT_CHAR_COUNT);
GuiTwo 0:f2f71eab6aef 789
GuiTwo 0:f2f71eab6aef 790 unsigned int index = 0;
GuiTwo 0:f2f71eab6aef 791 unsigned int x=Coord.x , y=Coord.y;
GuiTwo 0:f2f71eab6aef 792
GuiTwo 0:f2f71eab6aef 793 if(c < firstChar || c >= (firstChar+charCount)) {
GuiTwo 0:f2f71eab6aef 794 return 1;
GuiTwo 0:f2f71eab6aef 795 }
GuiTwo 0:f2f71eab6aef 796 c-= firstChar;
GuiTwo 0:f2f71eab6aef 797
GuiTwo 0:f2f71eab6aef 798 if( FontRead(Font+FONT_LENGTH) == 0 && FontRead(Font+FONT_LENGTH+1) == 0) {////////////////////////////
GuiTwo 0:f2f71eab6aef 799 // zero length is flag indicating fixed width font (array does not contain width data entries)
GuiTwo 0:f2f71eab6aef 800 width = FontRead(Font+FONT_FIXED_WIDTH);
GuiTwo 0:f2f71eab6aef 801 index = c*bytes*width+FONT_WIDTH_TABLE;
GuiTwo 0:f2f71eab6aef 802 }
GuiTwo 0:f2f71eab6aef 803 else{
GuiTwo 0:f2f71eab6aef 804 // variable width font, read width data, to get the index
GuiTwo 0:f2f71eab6aef 805 for(unsigned int i=0; i<c; i++) {
GuiTwo 0:f2f71eab6aef 806 index += FontRead(Font+FONT_WIDTH_TABLE+i);
GuiTwo 0:f2f71eab6aef 807 }
GuiTwo 0:f2f71eab6aef 808 index = index*bytes+charCount+FONT_WIDTH_TABLE;
GuiTwo 0:f2f71eab6aef 809 width = FontRead(Font+FONT_WIDTH_TABLE+c);
GuiTwo 0:f2f71eab6aef 810 }
GuiTwo 0:f2f71eab6aef 811
GuiTwo 0:f2f71eab6aef 812 // last but not least, draw the character
GuiTwo 0:f2f71eab6aef 813 for(unsigned int i=0; i<bytes; i++) {
GuiTwo 0:f2f71eab6aef 814 unsigned int page = i*width;
GuiTwo 0:f2f71eab6aef 815 for(unsigned int j=0; j<width; j++) {
GuiTwo 0:f2f71eab6aef 816 unsigned int data = FontRead(Font+index+page+j);
GuiTwo 0:f2f71eab6aef 817
GuiTwo 0:f2f71eab6aef 818 if(height > 8 && height < (i+1)*8) {
GuiTwo 0:f2f71eab6aef 819 data >>= (i+1)*8-height;
GuiTwo 0:f2f71eab6aef 820 }
GuiTwo 0:f2f71eab6aef 821
GuiTwo 0:f2f71eab6aef 822 WriteData(data);
GuiTwo 0:f2f71eab6aef 823
GuiTwo 0:f2f71eab6aef 824 }
GuiTwo 0:f2f71eab6aef 825 // 1px gap between chars
GuiTwo 0:f2f71eab6aef 826 WriteData(0x00);
GuiTwo 0:f2f71eab6aef 827 GotoXY(x,y+8);//GotoXY(x,Coord.y+8);
GuiTwo 0:f2f71eab6aef 828
GuiTwo 0:f2f71eab6aef 829 }
GuiTwo 0:f2f71eab6aef 830 GotoXY(x+width+1, y);
GuiTwo 0:f2f71eab6aef 831
GuiTwo 0:f2f71eab6aef 832
GuiTwo 0:f2f71eab6aef 833 return 0;
GuiTwo 0:f2f71eab6aef 834 }
GuiTwo 0:f2f71eab6aef 835
GuiTwo 0:f2f71eab6aef 836 void KS0108::PrintString(char* str) {
GuiTwo 0:f2f71eab6aef 837 int x = Coord.x;
GuiTwo 0:f2f71eab6aef 838 while(*str != 0) {
GuiTwo 0:f2f71eab6aef 839 if(*str == '\n') {
GuiTwo 0:f2f71eab6aef 840 GotoXY(x, Coord.y+ FontRead(Font+FONT_HEIGHT));
GuiTwo 0:f2f71eab6aef 841 } else {
GuiTwo 0:f2f71eab6aef 842 PrintChar(*str);
GuiTwo 0:f2f71eab6aef 843 }
GuiTwo 0:f2f71eab6aef 844 str++;
GuiTwo 0:f2f71eab6aef 845 }
GuiTwo 0:f2f71eab6aef 846 }
GuiTwo 0:f2f71eab6aef 847
GuiTwo 0:f2f71eab6aef 848 void KS0108::PrintNumber(long n){
GuiTwo 0:f2f71eab6aef 849 char buf[10]; // prints up to 10 digits
GuiTwo 0:f2f71eab6aef 850 char i=0;
GuiTwo 0:f2f71eab6aef 851 if(n==0)
GuiTwo 0:f2f71eab6aef 852 PrintChar('0');
GuiTwo 0:f2f71eab6aef 853 else{
GuiTwo 0:f2f71eab6aef 854 if(n < 0){
GuiTwo 0:f2f71eab6aef 855 PrintChar('-');
GuiTwo 0:f2f71eab6aef 856 n = -n;
GuiTwo 0:f2f71eab6aef 857 }
GuiTwo 0:f2f71eab6aef 858 while(n>0 && i <= 10){
GuiTwo 0:f2f71eab6aef 859 buf[i++] = n % 10; // n % base
GuiTwo 0:f2f71eab6aef 860 n /= 10; // n/= base
GuiTwo 0:f2f71eab6aef 861 }
GuiTwo 0:f2f71eab6aef 862 for(; i >0; i--)
GuiTwo 0:f2f71eab6aef 863 PrintChar((char) (buf[i-1] < 10 ? '0' + buf[i-1] : 'A' + buf[i-1] - 10));
GuiTwo 0:f2f71eab6aef 864 }
GuiTwo 0:f2f71eab6aef 865 }
GuiTwo 0:f2f71eab6aef 866
GuiTwo 0:f2f71eab6aef 867
GuiTwo 0:f2f71eab6aef 868
GuiTwo 0:f2f71eab6aef 869
GuiTwo 0:f2f71eab6aef 870
GuiTwo 0:f2f71eab6aef 871
GuiTwo 0:f2f71eab6aef 872