This class encapsulates all the algorithms required for the displaying strings and time dependent patterns on the lcd.
Dependents: 200943412_QuickClick
Display.cpp@1:41a1c20a2056, 2017-04-10 (annotated)
- Committer:
- domkay97
- Date:
- Mon Apr 10 20:10:26 2017 +0000
- Revision:
- 1:41a1c20a2056
- Parent:
- 0:dd78eca4b004
- Child:
- 2:f0ecd70c9ea2
draw 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 | 0:dd78eca4b004 | 9 | _w = 0.1; |
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 | //InterruptIn button_X(SW3); |
domkay97 | 0:dd78eca4b004 | 27 | //olatile int g_timer_flag = 0; |
domkay97 | 0:dd78eca4b004 | 28 | // volatile int X_flag = 0; |
domkay97 | 0:dd78eca4b004 | 29 | // button_X.fall(&X_isr); |
domkay97 | 0:dd78eca4b004 | 30 | // button_X.mode(PullNone); |
domkay97 | 0:dd78eca4b004 | 31 | _h = 0; |
domkay97 | 0:dd78eca4b004 | 32 | _w = 0.1; |
domkay97 | 0:dd78eca4b004 | 33 | _a = 0; |
domkay97 | 1:41a1c20a2056 | 34 | _A = 0; |
domkay97 | 1:41a1c20a2056 | 35 | _v = 0; |
domkay97 | 1:41a1c20a2056 | 36 | x = 0; |
domkay97 | 1:41a1c20a2056 | 37 | y = 0; |
domkay97 | 1:41a1c20a2056 | 38 | x0 = 0; |
domkay97 | 1:41a1c20a2056 | 39 | y0 = 0; |
domkay97 | 0:dd78eca4b004 | 40 | } |
domkay97 | 0:dd78eca4b004 | 41 | |
domkay97 | 0:dd78eca4b004 | 42 | void Display::drawCircle(N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 43 | { |
domkay97 | 1:41a1c20a2056 | 44 | Circle(lcd); |
domkay97 | 1:41a1c20a2056 | 45 | random_instruction(lcd); |
domkay97 | 0:dd78eca4b004 | 46 | for( int _A = 0; _A < 84; _A++ ) { |
domkay97 | 0:dd78eca4b004 | 47 | _a = _A; |
domkay97 | 0:dd78eca4b004 | 48 | |
domkay97 | 0:dd78eca4b004 | 49 | wait(_w); |
domkay97 | 0:dd78eca4b004 | 50 | lcd.refresh(); |
domkay97 | 0:dd78eca4b004 | 51 | lcd.setPixel(_a,22); |
domkay97 | 0:dd78eca4b004 | 52 | lcd.setPixel(_a,23); |
domkay97 | 0:dd78eca4b004 | 53 | lcd.setPixel(_a,24); |
domkay97 | 0:dd78eca4b004 | 54 | lcd.setPixel(_a,25); |
domkay97 | 0:dd78eca4b004 | 55 | lcd.setPixel(_a,26); |
domkay97 | 0:dd78eca4b004 | 56 | } |
domkay97 | 0:dd78eca4b004 | 57 | calculateWait(); |
domkay97 | 0:dd78eca4b004 | 58 | _h++; |
domkay97 | 1:41a1c20a2056 | 59 | lcd.clear(); |
domkay97 | 0:dd78eca4b004 | 60 | } |
domkay97 | 0:dd78eca4b004 | 61 | |
domkay97 | 0:dd78eca4b004 | 62 | void Display::calculateWait() |
domkay97 | 0:dd78eca4b004 | 63 | { |
domkay97 | 1:41a1c20a2056 | 64 | _w = 0.05*exp(-0.03465*_h); |
domkay97 | 1:41a1c20a2056 | 65 | } |
domkay97 | 1:41a1c20a2056 | 66 | // if (_h < 10) { |
domkay97 | 1:41a1c20a2056 | 67 | // _w = _w - _w/10; } |
domkay97 | 1:41a1c20a2056 | 68 | //else if (_h < 20) { |
domkay97 | 1:41a1c20a2056 | 69 | // _w = _w - _w/20; } |
domkay97 | 1:41a1c20a2056 | 70 | //else if (_h < 30) { |
domkay97 | 1:41a1c20a2056 | 71 | // _w = _w - _w/30; } |
domkay97 | 1:41a1c20a2056 | 72 | //else |
domkay97 | 1:41a1c20a2056 | 73 | // { _w = _w - _w/40; } |
domkay97 | 1:41a1c20a2056 | 74 | //} |
domkay97 | 1:41a1c20a2056 | 75 | void Display::random_instruction(N5110 &lcd) { |
domkay97 | 1:41a1c20a2056 | 76 | _v = rand() % 4 + 1; |
domkay97 | 1:41a1c20a2056 | 77 | if (_v == 1){ |
domkay97 | 1:41a1c20a2056 | 78 | lcd.printChar('A',45,2); } |
domkay97 | 1:41a1c20a2056 | 79 | else if (_v == 2){ |
domkay97 | 1:41a1c20a2056 | 80 | lcd.printChar('X',45,2); } |
domkay97 | 1:41a1c20a2056 | 81 | else if (_v == 3){ |
domkay97 | 1:41a1c20a2056 | 82 | lcd.printChar('Y',45,2); } |
domkay97 | 1:41a1c20a2056 | 83 | else { |
domkay97 | 1:41a1c20a2056 | 84 | lcd.printChar('B',45,2); } |
domkay97 | 1:41a1c20a2056 | 85 | lcd.refresh(); |
domkay97 | 1:41a1c20a2056 | 86 | } |
domkay97 | 1:41a1c20a2056 | 87 | |
domkay97 | 1:41a1c20a2056 | 88 | // function to draw circle |
domkay97 | 1:41a1c20a2056 | 89 | void Display::Circle(N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 90 | { |
domkay97 | 1:41a1c20a2056 | 91 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
domkay97 | 1:41a1c20a2056 | 92 | |
domkay97 | 1:41a1c20a2056 | 93 | |
domkay97 | 1:41a1c20a2056 | 94 | |
domkay97 | 1:41a1c20a2056 | 95 | for (int a = 0; a < 8; ++a) |
domkay97 | 1:41a1c20a2056 | 96 | { |
domkay97 | 1:41a1c20a2056 | 97 | Drawarc(lcd, a); |
domkay97 | 1:41a1c20a2056 | 98 | |
domkay97 | 1:41a1c20a2056 | 99 | lcd.refresh(); |
domkay97 | 1:41a1c20a2056 | 100 | |
domkay97 | 1:41a1c20a2056 | 101 | |
domkay97 | 1:41a1c20a2056 | 102 | wait(1); } |
domkay97 | 0:dd78eca4b004 | 103 | |
domkay97 | 1:41a1c20a2056 | 104 | } |
domkay97 | 1:41a1c20a2056 | 105 | |
domkay97 | 1:41a1c20a2056 | 106 | void Display::Drawarc(N5110 &lcd, int a) { |
domkay97 | 1:41a1c20a2056 | 107 | x = 20; |
domkay97 | 1:41a1c20a2056 | 108 | y = 0; |
domkay97 | 1:41a1c20a2056 | 109 | |
domkay97 | 1:41a1c20a2056 | 110 | int a1; |
domkay97 | 1:41a1c20a2056 | 111 | int b1; |
domkay97 | 1:41a1c20a2056 | 112 | radiusError = 1-x; |
domkay97 | 1:41a1c20a2056 | 113 | x0 = 42; |
domkay97 | 1:41a1c20a2056 | 114 | y0 = 24; |
domkay97 | 0:dd78eca4b004 | 115 | |
domkay97 | 1:41a1c20a2056 | 116 | while(x >= y) { |
domkay97 | 1:41a1c20a2056 | 117 | // if transparent, just draw outline |
domkay97 | 1:41a1c20a2056 | 118 | switch(a) { |
domkay97 | 1:41a1c20a2056 | 119 | case 0 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 120 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 121 | break; |
domkay97 | 1:41a1c20a2056 | 122 | } |
domkay97 | 1:41a1c20a2056 | 123 | case 1 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 124 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 125 | break; |
domkay97 | 1:41a1c20a2056 | 126 | } |
domkay97 | 1:41a1c20a2056 | 127 | case 2 : {a1 = x; |
domkay97 | 1:41a1c20a2056 | 128 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 129 | break; |
domkay97 | 1:41a1c20a2056 | 130 | } |
domkay97 | 1:41a1c20a2056 | 131 | case 3 : {a1 = y; |
domkay97 | 1:41a1c20a2056 | 132 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 133 | break; |
domkay97 | 1:41a1c20a2056 | 134 | } |
domkay97 | 1:41a1c20a2056 | 135 | case 4 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 136 | b1 = x; |
domkay97 | 1:41a1c20a2056 | 137 | break; |
domkay97 | 1:41a1c20a2056 | 138 | } |
domkay97 | 1:41a1c20a2056 | 139 | case 5 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 140 | b1 = y; |
domkay97 | 1:41a1c20a2056 | 141 | break; |
domkay97 | 1:41a1c20a2056 | 142 | } |
domkay97 | 1:41a1c20a2056 | 143 | case 6 : {a1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 144 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 145 | break; |
domkay97 | 1:41a1c20a2056 | 146 | } |
domkay97 | 1:41a1c20a2056 | 147 | case 7 : {a1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 148 | b1 = x * -1; |
domkay97 | 1:41a1c20a2056 | 149 | break; |
domkay97 | 1:41a1c20a2056 | 150 | } |
domkay97 | 1:41a1c20a2056 | 151 | default : { a1 = x; |
domkay97 | 1:41a1c20a2056 | 152 | b1 = y * -1; |
domkay97 | 1:41a1c20a2056 | 153 | break; |
domkay97 | 1:41a1c20a2056 | 154 | } |
domkay97 | 1:41a1c20a2056 | 155 | } |
domkay97 | 1:41a1c20a2056 | 156 | |
domkay97 | 1:41a1c20a2056 | 157 | |
domkay97 | 1:41a1c20a2056 | 158 | |
domkay97 | 1:41a1c20a2056 | 159 | lcd.setPixel( a1 + x0, b1 + y0); |
domkay97 | 1:41a1c20a2056 | 160 | |
domkay97 | 1:41a1c20a2056 | 161 | // lcd.setPixel(-x + x0, y + y0); |
domkay97 | 1:41a1c20a2056 | 162 | // lcd.setPixel( y + x0, x + y0); |
domkay97 | 1:41a1c20a2056 | 163 | // lcd.setPixel(-y + x0, x + y0); |
domkay97 | 1:41a1c20a2056 | 164 | // lcd.setPixel(-y + x0, -x + y0); |
domkay97 | 1:41a1c20a2056 | 165 | // lcd.setPixel( y + x0, -x + y0); |
domkay97 | 1:41a1c20a2056 | 166 | // lcd.setPixel( x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 167 | /// lcd.setPixel(-x + x0, -y + y0); |
domkay97 | 1:41a1c20a2056 | 168 | |
domkay97 | 1:41a1c20a2056 | 169 | |
domkay97 | 1:41a1c20a2056 | 170 | |
domkay97 | 1:41a1c20a2056 | 171 | y++; |
domkay97 | 1:41a1c20a2056 | 172 | |
domkay97 | 1:41a1c20a2056 | 173 | |
domkay97 | 1:41a1c20a2056 | 174 | if (radiusError<0) { |
domkay97 | 1:41a1c20a2056 | 175 | radiusError += 2 * y + 1; |
domkay97 | 1:41a1c20a2056 | 176 | } else { |
domkay97 | 1:41a1c20a2056 | 177 | x--; |
domkay97 | 1:41a1c20a2056 | 178 | radiusError += 2 * (y - x) + 1; |
domkay97 | 1:41a1c20a2056 | 179 | } |
domkay97 | 1:41a1c20a2056 | 180 | |
domkay97 | 1:41a1c20a2056 | 181 | } |
domkay97 | 1:41a1c20a2056 | 182 | } |