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