ELEC2645 (2018/19) / Mbed 2 deprecated el17ajf

Dependencies:   mbed

Fork of el17ajf by Angus Findlay

Revision:
14:53d2167d7c9a
Parent:
13:59e17cab320a
Child:
15:afeefa3ceb61
--- a/Graphics/Graphics.cpp	Mon Mar 18 18:09:57 2019 +0000
+++ b/Graphics/Graphics.cpp	Mon Mar 18 18:26:11 2019 +0000
@@ -1,79 +1,89 @@
 #include "Graphics.h"
 
-const int Graphics::SCREEN_HEIGHT = 84;
-const int Graphics::SCREEN_WIDTH = 48;
-const int Graphics::BLOCK_SIZE = 4;
-const int Graphics::BORDER_SIZE = 2;
-
-N5110 * Graphics::lcd;
-
-void Graphics::init() {
-    lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-    lcd->init();
-    lcd->setContrast(0.5);
-    lcd->setBrightness(0);
-}
-
-void Graphics::deinit() {
-    delete lcd;
-}
-
-void Graphics::clear() {
-    lcd->clear();
-}
-
-void Graphics::render() {
-    lcd->refresh();
-}
-
-void Graphics::drawPoint(int x, int y) {
-    lcd->setPixel(x, y, true);
-}
-
-void Graphics::drawLine(int x1, int y1, int x2, int y2) {
-    lcd->drawLine(x1, y1, x2, y2, 1);
-}
-
-void Graphics::drawDottedLine(int x1, int y1, int x2, int y2) {
-    lcd->drawLine(
-        y1,
-        x1,
-        y2,
-        x2,
-        2 // dotted
-    );
-}
-
-void Graphics::drawBox(int x1, int y1, int x2, int y2) {
-    Graphics::drawLine(x1, y1, x2, y1);
-    Graphics::drawLine(x2, y1, x2, y2);
-    Graphics::drawLine(x2, y2, x1, y2);
-    Graphics::drawLine(x1, y2, x1, y1);
-}
-
-void Graphics::drawBlock(int grid_x, int grid_y) {
-    // screen coords
-    int x = gridYToScreenX(grid_y);
-    int y = gridXToScreenY(grid_x);
-    Graphics::drawBox(x, y, x + 3, y - 3);
-    Graphics::drawPoint(x + 2, y - 2); 
-}
-
-void Graphics::drawBorder() {
-    Graphics::drawDottedLine(0, 0, 0, SCREEN_HEIGHT - 1);
-    Graphics::drawDottedLine(1, 1, 1, SCREEN_HEIGHT - 2);
+namespace Graphics {
+    const int SCREEN_HEIGHT = 84;
+    const int SCREEN_WIDTH = 48;
+    const int BLOCK_SIZE = 4;
+    const int BORDER_SIZE = 2;
+    
+    N5110 * lcd;
+    
+    // "private" methods
+    void drawPoint(int x, int y);
+    void drawLine(int x1, int y1, int x2, int y2);
+    void drawBox(int x1, int y1, int x2, int y2);
+    void drawDottedLine(int x1, int y1, int x2, int y2);
+    int gridYToScreenX(int grid_y);
+    int gridXToScreenY(int grid_x);
+    
+    void init() {
+        lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+        lcd->init();
+        lcd->setContrast(0.5);
+        lcd->setBrightness(0);
+    }
+    
+    void deinit() {
+        delete lcd;
+    }
+    
+    void clear() {
+        lcd->clear();
+    }
+    
+    void render() {
+        lcd->refresh();
+    }
+    
+    void drawPoint(int x, int y) {
+        lcd->setPixel(x, y, true);
+    }
+    
+    void drawLine(int x1, int y1, int x2, int y2) {
+        lcd->drawLine(x1, y1, x2, y2, 1);
+    }
     
-    Graphics::drawDottedLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1);
-    Graphics::drawDottedLine(1, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2);
+    void drawDottedLine(int x1, int y1, int x2, int y2) {
+        lcd->drawLine(
+            (SCREEN_WIDTH - 1) - y1,
+            x1,
+            (SCREEN_WIDTH - 1) - y2,
+            x2,
+            2 // dotted
+        );
+    }
+    
+    void drawBox(int x1, int y1, int x2, int y2) {
+        drawLine(x1, y1, x2, y1);
+        drawLine(x2, y1, x2, y2);
+        drawLine(x2, y2, x1, y2);
+        drawLine(x1, y2, x1, y1);
+    }
     
-    Graphics::drawDottedLine(SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, 0);
-    Graphics::drawDottedLine(SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, 1);
-}
-
-int Graphics::gridYToScreenX(int grid_y) {
-    return grid_y * BLOCK_SIZE + BORDER_SIZE;
-}
-
-int Graphics::gridXToScreenY(int grid_x) {
-    return SCREEN_WIDTH - (grid_x * BLOCK_SIZE) - BORDER_SIZE;
-}
+    void drawBlock(int grid_x, int grid_y) {
+        // screen coords
+        int x = gridYToScreenX(grid_y);
+        int y = gridXToScreenY(grid_x);
+        drawBox(x, y, x + 3, y - 3);
+        drawPoint(x + 2, y - 2); 
+    }
+    
+    void drawBorder() {
+        drawDottedLine(0, 0, 0, SCREEN_HEIGHT - 1);
+        drawDottedLine(1, 1, 1, SCREEN_HEIGHT - 2);
+        
+        drawDottedLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1 - 8, SCREEN_HEIGHT - 1);
+        drawDottedLine(1, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2 - 8, SCREEN_HEIGHT - 2);
+        
+        drawDottedLine(SCREEN_WIDTH - 1 - 8, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1 - 8, 0);
+        drawDottedLine(SCREEN_WIDTH - 2 - 8, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2 - 8, 1);
+    }
+    
+    int gridYToScreenX(int grid_y) {
+        return grid_y * BLOCK_SIZE + BORDER_SIZE;
+    }
+    
+    int gridXToScreenY(int grid_x) {
+        return SCREEN_WIDTH - (grid_x * BLOCK_SIZE) - BORDER_SIZE;
+    }
+};
\ No newline at end of file