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