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