Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17ajf by
Graphics/Graphics.cpp@17:cc448ab7266f, 2019-04-03 (annotated)
- Committer:
- el17ajf
- Date:
- Wed Apr 03 13:37:56 2019 +0000
- Revision:
- 17:cc448ab7266f
- Parent:
- 16:3f84f2d7b910
- Child:
- 18:24ce897024d0
Added UI and graphics separation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ajf | 5:3efbdcb3efaf | 1 | #include "Graphics.h" |
el17ajf | 17:cc448ab7266f | 2 | #include "Grid.h" |
el17ajf | 5:3efbdcb3efaf | 3 | |
el17ajf | 14:53d2167d7c9a | 4 | namespace Graphics { |
el17ajf | 14:53d2167d7c9a | 5 | |
el17ajf | 17:cc448ab7266f | 6 | namespace LCD { |
el17ajf | 17:cc448ab7266f | 7 | const int MAX_Y = 47; |
el17ajf | 17:cc448ab7266f | 8 | |
el17ajf | 17:cc448ab7266f | 9 | N5110 * lcd; |
el17ajf | 17:cc448ab7266f | 10 | |
el17ajf | 17:cc448ab7266f | 11 | int YToLcdX(int y) { |
el17ajf | 17:cc448ab7266f | 12 | return y; |
el17ajf | 17:cc448ab7266f | 13 | } |
el17ajf | 17:cc448ab7266f | 14 | |
el17ajf | 17:cc448ab7266f | 15 | int XToLcdY(int x) { |
el17ajf | 17:cc448ab7266f | 16 | return MAX_Y - x; |
el17ajf | 17:cc448ab7266f | 17 | } |
el17ajf | 17:cc448ab7266f | 18 | }; |
el17ajf | 14:53d2167d7c9a | 19 | |
el17ajf | 14:53d2167d7c9a | 20 | // "private" methods |
el17ajf | 14:53d2167d7c9a | 21 | void drawPoint(int x, int y); |
el17ajf | 14:53d2167d7c9a | 22 | void drawLine(int x1, int y1, int x2, int y2); |
el17ajf | 14:53d2167d7c9a | 23 | void drawBox(int x1, int y1, int x2, int y2); |
el17ajf | 14:53d2167d7c9a | 24 | void drawDottedLine(int x1, int y1, int x2, int y2); |
el17ajf | 14:53d2167d7c9a | 25 | |
el17ajf | 14:53d2167d7c9a | 26 | void init() { |
el17ajf | 17:cc448ab7266f | 27 | LCD::lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17ajf | 17:cc448ab7266f | 28 | LCD::lcd->init(); |
el17ajf | 17:cc448ab7266f | 29 | LCD::lcd->setContrast(0.5); |
el17ajf | 17:cc448ab7266f | 30 | LCD::lcd->setBrightness(0); |
el17ajf | 14:53d2167d7c9a | 31 | } |
el17ajf | 14:53d2167d7c9a | 32 | |
el17ajf | 14:53d2167d7c9a | 33 | void deinit() { |
el17ajf | 17:cc448ab7266f | 34 | delete LCD::lcd; |
el17ajf | 14:53d2167d7c9a | 35 | } |
el17ajf | 14:53d2167d7c9a | 36 | |
el17ajf | 14:53d2167d7c9a | 37 | void clear() { |
el17ajf | 17:cc448ab7266f | 38 | LCD::lcd->clear(); |
el17ajf | 14:53d2167d7c9a | 39 | } |
el17ajf | 14:53d2167d7c9a | 40 | |
el17ajf | 14:53d2167d7c9a | 41 | void render() { |
el17ajf | 17:cc448ab7266f | 42 | LCD::lcd->refresh(); |
el17ajf | 14:53d2167d7c9a | 43 | } |
el17ajf | 14:53d2167d7c9a | 44 | |
el17ajf | 14:53d2167d7c9a | 45 | void drawPoint(int x, int y) { |
el17ajf | 17:cc448ab7266f | 46 | LCD::lcd->setPixel( |
el17ajf | 17:cc448ab7266f | 47 | LCD::YToLcdX(y), |
el17ajf | 17:cc448ab7266f | 48 | LCD::XToLcdY(x), |
el17ajf | 17:cc448ab7266f | 49 | true |
el17ajf | 17:cc448ab7266f | 50 | ); |
el17ajf | 14:53d2167d7c9a | 51 | } |
el17ajf | 14:53d2167d7c9a | 52 | |
el17ajf | 14:53d2167d7c9a | 53 | void drawLine(int x1, int y1, int x2, int y2) { |
el17ajf | 17:cc448ab7266f | 54 | LCD::lcd->drawLine( |
el17ajf | 17:cc448ab7266f | 55 | LCD::YToLcdX(y1), |
el17ajf | 17:cc448ab7266f | 56 | LCD::XToLcdY(x1), |
el17ajf | 17:cc448ab7266f | 57 | LCD::YToLcdX(y2), |
el17ajf | 17:cc448ab7266f | 58 | LCD::XToLcdY(x2), |
el17ajf | 17:cc448ab7266f | 59 | 1 // not dotted |
el17ajf | 17:cc448ab7266f | 60 | ); |
el17ajf | 14:53d2167d7c9a | 61 | } |
el17ajf | 5:3efbdcb3efaf | 62 | |
el17ajf | 14:53d2167d7c9a | 63 | void drawDottedLine(int x1, int y1, int x2, int y2) { |
el17ajf | 17:cc448ab7266f | 64 | LCD::lcd->drawLine( |
el17ajf | 17:cc448ab7266f | 65 | LCD::YToLcdX(y1), |
el17ajf | 17:cc448ab7266f | 66 | LCD::XToLcdY(x1), |
el17ajf | 17:cc448ab7266f | 67 | LCD::YToLcdX(y2), |
el17ajf | 17:cc448ab7266f | 68 | LCD::XToLcdY(x2), |
el17ajf | 14:53d2167d7c9a | 69 | 2 // dotted |
el17ajf | 14:53d2167d7c9a | 70 | ); |
el17ajf | 14:53d2167d7c9a | 71 | } |
el17ajf | 14:53d2167d7c9a | 72 | |
el17ajf | 14:53d2167d7c9a | 73 | void drawBox(int x1, int y1, int x2, int y2) { |
el17ajf | 14:53d2167d7c9a | 74 | drawLine(x1, y1, x2, y1); |
el17ajf | 14:53d2167d7c9a | 75 | drawLine(x2, y1, x2, y2); |
el17ajf | 14:53d2167d7c9a | 76 | drawLine(x2, y2, x1, y2); |
el17ajf | 14:53d2167d7c9a | 77 | drawLine(x1, y2, x1, y1); |
el17ajf | 14:53d2167d7c9a | 78 | } |
el17ajf | 8:5066ce13a430 | 79 | |
el17ajf | 17:cc448ab7266f | 80 | namespace Game { |
el17ajf | 17:cc448ab7266f | 81 | int getBlockSize() { |
el17ajf | 17:cc448ab7266f | 82 | int blockWidth = SCREEN_WIDTH / Grid::GRID_WIDTH; |
el17ajf | 17:cc448ab7266f | 83 | int blockHeight = SCREEN_HEIGHT / Grid::GRID_HEIGHT; |
el17ajf | 17:cc448ab7266f | 84 | return blockWidth < blockHeight ? blockWidth : blockHeight; |
el17ajf | 17:cc448ab7266f | 85 | } |
el17ajf | 17:cc448ab7266f | 86 | |
el17ajf | 17:cc448ab7266f | 87 | int getBorderSizeX(int blockSize) { |
el17ajf | 17:cc448ab7266f | 88 | int xBlocks = SCREEN_WIDTH / blockSize; |
el17ajf | 17:cc448ab7266f | 89 | return (SCREEN_WIDTH - xBlocks * blockSize) / 2; |
el17ajf | 17:cc448ab7266f | 90 | } |
el17ajf | 17:cc448ab7266f | 91 | |
el17ajf | 17:cc448ab7266f | 92 | int getBorderSizeY(int blockSize) { |
el17ajf | 17:cc448ab7266f | 93 | int yBlocks = SCREEN_HEIGHT / blockSize; |
el17ajf | 17:cc448ab7266f | 94 | return (SCREEN_HEIGHT - yBlocks*blockSize) / 2; |
el17ajf | 17:cc448ab7266f | 95 | } |
el17ajf | 17:cc448ab7266f | 96 | |
el17ajf | 17:cc448ab7266f | 97 | int BLOCK_SIZE = getBlockSize(); |
el17ajf | 17:cc448ab7266f | 98 | int BORDER_SIZE_X = getBorderSizeX(BLOCK_SIZE); |
el17ajf | 17:cc448ab7266f | 99 | int BORDER_SIZE_Y = getBorderSizeY(BLOCK_SIZE); |
el17ajf | 17:cc448ab7266f | 100 | |
el17ajf | 17:cc448ab7266f | 101 | int gridYToY(int grid_y) { |
el17ajf | 17:cc448ab7266f | 102 | return grid_y * BLOCK_SIZE + BORDER_SIZE_Y; |
el17ajf | 17:cc448ab7266f | 103 | } |
el17ajf | 17:cc448ab7266f | 104 | |
el17ajf | 17:cc448ab7266f | 105 | int gridXToX(int grid_x) { |
el17ajf | 17:cc448ab7266f | 106 | return grid_x * BLOCK_SIZE + BORDER_SIZE_X; |
el17ajf | 17:cc448ab7266f | 107 | } |
el17ajf | 17:cc448ab7266f | 108 | |
el17ajf | 17:cc448ab7266f | 109 | void drawBlock(int grid_x, int grid_y) { |
el17ajf | 17:cc448ab7266f | 110 | // screen coords |
el17ajf | 17:cc448ab7266f | 111 | int x = gridXToX(grid_x); |
el17ajf | 17:cc448ab7266f | 112 | int y = gridYToY(grid_y); |
el17ajf | 17:cc448ab7266f | 113 | drawBox(x, y, x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1); |
el17ajf | 17:cc448ab7266f | 114 | drawPoint(x + BLOCK_SIZE / 2, y + BLOCK_SIZE / 2); |
el17ajf | 17:cc448ab7266f | 115 | } |
el17ajf | 17:cc448ab7266f | 116 | |
el17ajf | 17:cc448ab7266f | 117 | void drawBorder() { |
el17ajf | 17:cc448ab7266f | 118 | int minX = gridXToX(0); |
el17ajf | 17:cc448ab7266f | 119 | int maxX = gridXToX(Grid::GRID_WIDTH - 1) + BLOCK_SIZE - 1; |
el17ajf | 17:cc448ab7266f | 120 | int minY = gridYToY(0); |
el17ajf | 17:cc448ab7266f | 121 | int maxY = gridYToY(Grid::GRID_HEIGHT - 1) + BLOCK_SIZE - 1; |
el17ajf | 17:cc448ab7266f | 122 | |
el17ajf | 17:cc448ab7266f | 123 | drawDottedLine(minX, minY, minX, maxY); |
el17ajf | 17:cc448ab7266f | 124 | drawDottedLine(minX, maxY, maxX, maxY); |
el17ajf | 17:cc448ab7266f | 125 | drawDottedLine(maxX, maxY, maxX, minY); |
el17ajf | 17:cc448ab7266f | 126 | } |
el17ajf | 17:cc448ab7266f | 127 | }; |
el17ajf | 17:cc448ab7266f | 128 | |
el17ajf | 17:cc448ab7266f | 129 | namespace UI { |
el17ajf | 17:cc448ab7266f | 130 | void drawBorder(int x1, int y1, int x2, int y2) { |
el17ajf | 17:cc448ab7266f | 131 | drawBox(x1, y1, x2, y2); |
el17ajf | 17:cc448ab7266f | 132 | } |
el17ajf | 16:3f84f2d7b910 | 133 | |
el17ajf | 17:cc448ab7266f | 134 | void drawChar(int x, int y, char c); |
el17ajf | 17:cc448ab7266f | 135 | |
el17ajf | 17:cc448ab7266f | 136 | void drawText(int x, int y, const char * text) { |
el17ajf | 17:cc448ab7266f | 137 | for (int i = 0; text[i] != '\0'; i++) { |
el17ajf | 17:cc448ab7266f | 138 | drawChar(x + i * (CHAR_WIDTH + CHAR_SPACE), y, text[i]); |
el17ajf | 17:cc448ab7266f | 139 | } |
el17ajf | 17:cc448ab7266f | 140 | } |
el17ajf | 14:53d2167d7c9a | 141 | |
el17ajf | 17:cc448ab7266f | 142 | void drawChar(int x, int y, char c) { |
el17ajf | 17:cc448ab7266f | 143 | int binc = 0b1111111111111111111111111111; |
el17ajf | 17:cc448ab7266f | 144 | |
el17ajf | 17:cc448ab7266f | 145 | switch (c) { |
el17ajf | 17:cc448ab7266f | 146 | case 'a': case 'A': binc = 0b0000011010011001011100000000; break; |
el17ajf | 17:cc448ab7266f | 147 | case 'b': case 'B': binc = 0b1000111010011001111100000000; break; |
el17ajf | 17:cc448ab7266f | 148 | case 'c': case 'C': binc = 0b0000011110001000111100000000; break; |
el17ajf | 17:cc448ab7266f | 149 | case 'd': case 'D': binc = 0b0001011110011001111100000000; break; |
el17ajf | 17:cc448ab7266f | 150 | case 'e': case 'E': binc = 0b0000111110011000011000000000; break; |
el17ajf | 17:cc448ab7266f | 151 | case 'f': case 'F': binc = 0b0000001101000100011101000000; break; |
el17ajf | 17:cc448ab7266f | 152 | case 'g': case 'G': binc = 0b0000011110011001111100010110; break; |
el17ajf | 17:cc448ab7266f | 153 | case 'h': case 'H': binc = 0b1000111010011001100100000000; break; |
el17ajf | 17:cc448ab7266f | 154 | case 'i': case 'I': binc = 0b0100000001000100001000000000; break; |
el17ajf | 17:cc448ab7266f | 155 | case 'j': case 'J': binc = 0b0010000000100010001001000000; break; |
el17ajf | 17:cc448ab7266f | 156 | case 'k': case 'K': binc = 0b1000101011001010101000000000; break; |
el17ajf | 17:cc448ab7266f | 157 | case 'l': case 'L': binc = 0b0100010001000100001000000000; break; |
el17ajf | 17:cc448ab7266f | 158 | case 'm': case 'M': binc = 0b0000111110111001100100000000; break; |
el17ajf | 17:cc448ab7266f | 159 | case 'n': case 'N': binc = 0b0000111010011001100100000000; break; |
el17ajf | 17:cc448ab7266f | 160 | case 'o': case 'O': binc = 0b0000111110011001111100000000; break; |
el17ajf | 17:cc448ab7266f | 161 | case 'p': case 'P': binc = 0b0000111010011001111110000000; break; |
el17ajf | 17:cc448ab7266f | 162 | case 'q': case 'Q': binc = 0b0000011110011001111100010001; break; |
el17ajf | 17:cc448ab7266f | 163 | case 'r': case 'R': binc = 0b0000001001000100010000000000; break; |
el17ajf | 17:cc448ab7266f | 164 | case 's': case 'S': binc = 0b0000011111000011111000000000; break; |
el17ajf | 17:cc448ab7266f | 165 | case 't': case 'T': binc = 0b0100111001000100001000000000; break; |
el17ajf | 17:cc448ab7266f | 166 | case 'u': case 'U': binc = 0b0000100110011001011100000000; break; |
el17ajf | 17:cc448ab7266f | 167 | case 'v': case 'V': binc = 0b0000100110011001011000000000; break; |
el17ajf | 17:cc448ab7266f | 168 | case 'w': case 'W': binc = 0b0000100110011011111100000000; break; |
el17ajf | 17:cc448ab7266f | 169 | case 'x': case 'X': binc = 0b0000100101000010100100000000; break; |
el17ajf | 17:cc448ab7266f | 170 | case 'y': case 'Y': binc = 0b0000100110011001011100010110; break; |
el17ajf | 17:cc448ab7266f | 171 | case 'z': case 'Z': binc = 0b0000111000111100011100000000; break; |
el17ajf | 17:cc448ab7266f | 172 | case '!': binc = 0b0010001000100000001000000000; break; |
el17ajf | 17:cc448ab7266f | 173 | case ' ': binc = 0b0000000000000000000000000000; break; |
el17ajf | 17:cc448ab7266f | 174 | default: break; |
el17ajf | 17:cc448ab7266f | 175 | } |
el17ajf | 17:cc448ab7266f | 176 | |
el17ajf | 17:cc448ab7266f | 177 | for (int n = 0; n <= 27; n++) { |
el17ajf | 17:cc448ab7266f | 178 | if (binc & 0b1000000000000000000000000000 >> n) { |
el17ajf | 17:cc448ab7266f | 179 | int dx = n % CHAR_WIDTH; |
el17ajf | 17:cc448ab7266f | 180 | int dy = n / CHAR_WIDTH; |
el17ajf | 17:cc448ab7266f | 181 | drawPoint(x + dx, y + dy); |
el17ajf | 17:cc448ab7266f | 182 | } |
el17ajf | 17:cc448ab7266f | 183 | } |
el17ajf | 17:cc448ab7266f | 184 | } |
el17ajf | 17:cc448ab7266f | 185 | }; |
el17ajf | 14:53d2167d7c9a | 186 | }; |