Classic Snake game for RETRO
DisplayN18.cpp@1:52a4c894086e, 2015-01-12 (annotated)
- Committer:
- Architect
- Date:
- Mon Jan 12 20:23:34 2015 +0000
- Revision:
- 1:52a4c894086e
- Parent:
- 0:5160597f3364
Removed USBDevice since it is not needed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Architect | 0:5160597f3364 | 1 | #include "DisplayN18.h" |
Architect | 0:5160597f3364 | 2 | |
Architect | 0:5160597f3364 | 3 | const uint16_t DisplayN18::White = DisplayN18::rgbToShort(0xff, 0xff, 0xff); |
Architect | 0:5160597f3364 | 4 | const uint16_t DisplayN18::Silver = DisplayN18::rgbToShort(0xc0, 0xc0, 0xc0); |
Architect | 0:5160597f3364 | 5 | const uint16_t DisplayN18::Gray = DisplayN18::rgbToShort(0x80, 0x80, 0x80); |
Architect | 0:5160597f3364 | 6 | const uint16_t DisplayN18::Black = DisplayN18::rgbToShort(0x00, 0x00, 0x00); |
Architect | 0:5160597f3364 | 7 | const uint16_t DisplayN18::Red = DisplayN18::rgbToShort(0xff, 0x00, 0x00); |
Architect | 0:5160597f3364 | 8 | const uint16_t DisplayN18::Maroon = DisplayN18::rgbToShort(0x80, 0x00, 0x00); |
Architect | 0:5160597f3364 | 9 | const uint16_t DisplayN18::Yellow = DisplayN18::rgbToShort(0xff, 0xff, 0x00); |
Architect | 0:5160597f3364 | 10 | const uint16_t DisplayN18::Olive = DisplayN18::rgbToShort(0x80, 0x80, 0x00); |
Architect | 0:5160597f3364 | 11 | const uint16_t DisplayN18::Lime = DisplayN18::rgbToShort(0x00, 0xff, 0x00); |
Architect | 0:5160597f3364 | 12 | const uint16_t DisplayN18::Green = DisplayN18::rgbToShort(0x00, 0x80, 0x00); |
Architect | 0:5160597f3364 | 13 | const uint16_t DisplayN18::Aqua = DisplayN18::rgbToShort(0x00, 0xff, 0xff); |
Architect | 0:5160597f3364 | 14 | const uint16_t DisplayN18::Teal = DisplayN18::rgbToShort(0x00, 0x80, 0x80); |
Architect | 0:5160597f3364 | 15 | const uint16_t DisplayN18::Blue = DisplayN18::rgbToShort(0x00, 0x00, 0xff); |
Architect | 0:5160597f3364 | 16 | const uint16_t DisplayN18::Navy = DisplayN18::rgbToShort(0x00, 0x00, 0x80); |
Architect | 0:5160597f3364 | 17 | const uint16_t DisplayN18::Fuchsia = DisplayN18::rgbToShort(0xff, 0x00, 0xff); |
Architect | 0:5160597f3364 | 18 | const uint16_t DisplayN18::Purple = DisplayN18::rgbToShort(0x80, 0x00, 0x80); |
Architect | 0:5160597f3364 | 19 | |
Architect | 0:5160597f3364 | 20 | DisplayN18::DisplayN18() : resetPin(P0_20), backlightPin(P0_19), rsPin(P0_7), csPin(P0_2), spi(P0_21, P0_22, P1_15) { |
Architect | 0:5160597f3364 | 21 | this->resetPin.write(false); |
Architect | 0:5160597f3364 | 22 | this->backlightPin.write(true); |
Architect | 0:5160597f3364 | 23 | this->rsPin.write(false); |
Architect | 0:5160597f3364 | 24 | |
Architect | 0:5160597f3364 | 25 | this->spi.format(8, 3); |
Architect | 0:5160597f3364 | 26 | this->spi.frequency(15000000); |
Architect | 0:5160597f3364 | 27 | |
Architect | 0:5160597f3364 | 28 | this->initialize(); |
Architect | 0:5160597f3364 | 29 | } |
Architect | 0:5160597f3364 | 30 | |
Architect | 0:5160597f3364 | 31 | void DisplayN18::writeCommand(unsigned char command) { |
Architect | 0:5160597f3364 | 32 | this->rsPin.write(false); |
Architect | 0:5160597f3364 | 33 | |
Architect | 0:5160597f3364 | 34 | this->csPin.write(false); |
Architect | 0:5160597f3364 | 35 | |
Architect | 0:5160597f3364 | 36 | this->spi.write(command); |
Architect | 0:5160597f3364 | 37 | |
Architect | 0:5160597f3364 | 38 | this->csPin.write(true); |
Architect | 0:5160597f3364 | 39 | } |
Architect | 0:5160597f3364 | 40 | |
Architect | 0:5160597f3364 | 41 | void DisplayN18::writeData(unsigned char data) { |
Architect | 0:5160597f3364 | 42 | this->writeData(&data, 1); |
Architect | 0:5160597f3364 | 43 | } |
Architect | 0:5160597f3364 | 44 | |
Architect | 0:5160597f3364 | 45 | void DisplayN18::writeData(const unsigned char* data, unsigned int length) { |
Architect | 0:5160597f3364 | 46 | this->rsPin.write(true); |
Architect | 0:5160597f3364 | 47 | |
Architect | 0:5160597f3364 | 48 | this->csPin.write(false); |
Architect | 0:5160597f3364 | 49 | |
Architect | 0:5160597f3364 | 50 | for (unsigned int i = 0; i < length; i++) |
Architect | 0:5160597f3364 | 51 | this->spi.write(data[i]); |
Architect | 0:5160597f3364 | 52 | |
Architect | 0:5160597f3364 | 53 | this->csPin.write(true); |
Architect | 0:5160597f3364 | 54 | } |
Architect | 0:5160597f3364 | 55 | |
Architect | 0:5160597f3364 | 56 | void DisplayN18::reset() { |
Architect | 0:5160597f3364 | 57 | this->resetPin.write(false); |
Architect | 0:5160597f3364 | 58 | wait_ms(300); |
Architect | 0:5160597f3364 | 59 | |
Architect | 0:5160597f3364 | 60 | this->resetPin.write(true); |
Architect | 0:5160597f3364 | 61 | wait_ms(500); |
Architect | 0:5160597f3364 | 62 | } |
Architect | 0:5160597f3364 | 63 | |
Architect | 0:5160597f3364 | 64 | void DisplayN18::initialize() { |
Architect | 0:5160597f3364 | 65 | this->reset(); |
Architect | 0:5160597f3364 | 66 | |
Architect | 0:5160597f3364 | 67 | this->writeCommand(0x11); |
Architect | 0:5160597f3364 | 68 | |
Architect | 0:5160597f3364 | 69 | wait_ms(120); |
Architect | 0:5160597f3364 | 70 | |
Architect | 0:5160597f3364 | 71 | this->writeCommand(0xB1); |
Architect | 0:5160597f3364 | 72 | this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); |
Architect | 0:5160597f3364 | 73 | this->writeCommand(0xB2); |
Architect | 0:5160597f3364 | 74 | this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); |
Architect | 0:5160597f3364 | 75 | this->writeCommand(0xB3); |
Architect | 0:5160597f3364 | 76 | this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); |
Architect | 0:5160597f3364 | 77 | this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); |
Architect | 0:5160597f3364 | 78 | |
Architect | 0:5160597f3364 | 79 | this->writeCommand(0xB4); |
Architect | 0:5160597f3364 | 80 | this->writeData(0x07); |
Architect | 0:5160597f3364 | 81 | |
Architect | 0:5160597f3364 | 82 | this->writeCommand(0xC0); |
Architect | 0:5160597f3364 | 83 | this->writeData(0xA2); this->writeData(0x02); this->writeData(0x84); |
Architect | 0:5160597f3364 | 84 | this->writeCommand(0xC1); this->writeData(0xC5); |
Architect | 0:5160597f3364 | 85 | this->writeCommand(0xC2); |
Architect | 0:5160597f3364 | 86 | this->writeData(0x0A); this->writeData(0x00); |
Architect | 0:5160597f3364 | 87 | this->writeCommand(0xC3); |
Architect | 0:5160597f3364 | 88 | this->writeData(0x8A); this->writeData(0x2A); |
Architect | 0:5160597f3364 | 89 | this->writeCommand(0xC4); |
Architect | 0:5160597f3364 | 90 | this->writeData(0x8A); this->writeData(0xEE); |
Architect | 0:5160597f3364 | 91 | |
Architect | 0:5160597f3364 | 92 | this->writeCommand(0xC5); |
Architect | 0:5160597f3364 | 93 | this->writeData(0x0E); |
Architect | 0:5160597f3364 | 94 | |
Architect | 0:5160597f3364 | 95 | this->writeCommand(0x36); |
Architect | 0:5160597f3364 | 96 | this->writeData(0xA8); |
Architect | 0:5160597f3364 | 97 | |
Architect | 0:5160597f3364 | 98 | this->writeCommand(0xe0); |
Architect | 0:5160597f3364 | 99 | this->writeData(0x0f); this->writeData(0x1a); |
Architect | 0:5160597f3364 | 100 | this->writeData(0x0f); this->writeData(0x18); |
Architect | 0:5160597f3364 | 101 | this->writeData(0x2f); this->writeData(0x28); |
Architect | 0:5160597f3364 | 102 | this->writeData(0x20); this->writeData(0x22); |
Architect | 0:5160597f3364 | 103 | this->writeData(0x1f); this->writeData(0x1b); |
Architect | 0:5160597f3364 | 104 | this->writeData(0x23); this->writeData(0x37); this->writeData(0x00); |
Architect | 0:5160597f3364 | 105 | this->writeData(0x07); |
Architect | 0:5160597f3364 | 106 | this->writeData(0x02); this->writeData(0x10); |
Architect | 0:5160597f3364 | 107 | |
Architect | 0:5160597f3364 | 108 | this->writeCommand(0xe1); |
Architect | 0:5160597f3364 | 109 | this->writeData(0x0f); this->writeData(0x1b); |
Architect | 0:5160597f3364 | 110 | this->writeData(0x0f); this->writeData(0x17); |
Architect | 0:5160597f3364 | 111 | this->writeData(0x33); this->writeData(0x2c); |
Architect | 0:5160597f3364 | 112 | this->writeData(0x29); this->writeData(0x2e); |
Architect | 0:5160597f3364 | 113 | this->writeData(0x30); this->writeData(0x30); |
Architect | 0:5160597f3364 | 114 | this->writeData(0x39); this->writeData(0x3f); |
Architect | 0:5160597f3364 | 115 | this->writeData(0x00); this->writeData(0x07); |
Architect | 0:5160597f3364 | 116 | this->writeData(0x03); this->writeData(0x10); |
Architect | 0:5160597f3364 | 117 | |
Architect | 0:5160597f3364 | 118 | this->writeCommand(0x2a); |
Architect | 0:5160597f3364 | 119 | this->writeData(0x00); this->writeData(0x00); |
Architect | 0:5160597f3364 | 120 | this->writeData(0x00); this->writeData(0x7f); |
Architect | 0:5160597f3364 | 121 | this->writeCommand(0x2b); |
Architect | 0:5160597f3364 | 122 | this->writeData(0x00); this->writeData(0x00); |
Architect | 0:5160597f3364 | 123 | this->writeData(0x00); this->writeData(0x9f); |
Architect | 0:5160597f3364 | 124 | |
Architect | 0:5160597f3364 | 125 | this->writeCommand(0xF0); |
Architect | 0:5160597f3364 | 126 | this->writeData(0x01); |
Architect | 0:5160597f3364 | 127 | this->writeCommand(0xF6); |
Architect | 0:5160597f3364 | 128 | this->writeData(0x00); |
Architect | 0:5160597f3364 | 129 | |
Architect | 0:5160597f3364 | 130 | this->writeCommand(0x3A); |
Architect | 0:5160597f3364 | 131 | this->writeData(0x05); |
Architect | 0:5160597f3364 | 132 | |
Architect | 0:5160597f3364 | 133 | this->writeCommand(0x29); |
Architect | 0:5160597f3364 | 134 | |
Architect | 0:5160597f3364 | 135 | this->clear(); |
Architect | 0:5160597f3364 | 136 | } |
Architect | 0:5160597f3364 | 137 | |
Architect | 0:5160597f3364 | 138 | void DisplayN18::setClippingArea(unsigned char x, unsigned char y, unsigned char width, unsigned char height) { |
Architect | 0:5160597f3364 | 139 | unsigned char data[4] = { 0x00, 0x00, 0x00, 0x00 }; |
Architect | 0:5160597f3364 | 140 | |
Architect | 0:5160597f3364 | 141 | data[1] = x; |
Architect | 0:5160597f3364 | 142 | data[3] = x + width; |
Architect | 0:5160597f3364 | 143 | this->writeCommand(0x2A); |
Architect | 0:5160597f3364 | 144 | this->writeData(data, 4); |
Architect | 0:5160597f3364 | 145 | |
Architect | 0:5160597f3364 | 146 | data[1] = y; |
Architect | 0:5160597f3364 | 147 | data[3] = y + height; |
Architect | 0:5160597f3364 | 148 | this->writeCommand(0x2B); |
Architect | 0:5160597f3364 | 149 | this->writeData(data, 4); |
Architect | 0:5160597f3364 | 150 | } |
Architect | 0:5160597f3364 | 151 | |
Architect | 0:5160597f3364 | 152 | unsigned short DisplayN18::rgbToShort(unsigned char r, unsigned char g, unsigned char b) { |
Architect | 0:5160597f3364 | 153 | //return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3) ; |
Architect | 0:5160597f3364 | 154 | unsigned short red = r; |
Architect | 0:5160597f3364 | 155 | unsigned short green = g; |
Architect | 0:5160597f3364 | 156 | unsigned short blue = b; |
Architect | 0:5160597f3364 | 157 | |
Architect | 0:5160597f3364 | 158 | red >>= 3; |
Architect | 0:5160597f3364 | 159 | green >>= 2; |
Architect | 0:5160597f3364 | 160 | blue >>= 3; |
Architect | 0:5160597f3364 | 161 | |
Architect | 0:5160597f3364 | 162 | red &= 0x1F; |
Architect | 0:5160597f3364 | 163 | green &= 0x3F; |
Architect | 0:5160597f3364 | 164 | blue &= 0x1F; |
Architect | 0:5160597f3364 | 165 | |
Architect | 0:5160597f3364 | 166 | red <<= 8; |
Architect | 0:5160597f3364 | 167 | blue <<= 3; |
Architect | 0:5160597f3364 | 168 | green = ((green & 0x7) << 13) + ((green & 0x38) >> 3); |
Architect | 0:5160597f3364 | 169 | |
Architect | 0:5160597f3364 | 170 | return red | green | blue; |
Architect | 0:5160597f3364 | 171 | |
Architect | 0:5160597f3364 | 172 | } |
Architect | 0:5160597f3364 | 173 | |
Architect | 0:5160597f3364 | 174 | void DisplayN18::clear(unsigned short backColor) { |
Architect | 0:5160597f3364 | 175 | for (unsigned int i = 0; i < DisplayN18::WIDTH; i += 10) |
Architect | 0:5160597f3364 | 176 | for (unsigned int j = 0; j < DisplayN18::HEIGHT; j += 8) |
Architect | 0:5160597f3364 | 177 | this->fillRect(i, j, 10, 8, backColor); |
Architect | 0:5160597f3364 | 178 | } |
Architect | 0:5160597f3364 | 179 | |
Architect | 0:5160597f3364 | 180 | void DisplayN18::draw(const unsigned short* data, int x, int y, int width, int height) { |
Architect | 0:5160597f3364 | 181 | this->setClippingArea(x, y, width - 1, height - 1); |
Architect | 0:5160597f3364 | 182 | this->writeCommand(0x2C); |
Architect | 0:5160597f3364 | 183 | this->writeData(reinterpret_cast<const unsigned char*>(data), width * height * 2); |
Architect | 0:5160597f3364 | 184 | } |
Architect | 0:5160597f3364 | 185 | |
Architect | 0:5160597f3364 | 186 | void DisplayN18::setPixel(int x, int y, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 187 | this->draw(&foreColor, x, y, 1, 1); |
Architect | 0:5160597f3364 | 188 | } |
Architect | 0:5160597f3364 | 189 | |
Architect | 0:5160597f3364 | 190 | void DisplayN18::fillRect(int x, int y, int width, int height, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 191 | this->setClippingArea(static_cast<unsigned char>(x), static_cast<unsigned char>(y), static_cast<unsigned char>(width - 1), static_cast<unsigned char>(height)); |
Architect | 0:5160597f3364 | 192 | |
Architect | 0:5160597f3364 | 193 | this->writeCommand(0x2C); |
Architect | 0:5160597f3364 | 194 | |
Architect | 0:5160597f3364 | 195 | unsigned short buffer[50]; |
Architect | 0:5160597f3364 | 196 | for (int j = 0; j < sizeof(buffer) / 2; j++) |
Architect | 0:5160597f3364 | 197 | buffer[j] = foreColor; |
Architect | 0:5160597f3364 | 198 | |
Architect | 0:5160597f3364 | 199 | this->rsPin.write(true); |
Architect | 0:5160597f3364 | 200 | |
Architect | 0:5160597f3364 | 201 | int i; |
Architect | 0:5160597f3364 | 202 | for (i = sizeof(buffer); i < height * width * 2; i += sizeof(buffer)) |
Architect | 0:5160597f3364 | 203 | this->writeData(reinterpret_cast<unsigned char*>(buffer), sizeof(buffer)); |
Architect | 0:5160597f3364 | 204 | |
Architect | 0:5160597f3364 | 205 | i -= sizeof(buffer); |
Architect | 0:5160597f3364 | 206 | if (i != height * width * 2) |
Architect | 0:5160597f3364 | 207 | this->writeData(reinterpret_cast<unsigned char*>(buffer), height * width * 2 - i); |
Architect | 0:5160597f3364 | 208 | } |
Architect | 0:5160597f3364 | 209 | |
Architect | 0:5160597f3364 | 210 | void DisplayN18::drawRect(int x, int y, int width, int height, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 211 | this->drawLine(x, y, x + width, y, foreColor); |
Architect | 0:5160597f3364 | 212 | this->drawLine(x, y + height, x + width, y + height, foreColor); |
Architect | 0:5160597f3364 | 213 | this->drawLine(x, y, x, y + height, foreColor); |
Architect | 0:5160597f3364 | 214 | this->drawLine(x + width, y, x + width, y + height, foreColor); |
Architect | 0:5160597f3364 | 215 | } |
Architect | 0:5160597f3364 | 216 | |
Architect | 0:5160597f3364 | 217 | void DisplayN18::fillCircle(int x, int y, int radius, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 218 | int f = 1 - radius; |
Architect | 0:5160597f3364 | 219 | int dd_f_x = 1; |
Architect | 0:5160597f3364 | 220 | int dd_f_y = -2 * radius; |
Architect | 0:5160597f3364 | 221 | int x1 = 0; |
Architect | 0:5160597f3364 | 222 | int y1 = radius; |
Architect | 0:5160597f3364 | 223 | |
Architect | 0:5160597f3364 | 224 | for (int i = y - radius; i <= y + radius; i++) |
Architect | 0:5160597f3364 | 225 | this->setPixel(x, i, foreColor); |
Architect | 0:5160597f3364 | 226 | |
Architect | 0:5160597f3364 | 227 | while (x1 < y1) { |
Architect | 0:5160597f3364 | 228 | if (f >= 0) { |
Architect | 0:5160597f3364 | 229 | y1--; |
Architect | 0:5160597f3364 | 230 | dd_f_y += 2; |
Architect | 0:5160597f3364 | 231 | f += dd_f_y; |
Architect | 0:5160597f3364 | 232 | } |
Architect | 0:5160597f3364 | 233 | |
Architect | 0:5160597f3364 | 234 | x1++; |
Architect | 0:5160597f3364 | 235 | dd_f_x += 2; |
Architect | 0:5160597f3364 | 236 | f += dd_f_x; |
Architect | 0:5160597f3364 | 237 | |
Architect | 0:5160597f3364 | 238 | for (int i = y - y1; i <= y + y1; i++) { |
Architect | 0:5160597f3364 | 239 | this->setPixel(x + x1, i, foreColor); |
Architect | 0:5160597f3364 | 240 | this->setPixel(x - x1, i, foreColor); |
Architect | 0:5160597f3364 | 241 | } |
Architect | 0:5160597f3364 | 242 | |
Architect | 0:5160597f3364 | 243 | for (int i = y - x1; i <= y + x1; i++) { |
Architect | 0:5160597f3364 | 244 | this->setPixel(x + y1, i, foreColor); |
Architect | 0:5160597f3364 | 245 | this->setPixel(x - y1, i, foreColor); |
Architect | 0:5160597f3364 | 246 | } |
Architect | 0:5160597f3364 | 247 | } |
Architect | 0:5160597f3364 | 248 | } |
Architect | 0:5160597f3364 | 249 | |
Architect | 0:5160597f3364 | 250 | void DisplayN18::drawCircle(int x, int y, int radius, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 251 | int f = 1 - radius; |
Architect | 0:5160597f3364 | 252 | int dd_f_x = 1; |
Architect | 0:5160597f3364 | 253 | int dd_f_y = -2 * radius; |
Architect | 0:5160597f3364 | 254 | int x1 = 0; |
Architect | 0:5160597f3364 | 255 | int y1 = radius; |
Architect | 0:5160597f3364 | 256 | |
Architect | 0:5160597f3364 | 257 | this->setPixel(x, y + radius, foreColor); |
Architect | 0:5160597f3364 | 258 | this->setPixel(x, y - radius, foreColor); |
Architect | 0:5160597f3364 | 259 | this->setPixel(x + radius, y, foreColor); |
Architect | 0:5160597f3364 | 260 | this->setPixel(x - radius, y, foreColor); |
Architect | 0:5160597f3364 | 261 | |
Architect | 0:5160597f3364 | 262 | while (x1 < y1) { |
Architect | 0:5160597f3364 | 263 | if (f >= 0) { |
Architect | 0:5160597f3364 | 264 | y1--; |
Architect | 0:5160597f3364 | 265 | dd_f_y += 2; |
Architect | 0:5160597f3364 | 266 | f += dd_f_y; |
Architect | 0:5160597f3364 | 267 | } |
Architect | 0:5160597f3364 | 268 | |
Architect | 0:5160597f3364 | 269 | x1++; |
Architect | 0:5160597f3364 | 270 | dd_f_x += 2; |
Architect | 0:5160597f3364 | 271 | f += dd_f_x; |
Architect | 0:5160597f3364 | 272 | |
Architect | 0:5160597f3364 | 273 | this->setPixel(x + x1, y + y1, foreColor); |
Architect | 0:5160597f3364 | 274 | this->setPixel(x - x1, y + y1, foreColor); |
Architect | 0:5160597f3364 | 275 | this->setPixel(x + x1, y - y1, foreColor); |
Architect | 0:5160597f3364 | 276 | this->setPixel(x - x1, y - y1, foreColor); |
Architect | 0:5160597f3364 | 277 | |
Architect | 0:5160597f3364 | 278 | this->setPixel(x + y1, y + x1, foreColor); |
Architect | 0:5160597f3364 | 279 | this->setPixel(x - y1, y + x1, foreColor); |
Architect | 0:5160597f3364 | 280 | this->setPixel(x + y1, y - x1, foreColor); |
Architect | 0:5160597f3364 | 281 | this->setPixel(x - y1, y - x1, foreColor); |
Architect | 0:5160597f3364 | 282 | } |
Architect | 0:5160597f3364 | 283 | } |
Architect | 0:5160597f3364 | 284 | |
Architect | 0:5160597f3364 | 285 | void DisplayN18::drawLine(int x0, int y0, int x1, int y1, unsigned short foreColor) { |
Architect | 0:5160597f3364 | 286 | if (x0 == x1) { |
Architect | 0:5160597f3364 | 287 | if (y1 < y0) { |
Architect | 0:5160597f3364 | 288 | int temp = y0; |
Architect | 0:5160597f3364 | 289 | y0 = y1; |
Architect | 0:5160597f3364 | 290 | y1 = temp; |
Architect | 0:5160597f3364 | 291 | } |
Architect | 0:5160597f3364 | 292 | |
Architect | 0:5160597f3364 | 293 | this->setClippingArea(static_cast<unsigned char>(x0), static_cast<unsigned char>(y0), 0, static_cast<unsigned char>(y1 - y0 - 1)); |
Architect | 0:5160597f3364 | 294 | this->writeCommand(0x2C); |
Architect | 0:5160597f3364 | 295 | |
Architect | 0:5160597f3364 | 296 | unsigned short data[DisplayN18::STEP]; |
Architect | 0:5160597f3364 | 297 | for (int i = 0; i < DisplayN18::STEP; i++) |
Architect | 0:5160597f3364 | 298 | data[i] = foreColor; |
Architect | 0:5160597f3364 | 299 | |
Architect | 0:5160597f3364 | 300 | for (unsigned char thisY = y0; thisY < y1; thisY += DisplayN18::STEP) |
Architect | 0:5160597f3364 | 301 | this->writeData(reinterpret_cast<unsigned char*>(data), (thisY + DisplayN18::STEP <= y1 ? DisplayN18::STEP : y1 - thisY) * 2); |
Architect | 0:5160597f3364 | 302 | |
Architect | 0:5160597f3364 | 303 | return; |
Architect | 0:5160597f3364 | 304 | } |
Architect | 0:5160597f3364 | 305 | |
Architect | 0:5160597f3364 | 306 | if (y0 == y1) { |
Architect | 0:5160597f3364 | 307 | if (x1 < x0) { |
Architect | 0:5160597f3364 | 308 | int temp = x0; |
Architect | 0:5160597f3364 | 309 | x0 = x1; |
Architect | 0:5160597f3364 | 310 | x1 = temp; |
Architect | 0:5160597f3364 | 311 | } |
Architect | 0:5160597f3364 | 312 | |
Architect | 0:5160597f3364 | 313 | this->setClippingArea(static_cast<unsigned char>(x0), static_cast<unsigned char>(y0), static_cast<unsigned char>(x1 - x0 - 1), 0); |
Architect | 0:5160597f3364 | 314 | this->writeCommand(0x2C); |
Architect | 0:5160597f3364 | 315 | |
Architect | 0:5160597f3364 | 316 | unsigned short data[DisplayN18::STEP]; |
Architect | 0:5160597f3364 | 317 | for (int i = 0; i < DisplayN18::STEP; i++) |
Architect | 0:5160597f3364 | 318 | data[i] = foreColor; |
Architect | 0:5160597f3364 | 319 | |
Architect | 0:5160597f3364 | 320 | for (unsigned char thisX = x0; thisX < x1; thisX += DisplayN18::STEP) |
Architect | 0:5160597f3364 | 321 | this->writeData(reinterpret_cast<unsigned char*>(data), (thisX + DisplayN18::STEP <= x1 ? DisplayN18::STEP : x1 - thisX) * 2); |
Architect | 0:5160597f3364 | 322 | |
Architect | 0:5160597f3364 | 323 | return; |
Architect | 0:5160597f3364 | 324 | } |
Architect | 0:5160597f3364 | 325 | |
Architect | 0:5160597f3364 | 326 | int t; |
Architect | 0:5160597f3364 | 327 | bool steep = ((y1 - y0) < 0 ? -(y1 - y0) : (y1 - y0)) > ((x1 - x0) < 0 ? -(x1 - x0) : (x1 - x0)); |
Architect | 0:5160597f3364 | 328 | |
Architect | 0:5160597f3364 | 329 | if (steep) { |
Architect | 0:5160597f3364 | 330 | t = x0; |
Architect | 0:5160597f3364 | 331 | x0 = y0; |
Architect | 0:5160597f3364 | 332 | y0 = t; |
Architect | 0:5160597f3364 | 333 | t = x1; |
Architect | 0:5160597f3364 | 334 | x1 = y1; |
Architect | 0:5160597f3364 | 335 | y1 = t; |
Architect | 0:5160597f3364 | 336 | } |
Architect | 0:5160597f3364 | 337 | |
Architect | 0:5160597f3364 | 338 | if (x0 > x1) { |
Architect | 0:5160597f3364 | 339 | t = x0; |
Architect | 0:5160597f3364 | 340 | x0 = x1; |
Architect | 0:5160597f3364 | 341 | x1 = t; |
Architect | 0:5160597f3364 | 342 | |
Architect | 0:5160597f3364 | 343 | t = y0; |
Architect | 0:5160597f3364 | 344 | y0 = y1; |
Architect | 0:5160597f3364 | 345 | y1 = t; |
Architect | 0:5160597f3364 | 346 | } |
Architect | 0:5160597f3364 | 347 | |
Architect | 0:5160597f3364 | 348 | int dx, dy; |
Architect | 0:5160597f3364 | 349 | dx = x1 - x0; |
Architect | 0:5160597f3364 | 350 | dy = (y1 - y0) < 0 ? -(y1 - y0) : (y1 - y0); |
Architect | 0:5160597f3364 | 351 | |
Architect | 0:5160597f3364 | 352 | int err = (dx / 2); |
Architect | 0:5160597f3364 | 353 | int ystep; |
Architect | 0:5160597f3364 | 354 | |
Architect | 0:5160597f3364 | 355 | ystep = y0 < y1 ? 1 : -1; |
Architect | 0:5160597f3364 | 356 | |
Architect | 0:5160597f3364 | 357 | for (; x0 < x1; x0++) { |
Architect | 0:5160597f3364 | 358 | if (steep) |
Architect | 0:5160597f3364 | 359 | this->setPixel(y0, x0, foreColor); |
Architect | 0:5160597f3364 | 360 | else |
Architect | 0:5160597f3364 | 361 | this->setPixel(x0, y0, foreColor); |
Architect | 0:5160597f3364 | 362 | |
Architect | 0:5160597f3364 | 363 | err -= dy; |
Architect | 0:5160597f3364 | 364 | |
Architect | 0:5160597f3364 | 365 | if (err < 0) { |
Architect | 0:5160597f3364 | 366 | y0 += (char)ystep; |
Architect | 0:5160597f3364 | 367 | err += dx; |
Architect | 0:5160597f3364 | 368 | } |
Architect | 0:5160597f3364 | 369 | } |
Architect | 0:5160597f3364 | 370 | } |
Architect | 0:5160597f3364 | 371 | |
Architect | 0:5160597f3364 | 372 | unsigned char characters[95 * 5] = { |
Architect | 0:5160597f3364 | 373 | 0x00, 0x00, 0x00, 0x00, 0x00, /* Space 0x20 */ |
Architect | 0:5160597f3364 | 374 | 0x00, 0x00, 0x4f, 0x00, 0x00, /* ! */ |
Architect | 0:5160597f3364 | 375 | 0x00, 0x07, 0x00, 0x07, 0x00, /* " */ |
Architect | 0:5160597f3364 | 376 | 0x14, 0x7f, 0x14, 0x7f, 0x14, /* # */ |
Architect | 0:5160597f3364 | 377 | 0x24, 0x2a, 0x7f, 0x2a, 0x12, /* $ */ |
Architect | 0:5160597f3364 | 378 | 0x23, 0x13, 0x08, 0x64, 0x62, /* % */ |
Architect | 0:5160597f3364 | 379 | 0x36, 0x49, 0x55, 0x22, 0x20, /* & */ |
Architect | 0:5160597f3364 | 380 | 0x00, 0x05, 0x03, 0x00, 0x00, /* ' */ |
Architect | 0:5160597f3364 | 381 | 0x00, 0x1c, 0x22, 0x41, 0x00, /* ( */ |
Architect | 0:5160597f3364 | 382 | 0x00, 0x41, 0x22, 0x1c, 0x00, /* ) */ |
Architect | 0:5160597f3364 | 383 | 0x14, 0x08, 0x3e, 0x08, 0x14, /* // */ |
Architect | 0:5160597f3364 | 384 | 0x08, 0x08, 0x3e, 0x08, 0x08, /* + */ |
Architect | 0:5160597f3364 | 385 | 0x50, 0x30, 0x00, 0x00, 0x00, /* , */ |
Architect | 0:5160597f3364 | 386 | 0x08, 0x08, 0x08, 0x08, 0x08, /* - */ |
Architect | 0:5160597f3364 | 387 | 0x00, 0x60, 0x60, 0x00, 0x00, /* . */ |
Architect | 0:5160597f3364 | 388 | 0x20, 0x10, 0x08, 0x04, 0x02, /* / */ |
Architect | 0:5160597f3364 | 389 | 0x3e, 0x51, 0x49, 0x45, 0x3e, /* 0 0x30 */ |
Architect | 0:5160597f3364 | 390 | 0x00, 0x42, 0x7f, 0x40, 0x00, /* 1 */ |
Architect | 0:5160597f3364 | 391 | 0x42, 0x61, 0x51, 0x49, 0x46, /* 2 */ |
Architect | 0:5160597f3364 | 392 | 0x21, 0x41, 0x45, 0x4b, 0x31, /* 3 */ |
Architect | 0:5160597f3364 | 393 | 0x18, 0x14, 0x12, 0x7f, 0x10, /* 4 */ |
Architect | 0:5160597f3364 | 394 | 0x27, 0x45, 0x45, 0x45, 0x39, /* 5 */ |
Architect | 0:5160597f3364 | 395 | 0x3c, 0x4a, 0x49, 0x49, 0x30, /* 6 */ |
Architect | 0:5160597f3364 | 396 | 0x01, 0x71, 0x09, 0x05, 0x03, /* 7 */ |
Architect | 0:5160597f3364 | 397 | 0x36, 0x49, 0x49, 0x49, 0x36, /* 8 */ |
Architect | 0:5160597f3364 | 398 | 0x06, 0x49, 0x49, 0x29, 0x1e, /* 9 */ |
Architect | 0:5160597f3364 | 399 | 0x00, 0x36, 0x36, 0x00, 0x00, /* : */ |
Architect | 0:5160597f3364 | 400 | 0x00, 0x56, 0x36, 0x00, 0x00, /* ; */ |
Architect | 0:5160597f3364 | 401 | 0x08, 0x14, 0x22, 0x41, 0x00, /* < */ |
Architect | 0:5160597f3364 | 402 | 0x14, 0x14, 0x14, 0x14, 0x14, /* = */ |
Architect | 0:5160597f3364 | 403 | 0x00, 0x41, 0x22, 0x14, 0x08, /* > */ |
Architect | 0:5160597f3364 | 404 | 0x02, 0x01, 0x51, 0x09, 0x06, /* ? */ |
Architect | 0:5160597f3364 | 405 | 0x3e, 0x41, 0x5d, 0x55, 0x1e, /* @ 0x40 */ |
Architect | 0:5160597f3364 | 406 | 0x7e, 0x11, 0x11, 0x11, 0x7e, /* A */ |
Architect | 0:5160597f3364 | 407 | 0x7f, 0x49, 0x49, 0x49, 0x36, /* B */ |
Architect | 0:5160597f3364 | 408 | 0x3e, 0x41, 0x41, 0x41, 0x22, /* C */ |
Architect | 0:5160597f3364 | 409 | 0x7f, 0x41, 0x41, 0x22, 0x1c, /* D */ |
Architect | 0:5160597f3364 | 410 | 0x7f, 0x49, 0x49, 0x49, 0x41, /* E */ |
Architect | 0:5160597f3364 | 411 | 0x7f, 0x09, 0x09, 0x09, 0x01, /* F */ |
Architect | 0:5160597f3364 | 412 | 0x3e, 0x41, 0x49, 0x49, 0x7a, /* G */ |
Architect | 0:5160597f3364 | 413 | 0x7f, 0x08, 0x08, 0x08, 0x7f, /* H */ |
Architect | 0:5160597f3364 | 414 | 0x00, 0x41, 0x7f, 0x41, 0x00, /* I */ |
Architect | 0:5160597f3364 | 415 | 0x20, 0x40, 0x41, 0x3f, 0x01, /* J */ |
Architect | 0:5160597f3364 | 416 | 0x7f, 0x08, 0x14, 0x22, 0x41, /* K */ |
Architect | 0:5160597f3364 | 417 | 0x7f, 0x40, 0x40, 0x40, 0x40, /* L */ |
Architect | 0:5160597f3364 | 418 | 0x7f, 0x02, 0x0c, 0x02, 0x7f, /* M */ |
Architect | 0:5160597f3364 | 419 | 0x7f, 0x04, 0x08, 0x10, 0x7f, /* N */ |
Architect | 0:5160597f3364 | 420 | 0x3e, 0x41, 0x41, 0x41, 0x3e, /* O */ |
Architect | 0:5160597f3364 | 421 | 0x7f, 0x09, 0x09, 0x09, 0x06, /* P 0x50 */ |
Architect | 0:5160597f3364 | 422 | 0x3e, 0x41, 0x51, 0x21, 0x5e, /* Q */ |
Architect | 0:5160597f3364 | 423 | 0x7f, 0x09, 0x19, 0x29, 0x46, /* R */ |
Architect | 0:5160597f3364 | 424 | 0x26, 0x49, 0x49, 0x49, 0x32, /* S */ |
Architect | 0:5160597f3364 | 425 | 0x01, 0x01, 0x7f, 0x01, 0x01, /* T */ |
Architect | 0:5160597f3364 | 426 | 0x3f, 0x40, 0x40, 0x40, 0x3f, /* U */ |
Architect | 0:5160597f3364 | 427 | 0x1f, 0x20, 0x40, 0x20, 0x1f, /* V */ |
Architect | 0:5160597f3364 | 428 | 0x3f, 0x40, 0x38, 0x40, 0x3f, /* W */ |
Architect | 0:5160597f3364 | 429 | 0x63, 0x14, 0x08, 0x14, 0x63, /* X */ |
Architect | 0:5160597f3364 | 430 | 0x07, 0x08, 0x70, 0x08, 0x07, /* Y */ |
Architect | 0:5160597f3364 | 431 | 0x61, 0x51, 0x49, 0x45, 0x43, /* Z */ |
Architect | 0:5160597f3364 | 432 | 0x00, 0x7f, 0x41, 0x41, 0x00, /* [ */ |
Architect | 0:5160597f3364 | 433 | 0x02, 0x04, 0x08, 0x10, 0x20, /* \ */ |
Architect | 0:5160597f3364 | 434 | 0x00, 0x41, 0x41, 0x7f, 0x00, /* ] */ |
Architect | 0:5160597f3364 | 435 | 0x04, 0x02, 0x01, 0x02, 0x04, /* ^ */ |
Architect | 0:5160597f3364 | 436 | 0x40, 0x40, 0x40, 0x40, 0x40, /* _ */ |
Architect | 0:5160597f3364 | 437 | 0x00, 0x00, 0x03, 0x05, 0x00, /* ` 0x60 */ |
Architect | 0:5160597f3364 | 438 | 0x20, 0x54, 0x54, 0x54, 0x78, /* a */ |
Architect | 0:5160597f3364 | 439 | 0x7F, 0x44, 0x44, 0x44, 0x38, /* b */ |
Architect | 0:5160597f3364 | 440 | 0x38, 0x44, 0x44, 0x44, 0x44, /* c */ |
Architect | 0:5160597f3364 | 441 | 0x38, 0x44, 0x44, 0x44, 0x7f, /* d */ |
Architect | 0:5160597f3364 | 442 | 0x38, 0x54, 0x54, 0x54, 0x18, /* e */ |
Architect | 0:5160597f3364 | 443 | 0x04, 0x04, 0x7e, 0x05, 0x05, /* f */ |
Architect | 0:5160597f3364 | 444 | 0x08, 0x54, 0x54, 0x54, 0x3c, /* g */ |
Architect | 0:5160597f3364 | 445 | 0x7f, 0x08, 0x04, 0x04, 0x78, /* h */ |
Architect | 0:5160597f3364 | 446 | 0x00, 0x44, 0x7d, 0x40, 0x00, /* i */ |
Architect | 0:5160597f3364 | 447 | 0x20, 0x40, 0x44, 0x3d, 0x00, /* j */ |
Architect | 0:5160597f3364 | 448 | 0x7f, 0x10, 0x28, 0x44, 0x00, /* k */ |
Architect | 0:5160597f3364 | 449 | 0x00, 0x41, 0x7f, 0x40, 0x00, /* l */ |
Architect | 0:5160597f3364 | 450 | 0x7c, 0x04, 0x7c, 0x04, 0x78, /* m */ |
Architect | 0:5160597f3364 | 451 | 0x7c, 0x08, 0x04, 0x04, 0x78, /* n */ |
Architect | 0:5160597f3364 | 452 | 0x38, 0x44, 0x44, 0x44, 0x38, /* o */ |
Architect | 0:5160597f3364 | 453 | 0x7c, 0x14, 0x14, 0x14, 0x08, /* p 0x70 */ |
Architect | 0:5160597f3364 | 454 | 0x08, 0x14, 0x14, 0x14, 0x7c, /* q */ |
Architect | 0:5160597f3364 | 455 | 0x7c, 0x08, 0x04, 0x04, 0x00, /* r */ |
Architect | 0:5160597f3364 | 456 | 0x48, 0x54, 0x54, 0x54, 0x24, /* s */ |
Architect | 0:5160597f3364 | 457 | 0x04, 0x04, 0x3f, 0x44, 0x44, /* t */ |
Architect | 0:5160597f3364 | 458 | 0x3c, 0x40, 0x40, 0x20, 0x7c, /* u */ |
Architect | 0:5160597f3364 | 459 | 0x1c, 0x20, 0x40, 0x20, 0x1c, /* v */ |
Architect | 0:5160597f3364 | 460 | 0x3c, 0x40, 0x30, 0x40, 0x3c, /* w */ |
Architect | 0:5160597f3364 | 461 | 0x44, 0x28, 0x10, 0x28, 0x44, /* x */ |
Architect | 0:5160597f3364 | 462 | 0x0c, 0x50, 0x50, 0x50, 0x3c, /* y */ |
Architect | 0:5160597f3364 | 463 | 0x44, 0x64, 0x54, 0x4c, 0x44, /* z */ |
Architect | 0:5160597f3364 | 464 | 0x08, 0x36, 0x41, 0x41, 0x00, /* { */ |
Architect | 0:5160597f3364 | 465 | 0x00, 0x00, 0x77, 0x00, 0x00, /* | */ |
Architect | 0:5160597f3364 | 466 | 0x00, 0x41, 0x41, 0x36, 0x08, /* } */ |
Architect | 0:5160597f3364 | 467 | 0x08, 0x08, 0x2a, 0x1c, 0x08 /* ~ */ |
Architect | 0:5160597f3364 | 468 | }; |
Architect | 0:5160597f3364 | 469 | |
Architect | 0:5160597f3364 | 470 | void DisplayN18::drawCharacter(int x, int y, const char character, unsigned short foreColor, unsigned short backColor, unsigned char fontSize) { |
Architect | 0:5160597f3364 | 471 | if (character > 126 || character < 32) |
Architect | 0:5160597f3364 | 472 | return; |
Architect | 0:5160597f3364 | 473 | |
Architect | 0:5160597f3364 | 474 | unsigned short* horizontal = new unsigned short[DisplayN18::CHAR_HEIGHT * fontSize]; |
Architect | 0:5160597f3364 | 475 | for (int i = 0; i < DisplayN18::CHAR_WIDTH; i++) { |
Architect | 0:5160597f3364 | 476 | for (int j = 0; j < DisplayN18::CHAR_HEIGHT; j++) |
Architect | 0:5160597f3364 | 477 | for (int k = 0; k < fontSize; k++) |
Architect | 0:5160597f3364 | 478 | horizontal[j * fontSize + k] = characters[(character - 32) * 5 + i] & (1 << j) ? foreColor : backColor; |
Architect | 0:5160597f3364 | 479 | |
Architect | 0:5160597f3364 | 480 | for (int k = 0; k < fontSize; k++) |
Architect | 0:5160597f3364 | 481 | this->draw(horizontal, x + i * fontSize + k, y, 1, DisplayN18::CHAR_HEIGHT * fontSize); |
Architect | 0:5160597f3364 | 482 | } |
Architect | 0:5160597f3364 | 483 | |
Architect | 0:5160597f3364 | 484 | for (int i = 0; i < DisplayN18::CHAR_HEIGHT; i++) |
Architect | 0:5160597f3364 | 485 | for (int k = 0; k < fontSize; k++) |
Architect | 0:5160597f3364 | 486 | horizontal[i * fontSize + k] = backColor; |
Architect | 0:5160597f3364 | 487 | |
Architect | 0:5160597f3364 | 488 | for (int k = 0; k < fontSize; k++) |
Architect | 0:5160597f3364 | 489 | this->draw(horizontal, x + DisplayN18::CHAR_WIDTH * fontSize + k, y, 1, DisplayN18::CHAR_HEIGHT * fontSize); |
Architect | 0:5160597f3364 | 490 | |
Architect | 0:5160597f3364 | 491 | delete[] horizontal; |
Architect | 0:5160597f3364 | 492 | } |
Architect | 0:5160597f3364 | 493 | |
Architect | 0:5160597f3364 | 494 | void DisplayN18::drawString(int x, int y, const char* str, unsigned short foreColor, unsigned short backColor, unsigned char fontSize) { |
Architect | 0:5160597f3364 | 495 | if (*str == '\0') |
Architect | 0:5160597f3364 | 496 | return; |
Architect | 0:5160597f3364 | 497 | |
Architect | 0:5160597f3364 | 498 | do { |
Architect | 0:5160597f3364 | 499 | this->drawCharacter(x, y, *str, foreColor, backColor, fontSize); |
Architect | 0:5160597f3364 | 500 | |
Architect | 0:5160597f3364 | 501 | x += (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * fontSize; |
Architect | 0:5160597f3364 | 502 | } while (*(++str) != '\0'); |
Architect | 0:5160597f3364 | 503 | } |