Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Options_Engine/OptionsEngine.cpp@40:a1cdb6ab08af, 2019-05-09 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu May 09 10:52:00 2019 +0000
- Revision:
- 40:a1cdb6ab08af
- Parent:
- 38:a85bc227b907
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 22:4e305ff8a050 | 1 | #include "OptionsEngine.h" |
JamesCummins | 22:4e305ff8a050 | 2 | |
JamesCummins | 38:a85bc227b907 | 3 | //constructor |
JamesCummins | 22:4e305ff8a050 | 4 | OptionsEngine::OptionsEngine(){ |
JamesCummins | 22:4e305ff8a050 | 5 | } |
JamesCummins | 22:4e305ff8a050 | 6 | |
JamesCummins | 38:a85bc227b907 | 7 | //destructor |
JamesCummins | 22:4e305ff8a050 | 8 | OptionsEngine::~OptionsEngine(){ |
JamesCummins | 22:4e305ff8a050 | 9 | } |
JamesCummins | 22:4e305ff8a050 | 10 | |
JamesCummins | 38:a85bc227b907 | 11 | //initialiser |
JamesCummins | 22:4e305ff8a050 | 12 | void OptionsEngine::init(){ |
JamesCummins | 38:a85bc227b907 | 13 | _state = BRIGHTNESS; //first item in menu |
JamesCummins | 38:a85bc227b907 | 14 | _brightness = 0.5; //medium brightness |
JamesCummins | 38:a85bc227b907 | 15 | _ball_speed = 5; // 5/10 sensitivity |
JamesCummins | 22:4e305ff8a050 | 16 | } |
JamesCummins | 22:4e305ff8a050 | 17 | |
JamesCummins | 22:4e305ff8a050 | 18 | void OptionsEngine::display_options(N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 19 | lcd.printString("Options menu", 6, 0); //text for each line of display |
JamesCummins | 22:4e305ff8a050 | 20 | lcd.printString("Brightness", 12, 2); |
JamesCummins | 22:4e305ff8a050 | 21 | lcd.printString("Ball speed", 12, 3); |
JamesCummins | 22:4e305ff8a050 | 22 | lcd.printString("High scores", 9, 4); |
JamesCummins | 22:4e305ff8a050 | 23 | } |
JamesCummins | 22:4e305ff8a050 | 24 | |
JamesCummins | 22:4e305ff8a050 | 25 | Option OptionsEngine::option_selection(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 26 | OptionSelection fsm[3] = { //finite state machine to power the menu |
JamesCummins | 38:a85bc227b907 | 27 | {2,{HIGH_SCORES, BALL_SPEED, BRIGHTNESS}}, //output (e.g. 2,3,4) is line to print arrows on for user interface |
JamesCummins | 38:a85bc227b907 | 28 | {3,{BRIGHTNESS, HIGH_SCORES, BALL_SPEED}}, //next_state[0] is the option above current one |
JamesCummins | 38:a85bc227b907 | 29 | {4,{BALL_SPEED, BRIGHTNESS, HIGH_SCORES}} //next_state[1] is the option below current one |
JamesCummins | 38:a85bc227b907 | 30 | }; //next_state[2] is current state |
JamesCummins | 38:a85bc227b907 | 31 | if(gamepad.get_direction() == N){ _next_state = 0; } //move arrows up |
JamesCummins | 38:a85bc227b907 | 32 | else if(gamepad.get_direction() == S){ _next_state = 1; } //move arrows down |
JamesCummins | 38:a85bc227b907 | 33 | else{ _next_state = 2; } //keep arrows the same (default) |
JamesCummins | 38:a85bc227b907 | 34 | _state = fsm[_state].next_state[_next_state]; //calculate next state |
JamesCummins | 22:4e305ff8a050 | 35 | lcd.printChar('>', 0, fsm[_state].output); |
JamesCummins | 38:a85bc227b907 | 36 | lcd.printChar('<', 78, fsm[_state].output); //draw arrows |
JamesCummins | 22:4e305ff8a050 | 37 | return _state; |
JamesCummins | 22:4e305ff8a050 | 38 | } |
JamesCummins | 22:4e305ff8a050 | 39 | |
JamesCummins | 22:4e305ff8a050 | 40 | void OptionsEngine::change_brightness(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 41 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection |
JamesCummins | 22:4e305ff8a050 | 42 | lcd.clear(); |
JamesCummins | 38:a85bc227b907 | 43 | lcd.printString("Brightness", 12, 0); //text to display on each line |
JamesCummins | 22:4e305ff8a050 | 44 | lcd.printString("Use L and R to", 0, 3); |
JamesCummins | 22:4e305ff8a050 | 45 | lcd.printString("change", 24, 4); |
JamesCummins | 22:4e305ff8a050 | 46 | lcd.printString("A = confirm", 9, 5); |
JamesCummins | 38:a85bc227b907 | 47 | lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT); //outside of slider to display the brightness controller |
JamesCummins | 22:4e305ff8a050 | 48 | read_brightness_input(gamepad); |
JamesCummins | 22:4e305ff8a050 | 49 | for(int i = 0; i < _brightness*10; i ++){ |
JamesCummins | 38:a85bc227b907 | 50 | lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); //draw small rectangles to represent the current brightness |
JamesCummins | 22:4e305ff8a050 | 51 | } |
JamesCummins | 38:a85bc227b907 | 52 | lcd.setBrightness(_brightness); //set brightness with N5110 class |
JamesCummins | 22:4e305ff8a050 | 53 | lcd.refresh(); |
JamesCummins | 38:a85bc227b907 | 54 | wait(0.2); //avoid button bounce with large time between frames |
JamesCummins | 22:4e305ff8a050 | 55 | } |
JamesCummins | 22:4e305ff8a050 | 56 | } |
JamesCummins | 22:4e305ff8a050 | 57 | |
JamesCummins | 22:4e305ff8a050 | 58 | void OptionsEngine::read_brightness_input(Gamepad &gamepad){ |
JamesCummins | 23:61fa82f76808 | 59 | if(gamepad.check_event(gamepad.L_PRESSED)){ _brightness -= 0.1f; } //Use of f to explicitly convert to a float (to fit declaration type in header file). |
JamesCummins | 22:4e305ff8a050 | 60 | if(gamepad.check_event(gamepad.R_PRESSED)){ _brightness += 0.1f; } //Otherwise 0.1 is implicitly converted to a double (giving warning messages). |
JamesCummins | 38:a85bc227b907 | 61 | if(_brightness < 0){ _brightness = 0; } //keep within range of 0 - 1 |
JamesCummins | 22:4e305ff8a050 | 62 | if(_brightness > 1){ _brightness = 1; } |
JamesCummins | 22:4e305ff8a050 | 63 | } |
JamesCummins | 23:61fa82f76808 | 64 | |
JamesCummins | 23:61fa82f76808 | 65 | void OptionsEngine::change_ball_speed(Gamepad &gamepad, N5110 &lcd, Ball &ball){ |
JamesCummins | 38:a85bc227b907 | 66 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection |
JamesCummins | 23:61fa82f76808 | 67 | lcd.clear(); |
JamesCummins | 38:a85bc227b907 | 68 | lcd.printString("Ball Speed", 12, 0); //text for each line of display |
JamesCummins | 23:61fa82f76808 | 69 | lcd.printString("Use L and R to", 0, 3); |
JamesCummins | 23:61fa82f76808 | 70 | lcd.printString("change", 24, 4); |
JamesCummins | 23:61fa82f76808 | 71 | lcd.printString("A = confirm", 9, 5); |
JamesCummins | 23:61fa82f76808 | 72 | lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT); |
JamesCummins | 38:a85bc227b907 | 73 | read_ball_speed_input(gamepad); //get values for sensitivity/ball speed |
JamesCummins | 23:61fa82f76808 | 74 | for(int i = 0; i < _ball_speed; i ++){ |
JamesCummins | 38:a85bc227b907 | 75 | lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); //draw small rectangles in the same manner as brightness control |
JamesCummins | 23:61fa82f76808 | 76 | } |
JamesCummins | 23:61fa82f76808 | 77 | ball.set_ball_speed(_ball_speed); |
JamesCummins | 23:61fa82f76808 | 78 | lcd.refresh(); |
JamesCummins | 38:a85bc227b907 | 79 | wait(0.2); //avoid button bounce |
JamesCummins | 23:61fa82f76808 | 80 | } |
JamesCummins | 23:61fa82f76808 | 81 | } |
JamesCummins | 23:61fa82f76808 | 82 | |
JamesCummins | 23:61fa82f76808 | 83 | void OptionsEngine::read_ball_speed_input(Gamepad &gamepad){ |
JamesCummins | 38:a85bc227b907 | 84 | if(gamepad.check_event(gamepad.L_PRESSED)){ _ball_speed -= 1; } //increment on R press, decrement on L press |
JamesCummins | 23:61fa82f76808 | 85 | if(gamepad.check_event(gamepad.R_PRESSED)){ _ball_speed += 1; } |
JamesCummins | 38:a85bc227b907 | 86 | if(_ball_speed < 0){ _ball_speed = 0; } //keep within range 0 - 10 |
JamesCummins | 23:61fa82f76808 | 87 | if(_ball_speed > 10){ _ball_speed = 10; } |
JamesCummins | 24:c6415cc74b17 | 88 | } |
JamesCummins | 24:c6415cc74b17 | 89 | |
JamesCummins | 26:0dc10374546f | 90 | void OptionsEngine::view_high_scores(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 91 | _leaderboard = CLASSIC_MODE; //initialise to look at classic leaderboard first |
JamesCummins | 38:a85bc227b907 | 92 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection |
JamesCummins | 38:a85bc227b907 | 93 | if(gamepad.check_event(gamepad.R_PRESSED)){ _leaderboard = BRICKBREAKER_MODE; } //use L and R to flick between the two leaderboards |
JamesCummins | 24:c6415cc74b17 | 94 | if(gamepad.check_event(gamepad.L_PRESSED)){ _leaderboard = CLASSIC_MODE; } |
JamesCummins | 24:c6415cc74b17 | 95 | lcd.clear(); |
JamesCummins | 26:0dc10374546f | 96 | print_high_scores(lcd); |
JamesCummins | 24:c6415cc74b17 | 97 | lcd.refresh(); |
JamesCummins | 24:c6415cc74b17 | 98 | wait(0.2); |
JamesCummins | 24:c6415cc74b17 | 99 | } |
JamesCummins | 24:c6415cc74b17 | 100 | } |
JamesCummins | 24:c6415cc74b17 | 101 | |
JamesCummins | 35:138ad0faa42b | 102 | void OptionsEngine::read_classic_high_scores(){ |
JamesCummins | 38:a85bc227b907 | 103 | FILE *fp; //open file stream |
JamesCummins | 38:a85bc227b907 | 104 | fp = fopen("/sd/classichighscores.txt", "r"); //classic high scores file in read mode |
JamesCummins | 35:138ad0faa42b | 105 | |
JamesCummins | 35:138ad0faa42b | 106 | if(fp == NULL){ |
JamesCummins | 38:a85bc227b907 | 107 | printf("Error: Could not open file"); //check open |
JamesCummins | 35:138ad0faa42b | 108 | } else { |
JamesCummins | 35:138ad0faa42b | 109 | int i = 0; |
JamesCummins | 38:a85bc227b907 | 110 | rewind(fp); //reset to start of file to start reading |
JamesCummins | 35:138ad0faa42b | 111 | |
JamesCummins | 38:a85bc227b907 | 112 | while(fscanf(fp, "%d,%f", &_classic_index[i], &_classic_values[i]) != EOF){ //read each index and value into arrays till end of file |
JamesCummins | 38:a85bc227b907 | 113 | i++; //increment counter |
JamesCummins | 35:138ad0faa42b | 114 | } |
JamesCummins | 38:a85bc227b907 | 115 | fclose(fp); //close file stream |
JamesCummins | 26:0dc10374546f | 116 | } |
JamesCummins | 35:138ad0faa42b | 117 | } |
JamesCummins | 35:138ad0faa42b | 118 | |
JamesCummins | 35:138ad0faa42b | 119 | void OptionsEngine::read_bb_high_scores(){ |
JamesCummins | 38:a85bc227b907 | 120 | FILE *fp; //open file stream |
JamesCummins | 38:a85bc227b907 | 121 | fp = fopen("/sd/bbhighscores.txt", "r"); //brickbreaker high scores in read mode |
JamesCummins | 35:138ad0faa42b | 122 | |
JamesCummins | 35:138ad0faa42b | 123 | if(fp == NULL){ |
JamesCummins | 38:a85bc227b907 | 124 | printf("Error: Could not open file"); //check open |
JamesCummins | 35:138ad0faa42b | 125 | } else { |
JamesCummins | 35:138ad0faa42b | 126 | int i = 0; |
JamesCummins | 38:a85bc227b907 | 127 | rewind(fp); //reset to start of file |
JamesCummins | 35:138ad0faa42b | 128 | |
JamesCummins | 38:a85bc227b907 | 129 | while(fscanf(fp, "%d,%f", &_bb_index[i], &_bb_values[i]) != EOF){ //read each index and value into array till end of file |
JamesCummins | 38:a85bc227b907 | 130 | i++; //increment counter |
JamesCummins | 35:138ad0faa42b | 131 | } |
JamesCummins | 38:a85bc227b907 | 132 | fclose(fp); //close file stream |
JamesCummins | 26:0dc10374546f | 133 | } |
JamesCummins | 26:0dc10374546f | 134 | } |
JamesCummins | 26:0dc10374546f | 135 | |
JamesCummins | 26:0dc10374546f | 136 | void OptionsEngine::print_high_scores(N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 137 | if(_leaderboard == CLASSIC_MODE){ //check which leaderboard to display |
JamesCummins | 35:138ad0faa42b | 138 | read_classic_high_scores(); |
JamesCummins | 38:a85bc227b907 | 139 | lcd.printString("Classic", 21, 0); //text for user interface with leaderboard |
JamesCummins | 35:138ad0faa42b | 140 | lcd.printString("R>", 72, 3); |
JamesCummins | 35:138ad0faa42b | 141 | char buffer[14]; |
JamesCummins | 38:a85bc227b907 | 142 | for(int i = 0; i < 5; i++){ //iterate for each of 5 high scores |
JamesCummins | 38:a85bc227b907 | 143 | sprintf(buffer, "%d. %.0fs", _classic_index[i], _classic_values[i]); //update the buffer for each line of high scores |
JamesCummins | 38:a85bc227b907 | 144 | lcd.printString(buffer, 0, i + 1); //print the respective high score to screen |
JamesCummins | 35:138ad0faa42b | 145 | } |
JamesCummins | 24:c6415cc74b17 | 146 | } |
JamesCummins | 38:a85bc227b907 | 147 | if(_leaderboard == BRICKBREAKER_MODE){ //check which leaderboard to display |
JamesCummins | 26:0dc10374546f | 148 | read_bb_high_scores(); |
JamesCummins | 38:a85bc227b907 | 149 | lcd.printString("Brickbreak", 12, 0); //text for user interface |
JamesCummins | 35:138ad0faa42b | 150 | lcd.printString("<L", 72, 3); |
JamesCummins | 35:138ad0faa42b | 151 | char buffer[14]; |
JamesCummins | 38:a85bc227b907 | 152 | for(int i = 0; i < 5; i++){ //iterate for each of 5 high scores |
JamesCummins | 38:a85bc227b907 | 153 | sprintf(buffer, "%d. %.0f", _bb_index[i], _bb_values[i]); //update buffer for each line of high scores |
JamesCummins | 38:a85bc227b907 | 154 | lcd.printString(buffer, 0, i + 1); //print respective high score to screen |
JamesCummins | 26:0dc10374546f | 155 | } |
JamesCummins | 24:c6415cc74b17 | 156 | } |
JamesCummins | 23:61fa82f76808 | 157 | } |