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