This class encapsulates all the algorithms required for the displaying strings and time dependent patterns on the lcd.
Dependents: 200943412_QuickClick
Display.cpp@2:f0ecd70c9ea2, 2017-04-11 (annotated)
- Committer:
- domkay97
- Date:
- Tue Apr 11 22:42:33 2017 +0000
- Revision:
- 2:f0ecd70c9ea2
- Parent:
- 1:41a1c20a2056
- Child:
- 4:b74041216336
Working Circle
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 | 0:dd78eca4b004 | 48 | void Display::calculateWait() |
domkay97 | 0:dd78eca4b004 | 49 | { |
domkay97 | 2:f0ecd70c9ea2 | 50 | _w = 0.4*exp(-0.03465*_h); |
domkay97 | 1:41a1c20a2056 | 51 | } |
domkay97 | 2:f0ecd70c9ea2 | 52 | |
domkay97 | 1:41a1c20a2056 | 53 | void Display::random_instruction(N5110 &lcd) { |
domkay97 | 1:41a1c20a2056 | 54 | _v = rand() % 4 + 1; |
domkay97 | 1:41a1c20a2056 | 55 | if (_v == 1){ |
domkay97 | 2:f0ecd70c9ea2 | 56 | lcd.printChar('A',42,3); } |
domkay97 | 1:41a1c20a2056 | 57 | else if (_v == 2){ |
domkay97 | 2:f0ecd70c9ea2 | 58 | lcd.printChar('X',42,3); } |
domkay97 | 1:41a1c20a2056 | 59 | else if (_v == 3){ |
domkay97 | 2:f0ecd70c9ea2 | 60 | lcd.printChar('Y',42,3); } |
domkay97 | 1:41a1c20a2056 | 61 | else { |
domkay97 | 2:f0ecd70c9ea2 | 62 | lcd.printChar('B',42,3); } |
domkay97 | 1:41a1c20a2056 | 63 | lcd.refresh(); |
domkay97 | 1:41a1c20a2056 | 64 | } |
domkay97 | 1:41a1c20a2056 | 65 | |
domkay97 | 1:41a1c20a2056 | 66 | // function to draw circle |
domkay97 | 1:41a1c20a2056 | 67 | void Display::Circle(N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 68 | { |
domkay97 | 1:41a1c20a2056 | 69 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
domkay97 | 1:41a1c20a2056 | 70 | |
domkay97 | 1:41a1c20a2056 | 71 | |
domkay97 | 1:41a1c20a2056 | 72 | |
domkay97 | 1:41a1c20a2056 | 73 | for (int a = 0; a < 8; ++a) |
domkay97 | 1:41a1c20a2056 | 74 | { |
domkay97 | 1:41a1c20a2056 | 75 | Drawarc(lcd, a); |
domkay97 | 1:41a1c20a2056 | 76 | lcd.refresh(); |
domkay97 | 2:f0ecd70c9ea2 | 77 | lcd.setBrightness(1.0); |
domkay97 | 2:f0ecd70c9ea2 | 78 | wait(_w); |
domkay97 | 2:f0ecd70c9ea2 | 79 | lcd.setBrightness(0.0); |
domkay97 | 2:f0ecd70c9ea2 | 80 | wait(_w); |
domkay97 | 2:f0ecd70c9ea2 | 81 | } |
domkay97 | 0:dd78eca4b004 | 82 | |
domkay97 | 1:41a1c20a2056 | 83 | } |
domkay97 | 1:41a1c20a2056 | 84 | |
domkay97 | 1:41a1c20a2056 | 85 | void Display::Drawarc(N5110 &lcd, int a) { |
domkay97 | 1:41a1c20a2056 | 86 | x = 20; |
domkay97 | 1:41a1c20a2056 | 87 | y = 0; |
domkay97 | 1:41a1c20a2056 | 88 | |
domkay97 | 1:41a1c20a2056 | 89 | int a1; |
domkay97 | 1:41a1c20a2056 | 90 | int b1; |
domkay97 | 1:41a1c20a2056 | 91 | radiusError = 1-x; |
domkay97 | 1:41a1c20a2056 | 92 | x0 = 42; |
domkay97 | 1:41a1c20a2056 | 93 | y0 = 24; |
domkay97 | 0:dd78eca4b004 | 94 | |
domkay97 | 1:41a1c20a2056 | 95 | while(x >= y) { |
domkay97 | 1:41a1c20a2056 | 96 | // if transparent, just draw outline |
domkay97 | 1:41a1c20a2056 | 97 | switch(a) { |
domkay97 | 1:41a1c20a2056 | 98 | case 0 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 99 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 100 | break; |
domkay97 | 1:41a1c20a2056 | 101 | } |
domkay97 | 1:41a1c20a2056 | 102 | case 1 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 103 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 104 | break; |
domkay97 | 1:41a1c20a2056 | 105 | } |
domkay97 | 1:41a1c20a2056 | 106 | case 2 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 107 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 108 | break; |
domkay97 | 1:41a1c20a2056 | 109 | } |
domkay97 | 1:41a1c20a2056 | 110 | case 3 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 111 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 112 | break; |
domkay97 | 1:41a1c20a2056 | 113 | } |
domkay97 | 1:41a1c20a2056 | 114 | case 4 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 115 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 116 | break; |
domkay97 | 1:41a1c20a2056 | 117 | } |
domkay97 | 1:41a1c20a2056 | 118 | case 5 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 119 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 120 | break; |
domkay97 | 1:41a1c20a2056 | 121 | } |
domkay97 | 1:41a1c20a2056 | 122 | case 6 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 123 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 124 | break; |
domkay97 | 1:41a1c20a2056 | 125 | } |
domkay97 | 1:41a1c20a2056 | 126 | case 7 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 127 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 128 | break; |
domkay97 | 1:41a1c20a2056 | 129 | } |
domkay97 | 1:41a1c20a2056 | 130 | default : { a1 = x; |
domkay97 | 1:41a1c20a2056 | 131 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 132 | break; |
domkay97 | 1:41a1c20a2056 | 133 | } |
domkay97 | 1:41a1c20a2056 | 134 | } |
domkay97 | 1:41a1c20a2056 | 135 | |
domkay97 | 1:41a1c20a2056 | 136 | |
domkay97 | 1:41a1c20a2056 | 137 | |
domkay97 | 1:41a1c20a2056 | 138 | lcd.setPixel( a1 + x0, b1 + y0); |
domkay97 | 1:41a1c20a2056 | 139 | |
domkay97 | 1:41a1c20a2056 | 140 | // lcd.setPixel(-x + x0, y + y0); |
domkay97 | 1:41a1c20a2056 | 141 | // lcd.setPixel( y + x0, x + y0); |
domkay97 | 1:41a1c20a2056 | 142 | // lcd.setPixel(-y + x0, x + 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( x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 146 | /// lcd.setPixel(-x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 147 | |
domkay97 | 1:41a1c20a2056 | 148 | |
domkay97 | 1:41a1c20a2056 | 149 | |
domkay97 | 1:41a1c20a2056 | 150 | y++; |
domkay97 | 1:41a1c20a2056 | 151 | |
domkay97 | 1:41a1c20a2056 | 152 | |
domkay97 | 1:41a1c20a2056 | 153 | if (radiusError<0) { |
domkay97 | 1:41a1c20a2056 | 154 | radiusError += 2 * y + 1; |
domkay97 | 1:41a1c20a2056 | 155 | } else { |
domkay97 | 1:41a1c20a2056 | 156 | x--; |
domkay97 | 1:41a1c20a2056 | 157 | radiusError += 2 * (y - x) + 1; |
domkay97 | 1:41a1c20a2056 | 158 | } |
domkay97 | 1:41a1c20a2056 | 159 | |
domkay97 | 1:41a1c20a2056 | 160 | } |
domkay97 | 1:41a1c20a2056 | 161 | } |