This class encapsulates all the algorithms required for the displaying strings and time dependent patterns on the lcd.
Dependents: 200943412_QuickClick
Display.cpp@17:1c52ff51c028, 2017-05-02 (annotated)
- Committer:
- domkay97
- Date:
- Tue May 02 14:02:25 2017 +0000
- Revision:
- 17:1c52ff51c028
- Parent:
- 16:8d158d39d438
Correcting grammar in comments
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 | 6:8840d7e48ce7 | 3 | #include "Controller.h" |
domkay97 | 1:41a1c20a2056 | 4 | #include <math.h> |
domkay97 | 0:dd78eca4b004 | 5 | |
domkay97 | 0:dd78eca4b004 | 6 | Display::Display() |
domkay97 | 0:dd78eca4b004 | 7 | { |
domkay97 | 11:7ad2ea427784 | 8 | _h = 0; |
domkay97 | 16:8d158d39d438 | 9 | _w = 0.2; |
domkay97 | 11:7ad2ea427784 | 10 | _a = 0; |
domkay97 | 16:8d158d39d438 | 11 | _x = 0; |
domkay97 | 16:8d158d39d438 | 12 | _y = 0; |
domkay97 | 16:8d158d39d438 | 13 | _x0 = 0; |
domkay97 | 16:8d158d39d438 | 14 | _y0 = 0; |
domkay97 | 0:dd78eca4b004 | 15 | } |
domkay97 | 0:dd78eca4b004 | 16 | |
domkay97 | 0:dd78eca4b004 | 17 | Display::~Display() |
domkay97 | 0:dd78eca4b004 | 18 | { |
domkay97 | 0:dd78eca4b004 | 19 | |
domkay97 | 0:dd78eca4b004 | 20 | } |
domkay97 | 0:dd78eca4b004 | 21 | void Display::init() |
domkay97 | 0:dd78eca4b004 | 22 | { |
domkay97 | 0:dd78eca4b004 | 23 | |
domkay97 | 11:7ad2ea427784 | 24 | _h = 0; |
domkay97 | 16:8d158d39d438 | 25 | _w = 0.2; |
domkay97 | 11:7ad2ea427784 | 26 | _a = 0; |
domkay97 | 16:8d158d39d438 | 27 | _x = 0; |
domkay97 | 16:8d158d39d438 | 28 | _y = 0; |
domkay97 | 16:8d158d39d438 | 29 | _x0 = 0; |
domkay97 | 16:8d158d39d438 | 30 | _y0 = 0; |
domkay97 | 0:dd78eca4b004 | 31 | } |
domkay97 | 0:dd78eca4b004 | 32 | |
domkay97 | 9:1e25dcab4927 | 33 | void Display::drawCircle(Controller &pad, N5110 &lcd) |
domkay97 | 11:7ad2ea427784 | 34 | { |
domkay97 | 16:8d158d39d438 | 35 | _arc_selector(pad, lcd); |
domkay97 | 15:c14c72342de7 | 36 | //printf("Prev Wait:- %3.2f\n",_w); |
domkay97 | 16:8d158d39d438 | 37 | _calculateWait(); |
domkay97 | 13:505b4fb28caf | 38 | _h++; |
domkay97 | 15:c14c72342de7 | 39 | //printf("Score:- %d\n",_h); |
domkay97 | 11:7ad2ea427784 | 40 | lcd.clear(); |
domkay97 | 2:f0ecd70c9ea2 | 41 | lcd.refresh(); |
domkay97 | 11:7ad2ea427784 | 42 | } |
domkay97 | 0:dd78eca4b004 | 43 | |
domkay97 | 16:8d158d39d438 | 44 | void Display::_calculateWait() |
domkay97 | 11:7ad2ea427784 | 45 | { |
domkay97 | 16:8d158d39d438 | 46 | _w = 0.2*exp(-0.0231*_h); //this equation models how much time the user has |
domkay97 | 16:8d158d39d438 | 47 | //to complete action with larger amounts of iterations. |
domkay97 | 11:7ad2ea427784 | 48 | } |
domkay97 | 12:41e9ba6d65d3 | 49 | |
domkay97 | 12:41e9ba6d65d3 | 50 | |
domkay97 | 12:41e9ba6d65d3 | 51 | void Display::put_wait(float w) |
domkay97 | 12:41e9ba6d65d3 | 52 | { |
domkay97 | 12:41e9ba6d65d3 | 53 | _w = w; |
domkay97 | 12:41e9ba6d65d3 | 54 | |
domkay97 | 12:41e9ba6d65d3 | 55 | } |
domkay97 | 12:41e9ba6d65d3 | 56 | |
domkay97 | 12:41e9ba6d65d3 | 57 | float Display::get_wait() |
domkay97 | 12:41e9ba6d65d3 | 58 | { |
domkay97 | 12:41e9ba6d65d3 | 59 | return _w; |
domkay97 | 12:41e9ba6d65d3 | 60 | |
domkay97 | 12:41e9ba6d65d3 | 61 | } |
domkay97 | 11:7ad2ea427784 | 62 | |
domkay97 | 15:c14c72342de7 | 63 | void Display::display_instruction(N5110 &lcd, int ran) { //printing a random instruction on the screen |
domkay97 | 6:8840d7e48ce7 | 64 | lcd.clear(); |
domkay97 | 11:7ad2ea427784 | 65 | if (ran == 1){ |
domkay97 | 7:635883dd8c35 | 66 | lcd.printString("PRESS",28,2); |
domkay97 | 11:7ad2ea427784 | 67 | lcd.printChar('A',40,3); } |
domkay97 | 11:7ad2ea427784 | 68 | else if (ran == 2){ |
domkay97 | 7:635883dd8c35 | 69 | lcd.printString("PRESS",28,2); |
domkay97 | 11:7ad2ea427784 | 70 | lcd.printChar('B',40,3); } |
domkay97 | 11:7ad2ea427784 | 71 | else if (ran == 3){ |
domkay97 | 7:635883dd8c35 | 72 | lcd.printString("PRESS",28,2); |
domkay97 | 11:7ad2ea427784 | 73 | lcd.printChar('X',40,3); } |
domkay97 | 11:7ad2ea427784 | 74 | else if (ran == 4){ |
domkay97 | 7:635883dd8c35 | 75 | lcd.printString("PRESS",28,2); |
domkay97 | 11:7ad2ea427784 | 76 | lcd.printChar('Y',40,3); } |
domkay97 | 11:7ad2ea427784 | 77 | else if (ran == 5){ |
domkay97 | 7:635883dd8c35 | 78 | lcd.printString("FLICK",28,2); |
domkay97 | 11:7ad2ea427784 | 79 | lcd.printChar('L',40,3); } |
domkay97 | 11:7ad2ea427784 | 80 | else if (ran == 6){ |
domkay97 | 7:635883dd8c35 | 81 | lcd.printString("FLICK",28,2); |
domkay97 | 11:7ad2ea427784 | 82 | lcd.printChar('R',40,3); } |
domkay97 | 11:7ad2ea427784 | 83 | else { |
domkay97 | 7:635883dd8c35 | 84 | lcd.printString("PRESS",28,2); |
domkay97 | 11:7ad2ea427784 | 85 | lcd.printString("STICK",28,3); } |
domkay97 | 15:c14c72342de7 | 86 | //printf("Random Instruction int:- %d\n", ran); |
domkay97 | 11:7ad2ea427784 | 87 | lcd.refresh(); |
domkay97 | 11:7ad2ea427784 | 88 | } |
domkay97 | 11:7ad2ea427784 | 89 | |
domkay97 | 1:41a1c20a2056 | 90 | // function to draw circle |
domkay97 | 16:8d158d39d438 | 91 | void Display::_arc_selector(Controller &ctrl ,N5110 &lcd) |
domkay97 | 1:41a1c20a2056 | 92 | { |
domkay97 | 16:8d158d39d438 | 93 | for (int _a = 0; _a < 8; ++_a) |
domkay97 | 11:7ad2ea427784 | 94 | { |
domkay97 | 15:c14c72342de7 | 95 | //printf("for loop (a):- %d\n",a); |
domkay97 | 17:1c52ff51c028 | 96 | _drawArc(lcd, _a); //run through _drawArc function drawing octant specific to a value |
domkay97 | 11:7ad2ea427784 | 97 | lcd.refresh(); |
domkay97 | 15:c14c72342de7 | 98 | float Brightness = ctrl.pot_value(); //set brightness to petentiometer value |
domkay97 | 11:7ad2ea427784 | 99 | lcd.setBrightness(0.0); |
domkay97 | 15:c14c72342de7 | 100 | //printf("Brightness:- %3.2f\n",Brightness); |
domkay97 | 15:c14c72342de7 | 101 | //printf("Wait used:- %3.2f\n",_w); |
domkay97 | 11:7ad2ea427784 | 102 | wait(_w); |
domkay97 | 15:c14c72342de7 | 103 | lcd.setBrightness(Brightness); //Variable brightness will change with petentiometer value |
domkay97 | 17:1c52ff51c028 | 104 | wait(_w); //changes after every circle is drawn to make the game more exciting. |
domkay97 | 11:7ad2ea427784 | 105 | } |
domkay97 | 11:7ad2ea427784 | 106 | |
domkay97 | 11:7ad2ea427784 | 107 | } |
domkay97 | 11:7ad2ea427784 | 108 | |
domkay97 | 16:8d158d39d438 | 109 | void Display::_drawArc(N5110 &lcd, int _a) { |
domkay97 | 16:8d158d39d438 | 110 | _x = 20; |
domkay97 | 16:8d158d39d438 | 111 | _y = 0; |
domkay97 | 11:7ad2ea427784 | 112 | |
domkay97 | 1:41a1c20a2056 | 113 | int a1; |
domkay97 | 1:41a1c20a2056 | 114 | int b1; |
domkay97 | 16:8d158d39d438 | 115 | _radiusMod = 1-_x; |
domkay97 | 16:8d158d39d438 | 116 | _x0 = 42; |
domkay97 | 16:8d158d39d438 | 117 | _y0 = 24; |
domkay97 | 0:dd78eca4b004 | 118 | |
domkay97 | 17:1c52ff51c028 | 119 | //the algorithm below is a modified version of the midpoint circle algorithm from: |
domkay97 | 17:1c52ff51c028 | 120 | //https://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
domkay97 | 17:1c52ff51c028 | 121 | |
domkay97 | 16:8d158d39d438 | 122 | while(_x >= _y) { |
domkay97 | 13:505b4fb28caf | 123 | |
domkay97 | 16:8d158d39d438 | 124 | switch(_a) { |
domkay97 | 13:505b4fb28caf | 125 | |
domkay97 | 16:8d158d39d438 | 126 | case 0 : {a1 = _y; //each case statement draws a single octant by setting a1 and b1 accordingly |
domkay97 | 16:8d158d39d438 | 127 | b1 = _x * -1; |
domkay97 | 11:7ad2ea427784 | 128 | break; |
domkay97 | 11:7ad2ea427784 | 129 | } |
domkay97 | 16:8d158d39d438 | 130 | case 1 : {a1 = _x; |
domkay97 | 16:8d158d39d438 | 131 | b1 = _y * -1; |
domkay97 | 11:7ad2ea427784 | 132 | break; |
domkay97 | 11:7ad2ea427784 | 133 | } |
domkay97 | 16:8d158d39d438 | 134 | case 2 : {a1 = _x; |
domkay97 | 16:8d158d39d438 | 135 | b1 = _y; |
domkay97 | 11:7ad2ea427784 | 136 | break; |
domkay97 | 11:7ad2ea427784 | 137 | } |
domkay97 | 16:8d158d39d438 | 138 | case 3 : {a1 = _y; |
domkay97 | 16:8d158d39d438 | 139 | b1 = _x; |
domkay97 | 11:7ad2ea427784 | 140 | break; |
domkay97 | 11:7ad2ea427784 | 141 | } |
domkay97 | 16:8d158d39d438 | 142 | case 4 : {a1 = _y * -1; |
domkay97 | 16:8d158d39d438 | 143 | b1 = _x; |
domkay97 | 11:7ad2ea427784 | 144 | break; |
domkay97 | 11:7ad2ea427784 | 145 | } |
domkay97 | 16:8d158d39d438 | 146 | case 5 : {a1 = _x * -1; |
domkay97 | 16:8d158d39d438 | 147 | b1 = _y; |
domkay97 | 11:7ad2ea427784 | 148 | break; |
domkay97 | 11:7ad2ea427784 | 149 | } |
domkay97 | 16:8d158d39d438 | 150 | case 6 : {a1 = _x * -1; |
domkay97 | 16:8d158d39d438 | 151 | b1 = _y * -1; |
domkay97 | 11:7ad2ea427784 | 152 | break; |
domkay97 | 11:7ad2ea427784 | 153 | } |
domkay97 | 16:8d158d39d438 | 154 | case 7 : {a1 = _y * -1; |
domkay97 | 16:8d158d39d438 | 155 | b1 = _x * -1; |
domkay97 | 11:7ad2ea427784 | 156 | break; |
domkay97 | 11:7ad2ea427784 | 157 | } |
domkay97 | 16:8d158d39d438 | 158 | default : { a1 = _x; |
domkay97 | 16:8d158d39d438 | 159 | b1 = _y * -1; |
domkay97 | 10:6f5a88c24a07 | 160 | break; |
domkay97 | 11:7ad2ea427784 | 161 | } |
domkay97 | 1:41a1c20a2056 | 162 | } |
domkay97 | 11:7ad2ea427784 | 163 | |
domkay97 | 11:7ad2ea427784 | 164 | |
domkay97 | 16:8d158d39d438 | 165 | lcd.setPixel( a1 + _x0, b1 + _y0); //draws octant depending on a1 and b1 |
domkay97 | 11:7ad2ea427784 | 166 | |
domkay97 | 16:8d158d39d438 | 167 | _y++; |
domkay97 | 15:c14c72342de7 | 168 | |
domkay97 | 11:7ad2ea427784 | 169 | |
domkay97 | 16:8d158d39d438 | 170 | if (_radiusMod<0) { |
domkay97 | 16:8d158d39d438 | 171 | _radiusMod += 2*_y+1; |
domkay97 | 1:41a1c20a2056 | 172 | } else { |
domkay97 | 16:8d158d39d438 | 173 | _x--; |
domkay97 | 16:8d158d39d438 | 174 | _radiusMod += 2*(_y-_x)+1; |
domkay97 | 1:41a1c20a2056 | 175 | } |
domkay97 | 16:8d158d39d438 | 176 | //printf("_a1:- %d\n", _a1); |
domkay97 | 16:8d158d39d438 | 177 | //printf("_b1:- %d\n", _b1); |
domkay97 | 16:8d158d39d438 | 178 | //printf("_x:- %d\n", _x); |
domkay97 | 16:8d158d39d438 | 179 | //printf("_y:- %d\n", _y); |
domkay97 | 16:8d158d39d438 | 180 | //printf("_radiusMod:- %d\n",_radiusMod); |
domkay97 | 1:41a1c20a2056 | 181 | } |
domkay97 | 16:8d158d39d438 | 182 | } |