ELEC2645 (2018/19) / Mbed 2 deprecated el17ajf

Dependencies:   mbed

Fork of el17ajf by Angus Findlay

Committer:
el17ajf
Date:
Sun Mar 17 10:55:11 2019 +0000
Revision:
11:fba7d54fd36b
Parent:
10:5762d7fae033
Child:
12:beb0d7632531
got random tetrominos displaying on the screen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17ajf 5:3efbdcb3efaf 1 #include "Graphics.h"
el17ajf 5:3efbdcb3efaf 2
el17ajf 10:5762d7fae033 3 N5110 * Graphics::lcd;
el17ajf 10:5762d7fae033 4
el17ajf 8:5066ce13a430 5 void Graphics::init() {
el17ajf 8:5066ce13a430 6 lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el17ajf 9:3a7776a29a11 7 lcd->init();
el17ajf 11:fba7d54fd36b 8 lcd->setContrast(0.5);
el17ajf 8:5066ce13a430 9 }
el17ajf 8:5066ce13a430 10
el17ajf 8:5066ce13a430 11 void Graphics::deinit() {
el17ajf 8:5066ce13a430 12 delete lcd;
el17ajf 8:5066ce13a430 13 }
el17ajf 8:5066ce13a430 14
el17ajf 5:3efbdcb3efaf 15 void Graphics::clear() {
el17ajf 9:3a7776a29a11 16 lcd->clear();
el17ajf 5:3efbdcb3efaf 17 }
el17ajf 5:3efbdcb3efaf 18
el17ajf 5:3efbdcb3efaf 19 void Graphics::render() {
el17ajf 9:3a7776a29a11 20 lcd->refresh();
el17ajf 8:5066ce13a430 21 }
el17ajf 8:5066ce13a430 22
el17ajf 8:5066ce13a430 23 void Graphics::drawPoint(int x, int y) {
el17ajf 8:5066ce13a430 24 lcd->setPixel(x, y, true);
el17ajf 8:5066ce13a430 25 }
el17ajf 8:5066ce13a430 26
el17ajf 8:5066ce13a430 27 void Graphics::drawLine(int x1, int y1, int x2, int y2) {
el17ajf 8:5066ce13a430 28 lcd->drawLine(x1, y1, x2, y2, 1);
el17ajf 8:5066ce13a430 29 }
el17ajf 8:5066ce13a430 30
el17ajf 8:5066ce13a430 31 void Graphics::drawDottedLine(int x1, int y1, int x2, int y2) {
el17ajf 11:fba7d54fd36b 32 lcd->drawLine(
el17ajf 11:fba7d54fd36b 33 y1,
el17ajf 11:fba7d54fd36b 34 x1,
el17ajf 11:fba7d54fd36b 35 y2,
el17ajf 11:fba7d54fd36b 36 x2,
el17ajf 11:fba7d54fd36b 37 2 // dotted
el17ajf 11:fba7d54fd36b 38 );
el17ajf 8:5066ce13a430 39 }
el17ajf 8:5066ce13a430 40
el17ajf 8:5066ce13a430 41 void Graphics::drawBox(int x1, int y1, int x2, int y2) {
el17ajf 8:5066ce13a430 42 Graphics::drawLine(x1, y1, x2, y1);
el17ajf 8:5066ce13a430 43 Graphics::drawLine(x2, y1, x2, y2);
el17ajf 8:5066ce13a430 44 Graphics::drawLine(x2, y2, x1, y2);
el17ajf 8:5066ce13a430 45 Graphics::drawLine(x1, y2, x1, y1);
el17ajf 8:5066ce13a430 46 }
el17ajf 8:5066ce13a430 47
el17ajf 8:5066ce13a430 48 void Graphics::drawBlock(int grid_x, int grid_y) {
el17ajf 11:fba7d54fd36b 49 // screen coords
el17ajf 11:fba7d54fd36b 50 int x = gridYToScreenX(grid_y);
el17ajf 11:fba7d54fd36b 51 int y = gridXToScreenY(grid_x);
el17ajf 11:fba7d54fd36b 52 Graphics::drawBox(x, y, x + 3, y - 3);
el17ajf 11:fba7d54fd36b 53 Graphics::drawPoint(x + 2, y - 2);
el17ajf 8:5066ce13a430 54 }
el17ajf 8:5066ce13a430 55
el17ajf 8:5066ce13a430 56 void Graphics::drawBorder() {
el17ajf 10:5762d7fae033 57 Graphics::drawDottedLine(0, 0, 0, SCREEN_HEIGHT - 1);
el17ajf 10:5762d7fae033 58 Graphics::drawDottedLine(1, 1, 1, SCREEN_HEIGHT - 2);
el17ajf 5:3efbdcb3efaf 59
el17ajf 10:5762d7fae033 60 Graphics::drawDottedLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1);
el17ajf 10:5762d7fae033 61 Graphics::drawDottedLine(1, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2);
el17ajf 8:5066ce13a430 62
el17ajf 10:5762d7fae033 63 Graphics::drawDottedLine(SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, 0);
el17ajf 10:5762d7fae033 64 Graphics::drawDottedLine(SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, 1);
el17ajf 11:fba7d54fd36b 65 }
el17ajf 11:fba7d54fd36b 66
el17ajf 11:fba7d54fd36b 67 int Graphics::gridYToScreenX(int grid_y) {
el17ajf 11:fba7d54fd36b 68 return grid_y * BLOCK_SIZE + BORDER_SIZE;
el17ajf 11:fba7d54fd36b 69 }
el17ajf 11:fba7d54fd36b 70
el17ajf 11:fba7d54fd36b 71 int Graphics::gridXToScreenY(int grid_x) {
el17ajf 11:fba7d54fd36b 72 return SCREEN_WIDTH - (grid_x * BLOCK_SIZE) - BORDER_SIZE;
el17ajf 11:fba7d54fd36b 73 }