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.

Dependencies:   mbed

Fork of RETRO_Pong_Mod by G. Andrew Duthie

This is a mod of the official Pong game released with the RETRO game console.

Committer:
john_ghielec
Date:
Mon Nov 10 13:04:42 2014 +0000
Revision:
0:21669ea33448
Child:
1:cd8a3926f263
Initial commit.

Who changed what in which revision?

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