This class encapsulates all the algorithms required for the displaying strings and time dependent patterns on the lcd.
Dependents: 200943412_QuickClick
Display.cpp@4:b74041216336, 2017-04-11 (annotated)
- Committer:
- domkay97
- Date:
- Tue Apr 11 23:19:22 2017 +0000
- Revision:
- 4:b74041216336
- Parent:
- 2:f0ecd70c9ea2
- Child:
- 5:f8df5fe3dc6c
Correct Instruction must be performed or GameOver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
domkay97 | 0:dd78eca4b004 | 1 | #include "Display.h" |
domkay97 | 0:dd78eca4b004 | 2 | #include "N5110.h" |
domkay97 | 1:41a1c20a2056 | 3 | #include "Gamepad.h" |
domkay97 | 1:41a1c20a2056 | 4 | #include <math.h> |
domkay97 | 0:dd78eca4b004 | 5 | |
domkay97 | 0:dd78eca4b004 | 6 | Display::Display() |
domkay97 | 0:dd78eca4b004 | 7 | { |
domkay97 | 0:dd78eca4b004 | 8 | _h = 0; |
domkay97 | 2:f0ecd70c9ea2 | 9 | _w = 0.4; |
domkay97 | 0:dd78eca4b004 | 10 | _a = 0; |
domkay97 | 1:41a1c20a2056 | 11 | _A = 0; |
domkay97 | 1:41a1c20a2056 | 12 | _v = 0; |
domkay97 | 1:41a1c20a2056 | 13 | x = 0; |
domkay97 | 1:41a1c20a2056 | 14 | y = 0; |
domkay97 | 1:41a1c20a2056 | 15 | x0 = 0; |
domkay97 | 1:41a1c20a2056 | 16 | y0 = 0; |
domkay97 | 0:dd78eca4b004 | 17 | } |
domkay97 | 0:dd78eca4b004 | 18 | |
domkay97 | 0:dd78eca4b004 | 19 | Display::~Display() |
domkay97 | 0:dd78eca4b004 | 20 | { |
domkay97 | 0:dd78eca4b004 | 21 | |
domkay97 | 0:dd78eca4b004 | 22 | } |
domkay97 | 0:dd78eca4b004 | 23 | void Display::init() |
domkay97 | 0:dd78eca4b004 | 24 | { |
domkay97 | 0:dd78eca4b004 | 25 | |
domkay97 | 0:dd78eca4b004 | 26 | _h = 0; |
domkay97 | 2:f0ecd70c9ea2 | 27 | _w = 0.4; |
domkay97 | 0:dd78eca4b004 | 28 | _a = 0; |
domkay97 | 1:41a1c20a2056 | 29 | _A = 0; |
domkay97 | 1:41a1c20a2056 | 30 | _v = 0; |
domkay97 | 1:41a1c20a2056 | 31 | x = 0; |
domkay97 | 1:41a1c20a2056 | 32 | y = 0; |
domkay97 | 1:41a1c20a2056 | 33 | x0 = 0; |
domkay97 | 1:41a1c20a2056 | 34 | y0 = 0; |
domkay97 | 0:dd78eca4b004 | 35 | } |
domkay97 | 0:dd78eca4b004 | 36 | |
domkay97 | 0:dd78eca4b004 | 37 | void Display::drawCircle(N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 38 | { |
domkay97 | 2:f0ecd70c9ea2 | 39 | lcd.clear(); |
domkay97 | 1:41a1c20a2056 | 40 | random_instruction(lcd); |
domkay97 | 2:f0ecd70c9ea2 | 41 | Circle(lcd); |
domkay97 | 2:f0ecd70c9ea2 | 42 | calculateWait(); |
domkay97 | 2:f0ecd70c9ea2 | 43 | _h++; |
domkay97 | 2:f0ecd70c9ea2 | 44 | lcd.clear(); |
domkay97 | 2:f0ecd70c9ea2 | 45 | lcd.refresh(); |
domkay97 | 0:dd78eca4b004 | 46 | } |
domkay97 | 0:dd78eca4b004 | 47 | |
domkay97 | 4:b74041216336 | 48 | int Display::calculateWait() |
domkay97 | 0:dd78eca4b004 | 49 | { |
domkay97 | 2:f0ecd70c9ea2 | 50 | _w = 0.4*exp(-0.03465*_h); |
domkay97 | 4:b74041216336 | 51 | |
domkay97 | 4:b74041216336 | 52 | return _v; |
domkay97 | 1:41a1c20a2056 | 53 | } |
domkay97 | 2:f0ecd70c9ea2 | 54 | |
domkay97 | 1:41a1c20a2056 | 55 | void Display::random_instruction(N5110 &lcd) { |
domkay97 | 1:41a1c20a2056 | 56 | _v = rand() % 4 + 1; |
domkay97 | 1:41a1c20a2056 | 57 | if (_v == 1){ |
domkay97 | 2:f0ecd70c9ea2 | 58 | lcd.printChar('A',42,3); } |
domkay97 | 1:41a1c20a2056 | 59 | else if (_v == 2){ |
domkay97 | 4:b74041216336 | 60 | lcd.printChar('B',42,3); } |
domkay97 | 4:b74041216336 | 61 | else if (_v == 3){ |
domkay97 | 2:f0ecd70c9ea2 | 62 | lcd.printChar('X',42,3); } |
domkay97 | 4:b74041216336 | 63 | else { |
domkay97 | 2:f0ecd70c9ea2 | 64 | lcd.printChar('Y',42,3); } |
domkay97 | 1:41a1c20a2056 | 65 | lcd.refresh(); |
domkay97 | 1:41a1c20a2056 | 66 | } |
domkay97 | 1:41a1c20a2056 | 67 | |
domkay97 | 1:41a1c20a2056 | 68 | // function to draw circle |
domkay97 | 1:41a1c20a2056 | 69 | void Display::Circle(N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 70 | { |
domkay97 | 1:41a1c20a2056 | 71 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
domkay97 | 1:41a1c20a2056 | 72 | |
domkay97 | 1:41a1c20a2056 | 73 | |
domkay97 | 1:41a1c20a2056 | 74 | |
domkay97 | 1:41a1c20a2056 | 75 | for (int a = 0; a < 8; ++a) |
domkay97 | 1:41a1c20a2056 | 76 | { |
domkay97 | 1:41a1c20a2056 | 77 | Drawarc(lcd, a); |
domkay97 | 1:41a1c20a2056 | 78 | lcd.refresh(); |
domkay97 | 2:f0ecd70c9ea2 | 79 | lcd.setBrightness(1.0); |
domkay97 | 2:f0ecd70c9ea2 | 80 | wait(_w); |
domkay97 | 2:f0ecd70c9ea2 | 81 | lcd.setBrightness(0.0); |
domkay97 | 2:f0ecd70c9ea2 | 82 | wait(_w); |
domkay97 | 2:f0ecd70c9ea2 | 83 | } |
domkay97 | 0:dd78eca4b004 | 84 | |
domkay97 | 1:41a1c20a2056 | 85 | } |
domkay97 | 1:41a1c20a2056 | 86 | |
domkay97 | 1:41a1c20a2056 | 87 | void Display::Drawarc(N5110 &lcd, int a) { |
domkay97 | 1:41a1c20a2056 | 88 | x = 20; |
domkay97 | 1:41a1c20a2056 | 89 | y = 0; |
domkay97 | 1:41a1c20a2056 | 90 | |
domkay97 | 1:41a1c20a2056 | 91 | int a1; |
domkay97 | 1:41a1c20a2056 | 92 | int b1; |
domkay97 | 1:41a1c20a2056 | 93 | radiusError = 1-x; |
domkay97 | 1:41a1c20a2056 | 94 | x0 = 42; |
domkay97 | 1:41a1c20a2056 | 95 | y0 = 24; |
domkay97 | 0:dd78eca4b004 | 96 | |
domkay97 | 1:41a1c20a2056 | 97 | while(x >= y) { |
domkay97 | 1:41a1c20a2056 | 98 | // if transparent, just draw outline |
domkay97 | 1:41a1c20a2056 | 99 | switch(a) { |
domkay97 | 1:41a1c20a2056 | 100 | case 0 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 101 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 102 | break; |
domkay97 | 1:41a1c20a2056 | 103 | } |
domkay97 | 1:41a1c20a2056 | 104 | case 1 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 105 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 106 | break; |
domkay97 | 1:41a1c20a2056 | 107 | } |
domkay97 | 1:41a1c20a2056 | 108 | case 2 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 109 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 110 | break; |
domkay97 | 1:41a1c20a2056 | 111 | } |
domkay97 | 1:41a1c20a2056 | 112 | case 3 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 113 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 114 | break; |
domkay97 | 1:41a1c20a2056 | 115 | } |
domkay97 | 1:41a1c20a2056 | 116 | case 4 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 117 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 118 | break; |
domkay97 | 1:41a1c20a2056 | 119 | } |
domkay97 | 1:41a1c20a2056 | 120 | case 5 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 121 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 122 | break; |
domkay97 | 1:41a1c20a2056 | 123 | } |
domkay97 | 1:41a1c20a2056 | 124 | case 6 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 125 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 126 | break; |
domkay97 | 1:41a1c20a2056 | 127 | } |
domkay97 | 1:41a1c20a2056 | 128 | case 7 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 129 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 130 | break; |
domkay97 | 1:41a1c20a2056 | 131 | } |
domkay97 | 1:41a1c20a2056 | 132 | default : { a1 = x; |
domkay97 | 1:41a1c20a2056 | 133 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 134 | break; |
domkay97 | 1:41a1c20a2056 | 135 | } |
domkay97 | 1:41a1c20a2056 | 136 | } |
domkay97 | 1:41a1c20a2056 | 137 | |
domkay97 | 1:41a1c20a2056 | 138 | |
domkay97 | 1:41a1c20a2056 | 139 | |
domkay97 | 1:41a1c20a2056 | 140 | lcd.setPixel( a1 + x0, b1 + y0); |
domkay97 | 1:41a1c20a2056 | 141 | |
domkay97 | 1:41a1c20a2056 | 142 | // lcd.setPixel(-x + x0, y + y0); |
domkay97 | 1:41a1c20a2056 | 143 | // lcd.setPixel( y + x0, x + y0); |
domkay97 | 1:41a1c20a2056 | 144 | // lcd.setPixel(-y + x0, x + y0); |
domkay97 | 1:41a1c20a2056 | 145 | // lcd.setPixel(-y + x0, -x + y0); |
domkay97 | 1:41a1c20a2056 | 146 | // lcd.setPixel( y + x0, -x + y0); |
domkay97 | 1:41a1c20a2056 | 147 | // lcd.setPixel( x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 148 | /// lcd.setPixel(-x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 149 | |
domkay97 | 1:41a1c20a2056 | 150 | |
domkay97 | 1:41a1c20a2056 | 151 | |
domkay97 | 1:41a1c20a2056 | 152 | y++; |
domkay97 | 1:41a1c20a2056 | 153 | |
domkay97 | 1:41a1c20a2056 | 154 | |
domkay97 | 1:41a1c20a2056 | 155 | if (radiusError<0) { |
domkay97 | 1:41a1c20a2056 | 156 | radiusError += 2 * y + 1; |
domkay97 | 1:41a1c20a2056 | 157 | } else { |
domkay97 | 1:41a1c20a2056 | 158 | x--; |
domkay97 | 1:41a1c20a2056 | 159 | radiusError += 2 * (y - x) + 1; |
domkay97 | 1:41a1c20a2056 | 160 | } |
domkay97 | 1:41a1c20a2056 | 161 | |
domkay97 | 1:41a1c20a2056 | 162 | } |
domkay97 | 1:41a1c20a2056 | 163 | } |