Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Wed May 08 18:51:59 2019 +0000
Revision:
15:d855e8c666e7
Parent:
14:08ac9aaa34c3
Child:
16:c8d68cbd1ae2
Updated aesthetics. Testing documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellisbhastroud 1:6179c2d67d19 1 #include "Menu.h"
ellisbhastroud 1:6179c2d67d19 2
ellisbhastroud 15:d855e8c666e7 3 int mini_logo[15][60] = { //mini part of logo shown on welcome screen
ellisbhastroud 15:d855e8c666e7 4 { 0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 5 { 0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 6 { 0,0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 7 { 0,0,0,1,0,0,1,1,0,0,0,0,1,1,0,0,1,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 8 { 0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 9 { 0,0,0,1,0,0,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0,1,1,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 17 { 0,0,0,1,0,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 18 { 0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0 },
ellisbhastroud 14:08ac9aaa34c3 19 };
ellisbhastroud 14:08ac9aaa34c3 20
ellisbhastroud 15:d855e8c666e7 21 int golf_logo[15][60] = { //golf part of logo shown on welcome screen
ellisbhastroud 15:d855e8c666e7 22 { 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 23 { 0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 24 { 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0 },
ellisbhastroud 15:d855e8c666e7 25 { 0,0,0,1,0,0,0,1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,0,0,1,0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,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 33 { 0,0,0,1,0,0,0,1,1,1,1,1,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 34 { 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 35 { 0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 36 { 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0 },
ellisbhastroud 15:d855e8c666e7 37
ellisbhastroud 15:d855e8c666e7 38 };
ellisbhastroud 15:d855e8c666e7 39
ellisbhastroud 15:d855e8c666e7 40
ellisbhastroud 1:6179c2d67d19 41 // constructor
ellisbhastroud 1:6179c2d67d19 42
ellisbhastroud 1:6179c2d67d19 43 Menu::Menu()
ellisbhastroud 1:6179c2d67d19 44 {
ellisbhastroud 1:6179c2d67d19 45
ellisbhastroud 1:6179c2d67d19 46 }
ellisbhastroud 1:6179c2d67d19 47
ellisbhastroud 1:6179c2d67d19 48 //deconstructor
ellisbhastroud 1:6179c2d67d19 49
ellisbhastroud 1:6179c2d67d19 50 Menu::~Menu()
ellisbhastroud 1:6179c2d67d19 51 {
ellisbhastroud 1:6179c2d67d19 52
ellisbhastroud 1:6179c2d67d19 53 }
ellisbhastroud 1:6179c2d67d19 54
ellisbhastroud 1:6179c2d67d19 55 //public methods
ellisbhastroud 1:6179c2d67d19 56
ellisbhastroud 12:7f7fadb5c106 57 void Menu::init() //class variables automatically set here
ellisbhastroud 3:a8960004d261 58 {
ellisbhastroud 14:08ac9aaa34c3 59 reset_start_game_flag(); //resets start flag to make sure game enters menu loop when first run
ellisbhastroud 4:035448357749 60 _cursor_pos = 0; //cursor begins on position 0
ellisbhastroud 12:7f7fadb5c106 61 _frame_rate = 40; //frame rate, contrast and brightness can be changed in settings
ellisbhastroud 15:d855e8c666e7 62 _contrast = 0.55f;
ellisbhastroud 14:08ac9aaa34c3 63 _brightness = 1.0f; //full brightness makes ball easier to see when moving
ellisbhastroud 3:a8960004d261 64
ellisbhastroud 3:a8960004d261 65 }
ellisbhastroud 12:7f7fadb5c106 66
ellisbhastroud 14:08ac9aaa34c3 67 void Menu::welcome_loop(Gamepad &pad, N5110 &lcd) //prints welcome screen
ellisbhastroud 15:d855e8c666e7 68 {
ellisbhastroud 15:d855e8c666e7 69 while(pad.check_event(Gamepad::START_PRESSED) == false) {
ellisbhastroud 15:d855e8c666e7 70 lcd.clear();
ellisbhastroud 15:d855e8c666e7 71 lcd.drawSprite(0, 4, 15, 60, (int *)mini_logo);
ellisbhastroud 15:d855e8c666e7 72 lcd.drawSprite(22, 21, 15, 60, (int *)golf_logo);
ellisbhastroud 15:d855e8c666e7 73 lcd.refresh();
ellisbhastroud 15:d855e8c666e7 74 wait(0.3);
ellisbhastroud 15:d855e8c666e7 75 lcd.clear();
ellisbhastroud 15:d855e8c666e7 76 lcd.drawSprite(0, 6, 15, 60, (int *)mini_logo);
ellisbhastroud 15:d855e8c666e7 77 lcd.drawSprite(24, 21, 15, 60, (int *)golf_logo);
ellisbhastroud 15:d855e8c666e7 78 lcd.printString("PRESS START",12,5);
ellisbhastroud 15:d855e8c666e7 79 lcd.refresh();
ellisbhastroud 15:d855e8c666e7 80 wait(0.3);
ellisbhastroud 15:d855e8c666e7 81 }
ellisbhastroud 15:d855e8c666e7 82 pad.tone(659.25 , 0.25); //play note (E5)
ellisbhastroud 15:d855e8c666e7 83 lcd.setBrightness(_brightness); //set brightness to 100% to counter tone function messing with screen backlight
ellisbhastroud 1:6179c2d67d19 84 }
ellisbhastroud 12:7f7fadb5c106 85
ellisbhastroud 14:08ac9aaa34c3 86 void Menu::menu_loop(Gamepad &pad, N5110 &lcd) //prints menu creen and controls cursor and choices
ellisbhastroud 1:6179c2d67d19 87 {
ellisbhastroud 14:08ac9aaa34c3 88 while(pad.check_event(Gamepad::START_PRESSED) == false){ //loop until start and next screen chosen
ellisbhastroud 5:0b31909caf7f 89
ellisbhastroud 5:0b31909caf7f 90 lcd.clear();
ellisbhastroud 14:08ac9aaa34c3 91 lcd.printString("START GAME",10,1);
ellisbhastroud 14:08ac9aaa34c3 92 lcd.printString("HIGHSCORES",10,3);
ellisbhastroud 14:08ac9aaa34c3 93 lcd.printString("SETTINGS",10,5);
ellisbhastroud 13:681192091568 94 read_input(pad);
ellisbhastroud 4:035448357749 95 move_cursor(lcd);
ellisbhastroud 4:035448357749 96 lcd.refresh();
ellisbhastroud 4:035448357749 97 wait(0.2);
ellisbhastroud 4:035448357749 98 }
ellisbhastroud 14:08ac9aaa34c3 99 pad.tone(523.25, 0.1); //play note when button pressed (middle C) - buzzer effects frequency of pwn pins which changes brightness
ellisbhastroud 15:d855e8c666e7 100 change_screen();
ellisbhastroud 14:08ac9aaa34c3 101 }
ellisbhastroud 14:08ac9aaa34c3 102
ellisbhastroud 14:08ac9aaa34c3 103 void Menu::start_loop(Gamepad &pad, N5110 &lcd) //prints start screen and returns true when game started
ellisbhastroud 14:08ac9aaa34c3 104 {
ellisbhastroud 14:08ac9aaa34c3 105 while(pad.check_event(Gamepad::A_PRESSED) == false) { //does nothing until A button pressed and game loop begins
ellisbhastroud 15:d855e8c666e7 106 lcd.clear();
ellisbhastroud 15:d855e8c666e7 107 lcd.printString(" PRESS A ",16,2);
ellisbhastroud 15:d855e8c666e7 108 lcd.printString(" TO START!",14,3);
ellisbhastroud 15:d855e8c666e7 109 lcd.refresh();
ellisbhastroud 15:d855e8c666e7 110 wait(0.3);
ellisbhastroud 15:d855e8c666e7 111 lcd.clear();
ellisbhastroud 15:d855e8c666e7 112 lcd.refresh();
ellisbhastroud 15:d855e8c666e7 113 wait(0.3);
ellisbhastroud 14:08ac9aaa34c3 114 }
ellisbhastroud 14:08ac9aaa34c3 115 _start_game_flag = true;
ellisbhastroud 14:08ac9aaa34c3 116 pad.tone(659.25 , 0.25); //play note (E5)
ellisbhastroud 14:08ac9aaa34c3 117 lcd.clear();
ellisbhastroud 2:81cfa8310f55 118 }
ellisbhastroud 2:81cfa8310f55 119
ellisbhastroud 13:681192091568 120 void Menu::highscores_loop(Gamepad &pad, N5110 &lcd, SDFileSystem &sd) //highscores screen loop
ellisbhastroud 13:681192091568 121 {
ellisbhastroud 15:d855e8c666e7 122 sd_read(sd); //reads values from highscores.txt file and sorts in ascending order
ellisbhastroud 14:08ac9aaa34c3 123 while(pad.check_event(Gamepad::BACK_PRESSED) == false) { //loops until back pressed
ellisbhastroud 12:7f7fadb5c106 124
ellisbhastroud 13:681192091568 125 lcd.clear();
ellisbhastroud 14:08ac9aaa34c3 126 lcd.printString("HIGHSCORES",13,0);
ellisbhastroud 14:08ac9aaa34c3 127
ellisbhastroud 14:08ac9aaa34c3 128 for(int i = 0; i < _n ; i++) { //displays highscores
ellisbhastroud 13:681192091568 129 char buffer[14];
ellisbhastroud 13:681192091568 130 sprintf(buffer,"%i - %i", i + 1, _highscores_array[i]);
ellisbhastroud 13:681192091568 131 lcd.printString(buffer,0,i+1);
ellisbhastroud 13:681192091568 132 }
ellisbhastroud 13:681192091568 133
ellisbhastroud 13:681192091568 134 lcd.refresh();
ellisbhastroud 13:681192091568 135 }
ellisbhastroud 14:08ac9aaa34c3 136 pad.tone(392.00 , 0.25); //play note (G4)
ellisbhastroud 14:08ac9aaa34c3 137 _start_game_flag = false;
ellisbhastroud 14:08ac9aaa34c3 138 }
ellisbhastroud 14:08ac9aaa34c3 139
ellisbhastroud 14:08ac9aaa34c3 140 void Menu::settings_loop(Gamepad &pad, N5110 &lcd)//prints settings screen
ellisbhastroud 14:08ac9aaa34c3 141 {
ellisbhastroud 14:08ac9aaa34c3 142 _cursor_pos = 0;
ellisbhastroud 14:08ac9aaa34c3 143 while(pad.check_event(Gamepad::BACK_PRESSED) == false) { //loops until back button pressed
ellisbhastroud 14:08ac9aaa34c3 144
ellisbhastroud 14:08ac9aaa34c3 145 lcd.clear();
ellisbhastroud 14:08ac9aaa34c3 146 drawSettings(lcd); //draws settings paramters and changes value according to cursor position and potentiometer input
ellisbhastroud 14:08ac9aaa34c3 147 move_cursor(lcd); //moves cursor according to joystick position
ellisbhastroud 14:08ac9aaa34c3 148 lcd.refresh();
ellisbhastroud 14:08ac9aaa34c3 149 wait(0.2);
ellisbhastroud 14:08ac9aaa34c3 150 read_input(pad); //reads joystick and pot inputs
ellisbhastroud 14:08ac9aaa34c3 151 }
ellisbhastroud 14:08ac9aaa34c3 152 pad.tone(392.00 , 0.25); //play note (G4)
ellisbhastroud 14:08ac9aaa34c3 153 _start_game_flag = false;
ellisbhastroud 12:7f7fadb5c106 154 }
ellisbhastroud 4:035448357749 155
ellisbhastroud 12:7f7fadb5c106 156 int Menu::get_frame_rate() //returns frame rate 5-50
ellisbhastroud 12:7f7fadb5c106 157 {
ellisbhastroud 12:7f7fadb5c106 158 int frame_rate = _frame_rate;
ellisbhastroud 12:7f7fadb5c106 159 return frame_rate;
ellisbhastroud 12:7f7fadb5c106 160 }
ellisbhastroud 12:7f7fadb5c106 161
ellisbhastroud 14:08ac9aaa34c3 162 float Menu::get_brightness()
ellisbhastroud 14:08ac9aaa34c3 163 {
ellisbhastroud 14:08ac9aaa34c3 164 return _brightness;
ellisbhastroud 14:08ac9aaa34c3 165 }
ellisbhastroud 14:08ac9aaa34c3 166
ellisbhastroud 14:08ac9aaa34c3 167 bool Menu::get_start_game_flag()
ellisbhastroud 14:08ac9aaa34c3 168 {
ellisbhastroud 14:08ac9aaa34c3 169 return _start_game_flag;
ellisbhastroud 14:08ac9aaa34c3 170 }
ellisbhastroud 14:08ac9aaa34c3 171
ellisbhastroud 14:08ac9aaa34c3 172 void Menu::reset_start_game_flag()
ellisbhastroud 14:08ac9aaa34c3 173 {
ellisbhastroud 14:08ac9aaa34c3 174 _start_game_flag = false;
ellisbhastroud 14:08ac9aaa34c3 175 }
ellisbhastroud 14:08ac9aaa34c3 176
ellisbhastroud 14:08ac9aaa34c3 177 MenuChoice Menu::get_screen()
ellisbhastroud 14:08ac9aaa34c3 178 {
ellisbhastroud 14:08ac9aaa34c3 179 return _screen;
ellisbhastroud 14:08ac9aaa34c3 180 }
ellisbhastroud 14:08ac9aaa34c3 181
ellisbhastroud 12:7f7fadb5c106 182 //private methods
ellisbhastroud 12:7f7fadb5c106 183
ellisbhastroud 13:681192091568 184 void Menu::read_input(Gamepad &pad) //Reads input from gamepad into class variables
ellisbhastroud 13:681192091568 185 {
ellisbhastroud 13:681192091568 186 _joy_direction = pad.get_direction(); //joystick direction
ellisbhastroud 13:681192091568 187 _pot = pad.read_pot(); //potentiometer value 0-1 float
ellisbhastroud 13:681192091568 188
ellisbhastroud 13:681192091568 189 }
ellisbhastroud 13:681192091568 190
ellisbhastroud 13:681192091568 191 void Menu::sd_read(SDFileSystem &sd) //reads values from highscores file into array
ellisbhastroud 13:681192091568 192 {
ellisbhastroud 13:681192091568 193 FILE *fp;
ellisbhastroud 13:681192091568 194 fp = fopen("/sd/highscores.txt", "r"); //open highscores.txt file for reading
ellisbhastroud 13:681192091568 195 if(!(fp == NULL)) { //if file is opened
ellisbhastroud 13:681192091568 196
ellisbhastroud 13:681192091568 197 int n = 0; //no. of line stored in this
ellisbhastroud 13:681192091568 198
ellisbhastroud 13:681192091568 199 while (fscanf(fp, "%*d") != EOF) { //counts number of lines until reach EOF (end of file)
ellisbhastroud 13:681192091568 200 n++; // increment counter when read a line
ellisbhastroud 13:681192091568 201 }
ellisbhastroud 13:681192091568 202 _highscores_array = (int *)calloc(n, sizeof (int)); //creates array of length n and initialises to 0
ellisbhastroud 14:08ac9aaa34c3 203 _n = n; //number of values in array - used when displaying highscores
ellisbhastroud 13:681192091568 204
ellisbhastroud 13:681192091568 205 int i=0;
ellisbhastroud 13:681192091568 206 rewind(fp); //return to beginning of file
ellisbhastroud 13:681192091568 207
ellisbhastroud 13:681192091568 208 while (fscanf(fp, "%d",&_highscores_array[i]) != EOF) { //read into array
ellisbhastroud 13:681192091568 209 i++; // read data into array and increment index
ellisbhastroud 13:681192091568 210 }
ellisbhastroud 13:681192091568 211 fclose(fp); //close file
ellisbhastroud 13:681192091568 212
ellisbhastroud 13:681192091568 213 sort(_highscores_array, _highscores_array + n); //sorts array in ascending order - the lowest scores are top.
ellisbhastroud 13:681192091568 214
ellisbhastroud 13:681192091568 215
ellisbhastroud 13:681192091568 216 } else { //if file does not open
ellisbhastroud 13:681192091568 217 _highscores_array = (int *)calloc(5, sizeof (int)); //creates 5 term array initiliased to 0
ellisbhastroud 13:681192091568 218 }
ellisbhastroud 13:681192091568 219 }
ellisbhastroud 13:681192091568 220
ellisbhastroud 14:08ac9aaa34c3 221 void Menu::change_screen() //moves changes menu screen to screen of cursor position
ellisbhastroud 13:681192091568 222 {
ellisbhastroud 14:08ac9aaa34c3 223 switch(_cursor_pos) { //uses cursor position to change screen variable to
ellisbhastroud 14:08ac9aaa34c3 224 case 0:
ellisbhastroud 14:08ac9aaa34c3 225 _screen = START;
ellisbhastroud 14:08ac9aaa34c3 226 break;
ellisbhastroud 14:08ac9aaa34c3 227 case 1:
ellisbhastroud 14:08ac9aaa34c3 228 _screen = HIGHSCORES;
ellisbhastroud 14:08ac9aaa34c3 229 break;
ellisbhastroud 14:08ac9aaa34c3 230 case 2:
ellisbhastroud 14:08ac9aaa34c3 231 _screen = SETTINGS;
ellisbhastroud 14:08ac9aaa34c3 232 break;
ellisbhastroud 13:681192091568 233 }
ellisbhastroud 13:681192091568 234 }
ellisbhastroud 13:681192091568 235
ellisbhastroud 13:681192091568 236 void Menu::drawSettings(N5110 &lcd)
ellisbhastroud 13:681192091568 237 {
ellisbhastroud 4:035448357749 238 lcd.printString("LCD Contrast",0,0);
ellisbhastroud 4:035448357749 239 lcd.printString("LCD Brightness",0,2);
ellisbhastroud 4:035448357749 240 lcd.printString("Frame Rate",0,4);
ellisbhastroud 13:681192091568 241 switch(_cursor_pos) { //modifies current setting depending on cursor position and potentiometer
ellisbhastroud 4:035448357749 242 case 0:
ellisbhastroud 5:0b31909caf7f 243 _contrast = (_pot*4.0f/10.0f)+0.4f; //scale from 0-1 to 0.4-0.8 (below 0.4 no colour, above 0.8 no change)
ellisbhastroud 14:08ac9aaa34c3 244 lcd.setContrast(_contrast); //0.54 best to see pov of ball clearly
ellisbhastroud 14:08ac9aaa34c3 245 drawSettingsBars(lcd); //draws settings bars and prints values alongside
ellisbhastroud 4:035448357749 246 break;
ellisbhastroud 4:035448357749 247 case 1:
ellisbhastroud 4:035448357749 248 _brightness = _pot;
ellisbhastroud 14:08ac9aaa34c3 249 lcd.setBrightness(_brightness); //100% brightness and high contrast makes seeing ball easier
ellisbhastroud 14:08ac9aaa34c3 250 drawSettingsBars(lcd); //draws settings bars and prints values alongside
ellisbhastroud 4:035448357749 251 break;
ellisbhastroud 12:7f7fadb5c106 252 case 2:
ellisbhastroud 14:08ac9aaa34c3 253 _frame_rate = (_pot*30.0f)+20.0f; //scales 0-1 pot value to 5-50 fps
ellisbhastroud 14:08ac9aaa34c3 254 drawSettingsBars(lcd); //draws settings bars and prints values alongside
ellisbhastroud 4:035448357749 255 break;
ellisbhastroud 4:035448357749 256 }
ellisbhastroud 4:035448357749 257 }
ellisbhastroud 12:7f7fadb5c106 258
ellisbhastroud 12:7f7fadb5c106 259 void Menu::drawSettingsBars(N5110 &lcd) {
ellisbhastroud 12:7f7fadb5c106 260
ellisbhastroud 14:08ac9aaa34c3 261 lcd.drawRect(8,9,40,6,FILL_TRANSPARENT); //contrast bar
ellisbhastroud 14:08ac9aaa34c3 262 lcd.drawRect(8,25,40,6,FILL_TRANSPARENT); //brightness bar
ellisbhastroud 14:08ac9aaa34c3 263 lcd.drawRect(8,41,40,6,FILL_TRANSPARENT); //frame rate bar
ellisbhastroud 12:7f7fadb5c106 264 lcd.drawRect(8,9,40*(_contrast-0.4f)*(10.0f/4.0f),6,FILL_BLACK);
ellisbhastroud 12:7f7fadb5c106 265 lcd.drawRect(8,25,40*_brightness,6,FILL_BLACK);
ellisbhastroud 14:08ac9aaa34c3 266 lcd.drawRect(8,41,40*(_frame_rate-20.0f)/30.0f,6,FILL_BLACK);
ellisbhastroud 14:08ac9aaa34c3 267 char buffer[14];
ellisbhastroud 14:08ac9aaa34c3 268 sprintf(buffer,"%.2f%",_contrast); //print contrast value to 2dp
ellisbhastroud 14:08ac9aaa34c3 269 lcd.printString(buffer,52,1);
ellisbhastroud 14:08ac9aaa34c3 270 sprintf(buffer,"%.2f%",_brightness); //print brightness value to 2dp
ellisbhastroud 14:08ac9aaa34c3 271 lcd.printString(buffer,52,3);
ellisbhastroud 14:08ac9aaa34c3 272 sprintf(buffer,"%ifps",_frame_rate); //prints integer value of frame rate
ellisbhastroud 14:08ac9aaa34c3 273 lcd.printString(buffer,52,5);
ellisbhastroud 12:7f7fadb5c106 274 }
ellisbhastroud 12:7f7fadb5c106 275
ellisbhastroud 14:08ac9aaa34c3 276 void Menu::move_cursor(N5110 &lcd) //draws cursor in current position and moves to next position dependent on joystick direction
ellisbhastroud 4:035448357749 277 {
ellisbhastroud 4:035448357749 278 switch(_cursor_pos) {
ellisbhastroud 4:035448357749 279 case 0: //cursor on position 0
ellisbhastroud 13:681192091568 280 drawCursor(lcd); //draws cursor in position
ellisbhastroud 14:08ac9aaa34c3 281 cursor_pos_0(); //moves cursor dependent on current position and direction of joystick
ellisbhastroud 4:035448357749 282 break;
ellisbhastroud 4:035448357749 283 case 1: //cursor on position 1
ellisbhastroud 12:7f7fadb5c106 284 drawCursor(lcd);
ellisbhastroud 14:08ac9aaa34c3 285 cursor_pos_1(); //moves cursor dependent on current position and direction of joystick
ellisbhastroud 4:035448357749 286 break;
ellisbhastroud 4:035448357749 287 case 2: //cursor on position 2
ellisbhastroud 12:7f7fadb5c106 288 drawCursor(lcd);
ellisbhastroud 14:08ac9aaa34c3 289 cursor_pos_2(); //moves cursor dependent on current position and direction of joystick
ellisbhastroud 4:035448357749 290 break;
ellisbhastroud 4:035448357749 291 }
ellisbhastroud 4:035448357749 292 }
ellisbhastroud 12:7f7fadb5c106 293
ellisbhastroud 14:08ac9aaa34c3 294 void Menu::cursor_pos_0() //cursor movement from position 0
ellisbhastroud 14:08ac9aaa34c3 295 {
ellisbhastroud 14:08ac9aaa34c3 296 switch(_joy_direction) {
ellisbhastroud 14:08ac9aaa34c3 297 case 0: //joystick centered
ellisbhastroud 14:08ac9aaa34c3 298 _cursor_pos = 0;
ellisbhastroud 14:08ac9aaa34c3 299 break;
ellisbhastroud 14:08ac9aaa34c3 300 case 1: //joystick up
ellisbhastroud 14:08ac9aaa34c3 301 _cursor_pos = 2;
ellisbhastroud 14:08ac9aaa34c3 302 break;
ellisbhastroud 14:08ac9aaa34c3 303 case 5: //joystick down
ellisbhastroud 14:08ac9aaa34c3 304 _cursor_pos = 1;
ellisbhastroud 14:08ac9aaa34c3 305 break;
ellisbhastroud 14:08ac9aaa34c3 306 }
ellisbhastroud 14:08ac9aaa34c3 307 }
ellisbhastroud 14:08ac9aaa34c3 308
ellisbhastroud 14:08ac9aaa34c3 309 void Menu::cursor_pos_1() //cursor movement from position 1
ellisbhastroud 14:08ac9aaa34c3 310 {
ellisbhastroud 14:08ac9aaa34c3 311 switch(_joy_direction) {
ellisbhastroud 14:08ac9aaa34c3 312 case 0: //joystick centered
ellisbhastroud 14:08ac9aaa34c3 313 _cursor_pos = 1;
ellisbhastroud 14:08ac9aaa34c3 314 break;
ellisbhastroud 14:08ac9aaa34c3 315 case 1: //joystick up
ellisbhastroud 14:08ac9aaa34c3 316 _cursor_pos = 0;
ellisbhastroud 14:08ac9aaa34c3 317 break;
ellisbhastroud 14:08ac9aaa34c3 318 case 5: //joystick down
ellisbhastroud 14:08ac9aaa34c3 319 _cursor_pos = 2;
ellisbhastroud 14:08ac9aaa34c3 320 break;
ellisbhastroud 14:08ac9aaa34c3 321 }
ellisbhastroud 14:08ac9aaa34c3 322 }
ellisbhastroud 14:08ac9aaa34c3 323
ellisbhastroud 14:08ac9aaa34c3 324 void Menu::cursor_pos_2() //cursor movement from position 2
ellisbhastroud 14:08ac9aaa34c3 325 {
ellisbhastroud 14:08ac9aaa34c3 326 switch(_joy_direction) {
ellisbhastroud 14:08ac9aaa34c3 327 case 0: //joystick centered
ellisbhastroud 14:08ac9aaa34c3 328 _cursor_pos = 2;
ellisbhastroud 14:08ac9aaa34c3 329 break;
ellisbhastroud 14:08ac9aaa34c3 330 case 1: //joystick up
ellisbhastroud 14:08ac9aaa34c3 331 _cursor_pos = 1;
ellisbhastroud 14:08ac9aaa34c3 332 break;
ellisbhastroud 14:08ac9aaa34c3 333 case 5: //joystick down
ellisbhastroud 14:08ac9aaa34c3 334 _cursor_pos = 0;
ellisbhastroud 14:08ac9aaa34c3 335 break;
ellisbhastroud 14:08ac9aaa34c3 336 }
ellisbhastroud 14:08ac9aaa34c3 337 }
ellisbhastroud 14:08ac9aaa34c3 338
ellisbhastroud 12:7f7fadb5c106 339 void Menu::drawCursor(N5110 &lcd)
ellisbhastroud 12:7f7fadb5c106 340 {
ellisbhastroud 12:7f7fadb5c106 341 switch(_cursor_pos) {
ellisbhastroud 12:7f7fadb5c106 342 case 0: //cursor on position 0
ellisbhastroud 12:7f7fadb5c106 343 lcd.drawRect(0,9,6,6,FILL_TRANSPARENT);
ellisbhastroud 12:7f7fadb5c106 344 break;
ellisbhastroud 12:7f7fadb5c106 345 case 1: //cursor on position 1
ellisbhastroud 12:7f7fadb5c106 346 lcd.drawRect(0,25,6,6,FILL_TRANSPARENT);
ellisbhastroud 12:7f7fadb5c106 347 break;
ellisbhastroud 12:7f7fadb5c106 348 case 2: //cursor on position 2
ellisbhastroud 12:7f7fadb5c106 349 lcd.drawRect(0,41,6,6,FILL_TRANSPARENT);
ellisbhastroud 12:7f7fadb5c106 350 break;
ellisbhastroud 12:7f7fadb5c106 351 }
ellisbhastroud 14:08ac9aaa34c3 352 }